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:1fc280fa2177, 2010-06-02 (annotated)
- Committer:
- UAVguy
- Date:
- Wed Jun 02 21:40:10 2010 +0000
- Revision:
- 0:1fc280fa2177
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| UAVguy | 0:1fc280fa2177 | 1 | #include "Servo.h" |
| UAVguy | 0:1fc280fa2177 | 2 | |
| UAVguy | 0:1fc280fa2177 | 3 | Servo::Servo(PinName port) : PwmOut(port) |
| UAVguy | 0:1fc280fa2177 | 4 | { |
| UAVguy | 0:1fc280fa2177 | 5 | period = 20000; |
| UAVguy | 0:1fc280fa2177 | 6 | maximum = 2400; |
| UAVguy | 0:1fc280fa2177 | 7 | minimum = 600; |
| UAVguy | 0:1fc280fa2177 | 8 | center = 1500; |
| UAVguy | 0:1fc280fa2177 | 9 | position = 0; |
| UAVguy | 0:1fc280fa2177 | 10 | //set default values |
| UAVguy | 0:1fc280fa2177 | 11 | pulsewidth_us(0); |
| UAVguy | 0:1fc280fa2177 | 12 | period_us(period); |
| UAVguy | 0:1fc280fa2177 | 13 | } |
| UAVguy | 0:1fc280fa2177 | 14 | |
| UAVguy | 0:1fc280fa2177 | 15 | void Servo::setposition(int input) |
| UAVguy | 0:1fc280fa2177 | 16 | { |
| UAVguy | 0:1fc280fa2177 | 17 | position = input; |
| UAVguy | 0:1fc280fa2177 | 18 | if(position > 255) |
| UAVguy | 0:1fc280fa2177 | 19 | { |
| UAVguy | 0:1fc280fa2177 | 20 | position = 255; |
| UAVguy | 0:1fc280fa2177 | 21 | } |
| UAVguy | 0:1fc280fa2177 | 22 | else if (position < -255) |
| UAVguy | 0:1fc280fa2177 | 23 | { |
| UAVguy | 0:1fc280fa2177 | 24 | position = -255; |
| UAVguy | 0:1fc280fa2177 | 25 | } |
| UAVguy | 0:1fc280fa2177 | 26 | //saturate |
| UAVguy | 0:1fc280fa2177 | 27 | if(position >= 0) |
| UAVguy | 0:1fc280fa2177 | 28 | { |
| UAVguy | 0:1fc280fa2177 | 29 | pulsewidth_us(position * (maximum - center) / 255 + center); |
| UAVguy | 0:1fc280fa2177 | 30 | } |
| UAVguy | 0:1fc280fa2177 | 31 | else |
| UAVguy | 0:1fc280fa2177 | 32 | { |
| UAVguy | 0:1fc280fa2177 | 33 | pulsewidth_us(position * (center - minimum) / 255 + center); |
| UAVguy | 0:1fc280fa2177 | 34 | } |
| UAVguy | 0:1fc280fa2177 | 35 | //set position normalized to 255 |
| UAVguy | 0:1fc280fa2177 | 36 | } |
| UAVguy | 0:1fc280fa2177 | 37 | |
| UAVguy | 0:1fc280fa2177 | 38 | void Servo::setperiod(int input) |
| UAVguy | 0:1fc280fa2177 | 39 | { |
| UAVguy | 0:1fc280fa2177 | 40 | period = input; |
| UAVguy | 0:1fc280fa2177 | 41 | period_us(period); |
| UAVguy | 0:1fc280fa2177 | 42 | } |
| UAVguy | 0:1fc280fa2177 | 43 | |
| UAVguy | 0:1fc280fa2177 | 44 | void Servo::setcenter(int input) |
| UAVguy | 0:1fc280fa2177 | 45 | { |
| UAVguy | 0:1fc280fa2177 | 46 | center = input; |
| UAVguy | 0:1fc280fa2177 | 47 | } |
| UAVguy | 0:1fc280fa2177 | 48 | |
| UAVguy | 0:1fc280fa2177 | 49 | void Servo::setmaximum(int input) |
| UAVguy | 0:1fc280fa2177 | 50 | { |
| UAVguy | 0:1fc280fa2177 | 51 | maximum = input; |
| UAVguy | 0:1fc280fa2177 | 52 | } |
| UAVguy | 0:1fc280fa2177 | 53 | |
| UAVguy | 0:1fc280fa2177 | 54 | void Servo::setminimum(int input) |
| UAVguy | 0:1fc280fa2177 | 55 | { |
| UAVguy | 0:1fc280fa2177 | 56 | minimum = input; |
| UAVguy | 0:1fc280fa2177 | 57 | } |
| UAVguy | 0:1fc280fa2177 | 58 | |
| UAVguy | 0:1fc280fa2177 | 59 | Servo& Servo::operator =(int assignment) |
| UAVguy | 0:1fc280fa2177 | 60 | { |
| UAVguy | 0:1fc280fa2177 | 61 | Servo::setposition(assignment); |
| UAVguy | 0:1fc280fa2177 | 62 | //shorthand for setposition |
| UAVguy | 0:1fc280fa2177 | 63 | *this = assignment; |
| UAVguy | 0:1fc280fa2177 | 64 | return *this; |
| UAVguy | 0:1fc280fa2177 | 65 | //allow multiple assignment |
| UAVguy | 0:1fc280fa2177 | 66 | } |
| UAVguy | 0:1fc280fa2177 | 67 | |
| UAVguy | 0:1fc280fa2177 | 68 | Servo::operator int() |
| UAVguy | 0:1fc280fa2177 | 69 | { |
| UAVguy | 0:1fc280fa2177 | 70 | int measurement = read() * period; |
| UAVguy | 0:1fc280fa2177 | 71 | if(measurement >= center) |
| UAVguy | 0:1fc280fa2177 | 72 | { |
| UAVguy | 0:1fc280fa2177 | 73 | return 255 * (measurement - center) / (maximum - center); |
| UAVguy | 0:1fc280fa2177 | 74 | } |
| UAVguy | 0:1fc280fa2177 | 75 | else |
| UAVguy | 0:1fc280fa2177 | 76 | { |
| UAVguy | 0:1fc280fa2177 | 77 | return 255 * (measurement - position) / (center - minimum); |
| UAVguy | 0:1fc280fa2177 | 78 | } |
| UAVguy | 0:1fc280fa2177 | 79 | } |