Changed Up libary
Fork of Bertl by
ur_Bertl.cpp@1:fafbac0ba96d, 2015-03-19 (annotated)
- Committer:
- bulmecisco
- Date:
- Thu Mar 19 19:12:33 2015 +0000
- Revision:
- 1:fafbac0ba96d
- Parent:
- 0:66e9a0afcbd6
- Child:
- 2:1cd559ff516b
FrontIsClear() with ultrasonic sensor added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bulmecisco | 0:66e9a0afcbd6 | 1 | /*********************************** |
bulmecisco | 0:66e9a0afcbd6 | 2 | name: ur_Bertl.cpp Version: 1.1 |
bulmecisco | 0:66e9a0afcbd6 | 3 | author: PE HTL BULME |
bulmecisco | 0:66e9a0afcbd6 | 4 | email: pe@bulme.at |
bulmecisco | 0:66e9a0afcbd6 | 5 | description: |
bulmecisco | 0:66e9a0afcbd6 | 6 | Implementation portion of class ur_Bertl The Robot |
bulmecisco | 0:66e9a0afcbd6 | 7 | Blue LED and RGB LED test added |
bulmecisco | 0:66e9a0afcbd6 | 8 | |
bulmecisco | 0:66e9a0afcbd6 | 9 | ***********************************/ |
bulmecisco | 0:66e9a0afcbd6 | 10 | #include "mbed.h" |
bulmecisco | 0:66e9a0afcbd6 | 11 | #include "config.h" |
bulmecisco | 0:66e9a0afcbd6 | 12 | #include "ur_Bertl.h" |
bulmecisco | 0:66e9a0afcbd6 | 13 | |
bulmecisco | 0:66e9a0afcbd6 | 14 | // Constructor |
bulmecisco | 0:66e9a0afcbd6 | 15 | ur_Bertl::ur_Bertl() : _interrupt(P1_12) // left sensor P1_13 |
bulmecisco | 0:66e9a0afcbd6 | 16 | { |
bulmecisco | 0:66e9a0afcbd6 | 17 | i2c.frequency(40000); // I2C Frequenz 40kHz |
bulmecisco | 0:66e9a0afcbd6 | 18 | mg1 = mg2 = SPEED; |
bulmecisco | 0:66e9a0afcbd6 | 19 | _interrupt.rise(this, &ur_Bertl::increment); // attach increment function of this counter instance ie. motor sensor |
bulmecisco | 0:66e9a0afcbd6 | 20 | _count = 0; |
bulmecisco | 0:66e9a0afcbd6 | 21 | beepersInBag = 0; |
bulmecisco | 0:66e9a0afcbd6 | 22 | } |
bulmecisco | 0:66e9a0afcbd6 | 23 | |
bulmecisco | 0:66e9a0afcbd6 | 24 | ur_Bertl::ur_Bertl(PinName pin) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter |
bulmecisco | 0:66e9a0afcbd6 | 25 | { |
bulmecisco | 0:66e9a0afcbd6 | 26 | i2c.frequency(40000); // I2C Frequenz 40kHz |
bulmecisco | 0:66e9a0afcbd6 | 27 | mg1 = mg2 = SPEED; |
bulmecisco | 0:66e9a0afcbd6 | 28 | _interrupt.rise(this, &ur_Bertl::increment); // attach increment function of this counter instance ie. motor sensor |
bulmecisco | 0:66e9a0afcbd6 | 29 | _count = 0; |
bulmecisco | 0:66e9a0afcbd6 | 30 | beepersInBag = 0; |
bulmecisco | 0:66e9a0afcbd6 | 31 | } |
bulmecisco | 0:66e9a0afcbd6 | 32 | |
bulmecisco | 0:66e9a0afcbd6 | 33 | |
bulmecisco | 0:66e9a0afcbd6 | 34 | // Pulblic methodes |
bulmecisco | 0:66e9a0afcbd6 | 35 | void ur_Bertl::Move() |
bulmecisco | 0:66e9a0afcbd6 | 36 | { |
bulmecisco | 0:66e9a0afcbd6 | 37 | int count = _count; |
bulmecisco | 0:66e9a0afcbd6 | 38 | MotorR_EN=MotorL_EN=1; // both motor ENABLE |
bulmecisco | 0:66e9a0afcbd6 | 39 | MotorR_FORWARD = MotorL_FORWARD = 1; // both motor forward ON |
bulmecisco | 0:66e9a0afcbd6 | 40 | |
bulmecisco | 0:66e9a0afcbd6 | 41 | while(_count < count+DISTANCE) { |
bulmecisco | 0:66e9a0afcbd6 | 42 | //if(!FrontIsClear()) // more convenient because there are no accidents :-) |
bulmecisco | 0:66e9a0afcbd6 | 43 | // break; |
bulmecisco | 0:66e9a0afcbd6 | 44 | #ifdef FRONTBUTTON |
bulmecisco | 0:66e9a0afcbd6 | 45 | if(frontButtonPressed()) |
bulmecisco | 0:66e9a0afcbd6 | 46 | error(); |
bulmecisco | 0:66e9a0afcbd6 | 47 | #endif |
bulmecisco | 0:66e9a0afcbd6 | 48 | DEBUG_PRINT("count: %d _count: %d", count, _count); |
bulmecisco | 0:66e9a0afcbd6 | 49 | } |
bulmecisco | 0:66e9a0afcbd6 | 50 | MotorR_FORWARD = MotorL_FORWARD = 0; // both motor off |
bulmecisco | 0:66e9a0afcbd6 | 51 | MotorR_EN=MotorL_EN=0; |
bulmecisco | 0:66e9a0afcbd6 | 52 | wait_ms(250); |
bulmecisco | 0:66e9a0afcbd6 | 53 | } |
bulmecisco | 0:66e9a0afcbd6 | 54 | |
bulmecisco | 0:66e9a0afcbd6 | 55 | void ur_Bertl::PutBeeper() |
bulmecisco | 0:66e9a0afcbd6 | 56 | { |
bulmecisco | 0:66e9a0afcbd6 | 57 | // wait_ms(500); |
bulmecisco | 0:66e9a0afcbd6 | 58 | if(beepersInBag > 0) |
bulmecisco | 0:66e9a0afcbd6 | 59 | beepersInBag--; |
bulmecisco | 0:66e9a0afcbd6 | 60 | else |
bulmecisco | 0:66e9a0afcbd6 | 61 | error(); |
bulmecisco | 0:66e9a0afcbd6 | 62 | } |
bulmecisco | 0:66e9a0afcbd6 | 63 | |
bulmecisco | 0:66e9a0afcbd6 | 64 | void ur_Bertl::PickBeeper() |
bulmecisco | 0:66e9a0afcbd6 | 65 | { |
bulmecisco | 0:66e9a0afcbd6 | 66 | // wait_ms(500); |
bulmecisco | 0:66e9a0afcbd6 | 67 | if (linesensor) |
bulmecisco | 0:66e9a0afcbd6 | 68 | beepersInBag++; |
bulmecisco | 0:66e9a0afcbd6 | 69 | else |
bulmecisco | 0:66e9a0afcbd6 | 70 | error(); |
bulmecisco | 0:66e9a0afcbd6 | 71 | } |
bulmecisco | 0:66e9a0afcbd6 | 72 | |
bulmecisco | 0:66e9a0afcbd6 | 73 | void ur_Bertl::TurnLeft() |
bulmecisco | 0:66e9a0afcbd6 | 74 | { |
bulmecisco | 0:66e9a0afcbd6 | 75 | int count = _count; |
bulmecisco | 0:66e9a0afcbd6 | 76 | MotorR_EN=MotorL_EN=1; // motor left and right ENABLE |
bulmecisco | 0:66e9a0afcbd6 | 77 | |
bulmecisco | 0:66e9a0afcbd6 | 78 | MotorR_FORWARD = MotorL_REVERSE = 1; |
bulmecisco | 0:66e9a0afcbd6 | 79 | while(_count < count+ANGLE) { |
bulmecisco | 0:66e9a0afcbd6 | 80 | #ifdef FRONTBUTTON |
bulmecisco | 0:66e9a0afcbd6 | 81 | if(frontButtonPressed()) // get out if to much problems |
bulmecisco | 0:66e9a0afcbd6 | 82 | error(); |
bulmecisco | 0:66e9a0afcbd6 | 83 | #endif |
bulmecisco | 0:66e9a0afcbd6 | 84 | DEBUG_PRINT("count: %d _count: %d", count, _count); |
bulmecisco | 0:66e9a0afcbd6 | 85 | } |
bulmecisco | 0:66e9a0afcbd6 | 86 | MotorR_FORWARD = MotorL_REVERSE = 0; |
bulmecisco | 0:66e9a0afcbd6 | 87 | MotorR_EN=MotorL_EN=0; |
bulmecisco | 0:66e9a0afcbd6 | 88 | wait_ms(250); // only to step the robot |
bulmecisco | 0:66e9a0afcbd6 | 89 | } |
bulmecisco | 0:66e9a0afcbd6 | 90 | |
bulmecisco | 0:66e9a0afcbd6 | 91 | bool ur_Bertl::WaitUntilButtonPressed() |
bulmecisco | 0:66e9a0afcbd6 | 92 | { |
bulmecisco | 0:66e9a0afcbd6 | 93 | char cmd[3]; |
bulmecisco | 0:66e9a0afcbd6 | 94 | int16_t btns; |
bulmecisco | 0:66e9a0afcbd6 | 95 | bool wert; |
bulmecisco | 0:66e9a0afcbd6 | 96 | |
bulmecisco | 0:66e9a0afcbd6 | 97 | RGB_blue=RGB_red=RGB_green=0; |
bulmecisco | 0:66e9a0afcbd6 | 98 | cmd[0] = 0x06; |
bulmecisco | 0:66e9a0afcbd6 | 99 | cmd[1] = 0x00; |
bulmecisco | 0:66e9a0afcbd6 | 100 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 101 | |
bulmecisco | 0:66e9a0afcbd6 | 102 | cmd[0]=0x01; |
bulmecisco | 0:66e9a0afcbd6 | 103 | i2c.write(addr, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 104 | i2c.read(addr|1, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 105 | btns = cmd[0]; |
bulmecisco | 0:66e9a0afcbd6 | 106 | if( btns & (0xFF)) |
bulmecisco | 0:66e9a0afcbd6 | 107 | wert = false; |
bulmecisco | 0:66e9a0afcbd6 | 108 | else |
bulmecisco | 0:66e9a0afcbd6 | 109 | wert = true; |
bulmecisco | 0:66e9a0afcbd6 | 110 | DEBUG_PRINT("\right\nWERT: %d \right\n", wert); |
bulmecisco | 0:66e9a0afcbd6 | 111 | return wert; |
bulmecisco | 0:66e9a0afcbd6 | 112 | } |
bulmecisco | 0:66e9a0afcbd6 | 113 | |
bulmecisco | 0:66e9a0afcbd6 | 114 | void ur_Bertl::ShutOff() |
bulmecisco | 0:66e9a0afcbd6 | 115 | { |
bulmecisco | 0:66e9a0afcbd6 | 116 | MotorR_FORWARD = MotorL_FORWARD = 0; // motor OFF |
bulmecisco | 0:66e9a0afcbd6 | 117 | MotorR_EN=MotorL_EN=0; // motor disable |
bulmecisco | 0:66e9a0afcbd6 | 118 | } |
bulmecisco | 0:66e9a0afcbd6 | 119 | |
bulmecisco | 0:66e9a0afcbd6 | 120 | // LEDs methodes |
bulmecisco | 0:66e9a0afcbd6 | 121 | |
bulmecisco | 0:66e9a0afcbd6 | 122 | void ur_Bertl::BlueLedsON() |
bulmecisco | 0:66e9a0afcbd6 | 123 | { |
bulmecisco | 0:66e9a0afcbd6 | 124 | LED_blue=0; |
bulmecisco | 0:66e9a0afcbd6 | 125 | } |
bulmecisco | 0:66e9a0afcbd6 | 126 | |
bulmecisco | 0:66e9a0afcbd6 | 127 | void ur_Bertl::BlueLedsOFF() |
bulmecisco | 0:66e9a0afcbd6 | 128 | { |
bulmecisco | 0:66e9a0afcbd6 | 129 | LED_blue=1; |
bulmecisco | 0:66e9a0afcbd6 | 130 | } |
bulmecisco | 0:66e9a0afcbd6 | 131 | |
bulmecisco | 0:66e9a0afcbd6 | 132 | void ur_Bertl::RGBLed(bool red, bool green, bool blue) |
bulmecisco | 0:66e9a0afcbd6 | 133 | { |
bulmecisco | 0:66e9a0afcbd6 | 134 | RGB_blue=!blue; |
bulmecisco | 0:66e9a0afcbd6 | 135 | RGB_red=!red; |
bulmecisco | 0:66e9a0afcbd6 | 136 | RGB_green=!green; |
bulmecisco | 0:66e9a0afcbd6 | 137 | } |
bulmecisco | 0:66e9a0afcbd6 | 138 | |
bulmecisco | 0:66e9a0afcbd6 | 139 | void ur_Bertl::TurnLedOn(int16_t led) |
bulmecisco | 0:66e9a0afcbd6 | 140 | { |
bulmecisco | 0:66e9a0afcbd6 | 141 | char cmd[3]; |
bulmecisco | 0:66e9a0afcbd6 | 142 | |
bulmecisco | 1:fafbac0ba96d | 143 | if(led == 0xBB) { |
bulmecisco | 1:fafbac0ba96d | 144 | LED_blue=0; |
bulmecisco | 1:fafbac0ba96d | 145 | return; |
bulmecisco | 1:fafbac0ba96d | 146 | } else if (led == 0xFF) { |
bulmecisco | 1:fafbac0ba96d | 147 | RGBLed(1,1,1); |
bulmecisco | 1:fafbac0ba96d | 148 | LED_blue=0; |
bulmecisco | 1:fafbac0ba96d | 149 | } |
bulmecisco | 0:66e9a0afcbd6 | 150 | cmd[0] = 0x02; |
bulmecisco | 0:66e9a0afcbd6 | 151 | cmd[1] = ~led; |
bulmecisco | 0:66e9a0afcbd6 | 152 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 153 | wait(0.5); |
bulmecisco | 0:66e9a0afcbd6 | 154 | } |
bulmecisco | 0:66e9a0afcbd6 | 155 | |
bulmecisco | 0:66e9a0afcbd6 | 156 | void ur_Bertl::TurnLedOff(int16_t led) |
bulmecisco | 0:66e9a0afcbd6 | 157 | { |
bulmecisco | 0:66e9a0afcbd6 | 158 | char cmd[3]; |
bulmecisco | 1:fafbac0ba96d | 159 | if(led == 0xBB) { |
bulmecisco | 1:fafbac0ba96d | 160 | LED_blue=1; |
bulmecisco | 1:fafbac0ba96d | 161 | return; |
bulmecisco | 1:fafbac0ba96d | 162 | } else if (led == 0xFF) { |
bulmecisco | 1:fafbac0ba96d | 163 | RGBLed(0,0,0); //don't work? |
bulmecisco | 1:fafbac0ba96d | 164 | LED_blue=1; |
bulmecisco | 1:fafbac0ba96d | 165 | } |
bulmecisco | 0:66e9a0afcbd6 | 166 | cmd[0] = 0x02; |
bulmecisco | 0:66e9a0afcbd6 | 167 | cmd[1] = led; |
bulmecisco | 0:66e9a0afcbd6 | 168 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 169 | wait(0.5); |
bulmecisco | 0:66e9a0afcbd6 | 170 | } |
bulmecisco | 0:66e9a0afcbd6 | 171 | |
bulmecisco | 0:66e9a0afcbd6 | 172 | void ur_Bertl::NibbleLeds(int value) |
bulmecisco | 0:66e9a0afcbd6 | 173 | { |
bulmecisco | 0:66e9a0afcbd6 | 174 | NibbleLEDs = value%16; |
bulmecisco | 0:66e9a0afcbd6 | 175 | } |
bulmecisco | 0:66e9a0afcbd6 | 176 | |
bulmecisco | 0:66e9a0afcbd6 | 177 | //-----------------INTERNAL USE ONLY ---------------------------- |
bulmecisco | 0:66e9a0afcbd6 | 178 | void ur_Bertl::error() |
bulmecisco | 0:66e9a0afcbd6 | 179 | { |
bulmecisco | 0:66e9a0afcbd6 | 180 | int wait = 500; |
bulmecisco | 0:66e9a0afcbd6 | 181 | MotorR_FORWARD = MotorL_FORWARD = 0; // both motor off |
bulmecisco | 0:66e9a0afcbd6 | 182 | MotorR_REVERSE = MotorL_REVERSE = 0; // both motor off |
bulmecisco | 0:66e9a0afcbd6 | 183 | MotorR_EN=MotorL_EN=0; |
bulmecisco | 0:66e9a0afcbd6 | 184 | while(1) { |
bulmecisco | 0:66e9a0afcbd6 | 185 | TurnLedOff(0xFF); |
bulmecisco | 0:66e9a0afcbd6 | 186 | LED_D10 = LED_D11 = LED_D12 = LED_D13 = 0; |
bulmecisco | 0:66e9a0afcbd6 | 187 | LED_blue=1; |
bulmecisco | 0:66e9a0afcbd6 | 188 | RGB_blue=RGB_green=RGB_red=1; |
bulmecisco | 0:66e9a0afcbd6 | 189 | wait_ms(wait); |
bulmecisco | 0:66e9a0afcbd6 | 190 | TurnLedOn(0xFF); |
bulmecisco | 0:66e9a0afcbd6 | 191 | LED_D10 = LED_D11 = LED_D12 = LED_D13 = 1; |
bulmecisco | 0:66e9a0afcbd6 | 192 | LED_blue=0; |
bulmecisco | 0:66e9a0afcbd6 | 193 | RGB_blue=RGB_green=1;RGB_red=0; |
bulmecisco | 0:66e9a0afcbd6 | 194 | wait_ms(wait); |
bulmecisco | 0:66e9a0afcbd6 | 195 | } |
bulmecisco | 0:66e9a0afcbd6 | 196 | } |
bulmecisco | 0:66e9a0afcbd6 | 197 | |
bulmecisco | 0:66e9a0afcbd6 | 198 | bool ur_Bertl::frontButtonPressed() |
bulmecisco | 0:66e9a0afcbd6 | 199 | { |
bulmecisco | 0:66e9a0afcbd6 | 200 | char cmd[3]; // array for I2C |
bulmecisco | 0:66e9a0afcbd6 | 201 | int16_t btns; |
bulmecisco | 0:66e9a0afcbd6 | 202 | bool wert; |
bulmecisco | 0:66e9a0afcbd6 | 203 | |
bulmecisco | 0:66e9a0afcbd6 | 204 | cmd[0] = 0x06; |
bulmecisco | 0:66e9a0afcbd6 | 205 | cmd[1] = 0x00; |
bulmecisco | 0:66e9a0afcbd6 | 206 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 207 | |
bulmecisco | 0:66e9a0afcbd6 | 208 | cmd[0]=0x01; |
bulmecisco | 0:66e9a0afcbd6 | 209 | i2c.write(addr, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 210 | i2c.read(addr|1, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 211 | btns = cmd[0]; |
bulmecisco | 0:66e9a0afcbd6 | 212 | if( btns & (BTN_FL|BTN_FM|BTN_FR|BTN_FRR|BTN_FLL)) |
bulmecisco | 0:66e9a0afcbd6 | 213 | wert = true; |
bulmecisco | 0:66e9a0afcbd6 | 214 | else |
bulmecisco | 0:66e9a0afcbd6 | 215 | wert = false; |
bulmecisco | 0:66e9a0afcbd6 | 216 | DEBUG_PRINT("WERT: %d", wert); |
bulmecisco | 0:66e9a0afcbd6 | 217 | return wert; |
bulmecisco | 0:66e9a0afcbd6 | 218 | } |
bulmecisco | 0:66e9a0afcbd6 | 219 | |
bulmecisco | 0:66e9a0afcbd6 | 220 | // ISR |
bulmecisco | 0:66e9a0afcbd6 | 221 | |
bulmecisco | 0:66e9a0afcbd6 | 222 | void ur_Bertl::increment() |
bulmecisco | 0:66e9a0afcbd6 | 223 | { |
bulmecisco | 0:66e9a0afcbd6 | 224 | _count++; |
bulmecisco | 0:66e9a0afcbd6 | 225 | } |
bulmecisco | 0:66e9a0afcbd6 | 226 | |
bulmecisco | 0:66e9a0afcbd6 | 227 | int ur_Bertl::Read() |
bulmecisco | 0:66e9a0afcbd6 | 228 | { |
bulmecisco | 0:66e9a0afcbd6 | 229 | return _count; |
bulmecisco | 0:66e9a0afcbd6 | 230 | } |
bulmecisco | 0:66e9a0afcbd6 | 231 | |
bulmecisco | 0:66e9a0afcbd6 | 232 | //---------------------------------------------------------------------- |
bulmecisco | 1:fafbac0ba96d | 233 | |
bulmecisco | 0:66e9a0afcbd6 | 234 | bool ur_Bertl::FrontIsClear() |
bulmecisco | 0:66e9a0afcbd6 | 235 | { |
bulmecisco | 1:fafbac0ba96d | 236 | #ifdef HCSR |
bulmecisco | 0:66e9a0afcbd6 | 237 | int dist = 0; |
bulmecisco | 0:66e9a0afcbd6 | 238 | usensor.start(); |
bulmecisco | 0:66e9a0afcbd6 | 239 | wait_ms(10); |
bulmecisco | 0:66e9a0afcbd6 | 240 | dist=usensor.get_dist_cm(); |
bulmecisco | 0:66e9a0afcbd6 | 241 | if(dist < 5) |
bulmecisco | 0:66e9a0afcbd6 | 242 | return false; |
bulmecisco | 0:66e9a0afcbd6 | 243 | else |
bulmecisco | 0:66e9a0afcbd6 | 244 | return true; |
bulmecisco | 1:fafbac0ba96d | 245 | #else |
bulmecisco | 0:66e9a0afcbd6 | 246 | // if there is no ultra sonic sensor use this - with front buttons |
bulmecisco | 0:66e9a0afcbd6 | 247 | char cmd[3]; // array for I2C |
bulmecisco | 0:66e9a0afcbd6 | 248 | int16_t btns; |
bulmecisco | 0:66e9a0afcbd6 | 249 | bool wert; |
bulmecisco | 0:66e9a0afcbd6 | 250 | |
bulmecisco | 0:66e9a0afcbd6 | 251 | cmd[0] = 0x06; |
bulmecisco | 0:66e9a0afcbd6 | 252 | cmd[1] = 0x00; |
bulmecisco | 0:66e9a0afcbd6 | 253 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 254 | |
bulmecisco | 0:66e9a0afcbd6 | 255 | cmd[0]=0x01; |
bulmecisco | 0:66e9a0afcbd6 | 256 | i2c.write(addr, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 257 | i2c.read(addr|1, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 258 | btns = cmd[0]; |
bulmecisco | 0:66e9a0afcbd6 | 259 | if( btns & (BTN_FL|BTN_FM|BTN_FR)) |
bulmecisco | 0:66e9a0afcbd6 | 260 | wert = false; |
bulmecisco | 0:66e9a0afcbd6 | 261 | else |
bulmecisco | 0:66e9a0afcbd6 | 262 | wert = true; |
bulmecisco | 0:66e9a0afcbd6 | 263 | DEBUG_PRINT("WERT: %d", wert); |
bulmecisco | 0:66e9a0afcbd6 | 264 | return wert; |
bulmecisco | 1:fafbac0ba96d | 265 | #endif |
bulmecisco | 0:66e9a0afcbd6 | 266 | } |
bulmecisco | 0:66e9a0afcbd6 | 267 | |
bulmecisco | 1:fafbac0ba96d | 268 | /* |
bulmecisco | 0:66e9a0afcbd6 | 269 | bool ur_Bertl::NextToABeeper() |
bulmecisco | 0:66e9a0afcbd6 | 270 | { |
bulmecisco | 0:66e9a0afcbd6 | 271 | if (BottomIsBlack()) |
bulmecisco | 0:66e9a0afcbd6 | 272 | return true; |
bulmecisco | 0:66e9a0afcbd6 | 273 | else |
bulmecisco | 0:66e9a0afcbd6 | 274 | return false; |
bulmecisco | 0:66e9a0afcbd6 | 275 | } |
bulmecisco | 0:66e9a0afcbd6 | 276 | |
bulmecisco | 0:66e9a0afcbd6 | 277 | int ur_Bertl::AnyBeeperInBag() |
bulmecisco | 0:66e9a0afcbd6 | 278 | { |
bulmecisco | 0:66e9a0afcbd6 | 279 | if(beepersInBag > 0) |
bulmecisco | 0:66e9a0afcbd6 | 280 | return beepersInBag; |
bulmecisco | 0:66e9a0afcbd6 | 281 | else |
bulmecisco | 0:66e9a0afcbd6 | 282 | return 0; |
bulmecisco | 0:66e9a0afcbd6 | 283 | } |
bulmecisco | 0:66e9a0afcbd6 | 284 | |
bulmecisco | 0:66e9a0afcbd6 | 285 | bool ur_Bertl::backIsClear() |
bulmecisco | 0:66e9a0afcbd6 | 286 | { |
bulmecisco | 0:66e9a0afcbd6 | 287 | char cmd[3]; // array for I2C |
bulmecisco | 0:66e9a0afcbd6 | 288 | int16_t btns; |
bulmecisco | 0:66e9a0afcbd6 | 289 | bool wert; |
bulmecisco | 0:66e9a0afcbd6 | 290 | |
bulmecisco | 0:66e9a0afcbd6 | 291 | cmd[0] = 0x06; |
bulmecisco | 0:66e9a0afcbd6 | 292 | cmd[1] = 0x00; |
bulmecisco | 0:66e9a0afcbd6 | 293 | i2c.write(addr, cmd, 2); |
bulmecisco | 0:66e9a0afcbd6 | 294 | |
bulmecisco | 0:66e9a0afcbd6 | 295 | cmd[0]=0x01; |
bulmecisco | 0:66e9a0afcbd6 | 296 | i2c.write(addr, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 297 | i2c.read(addr|1, cmd, 1); |
bulmecisco | 0:66e9a0afcbd6 | 298 | btns = cmd[0]; |
bulmecisco | 0:66e9a0afcbd6 | 299 | if( btns & (BTN_BL|BTN_BM|BTN_BR)) |
bulmecisco | 0:66e9a0afcbd6 | 300 | wert = false; |
bulmecisco | 0:66e9a0afcbd6 | 301 | else |
bulmecisco | 0:66e9a0afcbd6 | 302 | wert = true; |
bulmecisco | 0:66e9a0afcbd6 | 303 | DEBUG_PRINT("WERT: %d", wert); |
bulmecisco | 0:66e9a0afcbd6 | 304 | return wert; |
bulmecisco | 0:66e9a0afcbd6 | 305 | } |
bulmecisco | 0:66e9a0afcbd6 | 306 | |
bulmecisco | 0:66e9a0afcbd6 | 307 | int ur_Bertl::BottomIsBlack() |
bulmecisco | 0:66e9a0afcbd6 | 308 | { |
bulmecisco | 0:66e9a0afcbd6 | 309 | int detect; |
bulmecisco | 0:66e9a0afcbd6 | 310 | |
bulmecisco | 0:66e9a0afcbd6 | 311 | detect = linesensor; |
bulmecisco | 0:66e9a0afcbd6 | 312 | return detect; |
bulmecisco | 0:66e9a0afcbd6 | 313 | } |
bulmecisco | 0:66e9a0afcbd6 | 314 | |
bulmecisco | 0:66e9a0afcbd6 | 315 | void ur_Bertl::MoveBackwards() |
bulmecisco | 0:66e9a0afcbd6 | 316 | { |
bulmecisco | 0:66e9a0afcbd6 | 317 | int count = _count; |
bulmecisco | 0:66e9a0afcbd6 | 318 | //wait_ms(250); // waite until Bertl stops |
bulmecisco | 0:66e9a0afcbd6 | 319 | MotorR_EN=MotorL_EN=1; // both motor ENABLE |
bulmecisco | 0:66e9a0afcbd6 | 320 | MotorR_REVERSE = MotorL_REVERSE = 1; // both motor backwards ON |
bulmecisco | 0:66e9a0afcbd6 | 321 | while(_count < count+DISTANCE) { |
bulmecisco | 0:66e9a0afcbd6 | 322 | if(!backIsClear()) |
bulmecisco | 0:66e9a0afcbd6 | 323 | break; |
bulmecisco | 0:66e9a0afcbd6 | 324 | DEBUG_PRINT("count: %d _count: %d", count, _count); |
bulmecisco | 0:66e9a0afcbd6 | 325 | } |
bulmecisco | 0:66e9a0afcbd6 | 326 | MotorR_REVERSE = MotorL_REVERSE = 0; // both motor off |
bulmecisco | 0:66e9a0afcbd6 | 327 | MotorR_EN=MotorL_EN=0; |
bulmecisco | 0:66e9a0afcbd6 | 328 | wait_ms(250); |
bulmecisco | 0:66e9a0afcbd6 | 329 | } |
bulmecisco | 0:66e9a0afcbd6 | 330 | */ |