【单片机】ESP32-S3+多TMC2209控制步进电机系列3 使用TMC2209库实现UART通讯

目录

1.下载TMC2209.h库

在Arudino环境中,有两个不错的库可以驱动TMC2209。

  • TMC2209库
  • TMCStepper库
    TMC2209库只针对TMC2209驱动器,而TMCStepper库除了能够支持TMC2209驱动器,还能够支持其他TMC的驱动器,如下图。
    TMC2209库体积小,不复杂。本文将使用TMC2209库来完成对驱动器的通讯。
    打开Arduino的库管理器,搜索TMC2209,点击安装即可。如果无法在线安装,可在其GitHub中下载离线ZIP进行安装,TMC2209库GitHub链接

2.代码部分

本文对是3个TMC2209驱动器进行通讯,对1个电机进行控制(手里就有一个电机现在。。)为例。

cpp 复制代码
#include <TMC2209.h>

//使用串口1对驱动进行通讯
HardwareSerial & serial_stream = Serial1;

// 细分步 1.8°的电机一圈所需脉冲微 360/1.8*MICROSTEPPING
#define MICROSTEPPING 64
// 实例化3个 TMC2209驱动器
TMC2209 stepper_driver_0;
const TMC2209::SerialAddress SERIAL_ADDRESS_0 = TMC2209::SERIAL_ADDRESS_0;
TMC2209 stepper_driver_1;
const TMC2209::SerialAddress SERIAL_ADDRESS_1 = TMC2209::SERIAL_ADDRESS_1;
TMC2209 stepper_driver_2;
const TMC2209::SerialAddress SERIAL_ADDRESS_2 = TMC2209::SERIAL_ADDRESS_2;
//串口通讯的波特率
const long SERIAL_BAUD_RATE = 115200;
const int StepperEnaPin = 3; // 驱动器使能引脚
const int RX_PIN = 18;
const int TX_PIN = 17;
//串口输出延迟
const int DELAY = 2000;
//驱动器响应延迟 
const uint8_t REPLY_DELAY = 4;

//电机运行电流 0~100%
const uint8_t RUN_CURRENT_PERCENT = 40;
const int32_t VELOCITY = 12800;

void setup()
{
  Serial.begin(SERIAL_BAUD_RATE);
  stepper_driver_0.setup(serial_stream, SERIAL_BAUD_RATE, SERIAL_ADDRESS_0, RX_PIN, TX_PIN);
  stepper_driver_0.setReplyDelay(REPLY_DELAY);
  stepper_driver_1.setup(serial_stream, SERIAL_BAUD_RATE, SERIAL_ADDRESS_1, RX_PIN, TX_PIN);
  stepper_driver_1.setReplyDelay(REPLY_DELAY);
  stepper_driver_2.setup(serial_stream, SERIAL_BAUD_RATE, SERIAL_ADDRESS_2, RX_PIN, TX_PIN);
  stepper_driver_2.setReplyDelay(REPLY_DELAY);
  // 设置引脚为输出模式
  pinMode(StepperEnaPin, OUTPUT);
  // 输出高电平
  digitalWrite(StepperEnaPin, LOW);
  delay(DELAY);

  if (stepper_driver_0.isSetupAndCommunicating())
  {
    Serial.println("[Success]:Stepper driver 0 is setup and communicating!");
  }
  else {
    Serial.println("[Fail]:Stepper driver 0 is not setup and communicating!");
  }

  if (stepper_driver_1.isSetupAndCommunicating())
  {
    Serial.println("[Success]:Stepper driver 1 is setup and communicating!");
  }
  else {
    Serial.println("[Fail]:Stepper driver 1 is not setup and communicating!");
  }

  if (stepper_driver_2.isSetupAndCommunicating())
  {
    Serial.println("[Success]:Stepper driver 2 is setup and communicating!");
  }
  else {
    Serial.println("[Fail]:Stepper driver 2 is not setup and communicating!");
  }
  
  // 设置微步
  stepper_driver_0.setMicrostepsPerStep(MICROSTEPPING);
  stepper_driver_0.setHardwareEnablePin(StepperEnaPin);
  stepper_driver_0.setRunCurrent(RUN_CURRENT_PERCENT);
  stepper_driver_0.enableCoolStep();
  stepper_driver_0.enable();
  stepper_driver_0.moveAtVelocity(VELOCITY);
  delay(DELAY);

}
void loop()
{

  if (not stepper_driver_0.isSetupAndCommunicating())
  {
    Serial.println("Stepper driver not setup and communicating!");
    return;
  }

  bool hardware_disabled = stepper_driver_0.hardwareDisabled();
  TMC2209::Settings settings = stepper_driver_0.getSettings();
  TMC2209::Status status = stepper_driver_0.getStatus();

  if (hardware_disabled)
  {
    Serial.println("Stepper driver is hardware disabled!");
  }
  else if (not settings.software_enabled)
  {
    Serial.println("Stepper driver is software disabled!");
  }
  else if ((not status.standstill))
  {
    Serial.print("Moving at velocity ");
    Serial.println(VELOCITY);

    uint32_t interstep_duration = stepper_driver_0.getInterstepDuration();
    Serial.print("which is equal to an interstep_duration of ");
    Serial.println(interstep_duration);
  }
  else
  {
    Serial.println("Not moving, something is wrong!");
  }

  Serial.println();
  delay(DELAY);


}

3.效果展示

如果硬件和代码都没问题的话,你就能看到电机在以每1RPM的转速转动,串口中出现一下通讯情况啦。

需要注意的是,TMC2209原生库只能完成对电机的速度控制,如果要实现对电机的微步控制,加速度控制等功能,就需要配合其他库来完成,如AccelStepper库。因为说白了,TMC2209只是对驱动器进行设置的库,驱动器还是需要接受来自控制器的脉冲信号才能驱动电机旋转。

下一篇将介绍如何使用TMC2209库和AccelStepper库完成对电机的精确位置控制。

相关推荐
微信153237942437 小时前
离线语音识别 ( 小语种国家都支持)可定制词组
嵌入式硬件
weixin_508821657 小时前
1ms软延时,不用定时器,stm32
stm32·单片机·嵌入式硬件
月阳羊8 小时前
【无人机】无人机PX4飞控系统高级软件架构
嵌入式硬件·架构·系统架构·无人机
weixin_508821659 小时前
stm32 主频216MHz,写个ms延时函数,us延时函数
stm32·单片机·嵌入式硬件
华清远见IT开放实验室9 小时前
【新书上市】《STM32嵌入式系统开发—基于STM32CubeMX和HAL库》,高校教学推荐,STM32入门必读书籍!
stm32·单片机·嵌入式硬件·虚拟仿真·必读书籍·高校教学
智木芯语9 小时前
【58】编程技巧:单片机编程命名规范
单片机·嵌入式硬件·嵌入式·#stm32·#stc8
捷配datasheet9 小时前
OP2177运算放大器:高性能模拟信号处理的关键元件
嵌入式硬件·信号处理
逼子格11 小时前
五种音频器件综合对比——《器件手册--音频器件》
嵌入式硬件·音视频·硬件工程师·硬件测试·电子器件·硬件笔试真题·音频器件
niuTaylor13 小时前
STM32平衡车开发实战教程:从零基础到项目精通
stm32·单片机·嵌入式硬件