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

Dependencies:   PinDetect mbed

Fork of RenBuggyServo by Renishaw

Revision:
0:d388aed56112
Child:
1:3e1290de9c8d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Car.cpp	Fri Mar 07 07:54:13 2014 +0000
@@ -0,0 +1,165 @@
+/*******************************************************************************
+* RenBED Car used to drive RenBuggy with servo and 1 motor                     *
+* Copyright (c) 2014 Mark Jones                                                *
+*                                                                              *
+* Permission is hereby granted, free of charge, to any person obtaining a copy *
+* of this software and associated documentation files (the "Software"), to deal*
+* in the Software without restriction, including without limitation the rights *
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell    *
+* copies of the Software, and to permit persons to whom the Software is        *
+* furnished to do so, subject to the following conditions:                     *
+*                                                                              *
+* The above copyright notice and this permission notice shall be included in   *
+* all copies or substantial portions of the Software.                          *
+*                                                                              *
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  *
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN    *
+* THE SOFTWARE.                                                                *
+*                                                                              *
+* Car.cpp                                                                      *
+*                                                                              *
+* V1.0 05/03/2014                                          Mark Jones          *
+*******************************************************************************/
+
+#ifndef CAR_C
+#define CAR_C
+
+#include "Car.h"
+#include "mbed.h"
+
+/*
+** Constructs the car with PwmOut objects for servo and motor.
+** @params servoPin: This is the pin used for pwm output for driving the servo.
+** @params motorPin: This is the pin used for pwm output for driving the motor.
+*/
+Car::Car(PinName servoPin, PinName motorPin) 
+ : m_servo(servoPin), m_motor(motorPin) {
+    
+}
+
+/*
+** Deconstructs the car.
+*/
+Car::~Car() {
+}
+
+/*
+** Moves the car in the direction it is pointing.
+** @params distance: The distance the car will move.
+** For every second the car is moving, it covers 6cm. So, 
+** the time to wait for travel will be the time it takes to
+** travel 6cm (so 1 second) + the distance specified, divided
+** by 6.
+*/
+void Car::forwards(float distance) {  
+    float singleMovement = 6;         // Distance travelled in 1 sec
+    float time = 1;                   // Time taken to travel 6cm.
+    
+    time = time + (distance / singleMovement);
+    
+    m_motor.pulsewidth(7000);
+    wait(time);
+}   
+
+/*
+** Make the car move forward, at the speed specified.
+** @params speed: Sets the speed that the car will move at.
+*/
+void Car::forwards(int speed) {
+    m_motor.pulsewidth(speed);
+}
+
+/*
+** Start the car moving with a default speed.
+*/ 
+void Car::forwards() {
+    m_motor.pulsewidth_us(15000);
+}
+
+/*
+** Stops the motor.
+*/
+void Car::stop() {
+    m_motor.pulsewidth_us(0);
+}
+
+/*
+** Set the direction the car is facing.
+** @params degrees: The degrees of the angle, where -45 is full 
+**                  left, 0 is centre and +45 is full right.
+*/
+void Car::setDirection(int degrees) {
+    float angleOffset = m_servoRange * (m_servoDegrees / degrees);
+    m_servo.pulsewidth_us(1500 + angleOffset);
+}
+
+/*
+** Configures the pulsewidth and perion for the servon, in microseconds.
+** @params pulsewidth_us: The pwm pulsewidth for the servo, in mircoseconds.
+** @params period_ms: The pwm period for the servo, in mircoseconds.
+** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms).
+** @params degrees: The angle to full right/left turn of the servo from centre (0).
+*/
+void Car::configureServo_us(int pulsewidth_us, int period_us, int range, float degrees) {
+    m_servo.pulsewidth_us(pulsewidth_us);
+    m_servo.period_us(period_us);
+    m_servoRange = range;
+    m_servoDegrees = degrees;
+}
+
+/* 
+** Configures the pulsewidth and period for the servo, in milliseconds.
+** @params pulsewidth_ms: The pwm pulsewidth for the servo, in milliseconds.
+** @params period_ms: The pwm period for the servo, in milliseconds.
+** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms)
+** @params degrees: The angle to full right/left turn of the servo from centre (0).
+*/
+void Car::configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees) {
+    m_servo.pulsewidth_ms(pulsewidth_ms);
+    m_servo.period_ms(period_ms);
+    m_servoRange = range;
+    m_servoDegrees = degrees;
+}
+
+/*
+** Configures the pulsewidth and period for the motor, in microseconds.
+** @params pulsewidth_us: The pwm pulsewidth for the motor, in mircoseconds.
+** @params period_us: The pwm period for the motor, in microseconds.
+*/
+void Car::configureMotor_us(int pulsewidth_us, int period_us) {
+    m_motor.pulsewidth_us(pulsewidth_us);
+    m_motor.period_us(period_us);
+}
+
+/*
+** Configures the pulsewidth and period for the motor, in milliseconds.
+** @params pulsewidth_ms: The pwm pulsewidth for the motor, in milliseconds.
+** @params period_ms: The pwm period for the motor, in milliseconds.
+*/
+void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) {
+    m_motor.pulsewidth_ms(pulsewidth_ms);
+    m_motor.period_ms(period_ms);
+}
+
+/*
+** Takes a distance specified by user, and calculates how far will 
+** be travelled in 1 second. It then calculates the time that will 
+** be required to travel the specified distance from this result.
+** @params distance: The distance that needs to be converted into 
+** a time value.
+*/
+float Car::distanceToTimeConverter(float distance) {
+    
+    float singleMovement = sqrt(distance);  // sqaure root of distance.
+    float time = 1;                         
+    
+    time = time + (distance / singleMovement);
+    
+    return time; 
+}
+
+#endif // CAR_C
\ No newline at end of file