Kallums Fork
Fork of SteeringServo by
SteeringServo.cpp@1:88fe796b4c2e, 2017-09-02 (annotated)
- Committer:
- Kallum
- Date:
- Sat Sep 02 00:52:36 2017 +0000
- Revision:
- 1:88fe796b4c2e
- Parent:
- 0:5141c5e51f39
- Child:
- 2:80a7bb0d71af
- Child:
- 3:f81145ed8daa
Updated with currentPulseWidth so the we can get the current Angle of the servo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
batchee7 | 0:5141c5e51f39 | 1 | #include "SteeringServo.h" |
batchee7 | 0:5141c5e51f39 | 2 | |
batchee7 | 0:5141c5e51f39 | 3 | // -- Constructor Function, Need to Initilise PWM Object |
batchee7 | 0:5141c5e51f39 | 4 | SteeringServo::SteeringServo(PinName pin, int centrePW, int minPW, int maxPW) : servo_(pin) |
batchee7 | 0:5141c5e51f39 | 5 | { |
batchee7 | 0:5141c5e51f39 | 6 | servo_.period_us(20000); // -- Set PWM time period (20 ms) |
batchee7 | 0:5141c5e51f39 | 7 | servo_.pulsewidth_us(centrePW); // -- Default Set Steering to Straight Position |
batchee7 | 0:5141c5e51f39 | 8 | minimumPulseWidth_ = minPW; |
batchee7 | 0:5141c5e51f39 | 9 | maximumPulseWidth_ = maxPW; |
batchee7 | 0:5141c5e51f39 | 10 | centrePulseWidth_ = centrePW; |
batchee7 | 0:5141c5e51f39 | 11 | } |
batchee7 | 0:5141c5e51f39 | 12 | |
batchee7 | 0:5141c5e51f39 | 13 | // -- Go to position |
Kallum | 1:88fe796b4c2e | 14 | void SteeringServo::goToAngle(int pulseWidth) |
Kallum | 1:88fe796b4c2e | 15 | { |
Kallum | 1:88fe796b4c2e | 16 | // -- Ensure that pulse width doesn't exceed limits |
Kallum | 1:88fe796b4c2e | 17 | if (pulseWidth < minimumPulseWidth_) |
Kallum | 1:88fe796b4c2e | 18 | { |
Kallum | 1:88fe796b4c2e | 19 | pulseWidth = mimimumPulseWidth_; |
batchee7 | 0:5141c5e51f39 | 20 | } |
Kallum | 1:88fe796b4c2e | 21 | else if (pulseWidth > maximumPulseWidth_) |
Kallum | 1:88fe796b4c2e | 22 | { |
Kallum | 1:88fe796b4c2e | 23 | pulseWidth = maximumPulseWidth_; |
Kallum | 1:88fe796b4c2e | 24 | } |
Kallum | 1:88fe796b4c2e | 25 | |
Kallum | 1:88fe796b4c2e | 26 | servo_.pulsewidth_us(currentPulseWidth_ = pulseWidth); // -- set servo to pulse width and also store it in currentPulseWidth |
Kallum | 1:88fe796b4c2e | 27 | } |
batchee7 | 0:5141c5e51f39 | 28 | |
batchee7 | 0:5141c5e51f39 | 29 | // -- Go to centre (generally used for debugging) |
Kallum | 1:88fe796b4c2e | 30 | void SteeringServo::goStraight(void) |
Kallum | 1:88fe796b4c2e | 31 | { |
Kallum | 1:88fe796b4c2e | 32 | servo_.pulsewidth_us(currentPulseWidth_ = centrePulseWidth_); |
Kallum | 1:88fe796b4c2e | 33 | } |
Kallum | 1:88fe796b4c2e | 34 | |
Kallum | 1:88fe796b4c2e | 35 | //-- return the current Pulse Width |
Kallum | 1:88fe796b4c2e | 36 | int SteeringServo::getPulseWidth(void) |
Kallum | 1:88fe796b4c2e | 37 | { |
Kallum | 1:88fe796b4c2e | 38 | return(currentPulseWidth); |
Kallum | 1:88fe796b4c2e | 39 | } |
Kallum | 1:88fe796b4c2e | 40 |