our little library for controlling a robot with two wheels and an LCD

Dependencies:   TextLCD

Dependents:   robots

Committer:
narendraj9
Date:
Sun Oct 27 09:17:38 2013 +0000
Revision:
1:c31299ed92f1
Parent:
0:bb5c5d6800a3
publish commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narendraj9 0:bb5c5d6800a3 1 #include "Robo.h"
narendraj9 0:bb5c5d6800a3 2 extern DigitalOut buzzer;
narendraj9 0:bb5c5d6800a3 3
narendraj9 0:bb5c5d6800a3 4 /* implementation of the Robo contructor */
narendraj9 0:bb5c5d6800a3 5 Robo::Robo(PinName l_en, PinName l_fwd, PinName l_rev,
narendraj9 0:bb5c5d6800a3 6 PinName r_en, PinName r_fwd, PinName r_rev,
narendraj9 0:bb5c5d6800a3 7 PinName rs, PinName en, PinName d4, PinName d5,
narendraj9 0:bb5c5d6800a3 8 PinName d6, PinName d7):
narendraj9 0:bb5c5d6800a3 9 lmotor(l_en, l_fwd, l_rev), rmotor(r_en, r_fwd, r_rev),
narendraj9 0:bb5c5d6800a3 10 lcd(rs, en, d4, d5, d6, d7)
narendraj9 0:bb5c5d6800a3 11 {
narendraj9 0:bb5c5d6800a3 12 lives = max_lives;
narendraj9 0:bb5c5d6800a3 13 init();
narendraj9 0:bb5c5d6800a3 14 }
narendraj9 0:bb5c5d6800a3 15
narendraj9 0:bb5c5d6800a3 16 /* I changed the cls function in the textLCD library
narendraj9 0:bb5c5d6800a3 17 * to make it clear only the first row
narendraj9 0:bb5c5d6800a3 18 */
narendraj9 0:bb5c5d6800a3 19 void Robo::printlcd(char *msg) // printf msg to the first row of LCD
narendraj9 0:bb5c5d6800a3 20 {
narendraj9 0:bb5c5d6800a3 21 lcd.cls();
narendraj9 0:bb5c5d6800a3 22 lcd.printf(msg);
narendraj9 0:bb5c5d6800a3 23 }
narendraj9 0:bb5c5d6800a3 24 void Robo::init() // print the initial msg on the LCD
narendraj9 0:bb5c5d6800a3 25 {
narendraj9 0:bb5c5d6800a3 26 buzzer = 1;
narendraj9 0:bb5c5d6800a3 27 lcd.cls_all();
narendraj9 0:bb5c5d6800a3 28 updateLivesLCD();
narendraj9 0:bb5c5d6800a3 29 for (int i = 0; i < 2; i++) {
narendraj9 0:bb5c5d6800a3 30 printlcd("Roger Here!");
narendraj9 0:bb5c5d6800a3 31 wait(1);
narendraj9 0:bb5c5d6800a3 32 buzzer = 0;
narendraj9 0:bb5c5d6800a3 33 printlcd(" ");
narendraj9 0:bb5c5d6800a3 34 wait(1);
narendraj9 0:bb5c5d6800a3 35
narendraj9 0:bb5c5d6800a3 36 }
narendraj9 0:bb5c5d6800a3 37 printlcd("Roger Here!");
narendraj9 0:bb5c5d6800a3 38
narendraj9 0:bb5c5d6800a3 39 }
narendraj9 0:bb5c5d6800a3 40
narendraj9 0:bb5c5d6800a3 41 /* I believe the following functions are pretty straight forward to
narendraj9 0:bb5c5d6800a3 42 * understand
narendraj9 0:bb5c5d6800a3 43 */
narendraj9 0:bb5c5d6800a3 44
narendraj9 0:bb5c5d6800a3 45 void Robo::goLeft()
narendraj9 0:bb5c5d6800a3 46 {
narendraj9 0:bb5c5d6800a3 47 rState temp = this->getState(); // store the current state
narendraj9 0:bb5c5d6800a3 48 lcd.cls();
narendraj9 0:bb5c5d6800a3 49 lcd.printf("Going Left! ");
narendraj9 0:bb5c5d6800a3 50 lmotor.direct(0); // stop the left motor
narendraj9 0:bb5c5d6800a3 51 rmotor.direct(1); // start the right motor
narendraj9 0:bb5c5d6800a3 52 wait(1);
narendraj9 0:bb5c5d6800a3 53 this->setState(temp); // restore original state
narendraj9 0:bb5c5d6800a3 54
narendraj9 0:bb5c5d6800a3 55 }
narendraj9 0:bb5c5d6800a3 56
narendraj9 0:bb5c5d6800a3 57 void Robo::moveRight()
narendraj9 0:bb5c5d6800a3 58 {
narendraj9 0:bb5c5d6800a3 59 lcd.cls();
narendraj9 0:bb5c5d6800a3 60 rState temp = this->getState();
narendraj9 0:bb5c5d6800a3 61 lcd.printf("Turning Right! ");
narendraj9 0:bb5c5d6800a3 62 rmotor.direct(0);
narendraj9 0:bb5c5d6800a3 63 lmotor.direct(1);
narendraj9 0:bb5c5d6800a3 64 wait(1);
narendraj9 0:bb5c5d6800a3 65 this->setState(temp);
narendraj9 0:bb5c5d6800a3 66 }
narendraj9 0:bb5c5d6800a3 67
narendraj9 0:bb5c5d6800a3 68 void Robo::stop()
narendraj9 0:bb5c5d6800a3 69 {
narendraj9 0:bb5c5d6800a3 70 lcd.cls();
narendraj9 0:bb5c5d6800a3 71 rmotor.direct(0); // stop motor left
narendraj9 0:bb5c5d6800a3 72 lmotor.direct(0); // stop motor right
narendraj9 0:bb5c5d6800a3 73 lcd.printf("Stopped! ");
narendraj9 0:bb5c5d6800a3 74 }
narendraj9 0:bb5c5d6800a3 75
narendraj9 0:bb5c5d6800a3 76 void Robo::goAhead()
narendraj9 0:bb5c5d6800a3 77 {
narendraj9 0:bb5c5d6800a3 78 lcd.cls();
narendraj9 0:bb5c5d6800a3 79 lcd.locate(0, 0);
narendraj9 0:bb5c5d6800a3 80 rmotor.direct(1);
narendraj9 0:bb5c5d6800a3 81 lmotor.direct(1);
narendraj9 0:bb5c5d6800a3 82 lcd.printf("Moving Forward!");
narendraj9 0:bb5c5d6800a3 83 }
narendraj9 0:bb5c5d6800a3 84
narendraj9 0:bb5c5d6800a3 85 void Robo::moveBack()
narendraj9 0:bb5c5d6800a3 86 {
narendraj9 0:bb5c5d6800a3 87 lcd.cls();
narendraj9 0:bb5c5d6800a3 88 rmotor.direct(-1);
narendraj9 0:bb5c5d6800a3 89 lmotor.direct(-1);
narendraj9 0:bb5c5d6800a3 90 lcd.printf("Going Back ");
narendraj9 0:bb5c5d6800a3 91 }
narendraj9 0:bb5c5d6800a3 92
narendraj9 0:bb5c5d6800a3 93 int Robo::getLives()
narendraj9 0:bb5c5d6800a3 94 {
narendraj9 0:bb5c5d6800a3 95 return lives;
narendraj9 0:bb5c5d6800a3 96 }
narendraj9 0:bb5c5d6800a3 97
narendraj9 0:bb5c5d6800a3 98 // update LCD with the current value of the variable lives
narendraj9 0:bb5c5d6800a3 99 void Robo::updateLivesLCD() {
narendraj9 0:bb5c5d6800a3 100 lcd.locate(0, 1);
narendraj9 0:bb5c5d6800a3 101 lcd.printf("lives : %d", lives);
narendraj9 0:bb5c5d6800a3 102 lcd.locate(0, 0);
narendraj9 0:bb5c5d6800a3 103 }
narendraj9 0:bb5c5d6800a3 104
narendraj9 0:bb5c5d6800a3 105 void Robo::decLives()
narendraj9 0:bb5c5d6800a3 106 {
narendraj9 0:bb5c5d6800a3 107 lives--;
narendraj9 0:bb5c5d6800a3 108 printlcd("oops!! ");
narendraj9 0:bb5c5d6800a3 109 updateLivesLCD();
narendraj9 0:bb5c5d6800a3 110 if (lives == 0) {
narendraj9 0:bb5c5d6800a3 111 stop();
narendraj9 0:bb5c5d6800a3 112 int i = 0;
narendraj9 0:bb5c5d6800a3 113 /* if all lives have been exhausted say goodbye to the world
narendraj9 0:bb5c5d6800a3 114 * and rest in a peaceful forever loop
narendraj9 0:bb5c5d6800a3 115 */
narendraj9 0:bb5c5d6800a3 116 while (++i) {
narendraj9 0:bb5c5d6800a3 117 printlcd("GoodBye!! ");
narendraj9 0:bb5c5d6800a3 118 wait(0.5);
narendraj9 0:bb5c5d6800a3 119 printlcd(" ");
narendraj9 0:bb5c5d6800a3 120 wait(0.5);
narendraj9 0:bb5c5d6800a3 121 if (i < 4)
narendraj9 0:bb5c5d6800a3 122 buzzer = 1;
narendraj9 0:bb5c5d6800a3 123 wait(1);
narendraj9 0:bb5c5d6800a3 124 buzzer = 0;
narendraj9 0:bb5c5d6800a3 125 }
narendraj9 0:bb5c5d6800a3 126 }
narendraj9 0:bb5c5d6800a3 127 buzzer = 1;
narendraj9 0:bb5c5d6800a3 128 wait(1);
narendraj9 0:bb5c5d6800a3 129 buzzer = 0;
narendraj9 0:bb5c5d6800a3 130 wait(2);
narendraj9 0:bb5c5d6800a3 131 }
narendraj9 0:bb5c5d6800a3 132
narendraj9 0:bb5c5d6800a3 133 rState Robo::getState()
narendraj9 0:bb5c5d6800a3 134 {
narendraj9 0:bb5c5d6800a3 135 mState lm = lmotor.getState();
narendraj9 0:bb5c5d6800a3 136 mState rm = rmotor.getState();
narendraj9 0:bb5c5d6800a3 137 rState temp = {lm, rm }; // return a struct var containing the
narendraj9 0:bb5c5d6800a3 138 return temp;
narendraj9 0:bb5c5d6800a3 139 }
narendraj9 0:bb5c5d6800a3 140
narendraj9 0:bb5c5d6800a3 141 void Robo::setState(rState roboState)
narendraj9 0:bb5c5d6800a3 142 {
narendraj9 0:bb5c5d6800a3 143 lmotor.setState(roboState.lm);
narendraj9 0:bb5c5d6800a3 144 rmotor.setState(roboState.rm);
narendraj9 0:bb5c5d6800a3 145 }