STM32配置
是用STM32CUBEMX配置HAL库
TMC5160可以是用单线UART或者全双工的SPI进行通信,为了便于观察和代码copy,我选择是用spi通信,以下是stm32的配置:
可以不用选择USART1和USART2,这里配置主要是为了是用单线UART通信,不是本文重点。
关于时钟树的配置可以按照自己的开发版进行配置。 我是用CLION进行开发,具体配置可以参考稚晖君的教程:https://zhuanlan.zhihu.com/p/145801160
TMC5160关键寄存器
关键:多读数据手册
1.基础寄存器配置:
-
GCONF寄存器配置基本的运动模式和方向:个人配置0x000000E4
-
CHOPCONF寄存器斩波器配置,个人配置0x000000E4
-
IHOLE_IRUN寄存器配置运行电流和保持电流:个人配置0x00060100。此寄存器与stallguard的灵敏度有关。
2.stallguard相关寄存器配置
- TCOOLTHRS寄存器,使能CoolStep和StallGuard2功能的下限速度,到达此值电机停止并保持。个人配置0x00000FEC
- SW_MODE寄存器的第十位sg_stop启用STALLGUARD 2功能。0x00000400* COOLCONF的16bit-22bit 用于设置灵敏度。
- DRV_STATUS寄存器的SG_RESULT:StallGuard2值和用于监测电机所剩的扭力。
- 速度相关寄存器:RAMPMODE用于选择位置/速度模式,以及速度和加速度相关的寄存器功能如折线图所示。
Stallguard灵敏度调节
要根据自己的电机做改动
相关寄存器:
- IHOLD_IRUN:IRUN的电流大小也和灵敏度相关,为正相关。
- COOLCONF:sgt0到sgt6是有符号位的灵敏度控制器,通过调整寄存器大小改变灵敏度
- TSTEP:监测此寄存器,如果读取值为0则说明灵敏度过小。
相关代码
tmc5160_writeInt(&tmc5160, TMC5160_GCONF, 0x000000E4);
tmc5160_writeInt(&tmc5160, TMC5160_CHOPCONF, 0x000000E4);
tmc5160_writeInt(&tmc5160, TMC5160_IHOLD_IRUN, 0x00060100);
tmc5160_writeInt(&tmc5160, TMC5160_AMAX, 0x0001FFFF);
tmc5160_writeInt(&tmc5160, TMC5160_VMAX, 0x004FF00);
tmc5160_writeInt(&tmc5160, TMC5160_SWMODE, 0x00000400);
tmc5160_writeInt(&tmc5160, TMC5160_COOLCONF, 0x00900000);
tmc5160_writeInt(&tmc5160, TMC5160_TCOOLTHRS, 0x00000FEC);
tmc5160_writeInt(&tmc5160, TMC5160_RAMPMODE,0x00000001);
while (1)
{
tmc5160_readInt(&tmc5160, TMC5160_TSTEP);
HAL_Delay(200);
}
// Write an integer to the given address
void tmc5160_writeInt(TMC5160TypeDef *tmc5160, uint8_t address, int32_t value)
{
tmc5160_writeDatagram(tmc5160, address, BYTE(value, 3), BYTE(value, 2), BYTE(value, 1), BYTE(value, 0));
}
// Writes (x1 << 24) | (x2 << 16) | (x3 << 8) | x4 to the given address
void tmc5160_writeDatagram(TMC5160TypeDef *tmc5160, uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4)
{
uint8_t data[5] = { address | TMC5160_WRITE_BIT, x1, x2, x3, x4 };
uint8_t receive[5];
tmc5160_readWriteArray(data, receive, 5, 1);
int32_t value = ((uint32_t)x1 << 24) | ((uint32_t)x2 << 16) | (x3 << 8) | x4;
// Write to the shadow register and mark the register dirty
address = TMC_ADDRESS(address);
tmc5160->config->shadowRegister[address] = value;
tmc5160->registerAccess[address] |= TMC_ACCESS_DIRTY;
}
HAL_StatusTypeDef tmc5160_readWriteArray(uint8_t *transmit, uint8_t *receive, size_t size, uint8_t timeout)
{
// Declare the status variable
HAL_StatusTypeDef status;
// Select the TMC5160 channel (if using multiple devices)
// This usually involves setting a GPIO pin to select the correct device.
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET); // Select device
// Transmit and receive data via SPI
status = HAL_SPI_TransmitReceive(&hspi1, transmit, receive, size, timeout);
// Deselect the TMC5160 channel
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET); // Deselect device
return status;
}
实际上就是套了好几层函数,底层还是是用HAL库的SPI收发函数实现。
小结
!!!!!!!!仔细读数据手册真的很重要!!!!!!!!!
文章来源:https://www.toymoban.com/news/detail-780090.html
完整的代码在github仓库:https://github.com/Tianli-Wang/TMC5160-Stallguard文章来源地址https://www.toymoban.com/news/detail-780090.html
到了这里,关于STM32配置TMC5160的stallguard堵转检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!