[英]Arduino: Using motor to control two motors and measuring distance traveled
我正在尝试使用来自 pololu 的电机驱动器和由树莓派指示的 arduino uno 来控制两个电机。 但是,当我尝试读取编码器输出时出现错误,以便我可以测量发生了多少整圈,从而知道机器人走了多远。
我希望该过程看起来像这样(伪代码),而显然我的代码看起来不会像这样我认为它更好地说明了我在寻找什么:
1rotation = 4in
distance_goal = 36in
rot_req = 1rotation / distance_goal
if rotation_count < rot_req:
m1.PowerOn
m2.PowerOn
rotation_count++
我实际的arduino代码:
#include "TB67H420FTG.h"
#include <QuadratureEncoder.h>
TB67H420FTG (2,3,11,4,5,10);
Encoders leftEncoder(7,6); // Create an Encoder object name leftEncoder, using digitalpin 2 & 3
Encoders ridriverghtEncoder(15,14); // Encoder object name rightEncoder using analog pin A0 and A1
unsigned long lastMilli = 0;
void encoderTest(){
// print encoder count every 50 millisecond
if(millis()-lastMilli > 50){
long currentLeftEncoderCount = leftEncoder.getEncoderCount(); //use this anytime you need to read encoder count
long currentRightEncoderCount = rightEncoder.getEncoderCount();//use this anytime you need to read encoder count
Serial.print(currentLeftEncoderCount);
Serial.print(" , ");
Serial.println(currentRightEncoderCount);
lastMilli = millis();
}
}
void setup()
{
driver.init(); // The only thing left to do is call init() so the library can initialize the pins
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
long currentLeftEncoderCount = leftEncoder.getEncoderCount();
long currentRightEncoderCount = rightEncoder.getEncoderCount();
Serial.println(currentLeftEncoderCount);
Serial.println(CurrentRightEncoderCount);
String data = Serial.readStringUntil('\n');
Serial.print("Command Received: " + String(data));
// Move the motor clockwise a bit (our motor speed is valued from -100 to +100)
if (data == "turn A right")
driver.setMotorAPower(50); // Set the motor speed to 50% in a clockwise direction
// Move the motor anti-clockwise a bit (our motor speed is valued from -100 to +100)
else if (data == "turn A left")
driver.setMotorAPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
// Move the motor clockwise a bit (our motor speed is valued from -100 to +100)
if (data == "turn B right")
driver.setMotorBPower(50); // Set the motor speed to 50% in a clockwise direction
// Move the motor anti-clockwise a bit (our motor speed is valued from -100 to +100)
else if (data == "turn B left")
driver.setMotorBPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
else if (data == "stop A")
driver.motorABrake(); // Notice here we can brake motor using the motorABrake() function
else if (data == "stop B")
driver.motorBBrake(); // Notice here we can brake motor using the motorBBrake() function
else if (data == "stop All")
driver.brakeAll(); // Notice here we can brake both motors using the brakeAll() function
else
Serial.print("Invalid Command Received");
}
}
我指示 arduino 的 Raspberry Pi 代码:
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
ser.write(b"turn A left\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn A right\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn B left\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn B right\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop A\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop B\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop All\n")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(5)
错误本身
Arduino: 1.8.19 (Linux), Board: "Arduino Uno"
ard_code:3:14: error: expected unqualified-id before numeric constant
TB67H420FTG (2,3,11,4,5,10);
^
ard_code:3:14: error: expected ')' before numeric constant
/home/garb/Desktop/test/ard_code/ard_code.ino: In function 'void encoderTest()':
ard_code:14:37: error: 'rightEncoder' was not declared in this scope
long currentRightEncoderCount = rightEncoder.getEncoderCount();//use this anytime you need to read encoder count
^~~~~~~~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:14:37: note: suggested alternative: 'leftEncoder'
long currentRightEncoderCount = rightEncoder.getEncoderCount();//use this anytime you need to read encoder count
^~~~~~~~~~~~
leftEncoder
/home/garb/Desktop/test/ard_code/ard_code.ino: In function 'void setup()':
ard_code:27:3: error: 'driver' was not declared in this scope
driver.init(); // The only thing left to do is call init() so the library can initialize the pins
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:27:3: note: suggested alternative: 'div'
driver.init(); // The only thing left to do is call init() so the library can initialize the pins
^~~~~~
div
/home/garb/Desktop/test/ard_code/ard_code.ino: In function 'void loop()':
ard_code:37:37: error: 'rightEncoder' was not declared in this scope
long currentRightEncoderCount = rightEncoder.getEncoderCount();
^~~~~~~~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:37:37: note: suggested alternative: 'leftEncoder'
long currentRightEncoderCount = rightEncoder.getEncoderCount();
^~~~~~~~~~~~
leftEncoder
ard_code:39:20: error: 'CurrentRightEncoderCount' was not declared in this scope
Serial.println(CurrentRightEncoderCount);
^~~~~~~~~~~~~~~~~~~~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:39:20: note: suggested alternative: 'currentRightEncoderCount'
Serial.println(CurrentRightEncoderCount);
^~~~~~~~~~~~~~~~~~~~~~~~
currentRightEncoderCount
ard_code:46:5: error: 'driver' was not declared in this scope
driver.setMotorAPower(50); // Set the motor speed to 50% in a clockwise direction
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:46:5: note: suggested alternative: 'div'
driver.setMotorAPower(50); // Set the motor speed to 50% in a clockwise direction
^~~~~~
div
ard_code:50:5: error: 'driver' was not declared in this scope
driver.setMotorAPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:50:5: note: suggested alternative: 'div'
driver.setMotorAPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
^~~~~~
div
ard_code:54:5: error: 'driver' was not declared in this scope
driver.setMotorBPower(50); // Set the motor speed to 50% in a clockwise direction
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:54:5: note: suggested alternative: 'div'
driver.setMotorBPower(50); // Set the motor speed to 50% in a clockwise direction
^~~~~~
div
ard_code:58:5: error: 'driver' was not declared in this scope
driver.setMotorBPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:58:5: note: suggested alternative: 'div'
driver.setMotorBPower(-50); // Set the motor speed to 50% in a anti-clockwise direction
^~~~~~
div
ard_code:61:5: error: 'driver' was not declared in this scope
driver.motorABrake(); // Notice here we can brake motor using the motorABrake() function
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:61:5: note: suggested alternative: 'div'
driver.motorABrake(); // Notice here we can brake motor using the motorABrake() function
^~~~~~
div
ard_code:64:5: error: 'driver' was not declared in this scope
driver.motorBBrake(); // Notice here we can brake motor using the motorBBrake() function
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:64:5: note: suggested alternative: 'div'
driver.motorBBrake(); // Notice here we can brake motor using the motorBBrake() function
^~~~~~
div
ard_code:67:5: error: 'driver' was not declared in this scope
driver.brakeAll(); // Notice here we can brake both motors using the brakeAll() function
^~~~~~
/home/garb/Desktop/test/ard_code/ard_code.ino:67:5: note: suggested alternative: 'div'
driver.brakeAll(); // Notice here we can brake both motors using the brakeAll() function
^~~~~~
div
exit status 1
expected unqualified-id before numeric constant
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
在这件事上的任何帮助将不胜感激!
这不是答案,只是对代码问题的描述。 还要记住,我知道 C++,但对 Arduino 一无所知。
我不确定您期望TB67H420FTG (2,3,11,4,5,10);
要做,但TB67H420FTG
是一个类的名称,应该以与下一行的Encoders
类似的方式使用。 您必须创建一个TB67H420FTG
对象。 这可以通过TB67H420FTG motor;
或TB67H420FTG motor(a, b);
其中motor
是对象的名称, a
和b
是指示电机方向的整数参数(无论是什么意思)。
稍后在代码中您开始使用一个名为drivers
的变量,例如driver.setMotorAPower(50);
尚未在任何地方宣布。 显然这需要一个声明,尽管我不知道那会是什么。
老实说,这些代码看起来就像您刚刚组合了您在互联网上找到的各种代码。 它看起来也不像您发布的伪代码。 看来您将不得不花一些时间学习基本的 C++。 这不是一种你可以随心所欲地学习的语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.