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

相关推荐
wenchm26 分钟前
细说STM32F407单片机SPI基础知识
stm32·单片机·嵌入式硬件
jikuaidi6yuan1 小时前
STM32 ADC 配置
stm32·单片机·嵌入式硬件
Be Legendary-CGK3 小时前
有源模拟滤波器的快速设计
嵌入式硬件·硬件工程
Anin蓝天(北京太速科技-陈)3 小时前
204-基于Xilinx Virtex-6 XC6VLX240T 和TI DSP TMS320C6678的信号处理板
嵌入式硬件·fpga开发·信号处理
Jack电子实验室3 小时前
STM32 出租车计价器系统设计(一) 江科大源码改写
c语言·stm32·单片机·嵌入式硬件·嵌入式
腾飞的信仰4 小时前
51单片机 串口UART
单片机·嵌入式硬件·51单片机
电子科技圈4 小时前
XMOS将在CES 2025上展出多款由边缘AI驱动的创新音效、音频、识别和处理解决方案
人工智能·科技·嵌入式硬件·mcu·物联网·音视频·iot
lantiandianzi5 小时前
基于单片机的输液速度监控系统设计
单片机·嵌入式硬件
紫阡星影5 小时前
【模块系列】STM32&PCF8563
c语言·stm32·单片机·嵌入式硬件
憧憬一下7 小时前
深入解析PCIe设备事务层与配置过程
arm开发·嵌入式硬件·嵌入式·pcie·linux驱动开发