Updated version of RenBuggy Servo that can accept instructions based on time or distance.

Dependencies:   PinDetect mbed

Fork of RenBuggyServo by Renishaw

Committer:
Markatron
Date:
Mon Mar 10 11:53:45 2014 +0000
Revision:
2:287a808baad7
Parent:
1:3e1290de9c8d
Child:
3:01bc89d7ae8e
Added documentation to the library.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:d388aed56112 32
Markatron 2:287a808baad7 33 /**
Markatron 2:287a808baad7 34 * RenBuggyServo Example:
Markatron 2:287a808baad7 35 * @code
Markatron 2:287a808baad7 36 *
Markatron 2:287a808baad7 37 *
Markatron 2:287a808baad7 38 #include "mbed.h"
Markatron 2:287a808baad7 39 #include "Car.h"
Markatron 2:287a808baad7 40
Markatron 2:287a808baad7 41 Car myCar(P1_25, P1_24);
Markatron 2:287a808baad7 42
Markatron 2:287a808baad7 43 // Main entry point of application.
Markatron 2:287a808baad7 44 int main() {
Markatron 2:287a808baad7 45
Markatron 2:287a808baad7 46 const int SERVO_PWM = 1500; // 1500 = centre.
Markatron 2:287a808baad7 47 const int SERVO_PWM_PERIOD = 2000;
Markatron 2:287a808baad7 48 const int SERVO_PWM_RANGE = 500; // + or - 500 microseconds.
Markatron 2:287a808baad7 49 const float SERVO_DEGREES_RANGE = 45.0; // + or - from centre is full right/left.
Markatron 2:287a808baad7 50
Markatron 2:287a808baad7 51 const int MOTOR_PWM = 20000;
Markatron 2:287a808baad7 52 const int MOTOR_PERIOD = 20000;
Markatron 2:287a808baad7 53
Markatron 2:287a808baad7 54 // Configure the servo and motor before use.
Markatron 2:287a808baad7 55 myCar.configureServo_us(SERVO_PWM, SERVO_PWM_PERIOD,
Markatron 2:287a808baad7 56 SERVO_PWM_RANGE, SERVO_DEGREES_RANGE);
Markatron 2:287a808baad7 57 myCar.configureMotor_us(MOTOR_PWM, MOTOR_PERIOD);
Markatron 2:287a808baad7 58
Markatron 2:287a808baad7 59 // Drive the RenBuggy!
Markatron 2:287a808baad7 60 myCar.setSpeed(20000);
Markatron 2:287a808baad7 61 myCar.setDirection(0);
Markatron 2:287a808baad7 62
Markatron 2:287a808baad7 63 myCar.forwards();
Markatron 2:287a808baad7 64 wait(1);
Markatron 2:287a808baad7 65 myCar.setDirection(45);
Markatron 2:287a808baad7 66 wait(1);
Markatron 2:287a808baad7 67 myCar.setDirection(-45);
Markatron 2:287a808baad7 68 wait(1);
Markatron 2:287a808baad7 69 myCar.setDirection(0);
Markatron 2:287a808baad7 70 myCar.stop();
Markatron 2:287a808baad7 71
Markatron 2:287a808baad7 72 myCar.forwards(10); // Move the car a final 10cm.
Markatron 2:287a808baad7 73 myCar.stop();
Markatron 2:287a808baad7 74 }
Markatron 2:287a808baad7 75 // End programme.
Markatron 2:287a808baad7 76 @endcode
Markatron 2:287a808baad7 77 */
Markatron 2:287a808baad7 78
Markatron 2:287a808baad7 79 /**
Markatron 2:287a808baad7 80 ** The car class for controlling the RenBuggy_Servo.
Markatron 2:287a808baad7 81 */
Markatron 0:d388aed56112 82 class Car {
Markatron 0:d388aed56112 83 private:
Markatron 2:287a808baad7 84
Markatron 0:d388aed56112 85 PwmOut m_servo;
Markatron 0:d388aed56112 86 PwmOut m_motor;
Markatron 1:3e1290de9c8d 87
Markatron 1:3e1290de9c8d 88 int m_speed;
Markatron 1:3e1290de9c8d 89
Markatron 0:d388aed56112 90 int m_servoRange; // Pulsewidth range to full left/right from centre (1.5ms)
Markatron 0:d388aed56112 91 float m_servoDegrees; // Angle to full right/left turn from centre (0).
Markatron 1:3e1290de9c8d 92
Markatron 1:3e1290de9c8d 93 float m_wheelCircumference; // The circumference of the wheel with stripes.
Markatron 1:3e1290de9c8d 94 int m_countsPerRevolution; // The number of stripes on the wheel.
Markatron 1:3e1290de9c8d 95 //InterruptIn m_stripeInterrupt;
Markatron 1:3e1290de9c8d 96
Markatron 1:3e1290de9c8d 97 //float distanceToTimeConverter(float distance);
Markatron 1:3e1290de9c8d 98
Markatron 0:d388aed56112 99 public:
Markatron 1:3e1290de9c8d 100
Markatron 2:287a808baad7 101 /** Constructs the car with PwmOut objects for servo and motor.
Markatron 2:287a808baad7 102 *
Markatron 2:287a808baad7 103 * @param servoPin is the pin used for pwm output for driving the servo.
Markatron 2:287a808baad7 104 * @param motorPin is the pin used for pwm output for driving the motor.
Markatron 2:287a808baad7 105 */
Markatron 0:d388aed56112 106 Car(PinName servoPin, PinName motorPin);
Markatron 2:287a808baad7 107
Markatron 2:287a808baad7 108 /** Constructs the car with PwmOut objects for servo and motor, and configures
Markatron 2:287a808baad7 109 * the encoder.
Markatron 2:287a808baad7 110 *
Markatron 2:287a808baad7 111 * @param servoPin is the pin used for pwm output for driving the servo.
Markatron 2:287a808baad7 112 * @param motorPin is the pin used for pwm output for driving the motor.
Markatron 2:287a808baad7 113 * @param countsPerRevolution is the number of counts the encoder
Markatron 2:287a808baad7 114 * makes in one full cycle of the wheel.
Markatron 2:287a808baad7 115 * @param wheelCircumference: The circumference of the wheel being
Markatron 2:287a808baad7 116 * read by the encoder.
Markatron 2:287a808baad7 117 */
Markatron 1:3e1290de9c8d 118 Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference);
Markatron 2:287a808baad7 119
Markatron 2:287a808baad7 120 /**
Markatron 2:287a808baad7 121 * Deconstructs the car.
Markatron 2:287a808baad7 122 */
Markatron 0:d388aed56112 123 ~Car();
Markatron 0:d388aed56112 124
Markatron 2:287a808baad7 125 /**
Markatron 2:287a808baad7 126 * Sets the speed the buggy will move at.
Markatron 2:287a808baad7 127 * @param speed_us is the speed the car will move at, in microseconds.
Markatron 2:287a808baad7 128 */
Markatron 1:3e1290de9c8d 129 void setSpeed(int speed_us);
Markatron 2:287a808baad7 130
Markatron 2:287a808baad7 131 /**
Markatron 2:287a808baad7 132 * Moves the car in the direction it is facing, for a specified
Markatron 2:287a808baad7 133 * distance.
Markatron 2:287a808baad7 134 * @param distance is how far the car will travel, in cm.
Markatron 2:287a808baad7 135 */
Markatron 0:d388aed56112 136 void forwards(float distance);
Markatron 2:287a808baad7 137
Markatron 2:287a808baad7 138 /**
Markatron 2:287a808baad7 139 * Moves the car in the direction it is facing, until a user
Markatron 2:287a808baad7 140 * tells it to stop.
Markatron 2:287a808baad7 141 */
Markatron 0:d388aed56112 142 void forwards();
Markatron 2:287a808baad7 143
Markatron 2:287a808baad7 144 /**
Markatron 2:287a808baad7 145 * Stops the car from moving.
Markatron 2:287a808baad7 146 */
Markatron 0:d388aed56112 147 void stop();
Markatron 2:287a808baad7 148
Markatron 2:287a808baad7 149 /**
Markatron 2:287a808baad7 150 * Sets the direction the car will face. This is used to navigate the car.
Markatron 2:287a808baad7 151 * @param degrees is the angle of the servo, where -45 is full
Markatron 2:287a808baad7 152 * left, 0 is centre and +45 is full right.
Markatron 2:287a808baad7 153 */
Markatron 0:d388aed56112 154 void setDirection(int degrees);
Markatron 0:d388aed56112 155
Markatron 2:287a808baad7 156 /**
Markatron 2:287a808baad7 157 * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and
Markatron 2:287a808baad7 158 * period is accepted in microsecond format.
Markatron 2:287a808baad7 159 * @param pulsewidth_us is pwm pulsewidth for the servo, in mircoseconds.
Markatron 2:287a808baad7 160 * @param period_us is the pwm period for the servo, in mircoseconds.
Markatron 2:287a808baad7 161 * @param range is the pulsewidth range to full left/right turn of the servo from centre (1500us).
Markatron 2:287a808baad7 162 * @param degrees is the angle to full right/left turn of the servo from centre (0).
Markatron 2:287a808baad7 163 */
Markatron 0:d388aed56112 164 void configureServo_us(int pulsewidth_us, int period_us, int range, float degrees);
Markatron 2:287a808baad7 165
Markatron 2:287a808baad7 166 /**
Markatron 2:287a808baad7 167 * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and
Markatron 2:287a808baad7 168 * period is accepted in millisecond format.
Markatron 2:287a808baad7 169 * @param pulsewidth_ms is pwm pulsewidth for the servo, in millisecond.
Markatron 2:287a808baad7 170 * @param period_ms is the pwm period for the servo, in millisecond.
Markatron 2:287a808baad7 171 * @param range is the pulsewidth range to full left/right turn of the servo from centre (1.5ms).
Markatron 2:287a808baad7 172 * @param degrees is the angle to full right/left turn of the servo from centre (0).
Markatron 2:287a808baad7 173 */
Markatron 0:d388aed56112 174 void configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees);
Markatron 0:d388aed56112 175
Markatron 2:287a808baad7 176 /**
Markatron 2:287a808baad7 177 * Configures the pulsewidth and period for the motor, in microseconds.
Markatron 2:287a808baad7 178 * @param pulsewidth_us is the pwm pulsewidth for the motor, in mircoseconds.
Markatron 2:287a808baad7 179 * @param period_us is the pwm period for the motor, in microseconds.
Markatron 2:287a808baad7 180 */
Markatron 0:d388aed56112 181 void configureMotor_us(int pulsewidth_us, int period_us);
Markatron 2:287a808baad7 182
Markatron 2:287a808baad7 183 /**
Markatron 2:287a808baad7 184 * Configures the pulsewidth and period for the motor, in milliseconds.
Markatron 2:287a808baad7 185 * @param pulsewidth_ms is the pwm pulsewidth for the motor, in milliseconds.
Markatron 2:287a808baad7 186 * @param period_ms is the pwm period for the motor, in milliseconds.
Markatron 2:287a808baad7 187 */
Markatron 0:d388aed56112 188 void configureMotor_ms(int pulsewidth_ms, int period_ms);
Markatron 1:3e1290de9c8d 189
Markatron 2:287a808baad7 190 /**
Markatron 2:287a808baad7 191 * Provides information required to make use of an encoder for
Markatron 2:287a808baad7 192 * specifying distance.
Markatron 2:287a808baad7 193 * @param countsPerRevolution is the number of counts the encoder
Markatron 2:287a808baad7 194 * makes in one full cycle of the wheel.
Markatron 2:287a808baad7 195 * @param wheelCircumference is the circumference of the wheel being
Markatron 2:287a808baad7 196 * read by the encoder.
Markatron 2:287a808baad7 197 */
Markatron 1:3e1290de9c8d 198 void configureEncoder(int countsPerRevolution, float wheelCircumference);
Markatron 0:d388aed56112 199 };
Markatron 0:d388aed56112 200
Markatron 0:d388aed56112 201 #endif // CAR_H