首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Arduino:用电机控制两台电机并测量行程

Arduino:用电机控制两台电机并测量行程
EN

Stack Overflow用户
提问于 2022-07-20 20:48:53
回答 1查看 116关注 0票数 -1

我试图使用一个两台电动机和一个arduino uno来控制来自pololu的马达驱动器,这是由一个覆盆子圆周率指示的。然而,当我试图读取编码器输出时,我会遇到错误,这样我就可以测量出发生了多少次完整的旋转,这样我就知道机器人走了多远。

我希望流程看起来像这样(psuedo代码),而我的代码显然不是这样的--我认为它更好地说明了我要寻找的是什么:

代码语言:javascript
运行
复制
1rotation = 4in
distance_goal = 36in
rot_req = 1rotation / distance_goal
if rotation_count < rot_req:
    m1.PowerOn
    m2.PowerOn
    rotation_count++

我的实际arduino代码:

代码语言:javascript
运行
复制
#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:

代码语言:javascript
运行
复制
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)

错误本身

代码语言:javascript
运行
复制
 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.

在这件事上有任何帮助将是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 06:50:41

这不是一个答案,只是对代码问题的描述。还请记住,我知道C++,但对Arduino一无所知。

我不知道您期望TB67H420FTG (2,3,11,4,5,10);做什么,但是TB67H420FTG是类的名称,应该以与下一行中的Encoders类似的方式使用。您必须创建一个TB67H420FTG对象。这可以通过以下两种方式之一实现:TB67H420FTG motor;TB67H420FTG motor(a, b);,其中motor是对象的名称,ab是指示电机方向的整数参数(无论这意味着什么)。

在后面的代码中,您开始使用一个名为driver的变量,例如driver.setMotorAPower(50);,它在任何地方都没有声明。显然,这需要一份声明,尽管我不知道那会是什么。

老实说,代码看起来就像你刚刚组合了你在互联网上找到的各种代码。它看起来也不像你发布的伪代码。看来你得花些时间学习基本的C++。这不是一种你可以随心所欲学习的语言。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73057974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档