Andy K
/
Roomba
Roomba robot class
main.cpp@0:b26b94a6a065, 2010-12-01 (annotated)
- Committer:
- AjK
- Date:
- Wed Dec 01 19:20:43 2010 +0000
- Revision:
- 0:b26b94a6a065
0.1 Untested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:b26b94a6a065 | 1 | |
AjK | 0:b26b94a6a065 | 2 | #include "Roomba.h" |
AjK | 0:b26b94a6a065 | 3 | |
AjK | 0:b26b94a6a065 | 4 | DigitalOut myled(LED1); |
AjK | 0:b26b94a6a065 | 5 | Roomba roomba(p9, p10); |
AjK | 0:b26b94a6a065 | 6 | |
AjK | 0:b26b94a6a065 | 7 | int main() { |
AjK | 0:b26b94a6a065 | 8 | |
AjK | 0:b26b94a6a065 | 9 | // Examples of sending commands. Note that when |
AjK | 0:b26b94a6a065 | 10 | // teh function returns, if it was successful |
AjK | 0:b26b94a6a065 | 11 | // it returns the command code. Otherwise it |
AjK | 0:b26b94a6a065 | 12 | // will return Roomba::NotIdle which means it |
AjK | 0:b26b94a6a065 | 13 | // couldn't send the command because the serial |
AjK | 0:b26b94a6a065 | 14 | // port was busy reading sensor data. |
AjK | 0:b26b94a6a065 | 15 | int result = roomba.command(Roomba::CmdPower); |
AjK | 0:b26b94a6a065 | 16 | if (result != Roomba::CmdPower) { |
AjK | 0:b26b94a6a065 | 17 | // Failed to send command because the |
AjK | 0:b26b94a6a065 | 18 | // serial port was busy getting sensor data |
AjK | 0:b26b94a6a065 | 19 | } |
AjK | 0:b26b94a6a065 | 20 | |
AjK | 0:b26b94a6a065 | 21 | // A sure fire way to make the command happen. |
AjK | 0:b26b94a6a065 | 22 | while (roomba.command(Roomba::CmdPower) != Roomba::CmdPower); |
AjK | 0:b26b94a6a065 | 23 | |
AjK | 0:b26b94a6a065 | 24 | // Some commands have additional bytes, heres an example |
AjK | 0:b26b94a6a065 | 25 | char leds[3]; |
AjK | 0:b26b94a6a065 | 26 | leds[0] = 0x01; // First byte sent |
AjK | 0:b26b94a6a065 | 27 | leds[1] = 0x05; |
AjK | 0:b26b94a6a065 | 28 | leds[2] = 0x76; // Last byte sent |
AjK | 0:b26b94a6a065 | 29 | roomba.command(Roomba::CmdLeds, 3, leds); |
AjK | 0:b26b94a6a065 | 30 | |
AjK | 0:b26b94a6a065 | 31 | // Sensors are read automartically. Here's how we |
AjK | 0:b26b94a6a065 | 32 | // get the wall sensor and voltage values. |
AjK | 0:b26b94a6a065 | 33 | uint8_t wall = roomba.sensor(Roomba::wall); |
AjK | 0:b26b94a6a065 | 34 | uint16_t voltage = roomba.sensor(Roomba::voltage); |
AjK | 0:b26b94a6a065 | 35 | |
AjK | 0:b26b94a6a065 | 36 | // Note, when reading sensor data make sure your variable |
AjK | 0:b26b94a6a065 | 37 | // is of the right type, either uint8_t for single byte |
AjK | 0:b26b94a6a065 | 38 | // and uint16_t for double byte variables. |
AjK | 0:b26b94a6a065 | 39 | |
AjK | 0:b26b94a6a065 | 40 | while(1) { |
AjK | 0:b26b94a6a065 | 41 | myled = 1; |
AjK | 0:b26b94a6a065 | 42 | wait(0.2); |
AjK | 0:b26b94a6a065 | 43 | myled = 0; |
AjK | 0:b26b94a6a065 | 44 | wait(0.2); |
AjK | 0:b26b94a6a065 | 45 | } |
AjK | 0:b26b94a6a065 | 46 | } |