Updated version of RenBuggy Servo that can accept instructions based on time or distance.
Fork of RenBuggyServo by
Revision 3:01bc89d7ae8e, committed 2014-03-13
- Comitter:
- Markatron
- Date:
- Thu Mar 13 07:31:10 2014 +0000
- Parent:
- 2:287a808baad7
- Child:
- 4:aac581bec332
- Commit message:
- Finished adding functionality for the use of an encoder.
Changed in this revision
--- 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
--- a/Car.h Mon Mar 10 11:53:45 2014 +0000 +++ b/Car.h Thu Mar 13 07:31:10 2014 +0000 @@ -29,6 +29,7 @@ #define CAR_H #include "mbed.h" +#include "PinDetect.h" /** * RenBuggyServo Example: @@ -60,7 +61,7 @@ myCar.setSpeed(20000); myCar.setDirection(0); - myCar.forwards(); + myCar.forwards(); wait(1); myCar.setDirection(45); wait(1); @@ -87,17 +88,19 @@ int m_speed; + int m_encoderCount; + int m_servoRange; // Pulsewidth range to full left/right from centre (1.5ms) float m_servoDegrees; // Angle to full right/left turn from centre (0). float m_wheelCircumference; // The circumference of the wheel with stripes. int m_countsPerRevolution; // The number of stripes on the wheel. - //InterruptIn m_stripeInterrupt; + + PinDetect m_sensor; // For debouncing. - //float distanceToTimeConverter(float distance); + void updateEncoderCount(); - public: - + public: /** Constructs the car with PwmOut objects for servo and motor. * * @param servoPin is the pin used for pwm output for driving the servo. @@ -112,10 +115,11 @@ * @param motorPin is the pin used for pwm output for driving the motor. * @param countsPerRevolution is the number of counts the encoder * makes in one full cycle of the wheel. - * @param wheelCircumference: The circumference of the wheel being + * @param wheelCircumference is the circumference of the wheel being * read by the encoder. + * @param sensorPin is the pin required for the encoder for debouncing. */ - Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference); + Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference, PinName sensorPin); /** * Deconstructs the car.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Thu Mar 13 07:31:10 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b