Library for using the TLC5940 as a servo controller.
Revision 5:56daa8c0697d, committed 2014-10-21
- Comitter:
- dudanian
- Date:
- Tue Oct 21 06:26:25 2014 +0000
- Parent:
- 4:95305d4b0544
- Child:
- 6:b001282e967b
- Commit message:
- Added mbed official docs for the Server inner class as well as support for the position(float degrees) function from the official mbed class.
Changed in this revision
TLC5940Servo.cpp | Show annotated file Show diff for this revision Revisions of this file |
TLC5940Servo.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/TLC5940Servo.cpp Tue Oct 21 06:14:58 2014 +0000 +++ b/TLC5940Servo.cpp Tue Oct 21 06:26:25 2014 +0000 @@ -87,14 +87,17 @@ write(0.5); } -// used source code, but converted pwm pulse to 12 bit value -// assuming 20ms period, divide to get percentage and multiply to get value void TLC5940Servo::Servo::write(float percent) { float offset = _range * 2.0 * (percent - 0.5); _pw = (int)(4095 - ((0.0015 + clamp(offset, -_range, _range))/0.02 * 4096.0)); _p = clamp(percent, 0.0, 1.0); } +void TLC5940Servo::Servo::position(float degrees) { + float offset = _range * (degrees / _degrees); + _pw = (int)(4095 - ((0.0015 + clamp(offset, -_range, _range))/0.02 * 4096.0)); +} + void TLC5940Servo::Servo::calibrate(float range, float degrees) { _range = range; _degrees = degrees; @@ -113,6 +116,11 @@ return *this; } +TLC5940Servo::Servo& TLC5940Servo::Servo::operator= (TLC5940Servo::Servo& rhs) { + write(rhs.read()); + return *this; +} + TLC5940Servo::Servo::operator float() { return read(); }
--- a/TLC5940Servo.h Tue Oct 21 06:14:58 2014 +0000 +++ b/TLC5940Servo.h Tue Oct 21 06:26:25 2014 +0000 @@ -36,24 +36,56 @@ */ class Servo { public: + /** + * Creat a servo object NOT connected to a specific PwmOut pin + */ Servo(); + /** * Set the servo position, normalised to it's full range * * @param percent A normalised number 0.0-1.0 to represent the full range. */ void write(float percent); - void calibrate(float range=0.0005, float degrees=45.0); + + /** + * Read the servo motors current position + * + * @param returns A normalised number 0.0-1.0 representing the full range. + */ float read(); + + /** + * Set the servo position + * + * @param degrees Servo position in degrees + */ + void position(float degrees); + + /** + * Allows calibration of the range and angles for a particular servo + * + * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds + * @param degrees Angle from centre to maximum/minimum position in degrees + */ + void calibrate(float range=0.0005, float degrees=45.0); + + /** + * Read the servo motors current TLC5940 pw value + */ int pulsewidth(); + + /** Shorthand for the write and read functions */ Servo& operator= (float percent); + Servo& operator= (Servo& rhs); operator float(); private: - float _p; float _range; float _degrees; + float _p; int _pw; }; + /** * Set up the TLC5940 *