Kallums Fork
Fork of SteeringServo by
SteeringServo.cpp@0:5141c5e51f39, 2017-06-23 (annotated)
- Committer:
- batchee7
- Date:
- Fri Jun 23 23:36:13 2017 +0000
- Revision:
- 0:5141c5e51f39
- Child:
- 1:88fe796b4c2e
initial
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 |
batchee7 | 0:5141c5e51f39 | 14 | void SteeringServo::goToAngle(int pulseWidth){ |
batchee7 | 0:5141c5e51f39 | 15 | // -- Ensure that pulse width doesn't exceed limits |
batchee7 | 0:5141c5e51f39 | 16 | if (pulseWidth < minimumPulseWidth_){ |
batchee7 | 0:5141c5e51f39 | 17 | servo_.pulsewidth_us(minimumPulseWidth_); |
batchee7 | 0:5141c5e51f39 | 18 | } |
batchee7 | 0:5141c5e51f39 | 19 | else if (pulseWidth < minimumPulseWidth_){ |
batchee7 | 0:5141c5e51f39 | 20 | servo_.pulsewidth_us(minimumPulseWidth_); |
batchee7 | 0:5141c5e51f39 | 21 | } |
batchee7 | 0:5141c5e51f39 | 22 | else{ |
batchee7 | 0:5141c5e51f39 | 23 | servo_.pulsewidth_us(pulseWidth); |
batchee7 | 0:5141c5e51f39 | 24 | } |
batchee7 | 0:5141c5e51f39 | 25 | } |
batchee7 | 0:5141c5e51f39 | 26 | |
batchee7 | 0:5141c5e51f39 | 27 | // -- Go to centre (generally used for debugging) |
batchee7 | 0:5141c5e51f39 | 28 | void SteeringServo::goStraight(void){ |
batchee7 | 0:5141c5e51f39 | 29 | servo_.pulsewidth_us(centrePulseWidth_); |
batchee7 | 0:5141c5e51f39 | 30 | } |