Narendra Joshi / Robo

Dependencies:   TextLCD

Dependents:   robots

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Robo.cpp Source File

Robo.cpp

00001 #include "Robo.h"
00002 extern DigitalOut buzzer;
00003 
00004 /* implementation of the Robo contructor */
00005 Robo::Robo(PinName l_en, PinName l_fwd, PinName l_rev,
00006          PinName r_en, PinName r_fwd, PinName r_rev,
00007          PinName rs, PinName en, PinName d4, PinName d5, 
00008          PinName d6, PinName d7):
00009          lmotor(l_en, l_fwd, l_rev), rmotor(r_en, r_fwd, r_rev),
00010          lcd(rs, en, d4, d5, d6, d7)
00011 {
00012     lives = max_lives;
00013     init();
00014 }
00015 
00016 /* I changed the cls function in the textLCD library
00017  * to make it clear only the first row
00018  */
00019 void Robo::printlcd(char *msg) // printf msg to the first row of LCD
00020 {
00021     lcd.cls();  
00022     lcd.printf(msg);
00023 }
00024 void Robo::init() // print the initial msg on the LCD
00025 {
00026     buzzer = 1;
00027     lcd.cls_all();
00028     updateLivesLCD();
00029     for (int i = 0; i < 2; i++) {
00030         printlcd("Roger Here!");
00031         wait(1);
00032         buzzer = 0;
00033         printlcd("            ");
00034         wait(1);
00035         
00036     }
00037     printlcd("Roger Here!");
00038     
00039 }
00040 
00041 /* I believe the following functions are pretty straight forward to 
00042  * understand
00043  */
00044 
00045 void Robo::goLeft() 
00046 {
00047     rState temp = this->getState(); // store the current state
00048     lcd.cls();
00049     lcd.printf("Going Left!    ");
00050     lmotor.direct(0);   // stop the left motor
00051     rmotor.direct(1);   // start the right motor
00052     wait(1);
00053     this->setState(temp);   // restore original state
00054     
00055 }
00056 
00057 void Robo::moveRight() 
00058 {
00059     lcd.cls();
00060     rState temp = this->getState();
00061     lcd.printf("Turning Right! ");
00062     rmotor.direct(0);
00063     lmotor.direct(1);
00064     wait(1);
00065     this->setState(temp);
00066 }
00067 
00068 void Robo::stop() 
00069 {
00070     lcd.cls();
00071     rmotor.direct(0); // stop motor left
00072     lmotor.direct(0); // stop motor right
00073     lcd.printf("Stopped!       ");
00074 }
00075 
00076 void Robo::goAhead() 
00077 {
00078     lcd.cls();
00079     lcd.locate(0, 0);
00080     rmotor.direct(1);
00081     lmotor.direct(1);
00082     lcd.printf("Moving Forward!");
00083 }
00084 
00085 void Robo::moveBack()
00086 {
00087     lcd.cls();
00088     rmotor.direct(-1);
00089     lmotor.direct(-1);
00090     lcd.printf("Going Back     ");
00091 }
00092 
00093 int Robo::getLives()
00094 {
00095     return lives;
00096 }
00097 
00098 // update LCD with the current value of the variable lives
00099 void Robo::updateLivesLCD() { 
00100     lcd.locate(0, 1);
00101     lcd.printf("lives : %d", lives);
00102     lcd.locate(0, 0);
00103 }
00104 
00105 void Robo::decLives()
00106 {
00107     lives--;
00108     printlcd("oops!!         ");
00109     updateLivesLCD();
00110     if (lives == 0) {
00111         stop();
00112         int i = 0;
00113     /* if all lives have been exhausted say goodbye to the world 
00114      * and rest in a peaceful forever loop
00115      */
00116         while (++i) {
00117             printlcd("GoodBye!!       ");
00118             wait(0.5);
00119             printlcd("           ");
00120             wait(0.5);
00121             if (i < 4)
00122                 buzzer = 1;
00123             wait(1);
00124             buzzer = 0;
00125         }
00126     }
00127     buzzer = 1;
00128     wait(1);
00129     buzzer = 0;
00130     wait(2);
00131 }
00132 
00133 rState Robo::getState()
00134 {
00135     mState lm = lmotor.getState();
00136     mState rm = rmotor.getState();
00137     rState temp = {lm, rm }; // return a struct var containing the
00138     return temp;
00139 }
00140 
00141 void Robo::setState(rState roboState) 
00142 {
00143     lmotor.setState(roboState.lm);
00144     rmotor.setState(roboState.rm);
00145 }