meArm arduino 机器人的 C++ 代码不起作用

问题描述 投票:0回答:1

我在使用我的 Arm arduino 机器人时遇到了一些问题。 它应该要求用户输入伺服电机的角度值并旋转它们,但是它只执行前两次,然后停止工作。
代码有错误吗?

#include <Servo.h>

int angle1;
int angle2;
int angle3;

Servo s1;
Servo s2;
Servo s3;
Servo s4;

const int BUFFER_SIZE = 100;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600);
  
  s1.attach(10);
  s2.attach(9);
  s3.attach(8);

  s1.write(90);
  s2.write(0);
  s3.write(0);
}

void loop() {
  Serial.println("Set the angle for s1 servo");
  while (Serial.available() == 0) {};
  angle1 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  Serial.println("Set the angle for s2 servo");
  while (Serial.available() == 0) {};
  angle2 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  Serial.println("Set the angle for s3 servo");
  while (Serial.available() == 0) {};
  angle3 = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);

  ForwardKinematics(angle1, angle2, angle3);
}

/**
 * Rotates the servo motors by the given angles.
 * 
 * @param angle1 The angle by which the servo s1 is rotated.
 * @param angle2 The angle by which the servo s2 is rotated.
 * @param angle3 The angle by which the servo s3 is rotated.
 */
void ForwardKinematics(int angle1, int angle2, int angle3) {
  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  delay(500);
}
arduino robotics arduino-c++
1个回答
0
投票

你最大的问题是,

Serial.readBytesUntil(' ', buf, BUFFER_SIZE);

仅返回读入缓冲区的字符数,而不返回数据。不知道你说的前两次有效是什么意思。您的意思是它循环两次,还是它只获取前两个伺服系统的数据,而不是第三个伺服系统的数据。无论如何,下面是在 UNO 上测试的工作代码。

#include <Servo.h>

int angle1;
int angle2;
int angle3;

Servo s1;
Servo s2;
Servo s3;
Servo s4;

String data = "";

const int BUFFER_SIZE = 100;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600);
  
  s1.attach(10);
  s2.attach(9);
  s3.attach(8);

  s1.write(90);
  s2.write(0);
  s3.write(0);
}

void loop() {
  angle1 = 0;
  angle2 = 0;
  angle3 = 0;
  
  Serial.println("Set the angle for s1 servo");
  while (Serial.available() == 0) {};
  int rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle1 = data.toInt();
  data = "";
  
  Serial.println("Set the angle for s2 servo");
  while (Serial.available() == 0) {};
  rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle2 = data.toInt();
  data = "";
  
  Serial.println("Set the angle for s3 servo");
  while (Serial.available() == 0) {};
  rlen = Serial.readBytesUntil('\n', buf, BUFFER_SIZE);
  for(int i = 0; i < rlen; i++){
    data += buf[i];
  }
  angle3 = data.toInt();
  data = "";
  

  ForwardKinematics(angle1, angle2, angle3);
}

/**
 * Rotates the servo motors by the given angles.
 * 
 * @param angle1 The angle by which the servo s1 is rotated.
 * @param angle2 The angle by which the servo s2 is rotated.
 * @param angle3 The angle by which the servo s3 is rotated.
 */
void ForwardKinematics(int angle1, int angle2, int angle3) {
  s1.write(angle1);
  s2.write(angle2);
  s3.write(angle3);
  delay(500);
}
© www.soinside.com 2019 - 2024. All rights reserved.