我使用的是一个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)
}延迟消失,但伺服位置确实变得难以预测。
发布于 2017-08-18 01:23:58
如果servoMin和servoMax超出伺服范围,你就会遇到这些症状.而他们就是。
更严重的是,这些伺服系统并不十分精确。一次递增1很可能是个问题。你正在经历某种形式的反弹。
您应该检查和钳制,在增量/递减之后,相对于范围进行计数。这是嵌入式编程的基本规则之一。当发射出射程值时,可能会危及生命,或摧毁设备。
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);
}https://stackoverflow.com/questions/45745766
复制相似问题