MSPM0L1306 控制 TB6612 驱动四个 JBGB37-520 编码器减速电机代码示例
#include <MSPM0L1306.h>
// \u5B9A\u4E49\u9A71\u52A8\u5F15\u811A #define AIN1 2 #define AIN2 3 #define BIN1 4 #define BIN2 5 #define STBY 6
// \u5B9A\u4E49\u7F16\u7801\u5668\u5F15\u811A #define ENC1A 7 #define ENC1B 8 #define ENC2A 9 #define ENC2B 10 #define ENC3A 11 #define ENC3B 12 #define ENC4A 13 #define ENC4B A0
// \u521D\u59CB\u5316\u9A71\u52A8\u5BF9\u8C61 MSPM0L1306 motorDriver(AIN1, AIN2, BIN1, BIN2, STBY);
// \u521D\u59CB\u5316\u7F16\u7801\u5668\u5BF9\u8C61 Encoder encoder1(ENC1A, ENC1B); Encoder encoder2(ENC2A, ENC2B); Encoder encoder3(ENC3A, ENC3B); Encoder encoder4(ENC4A, ENC4B);
// \u8BBE\u7F6E\u7535\u673A\u901F\u5EA6 void setMotorSpeed(int motorIndex, int speed) { if (speed >= 0) { motorDriver.setMotorSpeed(motorIndex, speed, FORWARD); } else { motorDriver.setMotorSpeed(motorIndex, -speed, BACKWARD); } }
// \u8BFB\u53D6\u7F16\u7801\u5668\u503C long readEncoderValue(int encoderIndex) { switch (encoderIndex) { case 1: return encoder1.read(); case 2: return encoder2.read(); case 3: return encoder3.read(); case 4: return encoder4.read(); default: return 0; } }
void setup() { // \u521D\u59CB\u5316\u4E32\u53E3\u901A\u4FE1 Serial.begin(9600);
// \u8BBE\u7F6E\u9A71\u52A8\u5F15\u811A\u4E3A\u8F93\u51FA\u6A21\u5F0F pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); pinMode(STBY, OUTPUT);
// \u542F\u52A8\u9A71\u52A8\u5668 motorDriver.begin();
// \u8BBE\u7F6E\u7F16\u7801\u5668\u5F15\u811A\u4E3A\u8F93\u5165\u6A21\u5F0F pinMode(ENC1A, INPUT_PULLUP); pinMode(ENC1B, INPUT_PULLUP); pinMode(ENC2A, INPUT_PULLUP); pinMode(ENC2B, INPUT_PULLUP); pinMode(ENC3A, INPUT_PULLUP); pinMode(ENC3B, INPUT_PULLUP); pinMode(ENC4A, INPUT_PULLUP); pinMode(ENC4B, INPUT_PULLUP); }
void loop() { // \u8BFB\u53D6\u7F16\u7801\u5668\u503C long encoder1Value = readEncoderValue(1); long encoder2Value = readEncoderValue(2); long encoder3Value = readEncoderValue(3); long encoder4Value = readEncoderValue(4);
// \u8F93\u51FA\u7F16\u7801\u5668\u503C Serial.print("Encoder 1: "); Serial.println(encoder1Value); Serial.print("Encoder 2: "); Serial.println(encoder2Value); Serial.print("Encoder 3: "); Serial.println(encoder3Value); Serial.print("Encoder 4: "); Serial.println(encoder4Value);
// \u8BBE\u7F6E\u7535\u673A\u901F\u5EA6 setMotorSpeed(1, 100); // \u8BBE\u7F6E\u7535\u673A1\u901F\u5EA6\u4E3A\u6B63\u5411100 setMotorSpeed(2, -50); // \u8BBE\u7F6E\u7535\u673A2\u901F\u5EA6\u4E3A\u53CD\u541150 setMotorSpeed(3, 0); // \u505C\u6B62\u7535\u673A3 setMotorSpeed(4, 75); // \u8BBE\u7F6E\u7535\u673A4\u901F\u5EA6\u4E3A\u6B63\u541175
delay(1000); // \u5延\u8fdf1\u79D2 }
\u6CE8\u610F\uff1a\u6B64\u4EE3\u7801\u53EA\u63D0\u4F9B\u57FA\u672C\u793A\u4F8B\uff0c\u5177\u4F53\u7684\u9A71\u52A8\u5668\u548C\u7F16\u7801\u5668\u5F15\u811A\u914D\u7F6E\u53EF\u80FD\u9700\u8981\u6839\u636E\u5B9E\u9645\u7684\u786C\u4EF6\u8FDE\u63A5\u8FDB\u884C\u8C03\u6574\u3002\u52A0\u4E0A\uff0c\u4F60\u53EF\u80FD\u6839\u636E\u5177\u4F53\u7684\u9700\u6C42\u4FEE\u6539\u4EE3\u7801\u4E2D\u7684\u7535\u673A\u901F\u5EA6\u548C\u5延\u8fdf\u65F6\u95F4\u3002
原文地址: https://www.cveoy.top/t/topic/pIsS 著作权归作者所有。请勿转载和采集!