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.
Servo.cpp@0:9e53989bcf57, 2012-04-28 (annotated)
- Committer:
- stretch
- Date:
- Sat Apr 28 21:08:24 2012 +0000
- Revision:
- 0:9e53989bcf57
Initial Public Release
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| stretch | 0:9e53989bcf57 | 1 | /* mbed R/C Servo Library |
| stretch | 0:9e53989bcf57 | 2 | * |
| stretch | 0:9e53989bcf57 | 3 | * Copyright (c) 2007-2010 sford, cstyles |
| stretch | 0:9e53989bcf57 | 4 | * |
| stretch | 0:9e53989bcf57 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| stretch | 0:9e53989bcf57 | 6 | * of this software and associated documentation files (the "Software"), to deal |
| stretch | 0:9e53989bcf57 | 7 | * in the Software without restriction, including without limitation the rights |
| stretch | 0:9e53989bcf57 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| stretch | 0:9e53989bcf57 | 9 | * copies of the Software, and to permit persons to whom the Software is |
| stretch | 0:9e53989bcf57 | 10 | * furnished to do so, subject to the following conditions: |
| stretch | 0:9e53989bcf57 | 11 | * |
| stretch | 0:9e53989bcf57 | 12 | * The above copyright notice and this permission notice shall be included in |
| stretch | 0:9e53989bcf57 | 13 | * all copies or substantial portions of the Software. |
| stretch | 0:9e53989bcf57 | 14 | * |
| stretch | 0:9e53989bcf57 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| stretch | 0:9e53989bcf57 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| stretch | 0:9e53989bcf57 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| stretch | 0:9e53989bcf57 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| stretch | 0:9e53989bcf57 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| stretch | 0:9e53989bcf57 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| stretch | 0:9e53989bcf57 | 21 | * THE SOFTWARE. |
| stretch | 0:9e53989bcf57 | 22 | */ |
| stretch | 0:9e53989bcf57 | 23 | |
| stretch | 0:9e53989bcf57 | 24 | #include "Servo.h" |
| stretch | 0:9e53989bcf57 | 25 | #include "mbed.h" |
| stretch | 0:9e53989bcf57 | 26 | |
| stretch | 0:9e53989bcf57 | 27 | static float clamp(float value, float min, float max) { |
| stretch | 0:9e53989bcf57 | 28 | if(value < min) { |
| stretch | 0:9e53989bcf57 | 29 | return min; |
| stretch | 0:9e53989bcf57 | 30 | } else if(value > max) { |
| stretch | 0:9e53989bcf57 | 31 | return max; |
| stretch | 0:9e53989bcf57 | 32 | } else { |
| stretch | 0:9e53989bcf57 | 33 | return value; |
| stretch | 0:9e53989bcf57 | 34 | } |
| stretch | 0:9e53989bcf57 | 35 | } |
| stretch | 0:9e53989bcf57 | 36 | |
| stretch | 0:9e53989bcf57 | 37 | Servo::Servo(PinName pin) : _pwm(pin) { |
| stretch | 0:9e53989bcf57 | 38 | calibrate(); |
| stretch | 0:9e53989bcf57 | 39 | write(0.5); |
| stretch | 0:9e53989bcf57 | 40 | } |
| stretch | 0:9e53989bcf57 | 41 | |
| stretch | 0:9e53989bcf57 | 42 | void Servo::calibrate(float range, float degrees, float center) { |
| stretch | 0:9e53989bcf57 | 43 | _range = range; |
| stretch | 0:9e53989bcf57 | 44 | _degrees = degrees; |
| stretch | 0:9e53989bcf57 | 45 | _center = center; |
| stretch | 0:9e53989bcf57 | 46 | } |
| stretch | 0:9e53989bcf57 | 47 | |
| stretch | 0:9e53989bcf57 | 48 | void Servo::write(float percent) { |
| stretch | 0:9e53989bcf57 | 49 | float offset = _range * 2.0 * (percent - 0.5); |
| stretch | 0:9e53989bcf57 | 50 | _pwm.pulsewidth(0.0015 + _center + clamp(offset, -_range, _range)); |
| stretch | 0:9e53989bcf57 | 51 | _p = clamp(percent, 0.0, 1.0); |
| stretch | 0:9e53989bcf57 | 52 | } |
| stretch | 0:9e53989bcf57 | 53 | |
| stretch | 0:9e53989bcf57 | 54 | void Servo::position(float degrees) { |
| stretch | 0:9e53989bcf57 | 55 | float offset = _range * (degrees / _degrees); |
| stretch | 0:9e53989bcf57 | 56 | _pwm.pulsewidth(0.0015 + _center + clamp(offset, -_range, _range)); |
| stretch | 0:9e53989bcf57 | 57 | } |
| stretch | 0:9e53989bcf57 | 58 | |
| stretch | 0:9e53989bcf57 | 59 | float Servo::read() { |
| stretch | 0:9e53989bcf57 | 60 | return _p; |
| stretch | 0:9e53989bcf57 | 61 | } |
| stretch | 0:9e53989bcf57 | 62 | |
| stretch | 0:9e53989bcf57 | 63 | Servo& Servo::operator= (float percent) { |
| stretch | 0:9e53989bcf57 | 64 | write(percent); |
| stretch | 0:9e53989bcf57 | 65 | return *this; |
| stretch | 0:9e53989bcf57 | 66 | } |
| stretch | 0:9e53989bcf57 | 67 | |
| stretch | 0:9e53989bcf57 | 68 | Servo& Servo::operator= (Servo& rhs) { |
| stretch | 0:9e53989bcf57 | 69 | write(rhs.read()); |
| stretch | 0:9e53989bcf57 | 70 | return *this; |
| stretch | 0:9e53989bcf57 | 71 | } |
| stretch | 0:9e53989bcf57 | 72 | |
| stretch | 0:9e53989bcf57 | 73 | Servo::operator float() { |
| stretch | 0:9e53989bcf57 | 74 | return read(); |
| stretch | 0:9e53989bcf57 | 75 | } |