【单片机】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库完成对电机的精确位置控制。

相关推荐
雯宝4 小时前
STM32 GPIO工作模式
stm32·单片机·嵌入式硬件
辰哥单片机设计6 小时前
STM32项目分享:智能厨房安全检测系统
stm32·单片机·嵌入式硬件
lshzdq7 小时前
【嵌入式开发】stm32 st-link 烧录
嵌入式硬件
山羊硬件Time8 小时前
详解单片机学的是什么?(电子硬件)
单片机·硬件工程师·硬件开发·电子工程师·电子硬件
Chambor_mak9 小时前
stm32单片机个人学习笔记14(USART串口数据包)
stm32·单片机·学习
tadus_zeng9 小时前
51单片机(三) UART协议与串口通信实验
单片机·嵌入式硬件·51单片机
ZLG_zhiyuan9 小时前
ZLG嵌入式笔记 | 电源设计避坑(下)
单片机·嵌入式硬件
wenchm11 小时前
细说STM32F407单片机电源低功耗StopMode模式及应用示例
stm32·单片机·嵌入式硬件
7yewh12 小时前
嵌入式知识点总结 C/C++ 专题提升(七)-位操作
c语言·c++·stm32·单片机·mcu·物联网·位操作
wenchm12 小时前
细说STM32F407单片机电源低功耗StandbyMode待机模式及应用示例
stm32·单片机·嵌入式硬件