10 years, 9 months ago.

Time based servo, how to stop motor when it reaches certain value?

code below will turn servo counter-clockwise. but when it reaches (p>-2) servo still rotates, was expecting it to stop when it reached the (-2) value.

my code below

#include "mbed.h"
#include "Servo.h"
Serial pc(USBTX, USBRX); // tx, rx
    
Servo myservo(p21);

int main() {
    float p;    
    for(p=0; p>-2; p -= 0.1) {
        pc.printf("p=%3.2f\r\n", p);
        myservo = p;
        wait(0.2);
    }

my printf

p=0.00
p=-0.10
p=-0.20
p=-0.30
p=-0.40
p=-0.50
p=-0.60
p=-0.70
p=-0.80
p=-0.90
p=-1.00
p=-1.10
p=-1.20
p=-1.30
p=-1.40
p=-1.50
p=-1.60
p=-1.70
p=-1.80
p=-1.90

1 Answer

10 years, 9 months ago.

1) What type of servo are you using? I guess it is modified for continuous movement, isn't it? That would explain this issue.

2) If you had a normal servo, so the parameter of function servo(float percent); will set up required position. Also this parametr must be in range 0.0-1.0, where 0.5 is the middle position. Read more: https://mbed.org/cookbook/Servo

3) If you have a really modified servo (without a feedback potentiometer, which reads position), so you have to use your own control function with http://mbed.org/handbook/PwmOut. Maybe you need to learn more about servo control pulse (timing, duty, ...).

4) Or you can remove the remaining control electronics from servo, and use it only like a motor via motor driver (L293D, etc.). You can find more about this there: https://mbed.org/cookbook/Motor