A Library to drive the RenBuggy_Servo.

Dependencies:   PinDetect

Dependents:  

Fork of RenBuggyServo by Renishaw

Committer:
ELloyd
Date:
Mon Mar 31 12:54:28 2014 +0000
Revision:
6:5767cb4ed8de
Parent:
5:c06b90175a54
Prevented overriding of functions.

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