A Library to drive the RenBuggy_Servo.

Dependencies:   PinDetect

Dependents:  

Fork of RenBuggyServo by Renishaw

Revision:
1:3e1290de9c8d
Parent:
0:d388aed56112
Child:
2:287a808baad7
--- a/Car.cpp	Fri Mar 07 07:54:13 2014 +0000
+++ b/Car.cpp	Mon Mar 10 10:15:22 2014 +0000
@@ -1,5 +1,5 @@
 /*******************************************************************************
-* RenBED Car used to drive RenBuggy with servo and 1 motor                     *
+* RenBED Car used to drive RenBuggy with servo, motor and encoder(optional)    *
 * Copyright (c) 2014 Mark Jones                                                *
 *                                                                              *
 * Permission is hereby granted, free of charge, to any person obtaining a copy *
@@ -38,7 +38,13 @@
 */
 Car::Car(PinName servoPin, PinName motorPin) 
  : m_servo(servoPin), m_motor(motorPin) {
-    
+    m_speed = 15000;
+}
+
+Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference)
+ : m_servo(servoPin), m_motor(motorPin) {
+    configureEncoder(countsPerRevolution, wheelCircumference);
+    m_speed = 15000;
 }
 
 /*
@@ -47,37 +53,40 @@
 Car::~Car() {
 }
 
+/* 
+**
+*/
+void Car::setSpeed(int speed_us) {
+    m_speed = speed_us;
+}
+
 /*
-** 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.
+** This function is for use in conjuction with
+** an encoder, and makes the car move a specified
+** distance. 
+** @params distance: The distance the car should 
+** move, in cm.
 */
 void Car::forwards(float distance) {  
-    float singleMovement = 6;         // Distance travelled in 1 sec
-    float time = 1;                   // Time taken to travel 6cm.
+    
+    int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference));
     
-    time = time + (distance / singleMovement);
+    // Tell encoder to keep reading, and have motor keep going forward
+    // until the specified number of counts (countsForward) has been read
+    // (e.g. countsForward = 10
+    //       while(encoderValue <= 10)
+    //       {
+    //           m_motor.pulsewidth(m_speed);
+    //       }
+    //       stop();
     
-    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);
+    m_motor.pulsewidth_us(m_speed);
 }
 
 /*
@@ -146,14 +155,27 @@
 }
 
 /*
+** Provides information required to make use of an encoder for 
+** specifying distance.
+** @params countsPerRevolution: The number of counts the encoder
+** makes in one full cycle of the wheel.
+** @params wheelCircumference: The circumference of the wheel being
+** read by the encoder.
+*/
+void Car::configureEncoder(int countsPerRevolution, float wheelCircumference) {
+    m_countsPerRevolution = countsPerRevolution;
+    m_wheelCircumference = wheelCircumference;
+}
+
+/*
 ** 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;                         
     
@@ -161,5 +183,6 @@
     
     return time; 
 }
+*/
 
 #endif // CAR_C
\ No newline at end of file