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 10:15:22 2014 +0000
Revision:
1:3e1290de9c8d
Parent:
0:d388aed56112
Child:
2:287a808baad7
RenBuggy Servo updated library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Markatron 0:d388aed56112 1 /*******************************************************************************
Markatron 1:3e1290de9c8d 2 * RenBED Car used to drive RenBuggy with servo, motor and encoder(optional) *
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.cpp *
Markatron 0:d388aed56112 24 * *
Markatron 0:d388aed56112 25 * V1.0 05/03/2014 Mark Jones *
Markatron 0:d388aed56112 26 *******************************************************************************/
Markatron 0:d388aed56112 27
Markatron 0:d388aed56112 28 #ifndef CAR_C
Markatron 0:d388aed56112 29 #define CAR_C
Markatron 0:d388aed56112 30
Markatron 0:d388aed56112 31 #include "Car.h"
Markatron 0:d388aed56112 32 #include "mbed.h"
Markatron 0:d388aed56112 33
Markatron 0:d388aed56112 34 /*
Markatron 0:d388aed56112 35 ** Constructs the car with PwmOut objects for servo and motor.
Markatron 0:d388aed56112 36 ** @params servoPin: This is the pin used for pwm output for driving the servo.
Markatron 0:d388aed56112 37 ** @params motorPin: This is the pin used for pwm output for driving the motor.
Markatron 0:d388aed56112 38 */
Markatron 0:d388aed56112 39 Car::Car(PinName servoPin, PinName motorPin)
Markatron 0:d388aed56112 40 : m_servo(servoPin), m_motor(motorPin) {
Markatron 1:3e1290de9c8d 41 m_speed = 15000;
Markatron 1:3e1290de9c8d 42 }
Markatron 1:3e1290de9c8d 43
Markatron 1:3e1290de9c8d 44 Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference)
Markatron 1:3e1290de9c8d 45 : m_servo(servoPin), m_motor(motorPin) {
Markatron 1:3e1290de9c8d 46 configureEncoder(countsPerRevolution, wheelCircumference);
Markatron 1:3e1290de9c8d 47 m_speed = 15000;
Markatron 0:d388aed56112 48 }
Markatron 0:d388aed56112 49
Markatron 0:d388aed56112 50 /*
Markatron 0:d388aed56112 51 ** Deconstructs the car.
Markatron 0:d388aed56112 52 */
Markatron 0:d388aed56112 53 Car::~Car() {
Markatron 0:d388aed56112 54 }
Markatron 0:d388aed56112 55
Markatron 1:3e1290de9c8d 56 /*
Markatron 1:3e1290de9c8d 57 **
Markatron 1:3e1290de9c8d 58 */
Markatron 1:3e1290de9c8d 59 void Car::setSpeed(int speed_us) {
Markatron 1:3e1290de9c8d 60 m_speed = speed_us;
Markatron 1:3e1290de9c8d 61 }
Markatron 1:3e1290de9c8d 62
Markatron 0:d388aed56112 63 /*
Markatron 1:3e1290de9c8d 64 ** This function is for use in conjuction with
Markatron 1:3e1290de9c8d 65 ** an encoder, and makes the car move a specified
Markatron 1:3e1290de9c8d 66 ** distance.
Markatron 1:3e1290de9c8d 67 ** @params distance: The distance the car should
Markatron 1:3e1290de9c8d 68 ** move, in cm.
Markatron 0:d388aed56112 69 */
Markatron 0:d388aed56112 70 void Car::forwards(float distance) {
Markatron 1:3e1290de9c8d 71
Markatron 1:3e1290de9c8d 72 int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference));
Markatron 0:d388aed56112 73
Markatron 1:3e1290de9c8d 74 // Tell encoder to keep reading, and have motor keep going forward
Markatron 1:3e1290de9c8d 75 // until the specified number of counts (countsForward) has been read
Markatron 1:3e1290de9c8d 76 // (e.g. countsForward = 10
Markatron 1:3e1290de9c8d 77 // while(encoderValue <= 10)
Markatron 1:3e1290de9c8d 78 // {
Markatron 1:3e1290de9c8d 79 // m_motor.pulsewidth(m_speed);
Markatron 1:3e1290de9c8d 80 // }
Markatron 1:3e1290de9c8d 81 // stop();
Markatron 0:d388aed56112 82
Markatron 0:d388aed56112 83 }
Markatron 0:d388aed56112 84
Markatron 0:d388aed56112 85 /*
Markatron 0:d388aed56112 86 ** Start the car moving with a default speed.
Markatron 0:d388aed56112 87 */
Markatron 0:d388aed56112 88 void Car::forwards() {
Markatron 1:3e1290de9c8d 89 m_motor.pulsewidth_us(m_speed);
Markatron 0:d388aed56112 90 }
Markatron 0:d388aed56112 91
Markatron 0:d388aed56112 92 /*
Markatron 0:d388aed56112 93 ** Stops the motor.
Markatron 0:d388aed56112 94 */
Markatron 0:d388aed56112 95 void Car::stop() {
Markatron 0:d388aed56112 96 m_motor.pulsewidth_us(0);
Markatron 0:d388aed56112 97 }
Markatron 0:d388aed56112 98
Markatron 0:d388aed56112 99 /*
Markatron 0:d388aed56112 100 ** Set the direction the car is facing.
Markatron 0:d388aed56112 101 ** @params degrees: The degrees of the angle, where -45 is full
Markatron 0:d388aed56112 102 ** left, 0 is centre and +45 is full right.
Markatron 0:d388aed56112 103 */
Markatron 0:d388aed56112 104 void Car::setDirection(int degrees) {
Markatron 0:d388aed56112 105 float angleOffset = m_servoRange * (m_servoDegrees / degrees);
Markatron 0:d388aed56112 106 m_servo.pulsewidth_us(1500 + angleOffset);
Markatron 0:d388aed56112 107 }
Markatron 0:d388aed56112 108
Markatron 0:d388aed56112 109 /*
Markatron 0:d388aed56112 110 ** Configures the pulsewidth and perion for the servon, in microseconds.
Markatron 0:d388aed56112 111 ** @params pulsewidth_us: The pwm pulsewidth for the servo, in mircoseconds.
Markatron 0:d388aed56112 112 ** @params period_ms: The pwm period for the servo, in mircoseconds.
Markatron 0:d388aed56112 113 ** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms).
Markatron 0:d388aed56112 114 ** @params degrees: The angle to full right/left turn of the servo from centre (0).
Markatron 0:d388aed56112 115 */
Markatron 0:d388aed56112 116 void Car::configureServo_us(int pulsewidth_us, int period_us, int range, float degrees) {
Markatron 0:d388aed56112 117 m_servo.pulsewidth_us(pulsewidth_us);
Markatron 0:d388aed56112 118 m_servo.period_us(period_us);
Markatron 0:d388aed56112 119 m_servoRange = range;
Markatron 0:d388aed56112 120 m_servoDegrees = degrees;
Markatron 0:d388aed56112 121 }
Markatron 0:d388aed56112 122
Markatron 0:d388aed56112 123 /*
Markatron 0:d388aed56112 124 ** Configures the pulsewidth and period for the servo, in milliseconds.
Markatron 0:d388aed56112 125 ** @params pulsewidth_ms: The pwm pulsewidth for the servo, in milliseconds.
Markatron 0:d388aed56112 126 ** @params period_ms: The pwm period for the servo, in milliseconds.
Markatron 0:d388aed56112 127 ** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms)
Markatron 0:d388aed56112 128 ** @params degrees: The angle to full right/left turn of the servo from centre (0).
Markatron 0:d388aed56112 129 */
Markatron 0:d388aed56112 130 void Car::configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees) {
Markatron 0:d388aed56112 131 m_servo.pulsewidth_ms(pulsewidth_ms);
Markatron 0:d388aed56112 132 m_servo.period_ms(period_ms);
Markatron 0:d388aed56112 133 m_servoRange = range;
Markatron 0:d388aed56112 134 m_servoDegrees = degrees;
Markatron 0:d388aed56112 135 }
Markatron 0:d388aed56112 136
Markatron 0:d388aed56112 137 /*
Markatron 0:d388aed56112 138 ** Configures the pulsewidth and period for the motor, in microseconds.
Markatron 0:d388aed56112 139 ** @params pulsewidth_us: The pwm pulsewidth for the motor, in mircoseconds.
Markatron 0:d388aed56112 140 ** @params period_us: The pwm period for the motor, in microseconds.
Markatron 0:d388aed56112 141 */
Markatron 0:d388aed56112 142 void Car::configureMotor_us(int pulsewidth_us, int period_us) {
Markatron 0:d388aed56112 143 m_motor.pulsewidth_us(pulsewidth_us);
Markatron 0:d388aed56112 144 m_motor.period_us(period_us);
Markatron 0:d388aed56112 145 }
Markatron 0:d388aed56112 146
Markatron 0:d388aed56112 147 /*
Markatron 0:d388aed56112 148 ** Configures the pulsewidth and period for the motor, in milliseconds.
Markatron 0:d388aed56112 149 ** @params pulsewidth_ms: The pwm pulsewidth for the motor, in milliseconds.
Markatron 0:d388aed56112 150 ** @params period_ms: The pwm period for the motor, in milliseconds.
Markatron 0:d388aed56112 151 */
Markatron 0:d388aed56112 152 void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) {
Markatron 0:d388aed56112 153 m_motor.pulsewidth_ms(pulsewidth_ms);
Markatron 0:d388aed56112 154 m_motor.period_ms(period_ms);
Markatron 0:d388aed56112 155 }
Markatron 0:d388aed56112 156
Markatron 0:d388aed56112 157 /*
Markatron 1:3e1290de9c8d 158 ** Provides information required to make use of an encoder for
Markatron 1:3e1290de9c8d 159 ** specifying distance.
Markatron 1:3e1290de9c8d 160 ** @params countsPerRevolution: The number of counts the encoder
Markatron 1:3e1290de9c8d 161 ** makes in one full cycle of the wheel.
Markatron 1:3e1290de9c8d 162 ** @params wheelCircumference: The circumference of the wheel being
Markatron 1:3e1290de9c8d 163 ** read by the encoder.
Markatron 1:3e1290de9c8d 164 */
Markatron 1:3e1290de9c8d 165 void Car::configureEncoder(int countsPerRevolution, float wheelCircumference) {
Markatron 1:3e1290de9c8d 166 m_countsPerRevolution = countsPerRevolution;
Markatron 1:3e1290de9c8d 167 m_wheelCircumference = wheelCircumference;
Markatron 1:3e1290de9c8d 168 }
Markatron 1:3e1290de9c8d 169
Markatron 1:3e1290de9c8d 170 /*
Markatron 0:d388aed56112 171 ** Takes a distance specified by user, and calculates how far will
Markatron 0:d388aed56112 172 ** be travelled in 1 second. It then calculates the time that will
Markatron 0:d388aed56112 173 ** be required to travel the specified distance from this result.
Markatron 0:d388aed56112 174 ** @params distance: The distance that needs to be converted into
Markatron 0:d388aed56112 175 ** a time value.
Markatron 0:d388aed56112 176 */
Markatron 1:3e1290de9c8d 177 /*
Markatron 0:d388aed56112 178 float Car::distanceToTimeConverter(float distance) {
Markatron 0:d388aed56112 179 float singleMovement = sqrt(distance); // sqaure root of distance.
Markatron 0:d388aed56112 180 float time = 1;
Markatron 0:d388aed56112 181
Markatron 0:d388aed56112 182 time = time + (distance / singleMovement);
Markatron 0:d388aed56112 183
Markatron 0:d388aed56112 184 return time;
Markatron 0:d388aed56112 185 }
Markatron 1:3e1290de9c8d 186 */
Markatron 0:d388aed56112 187
Markatron 0:d388aed56112 188 #endif // CAR_C