our little library for controlling a robot with two wheels and an LCD
Revision 0:bb5c5d6800a3, committed 2013-10-27
- Comitter:
- narendraj9
- Date:
- Sun Oct 27 09:16:19 2013 +0000
- Child:
- 1:c31299ed92f1
- Commit message:
- our main robot library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Robo.cpp Sun Oct 27 09:16:19 2013 +0000 @@ -0,0 +1,145 @@ +#include "Robo.h" +extern DigitalOut buzzer; + +/* implementation of the Robo contructor */ +Robo::Robo(PinName l_en, PinName l_fwd, PinName l_rev, + PinName r_en, PinName r_fwd, PinName r_rev, + PinName rs, PinName en, PinName d4, PinName d5, + PinName d6, PinName d7): + lmotor(l_en, l_fwd, l_rev), rmotor(r_en, r_fwd, r_rev), + lcd(rs, en, d4, d5, d6, d7) +{ + lives = max_lives; + init(); +} + +/* I changed the cls function in the textLCD library + * to make it clear only the first row + */ +void Robo::printlcd(char *msg) // printf msg to the first row of LCD +{ + lcd.cls(); + lcd.printf(msg); +} +void Robo::init() // print the initial msg on the LCD +{ + buzzer = 1; + lcd.cls_all(); + updateLivesLCD(); + for (int i = 0; i < 2; i++) { + printlcd("Roger Here!"); + wait(1); + buzzer = 0; + printlcd(" "); + wait(1); + + } + printlcd("Roger Here!"); + +} + +/* I believe the following functions are pretty straight forward to + * understand + */ + +void Robo::goLeft() +{ + rState temp = this->getState(); // store the current state + lcd.cls(); + lcd.printf("Going Left! "); + lmotor.direct(0); // stop the left motor + rmotor.direct(1); // start the right motor + wait(1); + this->setState(temp); // restore original state + +} + +void Robo::moveRight() +{ + lcd.cls(); + rState temp = this->getState(); + lcd.printf("Turning Right! "); + rmotor.direct(0); + lmotor.direct(1); + wait(1); + this->setState(temp); +} + +void Robo::stop() +{ + lcd.cls(); + rmotor.direct(0); // stop motor left + lmotor.direct(0); // stop motor right + lcd.printf("Stopped! "); +} + +void Robo::goAhead() +{ + lcd.cls(); + lcd.locate(0, 0); + rmotor.direct(1); + lmotor.direct(1); + lcd.printf("Moving Forward!"); +} + +void Robo::moveBack() +{ + lcd.cls(); + rmotor.direct(-1); + lmotor.direct(-1); + lcd.printf("Going Back "); +} + +int Robo::getLives() +{ + return lives; +} + +// update LCD with the current value of the variable lives +void Robo::updateLivesLCD() { + lcd.locate(0, 1); + lcd.printf("lives : %d", lives); + lcd.locate(0, 0); +} + +void Robo::decLives() +{ + lives--; + printlcd("oops!! "); + updateLivesLCD(); + if (lives == 0) { + stop(); + int i = 0; + /* if all lives have been exhausted say goodbye to the world + * and rest in a peaceful forever loop + */ + while (++i) { + printlcd("GoodBye!! "); + wait(0.5); + printlcd(" "); + wait(0.5); + if (i < 4) + buzzer = 1; + wait(1); + buzzer = 0; + } + } + buzzer = 1; + wait(1); + buzzer = 0; + wait(2); +} + +rState Robo::getState() +{ + mState lm = lmotor.getState(); + mState rm = rmotor.getState(); + rState temp = {lm, rm }; // return a struct var containing the + return temp; +} + +void Robo::setState(rState roboState) +{ + lmotor.setState(roboState.lm); + rmotor.setState(roboState.rm); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Robo.h Sun Oct 27 09:16:19 2013 +0000 @@ -0,0 +1,50 @@ +#ifndef __ROBO_H +#define __ROBO_H + +#include "Motor.h" +#include "mbed.h" +#include "TextLCD.h" + +// intial lives each robot has +const int max_lives = 5; + +/* for storing the motors' state, e.g. the robot is moving forward + * and it receives a request to go left. After it has turned left + * it must keep moving forward. So we use this structure to backup and restore + * motors' state. + */ +struct rState { + mState lm; // pin states of the left motor + mState rm; // pin states of the right motor +}; + +/* main Robo Class definition + */ +class Robo { + int lives; + Motor lmotor; + Motor rmotor; + TextLCD lcd; + public : + Robo(PinName l_en, PinName l_fwd, PinName l_rev, // left motor's enable, forward, and reverse + PinName r_en, PinName r_fwd, PinName r_rev, // right motor's enable, forward and reverse + PinName rs, PinName en, PinName d4, PinName d5, // pins for LCD rs, en , d4-d7 + PinName d6, PinName d7); + void init(); // for initialization of the robot + void printlcd(char *); // print a message on the robot's lcd + void updateLivesLCD(); // update the lives count + void goLeft(); + void moveRight(); + void stop(); + void goAhead(); + void moveBack(); + int getLives(); // get the number of lives left + void decLives(); // decrease the number of lives + rState getState(); // get the state of the robot + void setState(rState); // set the state of the robot + +}; + + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Sun Oct 27 09:16:19 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#228a730399f3