Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 0:0fd8d387c5a0, committed 2015-08-20
- Comitter:
- omarrasheedk
- Date:
- Thu Aug 20 21:50:32 2015 +0000
- Commit message:
- The old ParallaxRobotShield library
Changed in this revision
diff -r 000000000000 -r 0fd8d387c5a0 ParallaxRobotShield/ParallaxRobotShield.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ParallaxRobotShield/ParallaxRobotShield.cpp Thu Aug 20 21:50:32 2015 +0000 @@ -0,0 +1,241 @@ +#include "ParallaxRobotShield.h" +#include "mbed.h" + + +ParallaxRobotShield::ParallaxRobotShield(PinName leftServoPin, PinName rightServoPin, PinName leftWhiskerPin, PinName rightWhiskerPin) : + leftServo(leftServoPin) , rightServo(rightServoPin) , leftWhisker(leftWhiskerPin) , rightWhisker(rightWhiskerPin), leftServo1(leftServoPin) , rightServo1(rightServoPin) +{ + leftPosition = 1500; + rightPosition = 1500; +} + +void ParallaxRobotShield::left_servo(int speed) +{ + speed = 1500 - (speed * 2); + leftPosition = speed; +} + + +void ParallaxRobotShield::right_servo(int speed) +{ + speed = 1500 + (speed * 2); + rightPosition = speed; +} + + +void ParallaxRobotShield::forward(int speed, float time) +{ + backward(-speed, time); +} + + +void ParallaxRobotShield::backward(int speed, float time) +{ + time = time * 1000; // conversion to milliseconds + Timer t; + t.reset(); + t.start(); + while(t.read_ms() != time) + { + left_servo(speed); + right_servo(speed); + } + t.stop(); +} + + +void ParallaxRobotShield::left(int speed, float time) +{ + time = time * 1000; // conversion to milliseconds + Timer t; + t.reset(); + t.start(); + while(t.read_ms() != time) + { + left_servo(speed); + right_servo(-speed); + } + t.stop(); +} + + +void ParallaxRobotShield::right(int speed, float time) +{ + left(-speed, time); +} + + +void ParallaxRobotShield::turn_left(int speed, float time) +{ + time = time * 1000; // conversion to milliseconds + stopRight(); + Timer t; + t.reset(); + t.start(); + while(t.read_ms() != time) + left_servo(speed); + t.stop(); + enable_right_motor(); +} + + +void ParallaxRobotShield::turn_right(int speed, float time) +{ + time = time * 1000; // conversion to milliseconds + stopLeft(); + Timer t; + t.reset(); + t.start(); + while(t.read_ms() != time) + right_servo(speed); + t.stop(); + enable_left_motor(); +} + + +void ParallaxRobotShield::disable_left_motor() // Just for internal functions +{ + leftServo = 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::stop(int motor) +{ + if(motor == 1) + stopRight(); + else if(motor == 2) + stopLeft(); +} + + +void ParallaxRobotShield::stopLeft() +{ + leftPulse.detach(); +} + + +void ParallaxRobotShield::stopRight() +{ + rightPulse.detach(); +} + + +void ParallaxRobotShield::stopAll() +{ + stopRight(); + stopLeft(); +} + + +int ParallaxRobotShield::leftWhiskerContact() +{ + if(leftWhisker.read() == 0) + return 1; + else + return 0; +} + + +int ParallaxRobotShield::rightWhiskerContact() +{ + if(rightWhisker.read() == 0) + return 1; + else + return 0; +} + + +int ParallaxRobotShield::lightSensor(PinName lightSensor) +{ + DigitalInOut QTR(lightSensor); // the QTR-1RC light sensor + int black; + Timer t; + QTR.output(); + QTR = 1; + t.start(); + QTR.input(); + while (QTR == 1 || t.read_us() < 3000); + black = t.read_us(); + t.stop(); + t.reset(); + + if(black > 3100) // Black + return 1; + else + return 0; +} + + +int ParallaxRobotShield::bumpSensor(PinName bumpSensor) +{ + DigitalIn bump(bumpSensor); // the 10 cm Sharp GP2Y0D810Z0F Digital Distance Sensor + if(bump.read() == 0) + return 1; + else + return 0; +} + + +double ParallaxRobotShield::distanceSensor(PinName trigPin, PinName echoPin) +{ + DigitalOut trig(trigPin); // Sends out an Ultasonicsonic signal + DigitalIn echo(echoPin); // Recieves an Ultasonicsonic signal + Timer timer; // for measuring the duration of the signal's joyless journey + + timer.reset(); + trig = 0; + wait_us(2); + trig = 1; + wait_us(10); + trig = 0; + while(echo == 0); + timer.start(); + while(echo == 1); + timer.stop(); + + // Distance = ( (Duration of travel) * (Speed of Ultasonicsonic: 340m/s) ) / 2 + return (timer.read_us() * 0.034) / 2.0; // Microseconds are used for accuracy +} + + +/** Optional implementation for future use +void ParallaxRobotShield::whiskersLED(PinName left_LED, PinName right_LED) +{ + DigitalOut leftLED(left_LED); + DigitalOut rightLED(right_LED); + if(leftWhiskerContact()) + { + leftLED = 1; + wait(0.1); + } + if(rightWhiskerContact()) + { + rightLED = 1; + wait(0.1); + } + leftLED = 0; + rightLED = 0; +} +*/ \ No newline at end of file
diff -r 000000000000 -r 0fd8d387c5a0 ParallaxRobotShield/ParallaxRobotShield.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ParallaxRobotShield/ParallaxRobotShield.h Thu Aug 20 21:50:32 2015 +0000 @@ -0,0 +1,180 @@ +#include "mbed.h" + +/** Parallax Robot Shield Control Class + */ +class ParallaxRobotShield +{ + public: + + /** Create a new ParallaxRobotShield object on any mbed pin + * + * @param leftServoPin Left Servomotor Signal pin - default D10 + * @param rightServoPin Right Servomotor Signal pin - default D11 + * @param leftWhiskerPin Left Whisker digital pin connection + * @param rightWhiskerPin Right Whisker digital pin connection + */ + ParallaxRobotShield(PinName leftServoPin = D10, PinName rightServoPin = D11, PinName leftWhiskerPin = NC, PinName rightWhiskerPin = NC); + + + /** 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. - 100 by default + */ + void left_servo(int speed = 100); // Just for internal functions + + + /** 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. - 100 by default + */ + void right_servo(int speed = 100); // Just for internal functions + + + /** Switch on both servomotors, forwards at the given speed. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. - 100 by default + * @param time The time period to go forward (in seconds) - 1s by default + */ + void forward(int speed = 100, float time = 1); + + + /** Switch on both servomotors, backwards at the given speed. + * @param speed The speed, from 0 to 100 at which to spin the servomotors. - 100 by default + * @param time The time period to go backward (in seconds) - 1s by default + */ + void backward(int speed = 100, float time = 1); + + + /** 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. - 100 by default + * @param time The time period to take a left (in seconds) - 1s by default + */ + void left(int speed = 100, float time = 1); + + + /** 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. - 100 by default + * @param time The time period to take a right (in seconds) - 1s by default + */ + void right(int speed = 100, float time = 1); + + + /** Turns left by switching the right servo off and running the left servo. + * @param speed The speed, from 0 to 100 at which to spin the left servomotor. - 100 by default + * @param time The time period for the left servo to run (in seconds) - 1s by default + */ + void turn_left(int speed = 100, float time = 1); + + + /** Turns right by switching the left servo off and running the right servo. + * @param speed The speed, from 0 to 100 at which to spin the right servomotor. - 100 by default + * @param time The time period for the right servo to run (in seconds) - 1s by default + */ + void turn_right(int speed = 100, float time = 1); + + + /** 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 right 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. + */ + void stopAll(); // Different from disable. + + + /** Returns an integer corresponding to the state of the left whisker. + * @return 1 if the Left whisker is in contact, 0 otherwise. + */ + int leftWhiskerContact(); + + + /** Returns an integer corresponding to the state of the right whisker. + * @return 1 if the Right whisker is in contact, 0 otherwise. + */ + int rightWhiskerContact(); + + + /** Returns an integer correspondingly if there is a black color is detected. + * Uses the QTR-1RC Light sensor + * @param lightSensor the QTR-1RC signal pin + * @return 1 if Black color is detected, 0 otherwise. + */ + int lightSensor(PinName lightSensor); + + + /** Returns an integer correspondingly if an object is detected within 10cm. + * Uses the Sharp GP2Y0D810Z0F Digital Distance Sensor - 10cm + * @param bumpSensor the pin the bump sensor is connected to. + * @return 1 if something is detected, 0 otherwise. + */ + int bumpSensor(PinName bumpSensor); + + + /** Returns the distance of the nearest object. + * Uses the HC-SR04 Distance Sensor. + * @param trigPin the pin to which the trig pin is connected + * @param echoPin the pin to which the echo pin is connected + * @return the distance in cm. + */ + double distanceSensor(PinName trigPin, PinName echoPin); + + + /** Optional implementation for future use + * Turns on the corresponding LED to indicate if the whisker came into contact. + * @param left_LED the pin to which the left LED is connected to + * @param right_LED the pin to which the right LED is connected to + * + * void whiskersLED(PinName left_LED, PinName right_LED); + */ + + + private: + + PwmOut leftServo1; + PwmOut rightServo1; + + int leftPosition; // used for the left servo motor + int rightPosition; // used for the right servo motor + + DigitalOut leftServo; // the left servo motor + DigitalOut rightServo; // the right servo motor + + DigitalIn leftWhisker; // for the left Whisker + DigitalIn rightWhisker; // for the right Whisker + + Ticker leftPulse; // for the left servo motor + Ticker rightPulse; // for the right servo motor + + Timeout leftPulseStop; // for the left servo motor (Kill switch) + Timeout rightPulseStop; // for the right servo motor (Kill switch) + +}; \ No newline at end of file
diff -r 000000000000 -r 0fd8d387c5a0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Aug 20 21:50:32 2015 +0000 @@ -0,0 +1,18 @@ +#include "mbed.h" +#include "ParallaxRobotShield.h" + +int main( ) +{ + ParallaxRobotShield Parallax(D10, D11, PB_4, PA_8); // Corresponding pins are initialized + Parallax.enable_left_motor(); + Parallax.enable_right_motor(); + + DigitalOut led(PA_10); // An LED for testing the left whisker + while(1) + { + if(Parallax.lightSensor(PB_10) == 1) + Parallax.forward(100, 1); + Parallax.right(100, 1); + } + return 1; +} \ No newline at end of file
diff -r 000000000000 -r 0fd8d387c5a0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Aug 20 21:50:32 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file