Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: JS_1motor_20170707_ok Task_1_BallRidingbot_KeepingStationCatching_layingDown_1230 Task_2_BallRidingbot_MovingForwardCatching_backward_1230 testSSWMR_StationKeeping_200170830_OK ... more
Servo.cpp
- Committer:
- andrewrussell
- Date:
- 2013-02-15
- Revision:
- 0:e50cd1696bca
- Child:
- 1:c1b2ec2662fd
File content as of revision 0:e50cd1696bca:
#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;
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
* @param float max Maximum pulse width
* @param float min Minimum pulse width
*/
void Servo::calibrate(float period, float max, float min) {
_period = period;
_min = min;
_max = max;
_slope = max - min;
_pwm.period(_period);
}