V1 of the ParallaxRobotShield library
Revision 0:2ab9f4a3187a, committed 2015-08-20
- Comitter:
- omarrasheedk
- Date:
- Thu Aug 20 21:51:12 2015 +0000
- Commit message:
- V1 of ParallaxRobotShield
Changed in this revision
ParallaxRobotShield.cpp | Show annotated file Show diff for this revision Revisions of this file |
ParallaxRobotShield.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 2ab9f4a3187a ParallaxRobotShield.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ParallaxRobotShield.cpp Thu Aug 20 21:51:12 2015 +0000 @@ -0,0 +1,151 @@ +#include "ParallaxRobotShield.h" +#include "mbed.h" + +ParallaxRobotShield::ParallaxRobotShield(PinName leftPin, PinName rightPin) : leftServo(leftPin) , rightServo(rightPin) +{ + leftPosition = 1450; + rightPosition = 1550; +} + +void ParallaxRobotShield::left_servo(int speed) +{ + speed = 1496 - (speed * 2); + leftPosition = speed; +} + + +void ParallaxRobotShield::right_servo(int speed) +{ + speed = 1490 + (speed * 2); + rightPosition = speed; +} + +//void ParallaxRobotShield::StartPulse() +//{ +// ServoPin = 1; +// PulseStop.attach_us(this, &ParallaxRobotShield::EndPulse, Position); +//} + +void ParallaxRobotShield::disable_left_motor() // Just for internal functions +{ + leftServo = 0; +} +//void ParallaxRobotShield::EndPulse() +//{ +// ServoPin = 0; +//} +void ParallaxRobotShield::disable_right_motor() // Just for internal functions +{ + rightServo = 0; +} + + +void ParallaxRobotShield::enable_left_motor() +{ + leftServo = 1; + leftPulse.attach_us(this, &ParallaxRobotShield::enable_left_motor, 20000); // Period is set to 20000 us by default which is equal to 50 Hz + leftPulseStop.attach_us(this, &ParallaxRobotShield::disable_left_motor, leftPosition); +} + + +void ParallaxRobotShield::enable_right_motor() +{ + rightServo = 1; + rightPulse.attach_us(this, &ParallaxRobotShield::enable_right_motor, 20000); // Period is set to 20000 us by default which is equal to 50 Hz + rightPulseStop.attach_us(this, &ParallaxRobotShield::disable_right_motor, rightPosition); +} + + +void ParallaxRobotShield::turn_left(int speed) +{ + stopRight(); + int i; + for(i = 0; i < 100; i++) + { + left_servo(speed); + wait_ms(10); + } + enable_right_motor(); +} + + +void ParallaxRobotShield::turn_right(int speed) +{ + stopLeft(); + int i; + for(i = 0; i < 100; i++) + { + right_servo(speed); + wait_ms(10); + } + enable_left_motor(); +} + + +void ParallaxRobotShield::forward(int speed) +{ + int i; + for(i = 0; i < 100; i++) + { + left_servo(speed); + right_servo(speed); + wait_ms(20); + } +} + + +void ParallaxRobotShield::backward(int speed) +{ + forward(-speed); +} + + +void ParallaxRobotShield::left(int speed) +{ + int i; + for(i = 0; i < 100; i++) + { + left_servo(speed); + right_servo(-speed); + wait_ms(20); + } +} + + +void ParallaxRobotShield::right(int speed) +{ + left(-speed); +} + + +void ParallaxRobotShield::stopLeft() +{ + leftPulse.detach(); +} + +void ParallaxRobotShield::stopRight() +{ + rightPulse.detach(); +} + + +void ParallaxRobotShield::stop(int motor) +{ + if(motor == 1) + stopRight(); + else if(motor == 2) + stopLeft(); +} + + +void ParallaxRobotShield::stopAll() +{ + stopRight(); + stopLeft(); +} + +//void ParallaxRobotShield::Enable(int StartPos, int Period) +//{ +// leftPosition = StartPos; +// leftPulse.attach_us(this, &ParallaxRobotShield::enable_left_motor, Period); +//} \ No newline at end of file
diff -r 000000000000 -r 2ab9f4a3187a ParallaxRobotShield.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ParallaxRobotShield.h Thu Aug 20 21:51:12 2015 +0000 @@ -0,0 +1,189 @@ +#include "mbed.h" +// us means microseconds + +/** Parallax Robot Shield Control Class + * + */ +class ParallaxRobotShield +{ + public: + + /** Create a new ParallaxRobotShield object on any mbed pin + * + * @param leftPin Left Servomotor Signal pin + * @param rightPin Right Servomotor Signal pin + */ + ParallaxRobotShield(PinName leftPin = D10, PinName rightPin = D11); + + + /** Change the position of the servo. Position in us. + * + * @param NewPos The new value of the servos position (us) + */ + //void SetPosition(int NewPos); + + + /** Enable the servo. Without enabling, the servo won't be running. Startposition and period both in us. + * + * @param StartPos The position of the servo to start (us) + * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us) + */ + //void Enable(int StartPos, int Period); + + + /** Disable the servo. After disabling the servo won't get any signal anymore + * + */ + //void Disable(); + + + /** Create a robot shield object************************************************************************************************ + * + * @param leftServo Left Servomotor Signal pin, default D13 + * @param rightServo RightServomotor Signal pin, default D14 + * @param whisker Whisker digital pin connection, default D7 + * @param lightSensor Sensor signal pin, default D6 + * @param bumpSensor Sensor signal pin, default D5 + * @param HCSR04sensor Sensor in-right pin, default D4 // For later - Need Trigger and RCC + */ +//------>ParallaxRobotShield(PinName leftServo, PinName rightServo , PinName whisker, PinName lightSensor, PinName bumpSensor); + + + + /** Switch on the left servo at the given speed. Can be used with loops to run the left servomotor. + * @param speed The speed, from 0 to 100 at which to spin the servomotor. + */ + void left_servo(int speed); + + + + /** Switch on the right servo at the given speed. Can be used with loops to run the right servomotor. + * @param speed The speed, from 0 to 100 at which to spin the servomotor. + */ + void right_servo(int speed); + + + + /** Switch on both servomotors, forwards at the given speed. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void forward(int speed); + + + + /** Switch on both servomotors, backwards at the given speed. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void backward(int speed); + + + + /** Switch on both servomotors at the given speed, in opposite directions so as to turn left. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void left(int speed); + + + + /** Switch on both servomotors at the given speed, in opposite directions so as to turn right. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void right(int speed); + + + + /** Turns left. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void turn_left(int speed); + + + + /** Turns right. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. + */ + void turn_right(int speed); + + + + /** Disable the left motor + */ + void disable_left_motor(); // Just for internal functions + + + + /** Disable the right servomotor + */ + void disable_right_motor(); // Just for internal functions + + + + /** Enable the left servomotor + */ + void enable_left_motor(); + + + + /** Enable the left servomotor + */ + void enable_right_motor(); + + + + /** Stop a chosen motor. + * @param motor Number, either, 1 = Right or 2 = Left, choosing the motor. + */ + void stop(int motor); + + + + /** Stop left servomotor. + */ + void stopLeft(); + + + + /** Stop right servomotor. + */ + void stopRight(); + + + + /** Stop both servomotors at the same time. Different from disable. + */ + void stopAll(); + + + + // Need to do something to do with detected line... + + /** Gives an indication of the data given by the reflectivity sensors.************************************************************************************* + */ + float line_position(); + + //DigitalIn lightSensor; + //DigitalIn bumpSensor; + //DigitalIn whisker; + + + private: // Use whatever data type you need + + //void StartPulse(); + //void EndPulse(); + + int leftPosition; + int rightPosition; + + DigitalOut leftServo; + DigitalOut rightServo; + + Ticker leftPulse; + Ticker rightPulse; + + Timeout leftPulseStop; + Timeout rightPulseStop; + + //PwmOut rightServo; + //PwmOut leftServo; + +}; \ No newline at end of file