jim hamblen / Mbed 2 deprecated create_hack

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial device(p9, p10);  // tx, rx
00004 
00005 // Definitions of iRobot Roomba SCI Command Numbers
00006 // See the Create OpenInterface manual for a complete list
00007 
00008 
00009 //                 Create Command              // Arguments
00010 const char         Start = 128;
00011 const char         Control = 130;
00012 const char         FullMode = 132;
00013 const char         Drive = 137;                // 4:   [Vel. Hi] [Vel Low] [Rad. Hi] [Rad. Low]
00014 const char         Sensors = 142;              // 1:    Sensor Packet ID
00015 const char         CoverandDock = 143;         // 0:    Return to Charger
00016 const char         Clean = 135;                 // 0:    Start Cleaning
00017 const char         PlaySong = 141;
00018 const char         Song = 140;
00019                 /* iRobot Roomba Sensor IDs */
00020 const char         BumpsandDrops = 1;
00021 
00022 int speed =  400;
00023 int radius = 0x8000;
00024 void start();
00025 void forward();
00026 void reverse();
00027 void left();
00028 void right();
00029 void stop();
00030 void playsong();
00031 void charger();
00032 
00033 // Demo to move around using basic commands
00034 int main() {
00035 // wait for Roomba to power up to accept serial commands
00036     wait(5);
00037 // set baud rate for Roomba factory default
00038     device.baud(57600);
00039 // Start command mode and select sensor data to send back
00040     start();
00041     wait(.5);
00042 // Send commands to move around
00043     forward();
00044     wait(.5);
00045     stop();
00046     wait(.1);
00047     reverse();
00048     wait(.5);
00049     left();
00050     wait(1);
00051     stop();
00052     wait(.1);
00053     right();
00054     wait(1);
00055     stop();
00056     wait(.5);
00057 // Play a song
00058     playsong();
00059     wait(10);
00060 // Search for battery charger IR beacon
00061     charger();
00062 }
00063 
00064 
00065 // Start  - send start and safe mode, start streaming sensor data
00066 void start() {
00067    // device.printf("%c%c", Start, SafeMode);
00068     device.putc(Start);
00069     wait(.1);
00070     device.putc(Control);
00071     wait(.5);
00072   //  device.printf("%c%c", SensorStream, char(1));
00073     device.putc(Sensors);
00074     device.putc(BumpsandDrops);
00075     wait(.5);
00076 }
00077 // Stop  - turn off drive motors
00078 void stop() {
00079     device.printf("%c%c%c%c%c", Drive, char(0),  char(0),  char(0),  char(0));
00080 }
00081 // Forward  - turn on drive motors
00082 void forward() {
00083     device.printf("%c%c%c%c%c", Drive, char((speed>>8)&0xFF),  char(speed&0xFF),  
00084     char((radius>>8)&0xFF),  char(radius&0xFF));
00085 
00086 }
00087 // Reverse - reverse drive motors
00088 void reverse() {
00089     device.printf("%c%c%c%c%c", Drive, char(((-speed)>>8)&0xFF),  char((-speed)&0xFF),  
00090     char(((radius)>>8)&0xFF),  char((radius)&0xFF));
00091 
00092 }
00093 // Left - drive motors set to rotate to left
00094 void left() {
00095     device.printf("%c%c%c%c%c", Drive, char((speed>>8)&0xFF),  char(speed&0xFF),  
00096     char(((1)>>8)&0xFF),  char((1)&0xFF));
00097 }
00098 // Right - drive motors set to rotate to right
00099 void right() {
00100     device.printf("%c%c%c%c%c", Drive, char(((speed)>>8)&0xFF),  char((speed)&0xFF),  
00101     char((-1>>8)&0xFF),  char(-1&0xFF));
00102 
00103 }
00104 // Charger - search and return to charger using IR beacons (if found)
00105 void charger() {
00106     device.printf("%c", Clean );
00107     wait(.2);
00108     device.printf("%c", CoverandDock );
00109 }
00110 // Play Song  - define and play a song
00111 void playsong() { // Send out notes & duration to define song and then play song
00112 
00113     device.printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 
00114                   Song, char(0), char(16), char(91), char(24), char(89), char(12), char(87), char(36), char(87),
00115                   char(24), char(89), char(12), char(91), char(24), char(91), char(12), char(91), char(12), char(89),
00116                   char(12),char(87), char(12), char(89), char(12), char(91), char(12), char(89), char(12), char(87),
00117                   char(24), char(86), char(12), char(87), char(48));
00118 
00119     wait(.2);
00120     device.printf("%c%c", PlaySong, char(0));
00121 }