A Library to drive the RenBuggy_Servo.
Dependencies: PinDetect
Fork of RenBuggyServo by
Car.h@3:01bc89d7ae8e, 2014-03-13 (annotated)
- Committer:
- Markatron
- Date:
- Thu Mar 13 07:31:10 2014 +0000
- Revision:
- 3:01bc89d7ae8e
- Parent:
- 2:287a808baad7
- Child:
- 5:c06b90175a54
Finished adding functionality for the use of an encoder.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Markatron | 0:d388aed56112 | 1 | /******************************************************************************* |
Markatron | 0:d388aed56112 | 2 | * RenBED Car used to drive RenBuggy with servo and 1 motor * |
Markatron | 0:d388aed56112 | 3 | * Copyright (c) 2014 Mark Jones * |
Markatron | 0:d388aed56112 | 4 | * * |
Markatron | 0:d388aed56112 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy * |
Markatron | 0:d388aed56112 | 6 | * of this software and associated documentation files (the "Software"), to deal* |
Markatron | 0:d388aed56112 | 7 | * in the Software without restriction, including without limitation the rights * |
Markatron | 0:d388aed56112 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * |
Markatron | 0:d388aed56112 | 9 | * copies of the Software, and to permit persons to whom the Software is * |
Markatron | 0:d388aed56112 | 10 | * furnished to do so, subject to the following conditions: * |
Markatron | 0:d388aed56112 | 11 | * * |
Markatron | 0:d388aed56112 | 12 | * The above copyright notice and this permission notice shall be included in * |
Markatron | 0:d388aed56112 | 13 | * all copies or substantial portions of the Software. * |
Markatron | 0:d388aed56112 | 14 | * * |
Markatron | 0:d388aed56112 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * |
Markatron | 0:d388aed56112 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * |
Markatron | 0:d388aed56112 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * |
Markatron | 0:d388aed56112 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * |
Markatron | 0:d388aed56112 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* |
Markatron | 0:d388aed56112 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * |
Markatron | 0:d388aed56112 | 21 | * THE SOFTWARE. * |
Markatron | 0:d388aed56112 | 22 | * * |
Markatron | 0:d388aed56112 | 23 | * Car.h * |
Markatron | 0:d388aed56112 | 24 | * * |
Markatron | 0:d388aed56112 | 25 | * V1. 05/03/2014 Mark Jones * |
Markatron | 0:d388aed56112 | 26 | *******************************************************************************/ |
Markatron | 0:d388aed56112 | 27 | |
Markatron | 0:d388aed56112 | 28 | #ifndef CAR_H |
Markatron | 0:d388aed56112 | 29 | #define CAR_H |
Markatron | 0:d388aed56112 | 30 | |
Markatron | 0:d388aed56112 | 31 | #include "mbed.h" |
Markatron | 3:01bc89d7ae8e | 32 | #include "PinDetect.h" |
Markatron | 0:d388aed56112 | 33 | |
Markatron | 2:287a808baad7 | 34 | /** |
Markatron | 2:287a808baad7 | 35 | * RenBuggyServo Example: |
Markatron | 2:287a808baad7 | 36 | * @code |
Markatron | 2:287a808baad7 | 37 | * |
Markatron | 2:287a808baad7 | 38 | * |
Markatron | 2:287a808baad7 | 39 | #include "mbed.h" |
Markatron | 2:287a808baad7 | 40 | #include "Car.h" |
Markatron | 2:287a808baad7 | 41 | |
Markatron | 2:287a808baad7 | 42 | Car myCar(P1_25, P1_24); |
Markatron | 2:287a808baad7 | 43 | |
Markatron | 2:287a808baad7 | 44 | // Main entry point of application. |
Markatron | 2:287a808baad7 | 45 | int main() { |
Markatron | 2:287a808baad7 | 46 | |
Markatron | 2:287a808baad7 | 47 | const int SERVO_PWM = 1500; // 1500 = centre. |
Markatron | 2:287a808baad7 | 48 | const int SERVO_PWM_PERIOD = 2000; |
Markatron | 2:287a808baad7 | 49 | const int SERVO_PWM_RANGE = 500; // + or - 500 microseconds. |
Markatron | 2:287a808baad7 | 50 | const float SERVO_DEGREES_RANGE = 45.0; // + or - from centre is full right/left. |
Markatron | 2:287a808baad7 | 51 | |
Markatron | 2:287a808baad7 | 52 | const int MOTOR_PWM = 20000; |
Markatron | 2:287a808baad7 | 53 | const int MOTOR_PERIOD = 20000; |
Markatron | 2:287a808baad7 | 54 | |
Markatron | 2:287a808baad7 | 55 | // Configure the servo and motor before use. |
Markatron | 2:287a808baad7 | 56 | myCar.configureServo_us(SERVO_PWM, SERVO_PWM_PERIOD, |
Markatron | 2:287a808baad7 | 57 | SERVO_PWM_RANGE, SERVO_DEGREES_RANGE); |
Markatron | 2:287a808baad7 | 58 | myCar.configureMotor_us(MOTOR_PWM, MOTOR_PERIOD); |
Markatron | 2:287a808baad7 | 59 | |
Markatron | 2:287a808baad7 | 60 | // Drive the RenBuggy! |
Markatron | 2:287a808baad7 | 61 | myCar.setSpeed(20000); |
Markatron | 2:287a808baad7 | 62 | myCar.setDirection(0); |
Markatron | 2:287a808baad7 | 63 | |
Markatron | 3:01bc89d7ae8e | 64 | myCar.forwards(); |
Markatron | 2:287a808baad7 | 65 | wait(1); |
Markatron | 2:287a808baad7 | 66 | myCar.setDirection(45); |
Markatron | 2:287a808baad7 | 67 | wait(1); |
Markatron | 2:287a808baad7 | 68 | myCar.setDirection(-45); |
Markatron | 2:287a808baad7 | 69 | wait(1); |
Markatron | 2:287a808baad7 | 70 | myCar.setDirection(0); |
Markatron | 2:287a808baad7 | 71 | myCar.stop(); |
Markatron | 2:287a808baad7 | 72 | |
Markatron | 2:287a808baad7 | 73 | myCar.forwards(10); // Move the car a final 10cm. |
Markatron | 2:287a808baad7 | 74 | myCar.stop(); |
Markatron | 2:287a808baad7 | 75 | } |
Markatron | 2:287a808baad7 | 76 | // End programme. |
Markatron | 2:287a808baad7 | 77 | @endcode |
Markatron | 2:287a808baad7 | 78 | */ |
Markatron | 2:287a808baad7 | 79 | |
Markatron | 2:287a808baad7 | 80 | /** |
Markatron | 2:287a808baad7 | 81 | ** The car class for controlling the RenBuggy_Servo. |
Markatron | 2:287a808baad7 | 82 | */ |
Markatron | 0:d388aed56112 | 83 | class Car { |
Markatron | 0:d388aed56112 | 84 | private: |
Markatron | 2:287a808baad7 | 85 | |
Markatron | 0:d388aed56112 | 86 | PwmOut m_servo; |
Markatron | 0:d388aed56112 | 87 | PwmOut m_motor; |
Markatron | 1:3e1290de9c8d | 88 | |
Markatron | 1:3e1290de9c8d | 89 | int m_speed; |
Markatron | 1:3e1290de9c8d | 90 | |
Markatron | 3:01bc89d7ae8e | 91 | int m_encoderCount; |
Markatron | 3:01bc89d7ae8e | 92 | |
Markatron | 0:d388aed56112 | 93 | int m_servoRange; // Pulsewidth range to full left/right from centre (1.5ms) |
Markatron | 0:d388aed56112 | 94 | float m_servoDegrees; // Angle to full right/left turn from centre (0). |
Markatron | 1:3e1290de9c8d | 95 | |
Markatron | 1:3e1290de9c8d | 96 | float m_wheelCircumference; // The circumference of the wheel with stripes. |
Markatron | 1:3e1290de9c8d | 97 | int m_countsPerRevolution; // The number of stripes on the wheel. |
Markatron | 3:01bc89d7ae8e | 98 | |
Markatron | 3:01bc89d7ae8e | 99 | PinDetect m_sensor; // For debouncing. |
Markatron | 1:3e1290de9c8d | 100 | |
Markatron | 3:01bc89d7ae8e | 101 | void updateEncoderCount(); |
Markatron | 1:3e1290de9c8d | 102 | |
Markatron | 3:01bc89d7ae8e | 103 | public: |
Markatron | 2:287a808baad7 | 104 | /** Constructs the car with PwmOut objects for servo and motor. |
Markatron | 2:287a808baad7 | 105 | * |
Markatron | 2:287a808baad7 | 106 | * @param servoPin is the pin used for pwm output for driving the servo. |
Markatron | 2:287a808baad7 | 107 | * @param motorPin is the pin used for pwm output for driving the motor. |
Markatron | 2:287a808baad7 | 108 | */ |
Markatron | 0:d388aed56112 | 109 | Car(PinName servoPin, PinName motorPin); |
Markatron | 2:287a808baad7 | 110 | |
Markatron | 2:287a808baad7 | 111 | /** Constructs the car with PwmOut objects for servo and motor, and configures |
Markatron | 2:287a808baad7 | 112 | * the encoder. |
Markatron | 2:287a808baad7 | 113 | * |
Markatron | 2:287a808baad7 | 114 | * @param servoPin is the pin used for pwm output for driving the servo. |
Markatron | 2:287a808baad7 | 115 | * @param motorPin is the pin used for pwm output for driving the motor. |
Markatron | 2:287a808baad7 | 116 | * @param countsPerRevolution is the number of counts the encoder |
Markatron | 2:287a808baad7 | 117 | * makes in one full cycle of the wheel. |
Markatron | 3:01bc89d7ae8e | 118 | * @param wheelCircumference is the circumference of the wheel being |
Markatron | 2:287a808baad7 | 119 | * read by the encoder. |
Markatron | 3:01bc89d7ae8e | 120 | * @param sensorPin is the pin required for the encoder for debouncing. |
Markatron | 2:287a808baad7 | 121 | */ |
Markatron | 3:01bc89d7ae8e | 122 | Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference, PinName sensorPin); |
Markatron | 2:287a808baad7 | 123 | |
Markatron | 2:287a808baad7 | 124 | /** |
Markatron | 2:287a808baad7 | 125 | * Deconstructs the car. |
Markatron | 2:287a808baad7 | 126 | */ |
Markatron | 0:d388aed56112 | 127 | ~Car(); |
Markatron | 0:d388aed56112 | 128 | |
Markatron | 2:287a808baad7 | 129 | /** |
Markatron | 2:287a808baad7 | 130 | * Sets the speed the buggy will move at. |
Markatron | 2:287a808baad7 | 131 | * @param speed_us is the speed the car will move at, in microseconds. |
Markatron | 2:287a808baad7 | 132 | */ |
Markatron | 1:3e1290de9c8d | 133 | void setSpeed(int speed_us); |
Markatron | 2:287a808baad7 | 134 | |
Markatron | 2:287a808baad7 | 135 | /** |
Markatron | 2:287a808baad7 | 136 | * Moves the car in the direction it is facing, for a specified |
Markatron | 2:287a808baad7 | 137 | * distance. |
Markatron | 2:287a808baad7 | 138 | * @param distance is how far the car will travel, in cm. |
Markatron | 2:287a808baad7 | 139 | */ |
Markatron | 0:d388aed56112 | 140 | void forwards(float distance); |
Markatron | 2:287a808baad7 | 141 | |
Markatron | 2:287a808baad7 | 142 | /** |
Markatron | 2:287a808baad7 | 143 | * Moves the car in the direction it is facing, until a user |
Markatron | 2:287a808baad7 | 144 | * tells it to stop. |
Markatron | 2:287a808baad7 | 145 | */ |
Markatron | 0:d388aed56112 | 146 | void forwards(); |
Markatron | 2:287a808baad7 | 147 | |
Markatron | 2:287a808baad7 | 148 | /** |
Markatron | 2:287a808baad7 | 149 | * Stops the car from moving. |
Markatron | 2:287a808baad7 | 150 | */ |
Markatron | 0:d388aed56112 | 151 | void stop(); |
Markatron | 2:287a808baad7 | 152 | |
Markatron | 2:287a808baad7 | 153 | /** |
Markatron | 2:287a808baad7 | 154 | * Sets the direction the car will face. This is used to navigate the car. |
Markatron | 2:287a808baad7 | 155 | * @param degrees is the angle of the servo, where -45 is full |
Markatron | 2:287a808baad7 | 156 | * left, 0 is centre and +45 is full right. |
Markatron | 2:287a808baad7 | 157 | */ |
Markatron | 0:d388aed56112 | 158 | void setDirection(int degrees); |
Markatron | 0:d388aed56112 | 159 | |
Markatron | 2:287a808baad7 | 160 | /** |
Markatron | 2:287a808baad7 | 161 | * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and |
Markatron | 2:287a808baad7 | 162 | * period is accepted in microsecond format. |
Markatron | 2:287a808baad7 | 163 | * @param pulsewidth_us is pwm pulsewidth for the servo, in mircoseconds. |
Markatron | 2:287a808baad7 | 164 | * @param period_us is the pwm period for the servo, in mircoseconds. |
Markatron | 2:287a808baad7 | 165 | * @param range is the pulsewidth range to full left/right turn of the servo from centre (1500us). |
Markatron | 2:287a808baad7 | 166 | * @param degrees is the angle to full right/left turn of the servo from centre (0). |
Markatron | 2:287a808baad7 | 167 | */ |
Markatron | 0:d388aed56112 | 168 | void configureServo_us(int pulsewidth_us, int period_us, int range, float degrees); |
Markatron | 2:287a808baad7 | 169 | |
Markatron | 2:287a808baad7 | 170 | /** |
Markatron | 2:287a808baad7 | 171 | * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and |
Markatron | 2:287a808baad7 | 172 | * period is accepted in millisecond format. |
Markatron | 2:287a808baad7 | 173 | * @param pulsewidth_ms is pwm pulsewidth for the servo, in millisecond. |
Markatron | 2:287a808baad7 | 174 | * @param period_ms is the pwm period for the servo, in millisecond. |
Markatron | 2:287a808baad7 | 175 | * @param range is the pulsewidth range to full left/right turn of the servo from centre (1.5ms). |
Markatron | 2:287a808baad7 | 176 | * @param degrees is the angle to full right/left turn of the servo from centre (0). |
Markatron | 2:287a808baad7 | 177 | */ |
Markatron | 0:d388aed56112 | 178 | void configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees); |
Markatron | 0:d388aed56112 | 179 | |
Markatron | 2:287a808baad7 | 180 | /** |
Markatron | 2:287a808baad7 | 181 | * Configures the pulsewidth and period for the motor, in microseconds. |
Markatron | 2:287a808baad7 | 182 | * @param pulsewidth_us is the pwm pulsewidth for the motor, in mircoseconds. |
Markatron | 2:287a808baad7 | 183 | * @param period_us is the pwm period for the motor, in microseconds. |
Markatron | 2:287a808baad7 | 184 | */ |
Markatron | 0:d388aed56112 | 185 | void configureMotor_us(int pulsewidth_us, int period_us); |
Markatron | 2:287a808baad7 | 186 | |
Markatron | 2:287a808baad7 | 187 | /** |
Markatron | 2:287a808baad7 | 188 | * Configures the pulsewidth and period for the motor, in milliseconds. |
Markatron | 2:287a808baad7 | 189 | * @param pulsewidth_ms is the pwm pulsewidth for the motor, in milliseconds. |
Markatron | 2:287a808baad7 | 190 | * @param period_ms is the pwm period for the motor, in milliseconds. |
Markatron | 2:287a808baad7 | 191 | */ |
Markatron | 0:d388aed56112 | 192 | void configureMotor_ms(int pulsewidth_ms, int period_ms); |
Markatron | 1:3e1290de9c8d | 193 | |
Markatron | 2:287a808baad7 | 194 | /** |
Markatron | 2:287a808baad7 | 195 | * Provides information required to make use of an encoder for |
Markatron | 2:287a808baad7 | 196 | * specifying distance. |
Markatron | 2:287a808baad7 | 197 | * @param countsPerRevolution is the number of counts the encoder |
Markatron | 2:287a808baad7 | 198 | * makes in one full cycle of the wheel. |
Markatron | 2:287a808baad7 | 199 | * @param wheelCircumference is the circumference of the wheel being |
Markatron | 2:287a808baad7 | 200 | * read by the encoder. |
Markatron | 2:287a808baad7 | 201 | */ |
Markatron | 1:3e1290de9c8d | 202 | void configureEncoder(int countsPerRevolution, float wheelCircumference); |
Markatron | 0:d388aed56112 | 203 | }; |
Markatron | 0:d388aed56112 | 204 | |
Markatron | 0:d388aed56112 | 205 | #endif // CAR_H |