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

Dependencies:   PinDetect mbed

Fork of RenBuggyServo by Renishaw

Revision:
3:01bc89d7ae8e
Parent:
2:287a808baad7
Child:
4:aac581bec332
--- a/Car.cpp	Mon Mar 10 11:53:45 2014 +0000
+++ b/Car.cpp	Thu Mar 13 07:31:10 2014 +0000
@@ -34,12 +34,23 @@
 Car::Car(PinName servoPin, PinName motorPin) 
  : m_servo(servoPin), m_motor(motorPin) {
     m_speed = 15000;
+    m_countsPerRevolution = 0;
+    m_wheelCircumference = 0;
 }
 
-Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference)
- : m_servo(servoPin), m_motor(motorPin) {
+Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference, PinName sensorPin)
+ : m_servo(servoPin), m_motor(motorPin), m_sensor(sensorPin) {
     configureEncoder(countsPerRevolution, wheelCircumference);
+    
     m_speed = 15000;
+    setDirection(0);
+    
+    m_encoderCount = 0;
+    m_sensor.setSampleFrequency(1000);
+    m_sensor.setSamplesTillHeld(5);
+
+    Car* basePointer = dynamic_cast<Car*>(this);    
+    m_sensor.attach_deasserted_held(basePointer, &Car::updateEncoderCount);
 }
 
 Car::~Car() {
@@ -49,19 +60,29 @@
     m_speed = speed_us;
 }
 
+void Car::updateEncoderCount() {
+    m_encoderCount++;
+}
+
 void Car::forwards(float distance) {  
     
     int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference));
     
-    // 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_encoderCount = 0;
+    bool isMoving = true;
+    m_motor.pulsewidth_us(m_speed);
     
+    while(isMoving) {
+        wait(0.2); // <<-- for some unknown reason, this requires a delay to work :-S
+        if(countsForward < m_encoderCount)
+        {
+            isMoving = false;
+        }
+    }
+    // When it's finished, stop the buggy.
+    stop();
+    
+    return;
 }   
 
 void Car::forwards() {
@@ -103,25 +124,7 @@
 
 void Car::configureEncoder(int countsPerRevolution, float wheelCircumference) {
     m_countsPerRevolution = countsPerRevolution;
-    m_wheelCircumference = wheelCircumference;
+    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;                         
-    
-    time = time + (distance / singleMovement);
-    
-    return time; 
-}
-*/
-
 #endif // CAR_C
\ No newline at end of file