我正试图使用pololu的电机驱动器和由树莓pi指示的arduino uno来控制两个电机。然而,当我试图读取编码器输出时,我会遇到错误,这样我就可以测量发生了多少整圈,这样我就能知道机器人走了多远。
我希望这个过程看起来像这样(psuedo代码(,虽然很明显我的代码不会像这样,但我认为它更好地说明了我正在寻找的东西:
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");
}
}
我的Raspberry Pi代码,指示arduino:
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
ser.write(b"turn A leftn")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn A rightn")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn B leftn")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"turn B rightn")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop An")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop Bn")
line = ser.readline().decode('utf-8').rstrip()
print("Data Received from Arduino: ",line)
time.sleep(3)
ser.write(b"stop Alln")
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
是指示电机方向的整数参数(不管这意味着什么(。
在代码的后面,您开始使用一个名为driver
的变量,例如driver.setMotorAPower(50);
,它在任何地方都没有声明。显然,这需要一份声明,尽管我不知道那会是什么。
老实说,代码看起来就像你刚刚把你在互联网上找到的各种代码组合在一起。它看起来也一点也不像你发布的伪代码。看来你将不得不花一些时间学习基本的C++。这不是一种你可以边走边学的语言。