Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 5 months ago.
Help with Servo.read() function
Hi there!
I am currently trying to use a servo motor library:
Servo Library https://os.mbed.com/users/simon/code/Servo/ Servo library
It works fine with everything except with the .read() function. I put the output to a serial output to see the output. It is not showing what is the current condition, and just showed "0.5000" not the real value, like in the picture I attach below:

The code is:
Servo - trial using Servo.h as the library
#include "mbed.h"
#include "Servo.h"
Servo myservo(PWM_OUT);
Serial pc(USBTX, USBRX);
int main() {
float range = 0.0005;
float position = 0.5;
float state = 0.1f;
printf("I am on\n");
myservo.calibrate (range, 90.0);
while (1){
pc.printf("start new!");
myservo = 0.5;
wait(1);
state = myservo.read();
pc.printf("Current: %.5f \n", state);
state = 0;
wait(1);
myservo.position(-45);
wait(1);
state = myservo.read();
pc.printf("Current: %.5f \n", state);
state = 0;
wait(1);
myservo.position(0);
wait(1);
state = myservo.read();
pc.printf("Current: %.5f \n", state);
state = 0;
wait(1);
myservo.position(30);
wait(1);
state = myservo.read();
pc.printf("Current: %.2f \n", state);
state = 0;
wait(1);
myservo.position(45);
wait(1);
state = myservo.read();
pc.printf("Current: %.2f \n", state);
state = 0;
wait(1);
myservo.position(90);
wait(1);
state = myservo.read();
pc.printf("Current: %.2f \n", state);
state = 0;
wait(1);
}
}
If someone can help me, it'll be really great!
Thank you soo much for your help!
Cheers!
1 Answer
7 years, 5 months ago.
I looked at the definition of the position() method. It does not save the position _p so that it can be read back with read. Seems like a bug.
it you were to use "write" in place of "position" then "read" might work.
It would be an easy fix to the library. the position() methane needs to compute and save a value in _p
It is a one line fix.