our little library for controlling a robot with two wheels and an LCD
Robo.h@0:bb5c5d6800a3, 2013-10-27 (annotated)
- Committer:
- narendraj9
- Date:
- Sun Oct 27 09:16:19 2013 +0000
- Revision:
- 0:bb5c5d6800a3
our main robot library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narendraj9 | 0:bb5c5d6800a3 | 1 | #ifndef __ROBO_H |
narendraj9 | 0:bb5c5d6800a3 | 2 | #define __ROBO_H |
narendraj9 | 0:bb5c5d6800a3 | 3 | |
narendraj9 | 0:bb5c5d6800a3 | 4 | #include "Motor.h" |
narendraj9 | 0:bb5c5d6800a3 | 5 | #include "mbed.h" |
narendraj9 | 0:bb5c5d6800a3 | 6 | #include "TextLCD.h" |
narendraj9 | 0:bb5c5d6800a3 | 7 | |
narendraj9 | 0:bb5c5d6800a3 | 8 | // intial lives each robot has |
narendraj9 | 0:bb5c5d6800a3 | 9 | const int max_lives = 5; |
narendraj9 | 0:bb5c5d6800a3 | 10 | |
narendraj9 | 0:bb5c5d6800a3 | 11 | /* for storing the motors' state, e.g. the robot is moving forward |
narendraj9 | 0:bb5c5d6800a3 | 12 | * and it receives a request to go left. After it has turned left |
narendraj9 | 0:bb5c5d6800a3 | 13 | * it must keep moving forward. So we use this structure to backup and restore |
narendraj9 | 0:bb5c5d6800a3 | 14 | * motors' state. |
narendraj9 | 0:bb5c5d6800a3 | 15 | */ |
narendraj9 | 0:bb5c5d6800a3 | 16 | struct rState { |
narendraj9 | 0:bb5c5d6800a3 | 17 | mState lm; // pin states of the left motor |
narendraj9 | 0:bb5c5d6800a3 | 18 | mState rm; // pin states of the right motor |
narendraj9 | 0:bb5c5d6800a3 | 19 | }; |
narendraj9 | 0:bb5c5d6800a3 | 20 | |
narendraj9 | 0:bb5c5d6800a3 | 21 | /* main Robo Class definition |
narendraj9 | 0:bb5c5d6800a3 | 22 | */ |
narendraj9 | 0:bb5c5d6800a3 | 23 | class Robo { |
narendraj9 | 0:bb5c5d6800a3 | 24 | int lives; |
narendraj9 | 0:bb5c5d6800a3 | 25 | Motor lmotor; |
narendraj9 | 0:bb5c5d6800a3 | 26 | Motor rmotor; |
narendraj9 | 0:bb5c5d6800a3 | 27 | TextLCD lcd; |
narendraj9 | 0:bb5c5d6800a3 | 28 | public : |
narendraj9 | 0:bb5c5d6800a3 | 29 | Robo(PinName l_en, PinName l_fwd, PinName l_rev, // left motor's enable, forward, and reverse |
narendraj9 | 0:bb5c5d6800a3 | 30 | PinName r_en, PinName r_fwd, PinName r_rev, // right motor's enable, forward and reverse |
narendraj9 | 0:bb5c5d6800a3 | 31 | PinName rs, PinName en, PinName d4, PinName d5, // pins for LCD rs, en , d4-d7 |
narendraj9 | 0:bb5c5d6800a3 | 32 | PinName d6, PinName d7); |
narendraj9 | 0:bb5c5d6800a3 | 33 | void init(); // for initialization of the robot |
narendraj9 | 0:bb5c5d6800a3 | 34 | void printlcd(char *); // print a message on the robot's lcd |
narendraj9 | 0:bb5c5d6800a3 | 35 | void updateLivesLCD(); // update the lives count |
narendraj9 | 0:bb5c5d6800a3 | 36 | void goLeft(); |
narendraj9 | 0:bb5c5d6800a3 | 37 | void moveRight(); |
narendraj9 | 0:bb5c5d6800a3 | 38 | void stop(); |
narendraj9 | 0:bb5c5d6800a3 | 39 | void goAhead(); |
narendraj9 | 0:bb5c5d6800a3 | 40 | void moveBack(); |
narendraj9 | 0:bb5c5d6800a3 | 41 | int getLives(); // get the number of lives left |
narendraj9 | 0:bb5c5d6800a3 | 42 | void decLives(); // decrease the number of lives |
narendraj9 | 0:bb5c5d6800a3 | 43 | rState getState(); // get the state of the robot |
narendraj9 | 0:bb5c5d6800a3 | 44 | void setState(rState); // set the state of the robot |
narendraj9 | 0:bb5c5d6800a3 | 45 | |
narendraj9 | 0:bb5c5d6800a3 | 46 | }; |
narendraj9 | 0:bb5c5d6800a3 | 47 | |
narendraj9 | 0:bb5c5d6800a3 | 48 | |
narendraj9 | 0:bb5c5d6800a3 | 49 | |
narendraj9 | 0:bb5c5d6800a3 | 50 | #endif |