/* * HelloRoomba -- my hello world for arduino/roomba control * * * based on: http://roombahacking.com/software/arduino/RoombaBumpTurn.pde * * RoombaBumpTurn * -------------- * Implement the RoombaComm BumpTurn program in Arduino * A simple algorithm that allows the Roomba to drive around * and avoid obstacles. * * Arduino pin 0 (RX) is connected to Roomba TXD * Arduino pin 1 (TX) is connected to Roomba RXD * Arduino pin 2 is conencted to Roomba DD * * Updated 20 November 2006 * - changed Serial.prints() to use single print(v,BYTE) calls instead of * character arrays until Arduino settles on a style of raw byte arrays * * Created 1 August 2006 * copyleft 2006 Tod E. Kurt * http://hackingroomba.com/ * * http://roombahacking.com/software/arduino/RoombaBumpTurn.pde * */ #include /// serial ports // int rxPin = 0; // (FIXME: serial pins don't need to be defined) int txPin = 1; int rxPin_nss = 3; int txPin_nss = 4; // need both the SCI conn to the roomba and the XBee radio (for debugging) to be connected, // one to the hardware UART and one via NewSoftSerial (NSS). Uncomment one of the blocks // below. [one must be commented out] // // --------> OPTION ONE: SCI on the UART / XBee on NSS: /* int ddPin = 2; // keep ddPin next to the SCI connector #define sciSerial Serial NewSoftSerial xbSerial(rxPin_nss,txPin_nss); // --------> OPTION TWO: SCI on NSS / XBee on the UART: */ /**/ int ddPin = 5; // keep ddPin next to SCI #define xbSerial Serial NewSoftSerial sciSerial(rxPin_nss,txPin_nss); /**/ int ledPin = 13; char sensorbytes[10]; #define bumpright (sensorbytes[0] & 0x01) #define bumpleft (sensorbytes[0] & 0x02) void setup() { // pinMode(txPin, OUTPUT); pinMode(ddPin, OUTPUT); // sets the pins as output pinMode(ledPin, OUTPUT); // sets the pins as output sciSerial.begin(57600); //sciSerial.begin(19200); xbSerial.begin(9600); xbSerial.print("starting up... "); digitalWrite(ledPin, HIGH); // say we're alive // wake up the robot digitalWrite(ddPin, HIGH); delay(100); digitalWrite(ddPin, LOW); delay(500); digitalWrite(ddPin, HIGH); delay(2000); // set up ROI to receive commands sciSerial.print(128, BYTE); // START delay(50); sciSerial.print(130, BYTE); // CONTROL delay(50); digitalWrite(ledPin, LOW); // say we've finished setup xbSerial.println("ready!"); // [re]set baud rate to 57600 //sciSerial.print(129, BYTE); // BAUD //sciSerial.print(10, BYTE); // 57600 //delay(100); dance(); // demonstrate that controls work } void dance() { xbSerial.println("top of dance()"); updateSensors(); goForward(); delay(1000); goBackward(); delay(1000); stopMoving(); } void loop() { digitalWrite(ledPin, HIGH); // say we're starting loop updateSensors(); digitalWrite(ledPin, LOW); // say we're after updateSensors if(bumpleft) { xbSerial.println("left bump!"); stopMoving(); delay(500); spinRight(); delay(1000); stopMoving(); delay(500); } else if(bumpright) { xbSerial.println("right bump!"); stopMoving(); delay(500); spinLeft(); delay(1000); stopMoving(); delay(500); } goForward(); } void goForward() { sciSerial.print(137, BYTE); // DRIVE sciSerial.print(0x00,BYTE); // 0x00c8 == 200 sciSerial.print(0xc8,BYTE); sciSerial.print(0x80,BYTE); sciSerial.print(0x00,BYTE); } void goBackward() { sciSerial.print(137, BYTE); // DRIVE sciSerial.print(0xff,BYTE); // 0xff38 == -200 sciSerial.print(0x38,BYTE); sciSerial.print(0x80,BYTE); sciSerial.print(0x00,BYTE); } void spinLeft() { sciSerial.print(137, BYTE); // DRIVE sciSerial.print(0x00,BYTE); // 0x00c8 == 200 sciSerial.print(0xc8,BYTE); sciSerial.print(0x00,BYTE); sciSerial.print(0x01,BYTE); // 0x0001 == spin left } void spinRight() { sciSerial.print(137, BYTE); // DRIVE sciSerial.print(0x00,BYTE); // 0x00c8 == 200 sciSerial.print(0xc8,BYTE); sciSerial.print(0xff,BYTE); sciSerial.print(0xff,BYTE); // 0xffff == -1 == spin right } void stopMoving() { sciSerial.print(137, BYTE); // DRIVE sciSerial.print(0x00,BYTE); // 0x0000 == 0 sciSerial.print(0x00,BYTE); sciSerial.print(0x00,BYTE); sciSerial.print(0x00,BYTE); // 0x0000 } void updateSensors() { //xbSerial.println("in updateSensors()"); sciSerial.print(142, BYTE); sciSerial.print(1, BYTE); // sensor packet 1, 10 bytes //delay(100); // wait for sensors delay(64); // wipe old sensor data char i = 0; while (i < 10) { sensorbytes[i++] = 0; } i = 0; while(sciSerial.available()) { //xbSerial.println("rsd"); // reading sensor data int c = sciSerial.read(); if( c==-1 ) { xbSerial.println("error read()ing sensors"); for( int i=0; i<5; i ++ ) { // say we had an error via the LED digitalWrite(ledPin, HIGH); delay(50); digitalWrite(ledPin, LOW); delay(50); } } sensorbytes[i++] = c; } if (i != 10) { // debug: print # bytes read (should always be 10?) xbSerial.print("error: only "); xbSerial.print(i,DEC); xbSerial.println(" bytes read"); } /* i = 0; while (i < 10) { if (sciSerial.available()) { xbSerial.println("rsd"); // reading sensor data int c = sciSerial.read(); if( c==-1 ) { // shouldn't happen xbSerial.println("error read()ing sensors"); } sensorbytes[i++] = c; } */ }