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:
Fri Mar 07 07:54:13 2014 +0000
Revision:
0:d388aed56112
Child:
1:3e1290de9c8d
Library for the RenBuggy_Servo.

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.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 0:d388aed56112 41
Markatron 0:d388aed56112 42 }
Markatron 0:d388aed56112 43
Markatron 0:d388aed56112 44 /*
Markatron 0:d388aed56112 45 ** Deconstructs the car.
Markatron 0:d388aed56112 46 */
Markatron 0:d388aed56112 47 Car::~Car() {
Markatron 0:d388aed56112 48 }
Markatron 0:d388aed56112 49
Markatron 0:d388aed56112 50 /*
Markatron 0:d388aed56112 51 ** Moves the car in the direction it is pointing.
Markatron 0:d388aed56112 52 ** @params distance: The distance the car will move.
Markatron 0:d388aed56112 53 ** For every second the car is moving, it covers 6cm. So,
Markatron 0:d388aed56112 54 ** the time to wait for travel will be the time it takes to
Markatron 0:d388aed56112 55 ** travel 6cm (so 1 second) + the distance specified, divided
Markatron 0:d388aed56112 56 ** by 6.
Markatron 0:d388aed56112 57 */
Markatron 0:d388aed56112 58 void Car::forwards(float distance) {
Markatron 0:d388aed56112 59 float singleMovement = 6; // Distance travelled in 1 sec
Markatron 0:d388aed56112 60 float time = 1; // Time taken to travel 6cm.
Markatron 0:d388aed56112 61
Markatron 0:d388aed56112 62 time = time + (distance / singleMovement);
Markatron 0:d388aed56112 63
Markatron 0:d388aed56112 64 m_motor.pulsewidth(7000);
Markatron 0:d388aed56112 65 wait(time);
Markatron 0:d388aed56112 66 }
Markatron 0:d388aed56112 67
Markatron 0:d388aed56112 68 /*
Markatron 0:d388aed56112 69 ** Make the car move forward, at the speed specified.
Markatron 0:d388aed56112 70 ** @params speed: Sets the speed that the car will move at.
Markatron 0:d388aed56112 71 */
Markatron 0:d388aed56112 72 void Car::forwards(int speed) {
Markatron 0:d388aed56112 73 m_motor.pulsewidth(speed);
Markatron 0:d388aed56112 74 }
Markatron 0:d388aed56112 75
Markatron 0:d388aed56112 76 /*
Markatron 0:d388aed56112 77 ** Start the car moving with a default speed.
Markatron 0:d388aed56112 78 */
Markatron 0:d388aed56112 79 void Car::forwards() {
Markatron 0:d388aed56112 80 m_motor.pulsewidth_us(15000);
Markatron 0:d388aed56112 81 }
Markatron 0:d388aed56112 82
Markatron 0:d388aed56112 83 /*
Markatron 0:d388aed56112 84 ** Stops the motor.
Markatron 0:d388aed56112 85 */
Markatron 0:d388aed56112 86 void Car::stop() {
Markatron 0:d388aed56112 87 m_motor.pulsewidth_us(0);
Markatron 0:d388aed56112 88 }
Markatron 0:d388aed56112 89
Markatron 0:d388aed56112 90 /*
Markatron 0:d388aed56112 91 ** Set the direction the car is facing.
Markatron 0:d388aed56112 92 ** @params degrees: The degrees of the angle, where -45 is full
Markatron 0:d388aed56112 93 ** left, 0 is centre and +45 is full right.
Markatron 0:d388aed56112 94 */
Markatron 0:d388aed56112 95 void Car::setDirection(int degrees) {
Markatron 0:d388aed56112 96 float angleOffset = m_servoRange * (m_servoDegrees / degrees);
Markatron 0:d388aed56112 97 m_servo.pulsewidth_us(1500 + angleOffset);
Markatron 0:d388aed56112 98 }
Markatron 0:d388aed56112 99
Markatron 0:d388aed56112 100 /*
Markatron 0:d388aed56112 101 ** Configures the pulsewidth and perion for the servon, in microseconds.
Markatron 0:d388aed56112 102 ** @params pulsewidth_us: The pwm pulsewidth for the servo, in mircoseconds.
Markatron 0:d388aed56112 103 ** @params period_ms: The pwm period for the servo, in mircoseconds.
Markatron 0:d388aed56112 104 ** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms).
Markatron 0:d388aed56112 105 ** @params degrees: The angle to full right/left turn of the servo from centre (0).
Markatron 0:d388aed56112 106 */
Markatron 0:d388aed56112 107 void Car::configureServo_us(int pulsewidth_us, int period_us, int range, float degrees) {
Markatron 0:d388aed56112 108 m_servo.pulsewidth_us(pulsewidth_us);
Markatron 0:d388aed56112 109 m_servo.period_us(period_us);
Markatron 0:d388aed56112 110 m_servoRange = range;
Markatron 0:d388aed56112 111 m_servoDegrees = degrees;
Markatron 0:d388aed56112 112 }
Markatron 0:d388aed56112 113
Markatron 0:d388aed56112 114 /*
Markatron 0:d388aed56112 115 ** Configures the pulsewidth and period for the servo, in milliseconds.
Markatron 0:d388aed56112 116 ** @params pulsewidth_ms: The pwm pulsewidth for the servo, in milliseconds.
Markatron 0:d388aed56112 117 ** @params period_ms: The pwm period for the servo, in milliseconds.
Markatron 0:d388aed56112 118 ** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms)
Markatron 0:d388aed56112 119 ** @params degrees: The angle to full right/left turn of the servo from centre (0).
Markatron 0:d388aed56112 120 */
Markatron 0:d388aed56112 121 void Car::configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees) {
Markatron 0:d388aed56112 122 m_servo.pulsewidth_ms(pulsewidth_ms);
Markatron 0:d388aed56112 123 m_servo.period_ms(period_ms);
Markatron 0:d388aed56112 124 m_servoRange = range;
Markatron 0:d388aed56112 125 m_servoDegrees = degrees;
Markatron 0:d388aed56112 126 }
Markatron 0:d388aed56112 127
Markatron 0:d388aed56112 128 /*
Markatron 0:d388aed56112 129 ** Configures the pulsewidth and period for the motor, in microseconds.
Markatron 0:d388aed56112 130 ** @params pulsewidth_us: The pwm pulsewidth for the motor, in mircoseconds.
Markatron 0:d388aed56112 131 ** @params period_us: The pwm period for the motor, in microseconds.
Markatron 0:d388aed56112 132 */
Markatron 0:d388aed56112 133 void Car::configureMotor_us(int pulsewidth_us, int period_us) {
Markatron 0:d388aed56112 134 m_motor.pulsewidth_us(pulsewidth_us);
Markatron 0:d388aed56112 135 m_motor.period_us(period_us);
Markatron 0:d388aed56112 136 }
Markatron 0:d388aed56112 137
Markatron 0:d388aed56112 138 /*
Markatron 0:d388aed56112 139 ** Configures the pulsewidth and period for the motor, in milliseconds.
Markatron 0:d388aed56112 140 ** @params pulsewidth_ms: The pwm pulsewidth for the motor, in milliseconds.
Markatron 0:d388aed56112 141 ** @params period_ms: The pwm period for the motor, in milliseconds.
Markatron 0:d388aed56112 142 */
Markatron 0:d388aed56112 143 void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) {
Markatron 0:d388aed56112 144 m_motor.pulsewidth_ms(pulsewidth_ms);
Markatron 0:d388aed56112 145 m_motor.period_ms(period_ms);
Markatron 0:d388aed56112 146 }
Markatron 0:d388aed56112 147
Markatron 0:d388aed56112 148 /*
Markatron 0:d388aed56112 149 ** Takes a distance specified by user, and calculates how far will
Markatron 0:d388aed56112 150 ** be travelled in 1 second. It then calculates the time that will
Markatron 0:d388aed56112 151 ** be required to travel the specified distance from this result.
Markatron 0:d388aed56112 152 ** @params distance: The distance that needs to be converted into
Markatron 0:d388aed56112 153 ** a time value.
Markatron 0:d388aed56112 154 */
Markatron 0:d388aed56112 155 float Car::distanceToTimeConverter(float distance) {
Markatron 0:d388aed56112 156
Markatron 0:d388aed56112 157 float singleMovement = sqrt(distance); // sqaure root of distance.
Markatron 0:d388aed56112 158 float time = 1;
Markatron 0:d388aed56112 159
Markatron 0:d388aed56112 160 time = time + (distance / singleMovement);
Markatron 0:d388aed56112 161
Markatron 0:d388aed56112 162 return time;
Markatron 0:d388aed56112 163 }
Markatron 0:d388aed56112 164
Markatron 0:d388aed56112 165 #endif // CAR_C