fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
948 stars 89 forks source link

Control Servo to Specific Angles #94

Closed bradocchs closed 4 years ago

bradocchs commented 4 years ago

Hello,

I'm trying to control a servo to specific angles, but I'm having trouble figuring out how. Is there any example code on how to move a servo to a specific angle and then hold that position?

I've tried different things with both servoWrite and pwmWrite with no luck.

Thanks!!

Brad

fivdi commented 4 years ago

The angle will have to be converted to microseconds with code something like this:

let angle = 45;
let min = 1000; // microsecond value for fully counter-clockwise
let max = 2000; // microsecond value for fully clockwise
let microseconds = (min + (angle / 180) * (max - min));

pin.servoWrite(microseconds);
fivdi commented 4 years ago

I think the required information has been provided so I'll go ahead and close this issue.

bradocchs commented 4 years ago

When I do this it just spins CW or CCW (depending on microsecond value) until the next command or end of program. I've tried with and without setInterval. I'm using a Parallax continuous rotation servo for testing on GPIO 10 with a RPI 4. I was hoping to be able to rotate 90 degrees and stop. It also seems to coast instead of brake. I may end up using an Arduino which works fine.

fivdi commented 4 years ago

If it's a continuous servo set the angle to 90 degrees (approx. 1500 microseconds) to stop and hold the servo.

bradocchs commented 4 years ago

Quick update: I had to adjust the pot on the continuous to get it to stop and then I ended up switching to a standard servo and everything works great!