当前位置: 首页 > news >正文

【电机控制】42步进电机+arduino:WHEELTEC_MS42DDC

轮趣科技

42步进电机+arduino:WHEELTEC_MS42DDC

接线方式:

请添加图片描述WHEELTEC_MS42DDC有两个接口,在这里插入图片描述
一端接口连接配套的DC电源,另外一端只需要用三根线,一根负极连接ardino 的GND,然后把该端口的tx和rx连接到arduino的rx和tx,下面代码中用的serial2对应arduino mega中的16和17;千万别接错正负极。

代码如下

#include <Arduino.h>

// 定义串口通信的波特率,根据 MS42DC 电机的 USB 串口控制协议,波特率为 115200
const long baudRate = 115200;

// 帧头,为固定值 0x7B
const byte START_BYTE = 0x7B;
// 控制 ID,现在为 0x02
const byte CONTROL_ID = 0x01;

void setup() {
  // 初始化调试串口
  Serial.begin(115200);
  while (!Serial) delay(1);

  // 初始化与电机通信的串口
  Serial2.begin(baudRate);
  Serial.println("Serial communication initialized");
}

// 发送控制信息到电机的函数
void sendMotorCommand(byte controlMode, byte direction, byte microstepping, int value1, int value2) {
  byte command[11];
  // 帧头
  command[0] = START_BYTE;
  // 控制 ID
  command[1] = CONTROL_ID;
  // 控制模式
  command[2] = controlMode;
  // 转向
  command[3] = direction;
  // 细分值
  command[4] = microstepping;
  // 数据字节 1
  command[5] = highByte(value1);
  // 数据字节 2
  command[6] = lowByte(value1);
  // 数据字节 3
  command[7] = highByte(value2);
  // 数据字节 4
  command[8] = lowByte(value2);
  // 计算 BCC 校验位,为前面九个字节的异或和
  byte bcc = command[0] ^ command[1] ^ command[2] ^ command[3] ^ command[4] ^ command[5] ^ command[6] ^ command[7] ^ command[8];
  command[9] = bcc;
  // 帧尾
  command[10] = 0x7D;
  
  Serial2.write(command, 11);
  Serial.print("Sent command to motor: ");
  for (int i = 0; i < 11; i++) {
    Serial.print(command[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

void loop() {
  // // 速度控制模式示例
  // sendMotorCommand(0x01, 1, 0x20, 0, 0x0064); // 顺时针,32 细分,速度为 10 Rad/s
  // delay(1000);
  
  // // 位置控制模式示例
  // sendMotorCommand(0x02, 0, 0x20, 0x2710, 0x0064); // 逆时针,32 细分,位置为 1000 度,速度为 10 Rad/s
  // delay(1000);
  
  // // 力矩控制模式示例
  // sendMotorCommand(0x03, 1, 0x20, 0x03E8, 0x0064); // 顺时针,32 细分,电流为 1000 mA,速度为 10 Rad/s
  // delay(1000);
  
  // 单圈绝对角度控制模式示例
  sendMotorCommand(0x04, 0, 0x20, 0x04B0, 0x0064); // 逆时针,32 细分,目标角度为 100 度,速度为 10 Rad/s
  delay(1000);
  sendMotorCommand(0x04, 0, 0x20, 0x03E8, 0x0064); // 逆时针,32 细分,目标角度为 100 度,速度为 10 Rad/s
  delay(1000);
  sendMotorCommand(0x04, 0, 0x20, 0x0320, 0x0064); // 逆时针,32 细分,目标角度为 100 度,速度为 10 Rad/s
  delay(1000);
  sendMotorCommand(0x04, 0, 0x20, 0x0258, 0x0064); // 逆时针,32 细分,目标角度为 100 度,速度为 10 Rad/s
  delay(1000);
  // 接收电机的状态反馈(如果有)
  if (Serial2.available() > 0) {
    byte buffer[32];
    int bytesRead = Serial2.readBytes(buffer, Serial2.available());
    Serial.print("Received ");
    Serial.print(bytesRead);
    Serial.println(" bytes from motor:");
    for (int i = 0; i < bytesRead; i++) {
      Serial.print(buffer[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

相关文章:

  • ubuntu部署小笔记-采坑
  • 【Java】File 类
  • SQLite 删除表
  • 054 redisson
  • 安全面试3
  • Python 中read、readline、readlines 有哪些区别?
  • 针对不同文本长度的处理方案,结合层次化编码和检索优化策略
  • C语言数据结构———循环消息队列,数组方式实现
  • 零基础学QT、C++(六)制作桌面摄像头软件
  • 利用miniqmt实现远程下单:量化交易的新突破
  • Python爬虫-批量爬取股票数据猫各股票代码
  • Open WebUI 是什么
  • centos9安装k8s集群
  • C#从入门到精通(34)—如何防止winform程序被同时打开多次
  • 嵌入式之宏定义编程机制
  • vue中将el-table导出为excel文件
  • Django Admin: 动态合并数据库和预定义选项的高级过滤器实现
  • 【废物研究生刷算法】字符串
  • 嵌入式标志位解决程序卡顿问题
  • 工具--安川伺服故障代码
  • 秦洪看盘|涌现新逻辑,A股放量回升
  • 金沙记忆|元谋龙街渡:是起点也是终点
  • 首次面向上海、江苏招收本科生,西湖大学接连发布招生简章
  • “译通天下·言立寰宇”:华东师大翻译家的精神传承
  • 新能源车盈利拐点:8家上市车企去年合计净利854亿元,多家扭亏
  • 五一去哪儿|外国朋友来中国,“买买买”成为跨境旅游新趋势