A very simple wrapper around the PWM library.
Dependents: JS_1motor_20170707_ok Task_1_BallRidingbot_KeepingStationCatching_layingDown_1230 Task_2_BallRidingbot_MovingForwardCatching_backward_1230 testSSWMR_StationKeeping_200170830_OK ... more
This is my own wrapper around the PWM library. It handles servos with very long pulse intervals much better. It doesn't HAVE to be used with a servo, but I'm not too sure what else you could use it for. In any case, you're welcome to use it for whatever purpose you want.
Example code is shown below:
#include "mbed.h" #include "Servo.h" Servo servo(p26); int main(void) { // .calibrate(pulse interval, minimum pulse width, maximum pulse width) servo.calibrate(0.02, 0.05*0.02, 0.10*0.02); float val = 0.0; while(1) { servo.write(val); val = val < 1.0 ? val + 0.1 : 0; wait(1); } }
Servo.cpp
- Committer:
- andrewrussell
- Date:
- 2013-02-15
- Revision:
- 2:4c315bcd91b4
- Parent:
- 1:c1b2ec2662fd
File content as of revision 2:4c315bcd91b4:
#include "Servo.h" #include "mbed.h" /** * Servo constructor * * Initialize variables, attach to a PWM-enabled pin. */ Servo::Servo(PinName pwm32) : _pwm(pwm32) { _period = 0; _min = 0; _max = 0; _pos = 0; invert = 0; } /** * float read * * Read the value shat should be at the servo. */ float Servo::read(void) { return _pos; } /** * void write * * Write a value to the servo * * @param float to Positional data taking values of 0.0 to 1.0. */ void Servo::write(float to) { if(to < 0 || to > 1) return; _pos = to; float dest_pw; if(invert == 0) { dest_pw = to * _slope + _min; } else if(invert == 1) { dest_pw = to * -_slope + _max; } else { return; } _pwm.pulsewidth(dest_pw); } /** * void calibrate * * Set the limits of the PWM. * * @param float period Pulse period in seconds * @param float max Maximum pulse width in seconds * @param float min Minimum pulse width in seconds */ void Servo::calibrate(float period, float min, float max) { _period = period; _min = min; _max = max; _slope = max - min; _pwm.period(_period); }