0

我正在使用 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)
}

延迟消失了,但伺服位置确实变得难以预测。

4

1 回答 1

0

如果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);
}
于 2017-08-18T01:23:58.740 回答