方向改变时的伺服延迟
Servo delay on direction change
我正在使用 SG-90 伺服系统并在两个值之间左右扫动。
我的问题是方向变化之间有大约 500 毫秒的明显延迟。我希望舵机的位置是可预测的,但由于方向改变,它不是。
bool servoDir = true;
int servoCnt = 90;
int servoInc = 1;
int servoMin = servoCnt - 5;
int servoMax = servoCnt + 5;
void loop() {
if (servoDir) {
dx = servoInc;
if (servoCnt > servoMax) {
servoDir = false;
}
} else {
dx = -servoInc;
if (servoCnt < servoMin) {
servoDir = true;
}
}
servoCnt += dx;
servo.write(servoCnt);
// delay or other code here (mine is 40ms)
}
Arduino Servo 库和 VarspeedServo 库我都试过了。他们都显示相同的东西。
这是什么原因,我该怎么办?
更新
所以如果我在变向时加快速度,就像这样:
int extra = 5;
void loop() {
if (servoDir) {
dx = servoInc;
if (servoCnt > servoMax) {
dx -= extra;
servoDir = false;
}
} else {
dx = -servoInc;
if (servoCnt < servoMin) {
dx += extra;
servoDir = true;
}
}
servoCnt += dx;
servo.write(servoCnt);
// delay or other code here (mine is 40ms)
}
延迟消失,但伺服位置确实变得更不可预测。
如果 servoMin 和 servoMax 超出伺服范围,您将完全遇到这些症状......它们确实如此。
更严重的是,这些舵机不是很精确。一次增加 1 可能是问题所在。您正在经历某种形式的反弹。
您应该检查并钳制 incrementing/decrementing 之后的范围内的计数。这是嵌入式编程的基本规则之一。发送超出范围的值可能会危及生命,或损坏设备。
bool servoDir = true;
int servoCnt = 90;
int servoInc = 1; // <-- exclusively use this to control speed.
int servoMin = servoCnt - 5; // <-- this range is a bit short for testing
int servoMax = servoCnt + 5;
void loop() {
// 'jog' the servo.
servoCnt += (servoDir) ? servoInc : -servoInc;
// past this point, all position jogging is final
if (servoCnt > servoMax)
{
servoCnt = servoMax;
servoDir = false;
}
else if (servoCnt < servoMin)
{
servoCnt = servoMin;
servoDir = true;
}
servo.write(servoCnt);
// 40ms sounds a bit short for tests.
// 100 ms should give you a 2 seconds (0.5Hz) oscillation
delay(100);
}
我正在使用 SG-90 伺服系统并在两个值之间左右扫动。 我的问题是方向变化之间有大约 500 毫秒的明显延迟。我希望舵机的位置是可预测的,但由于方向改变,它不是。
bool servoDir = true;
int servoCnt = 90;
int servoInc = 1;
int servoMin = servoCnt - 5;
int servoMax = servoCnt + 5;
void loop() {
if (servoDir) {
dx = servoInc;
if (servoCnt > servoMax) {
servoDir = false;
}
} else {
dx = -servoInc;
if (servoCnt < servoMin) {
servoDir = true;
}
}
servoCnt += dx;
servo.write(servoCnt);
// delay or other code here (mine is 40ms)
}
Arduino Servo 库和 VarspeedServo 库我都试过了。他们都显示相同的东西。
这是什么原因,我该怎么办?
更新
所以如果我在变向时加快速度,就像这样:
int extra = 5;
void loop() {
if (servoDir) {
dx = servoInc;
if (servoCnt > servoMax) {
dx -= extra;
servoDir = false;
}
} else {
dx = -servoInc;
if (servoCnt < servoMin) {
dx += extra;
servoDir = true;
}
}
servoCnt += dx;
servo.write(servoCnt);
// delay or other code here (mine is 40ms)
}
延迟消失,但伺服位置确实变得更不可预测。
如果 servoMin 和 servoMax 超出伺服范围,您将完全遇到这些症状......它们确实如此。
更严重的是,这些舵机不是很精确。一次增加 1 可能是问题所在。您正在经历某种形式的反弹。
您应该检查并钳制 incrementing/decrementing 之后的范围内的计数。这是嵌入式编程的基本规则之一。发送超出范围的值可能会危及生命,或损坏设备。
bool servoDir = true;
int servoCnt = 90;
int servoInc = 1; // <-- exclusively use this to control speed.
int servoMin = servoCnt - 5; // <-- this range is a bit short for testing
int servoMax = servoCnt + 5;
void loop() {
// 'jog' the servo.
servoCnt += (servoDir) ? servoInc : -servoInc;
// past this point, all position jogging is final
if (servoCnt > servoMax)
{
servoCnt = servoMax;
servoDir = false;
}
else if (servoCnt < servoMin)
{
servoCnt = servoMin;
servoDir = true;
}
servo.write(servoCnt);
// 40ms sounds a bit short for tests.
// 100 ms should give you a 2 seconds (0.5Hz) oscillation
delay(100);
}