我的伺服不动,只有在尝试写入 180 度时才移动
My Servo is Not Moving, Only When Trying Write 180 degrees
我的舵机仅在设置为 180 度时才开始 "change"(代码告诉我的)。
我已经尝试搜索该网站以及 Google,但这似乎必须非常具体地以一种高级的不可重复的方式写入 Servo。
改变伺服的代码是:
#include <SoftwareSerial.h>
#include <Servo.h>
const int rxPin = 12;
const int txPin = 13;
SoftwareSerial bluetooth (rxPin, txPin);
Servo myServo;
boolean canMove = true;
int rotation = 0;
int currentRotation = 0;
void setup() {
Serial.begin(9600); // Begins Serial communication
bluetooth.begin(9600);
myServo.attach(11); // Sets a servo on pin 11
}
void loop() {
// put your main code here, to run repeatedly:
// Checks if Serial has something to read
if (bluetooth.available() > 0) {
rotation = bluetooth.read(); // Reads it and sets it to an integer
}
currentRotation = myServo.read();
if (currentRotation != rotation) {
Serial.println("Current Rotation:");
Serial.println(currentRotation);
Serial.println("Set to:");
Serial.println(rotation);
for (int i = currentRotation; i < rotation; i++) {
Serial.println("Writing to servo");
myServo.write(i);
}
}
}
有一个处理程序将数据发送到 Arduino,但我可以完美地看到 Arduino 的串行监视器中输入的数字(它们从 0 到 180 不等)
执行此操作后,串行监视器中唯一显示的是:
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
一遍又一遍。伺服只是来回抽动。它唯一一次改变(要设置的数字来自处理程序)是当数字设置为 180 时。然后它来回移动得更多并说:
Current Rotation:
179
Set to:
180
Writing to servo
Current Rotation:
179
Set to:
180
Writing to servo
一遍又一遍。这是怎么回事,我该如何解决?干杯,感谢您的帮助!
您的代码存在一些问题:
不需要读取当前舵机值,只需要保存上次给定的值即可。
一步步移动舵机不是一个好的选择,因为它可能会有一些移动误差。您需要根据移动阈值找到动态延迟。例如,假设您的舵机从 0 到 180 完全移动的最大延迟时间为 2 秒,那么如果您需要将舵机移动 90 度,则需要 1 秒延迟。
你只需要在新数据到来时移动舵机,所以在新数据到来时设置舵机。
记得根据你的舵机设置max_delay
#include <SoftwareSerial.h>
#include <Servo.h>
const int rxPin = 12;
const int txPin = 13;
SoftwareSerial bluetooth(rxPin, txPin);
Servo myServo;
boolean canMove = true;
int rotation = 0;
int currentRotation = 0;
// how much time takes servo to move
int move_delay = 0;
// maximum time for changing servo state from lowest to highest value
const int max_delay = 2;
void setup()
{
Serial.begin(9600); // Begins Serial communication
bluetooth.begin(9600);
myServo.attach(11); // Sets a servo on pin 11
}
void loop()
{
// put your main code here, to run repeatedly:
// Checks if Serial has something to read
if (bluetooth.available() > 0)
{
rotation = bluetooth.read(); // Reads it and sets it to an integer
Serial.print("New value: ");
Serial.println(rotation);
Serial.print("Current Rotation: ");
Serial.println(rotation);
if (currentRotation != rotation)
{
Serial.println("Setting new value");
// find a proper delay
move_delay = (max_delay / 180) * (abs(rotation - currentRotation)) * 1000;
myServo.write(rotation);
delay(move_delay);
currentRotation = rotation;
}
}
}
我的舵机仅在设置为 180 度时才开始 "change"(代码告诉我的)。
我已经尝试搜索该网站以及 Google,但这似乎必须非常具体地以一种高级的不可重复的方式写入 Servo。
改变伺服的代码是:
#include <SoftwareSerial.h>
#include <Servo.h>
const int rxPin = 12;
const int txPin = 13;
SoftwareSerial bluetooth (rxPin, txPin);
Servo myServo;
boolean canMove = true;
int rotation = 0;
int currentRotation = 0;
void setup() {
Serial.begin(9600); // Begins Serial communication
bluetooth.begin(9600);
myServo.attach(11); // Sets a servo on pin 11
}
void loop() {
// put your main code here, to run repeatedly:
// Checks if Serial has something to read
if (bluetooth.available() > 0) {
rotation = bluetooth.read(); // Reads it and sets it to an integer
}
currentRotation = myServo.read();
if (currentRotation != rotation) {
Serial.println("Current Rotation:");
Serial.println(currentRotation);
Serial.println("Set to:");
Serial.println(rotation);
for (int i = currentRotation; i < rotation; i++) {
Serial.println("Writing to servo");
myServo.write(i);
}
}
}
有一个处理程序将数据发送到 Arduino,但我可以完美地看到 Arduino 的串行监视器中输入的数字(它们从 0 到 180 不等)
执行此操作后,串行监视器中唯一显示的是:
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
Current Rotation:
93
Set to:
0
一遍又一遍。伺服只是来回抽动。它唯一一次改变(要设置的数字来自处理程序)是当数字设置为 180 时。然后它来回移动得更多并说:
Current Rotation:
179
Set to:
180
Writing to servo
Current Rotation:
179
Set to:
180
Writing to servo
一遍又一遍。这是怎么回事,我该如何解决?干杯,感谢您的帮助!
您的代码存在一些问题:
不需要读取当前舵机值,只需要保存上次给定的值即可。
一步步移动舵机不是一个好的选择,因为它可能会有一些移动误差。您需要根据移动阈值找到动态延迟。例如,假设您的舵机从 0 到 180 完全移动的最大延迟时间为 2 秒,那么如果您需要将舵机移动 90 度,则需要 1 秒延迟。
你只需要在新数据到来时移动舵机,所以在新数据到来时设置舵机。
记得根据你的舵机设置max_delay
#include <SoftwareSerial.h>
#include <Servo.h>
const int rxPin = 12;
const int txPin = 13;
SoftwareSerial bluetooth(rxPin, txPin);
Servo myServo;
boolean canMove = true;
int rotation = 0;
int currentRotation = 0;
// how much time takes servo to move
int move_delay = 0;
// maximum time for changing servo state from lowest to highest value
const int max_delay = 2;
void setup()
{
Serial.begin(9600); // Begins Serial communication
bluetooth.begin(9600);
myServo.attach(11); // Sets a servo on pin 11
}
void loop()
{
// put your main code here, to run repeatedly:
// Checks if Serial has something to read
if (bluetooth.available() > 0)
{
rotation = bluetooth.read(); // Reads it and sets it to an integer
Serial.print("New value: ");
Serial.println(rotation);
Serial.print("Current Rotation: ");
Serial.println(rotation);
if (currentRotation != rotation)
{
Serial.println("Setting new value");
// find a proper delay
move_delay = (max_delay / 180) * (abs(rotation - currentRotation)) * 1000;
myServo.write(rotation);
delay(move_delay);
currentRotation = rotation;
}
}
}