A Library to drive the RenBuggy_Servo.
Dependencies: PinDetect
Fork of RenBuggyServo by
Diff: Car.h
- Revision:
- 2:287a808baad7
- Parent:
- 1:3e1290de9c8d
- Child:
- 3:01bc89d7ae8e
diff -r 3e1290de9c8d -r 287a808baad7 Car.h --- a/Car.h Mon Mar 10 10:15:22 2014 +0000 +++ b/Car.h Mon Mar 10 11:53:45 2014 +0000 @@ -30,9 +30,58 @@ #include "mbed.h" +/** +* RenBuggyServo Example: +* @code +* +* +#include "mbed.h" +#include "Car.h" + +Car myCar(P1_25, P1_24); + +// Main entry point of application. +int main() { + + const int SERVO_PWM = 1500; // 1500 = centre. + const int SERVO_PWM_PERIOD = 2000; + const int SERVO_PWM_RANGE = 500; // + or - 500 microseconds. + const float SERVO_DEGREES_RANGE = 45.0; // + or - from centre is full right/left. + + const int MOTOR_PWM = 20000; + const int MOTOR_PERIOD = 20000; + + // Configure the servo and motor before use. + myCar.configureServo_us(SERVO_PWM, SERVO_PWM_PERIOD, + SERVO_PWM_RANGE, SERVO_DEGREES_RANGE); + myCar.configureMotor_us(MOTOR_PWM, MOTOR_PERIOD); + + // Drive the RenBuggy! + myCar.setSpeed(20000); + myCar.setDirection(0); + + myCar.forwards(); + wait(1); + myCar.setDirection(45); + wait(1); + myCar.setDirection(-45); + wait(1); + myCar.setDirection(0); + myCar.stop(); + + myCar.forwards(10); // Move the car a final 10cm. + myCar.stop(); +} +// End programme. +@endcode +*/ + +/** +** The car class for controlling the RenBuggy_Servo. +*/ class Car { private: - + PwmOut m_servo; PwmOut m_motor; @@ -49,22 +98,103 @@ public: + /** Constructs the car with PwmOut objects for servo and motor. + * + * @param servoPin is the pin used for pwm output for driving the servo. + * @param motorPin is the pin used for pwm output for driving the motor. + */ Car(PinName servoPin, PinName motorPin); + + /** Constructs the car with PwmOut objects for servo and motor, and configures + * the encoder. + * + * @param servoPin is the pin used for pwm output for driving the servo. + * @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 + * read by the encoder. + */ Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference); + + /** + * Deconstructs the car. + */ ~Car(); + /** + * Sets the speed the buggy will move at. + * @param speed_us is the speed the car will move at, in microseconds. + */ void setSpeed(int speed_us); + + /** + * Moves the car in the direction it is facing, for a specified + * distance. + * @param distance is how far the car will travel, in cm. + */ void forwards(float distance); + + /** + * Moves the car in the direction it is facing, until a user + * tells it to stop. + */ void forwards(); + + /** + * Stops the car from moving. + */ void stop(); + + /** + * Sets the direction the car will face. This is used to navigate the car. + * @param degrees is the angle of the servo, where -45 is full + * left, 0 is centre and +45 is full right. + */ void setDirection(int degrees); + /** + * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and + * period is accepted in microsecond format. + * @param pulsewidth_us is pwm pulsewidth for the servo, in mircoseconds. + * @param period_us is the pwm period for the servo, in mircoseconds. + * @param range is the pulsewidth range to full left/right turn of the servo from centre (1500us). + * @param degrees is the angle to full right/left turn of the servo from centre (0). + */ void configureServo_us(int pulsewidth_us, int period_us, int range, float degrees); + + /** + * Configures the servo with a pulsewidth, period, range and degrees. Pulsewidth and + * period is accepted in millisecond format. + * @param pulsewidth_ms is pwm pulsewidth for the servo, in millisecond. + * @param period_ms is the pwm period for the servo, in millisecond. + * @param range is the pulsewidth range to full left/right turn of the servo from centre (1.5ms). + * @param degrees is the angle to full right/left turn of the servo from centre (0). + */ void configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees); + /** + * Configures the pulsewidth and period for the motor, in microseconds. + * @param pulsewidth_us is the pwm pulsewidth for the motor, in mircoseconds. + * @param period_us is the pwm period for the motor, in microseconds. + */ void configureMotor_us(int pulsewidth_us, int period_us); + + /** + * Configures the pulsewidth and period for the motor, in milliseconds. + * @param pulsewidth_ms is the pwm pulsewidth for the motor, in milliseconds. + * @param period_ms is the pwm period for the motor, in milliseconds. + */ void configureMotor_ms(int pulsewidth_ms, int period_ms); + /** + * Provides information required to make use of an encoder for + * specifying distance. + * @param countsPerRevolution is the number of counts the encoder + * makes in one full cycle of the wheel. + * @param wheelCircumference is the circumference of the wheel being + * read by the encoder. + */ void configureEncoder(int countsPerRevolution, float wheelCircumference); };