A Library to drive the RenBuggy_Servo.
Dependencies: PinDetect
Fork of RenBuggyServo by
Revision 1:3e1290de9c8d, committed 2014-03-10
- Comitter:
- Markatron
- Date:
- Mon Mar 10 10:15:22 2014 +0000
- Parent:
- 0:d388aed56112
- Child:
- 2:287a808baad7
- Commit message:
- RenBuggy Servo updated library
Changed in this revision
Car.cpp | Show annotated file Show diff for this revision Revisions of this file |
Car.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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
--- a/Car.h Fri Mar 07 07:54:13 2014 +0000 +++ b/Car.h Mon Mar 10 10:15:22 2014 +0000 @@ -32,18 +32,29 @@ class Car { private: + PwmOut m_servo; PwmOut m_motor; - int m_pwmPeriod; // Period used in PWM. + + int m_speed; + 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 distanceToTimeConverter(float distance); + + float m_wheelCircumference; // The circumference of the wheel with stripes. + int m_countsPerRevolution; // The number of stripes on the wheel. + //InterruptIn m_stripeInterrupt; + + //float distanceToTimeConverter(float distance); + public: + Car(PinName servoPin, PinName motorPin); + Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference); ~Car(); + void setSpeed(int speed_us); void forwards(float distance); - void forwards(int speed); void forwards(); void stop(); void setDirection(int degrees); @@ -53,6 +64,8 @@ void configureMotor_us(int pulsewidth_us, int period_us); void configureMotor_ms(int pulsewidth_ms, int period_ms); + + void configureEncoder(int countsPerRevolution, float wheelCircumference); }; #endif // CAR_H \ No newline at end of file