Class Bertl added

Dependencies:   HCSR

Dependents:   BertlDrive_V2 BertlDrive_V2

Committer:
bulmecisco
Date:
Mon Apr 13 15:28:10 2015 +0000
Revision:
7:e7f74f072564
Parent:
5:6b667e2cb800
Child:
12:cedc088eaf05
int ReturnButtonPressed() added which returns the int value of button pressed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bulmecisco 0:66e9a0afcbd6 1 /***********************************
bulmecisco 7:e7f74f072564 2 name: ur_Bertl.h Version: 2.1
bulmecisco 0:66e9a0afcbd6 3 author: PE HTL BULME
bulmecisco 0:66e9a0afcbd6 4 email: pe@bulme.at
bulmecisco 4:76acfddc26fb 5 WIKI: https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
bulmecisco 0:66e9a0afcbd6 6 description:
bulmecisco 4:76acfddc26fb 7 Definition portion of the class ur_Bertl The Robot
bulmecisco 4:76acfddc26fb 8 boolean commands added for if/else, while, ...
bulmecisco 7:e7f74f072564 9 int ReturnButtonPressed() added which returns the int value of button pressed
bulmecisco 0:66e9a0afcbd6 10 ***********************************/
bulmecisco 0:66e9a0afcbd6 11 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 12
bulmecisco 0:66e9a0afcbd6 13 #ifndef UR_BERTL_H
bulmecisco 0:66e9a0afcbd6 14 #define UR_BERTL_H
bulmecisco 0:66e9a0afcbd6 15
bulmecisco 0:66e9a0afcbd6 16 #define LEFTSENSOR P1_12
bulmecisco 0:66e9a0afcbd6 17 #define RIGHTSENSOR P1_13
bulmecisco 0:66e9a0afcbd6 18 /********************************************//**
bulmecisco 4:76acfddc26fb 19 name: ur_Bertl.h \n
bulmecisco 4:76acfddc26fb 20 version: 2.0 \n
bulmecisco 4:76acfddc26fb 21 author:PE HTL BULME \n
bulmecisco 4:76acfddc26fb 22 email: pe@bulme.at \n
bulmecisco 4:76acfddc26fb 23 WIKI: https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/wiki/Homepage \n
bulmecisco 4:76acfddc26fb 24 description:
bulmecisco 0:66e9a0afcbd6 25 Definition and documentation portion of the class ur_Bertl The Robot.
bulmecisco 0:66e9a0afcbd6 26
bulmecisco 0:66e9a0afcbd6 27 ***********************************************/
bulmecisco 0:66e9a0afcbd6 28 /**
bulmecisco 0:66e9a0afcbd6 29 @code
bulmecisco 0:66e9a0afcbd6 30 //
bulmecisco 0:66e9a0afcbd6 31 @endcode
bulmecisco 0:66e9a0afcbd6 32
bulmecisco 0:66e9a0afcbd6 33 Example motor sensor test:
bulmecisco 0:66e9a0afcbd6 34 @code
bulmecisco 0:66e9a0afcbd6 35 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 36 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 37 #include "const.h"
bulmecisco 0:66e9a0afcbd6 38
bulmecisco 0:66e9a0afcbd6 39 int main()
bulmecisco 0:66e9a0afcbd6 40 {
bulmecisco 0:66e9a0afcbd6 41 ur_Bertl karel(LEFTSENSOR); // RIGHTSENSOR
bulmecisco 0:66e9a0afcbd6 42
bulmecisco 0:66e9a0afcbd6 43 while(true) {
bulmecisco 0:66e9a0afcbd6 44 karel.NibbleLeds(karel.Read());
bulmecisco 0:66e9a0afcbd6 45 }
bulmecisco 0:66e9a0afcbd6 46 }
bulmecisco 0:66e9a0afcbd6 47 @endcode
bulmecisco 0:66e9a0afcbd6 48
bulmecisco 0:66e9a0afcbd6 49 Example moving the robot around:
bulmecisco 0:66e9a0afcbd6 50 @code
bulmecisco 0:66e9a0afcbd6 51 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 52 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 53 #include "const.h"
bulmecisco 0:66e9a0afcbd6 54
bulmecisco 0:66e9a0afcbd6 55 int main()
bulmecisco 0:66e9a0afcbd6 56 {
bulmecisco 0:66e9a0afcbd6 57 ur_Bertl karel;
bulmecisco 0:66e9a0afcbd6 58
bulmecisco 0:66e9a0afcbd6 59 while(karel.WaitUntilButtonPressed()){}
bulmecisco 0:66e9a0afcbd6 60 //karel.Move();
bulmecisco 0:66e9a0afcbd6 61 karel.TurnLeft();
bulmecisco 0:66e9a0afcbd6 62 karel.ShutOff();
bulmecisco 0:66e9a0afcbd6 63 }
bulmecisco 0:66e9a0afcbd6 64 @endcode
bulmecisco 0:66e9a0afcbd6 65
bulmecisco 0:66e9a0afcbd6 66 Example LEDs:
bulmecisco 0:66e9a0afcbd6 67 @code
bulmecisco 0:66e9a0afcbd6 68 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 69 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 70 #include "const.h"
bulmecisco 0:66e9a0afcbd6 71
bulmecisco 0:66e9a0afcbd6 72 int main()
bulmecisco 0:66e9a0afcbd6 73 {
bulmecisco 0:66e9a0afcbd6 74 ur_Bertl karel;
bulmecisco 0:66e9a0afcbd6 75
bulmecisco 0:66e9a0afcbd6 76 while(karel.WaitUntilButtonPressed()){}
bulmecisco 0:66e9a0afcbd6 77
bulmecisco 0:66e9a0afcbd6 78 karel.TurnLedOn(LED_FL1 | LED_FR1); // see const.h
bulmecisco 0:66e9a0afcbd6 79 wait(1);
bulmecisco 0:66e9a0afcbd6 80 karel.TurnLedOn(0xFF); // or use hex
bulmecisco 0:66e9a0afcbd6 81 wait(1);
bulmecisco 0:66e9a0afcbd6 82 karel.RGBLed(1,0,0); // red
bulmecisco 0:66e9a0afcbd6 83 wait(1);
bulmecisco 0:66e9a0afcbd6 84 karel.RGBLed(0,1,0); // green
bulmecisco 0:66e9a0afcbd6 85 wait(1);
bulmecisco 0:66e9a0afcbd6 86 karel.RGBLed(0,0,1); // blue
bulmecisco 0:66e9a0afcbd6 87 karel.BlueLedsON();
bulmecisco 0:66e9a0afcbd6 88 karel.NibbleLeds(karel.Read());
bulmecisco 0:66e9a0afcbd6 89 wait(1);
bulmecisco 0:66e9a0afcbd6 90 karel.BlueLedsOFF();
bulmecisco 0:66e9a0afcbd6 91 karel.TurnLedOff(0xFF);
bulmecisco 0:66e9a0afcbd6 92 karel.ShutOff();
bulmecisco 0:66e9a0afcbd6 93 }
bulmecisco 0:66e9a0afcbd6 94 @endcode
bulmecisco 4:76acfddc26fb 95 Example IF/ELSE Commands (update ur_Bertl 2.0 from https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
bulmecisco 4:76acfddc26fb 96 @code
bulmecisco 4:76acfddc26fb 97 /* Sorry, but there are Javascript problems with this code
bulmecisco 4:76acfddc26fb 98
bulmecisco 4:76acfddc26fb 99 int main()
bulmecisco 4:76acfddc26fb 100 {
bulmecisco 4:76acfddc26fb 101 ur_Bertl karel;
bulmecisco 4:76acfddc26fb 102
bulmecisco 4:76acfddc26fb 103 while( karel.WaitUntilButtonPressed() ) {}
bulmecisco 4:76acfddc26fb 104 if( karel.NextToABeeper()) {
bulmecisco 4:76acfddc26fb 105 karel.PickBeeper();
bulmecisco 4:76acfddc26fb 106 karel.NibbleLeds(karel.AnyBeeperInBag()); //show number of beepers in bag on 4 yellow Leds
bulmecisco 4:76acfddc26fb 107 }
bulmecisco 4:76acfddc26fb 108 wait(1);
bulmecisco 4:76acfddc26fb 109 if( karel.AnyBeeperInBag() ) {
bulmecisco 4:76acfddc26fb 110 karel.PutBeeper();
bulmecisco 4:76acfddc26fb 111 karel.NibbleLeds(karel.AnyBeeperInBag());
bulmecisco 4:76acfddc26fb 112 }
bulmecisco 4:76acfddc26fb 113 wait(1);
bulmecisco 4:76acfddc26fb 114 if( karel.FrontIsClear() )
bulmecisco 4:76acfddc26fb 115 karel.Move();
bulmecisco 4:76acfddc26fb 116 else
bulmecisco 4:76acfddc26fb 117 karel.TurnLeft();
bulmecisco 4:76acfddc26fb 118
bulmecisco 4:76acfddc26fb 119 karel.ShutOff();
bulmecisco 4:76acfddc26fb 120 }
bulmecisco 4:76acfddc26fb 121
bulmecisco 4:76acfddc26fb 122
bulmecisco 4:76acfddc26fb 123 @endcode
bulmecisco 0:66e9a0afcbd6 124 */
bulmecisco 0:66e9a0afcbd6 125 class ur_Bertl
bulmecisco 0:66e9a0afcbd6 126 {
bulmecisco 0:66e9a0afcbd6 127 protected:
bulmecisco 4:76acfddc26fb 128 int beepersInBag; /**< how many beepers does the robot have in his bag;\n you can show it with: karel.NibbleLeds(karel.AnyBeeperInBag())*/
bulmecisco 0:66e9a0afcbd6 129 char cmd[3]; /**< I2C command */
bulmecisco 0:66e9a0afcbd6 130 int16_t btns; /**< which button is pressed */
bulmecisco 0:66e9a0afcbd6 131 InterruptIn _interrupt; /**< interrupted used*/
bulmecisco 0:66e9a0afcbd6 132 volatile int _count; /**< values of motor sensor*/
bulmecisco 0:66e9a0afcbd6 133
bulmecisco 0:66e9a0afcbd6 134 /**
bulmecisco 0:66e9a0afcbd6 135 protected methodes for internal purposes only
bulmecisco 0:66e9a0afcbd6 136 */
bulmecisco 0:66e9a0afcbd6 137 void increment(); /**< ISR to increment sensor values of motor */
bulmecisco 0:66e9a0afcbd6 138 bool backIsClear(); /**< don't now for what */
bulmecisco 0:66e9a0afcbd6 139 bool frontButtonPressed();/**< TRUE if a a button on the front of Robot is pressed else FALSE */
bulmecisco 4:76acfddc26fb 140 int bottomIsBlack(); /**< check line sensor; returns BCD value */
bulmecisco 0:66e9a0afcbd6 141 void error(); /**< Error: stops the robot and all LEDs are blinking*/
bulmecisco 0:66e9a0afcbd6 142
bulmecisco 0:66e9a0afcbd6 143 public:
bulmecisco 0:66e9a0afcbd6 144 ur_Bertl(); /**< default constructor; you have to define constants in config.h such as SPEED, DISTANCE or ANGLE*/
bulmecisco 0:66e9a0afcbd6 145 ur_Bertl(PinName pin); /**< parameterized constructor; on what pin should the interrupt work; SPEED, DISTANCE or ANGLE have to bee defined in config.h */
bulmecisco 0:66e9a0afcbd6 146
bulmecisco 0:66e9a0afcbd6 147 void Move(); /**< Robot moves one turn as much as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 0:66e9a0afcbd6 148 void TurnLeft(); /**< Robot turns left as much as the constant ANGLE*/
bulmecisco 0:66e9a0afcbd6 149 void PutBeeper(); /**< if Robot has any Beepers in his bag he can put one or more Beeper; if not --> Error(()*/
bulmecisco 4:76acfddc26fb 150 void PickBeeper(); /**< if Robot stands on a black item he can pick one or more Beeper (max. 15 --> Error()); if not --> Error()*/
bulmecisco 4:76acfddc26fb 151 void ShutOff(); /**< turnes the robot off */
bulmecisco 4:76acfddc26fb 152 void MoveBackwards(); /**< Robot moves as much back as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 4:76acfddc26fb 153 bool WaitUntilButtonPressed(); /**< wait until any button is pressed at the robot */
bulmecisco 1:fafbac0ba96d 154 bool FrontIsClear(); /**< returns a boolean value true if front is free; if not false */
bulmecisco 4:76acfddc26fb 155 bool NextToABeeper(); /**< returns a boolean value true if the robot is on a black place or line; if not --> false */
bulmecisco 7:e7f74f072564 156 bool IsButtonPressed(const int btn); /**< returns true if Button btn is pressed, else false */
bulmecisco 7:e7f74f072564 157 int ReturnButtonPressed(); /**< returns the int value of button pressed */
bulmecisco 4:76acfddc26fb 158 int AnyBeeperInBag(); /**< returns an int value (if > 0 equal true) how many beepers in bag; if zero --> false */
bulmecisco 4:76acfddc26fb 159 void NibbleLeds(int value); /**< methode for the 4 (half byte) yellow LEDs at the back left side; ie. you can show how many beeper a robot has in his bag with: karel.NibbleLeds(karel.AnyBeeperInBag())*/
bulmecisco 0:66e9a0afcbd6 160 void TurnLedOn(int16_t led);/**< turns the specified one or more LEDs ON; description and name in const.h, such as LED_FL1 = 0x01; front LED white */
bulmecisco 0:66e9a0afcbd6 161 void TurnLedOff(int16_t led);/**< turns the specified one or more LEDs OFF; description and name in const.h, such as LED_FL1 = 0x01; front LED white */
bulmecisco 0:66e9a0afcbd6 162 void RGBLed(bool red, bool green, bool blue); /**<RGB Led with red, green and blue component of the Color */
bulmecisco 0:66e9a0afcbd6 163 void BlueLedsOFF(); /**< OFF all blue LEDs which are on the same Port 1_28 */
bulmecisco 0:66e9a0afcbd6 164 void BlueLedsON(); /**< ON all blue LEDs which are on the same Port 1_28 */
bulmecisco 0:66e9a0afcbd6 165 int Read();
bulmecisco 0:66e9a0afcbd6 166 };
bulmecisco 0:66e9a0afcbd6 167 #endif