Final Project

Dependencies:   mbed

Committer:
pwilson39
Date:
Fri Apr 17 00:07:38 2015 +0000
Revision:
0:21c11d775cc4
Initial Commit of final project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pwilson39 0:21c11d775cc4 1 #include "mbed.h"
pwilson39 0:21c11d775cc4 2
pwilson39 0:21c11d775cc4 3 Serial device(p9, p10); // tx, rx
pwilson39 0:21c11d775cc4 4 AnalogIn sensor(p20);
pwilson39 0:21c11d775cc4 5 DigitalOut led1(LED1);
pwilson39 0:21c11d775cc4 6 DigitalOut led2(LED2);
pwilson39 0:21c11d775cc4 7 DigitalOut led3(LED3);
pwilson39 0:21c11d775cc4 8 DigitalOut led4(LED4);
pwilson39 0:21c11d775cc4 9
pwilson39 0:21c11d775cc4 10 // Definitions of iRobot Create OpenInterface Command Numbers
pwilson39 0:21c11d775cc4 11 // See the Create OpenInterface manual for a complete list
pwilson39 0:21c11d775cc4 12
pwilson39 0:21c11d775cc4 13
pwilson39 0:21c11d775cc4 14 // Create Command // Arguments
pwilson39 0:21c11d775cc4 15 const char Start = 128;
pwilson39 0:21c11d775cc4 16 const char SafeMode = 131;
pwilson39 0:21c11d775cc4 17 const char FullMode = 132;
pwilson39 0:21c11d775cc4 18 const char Drive = 137; // 4: [Vel. Hi] [Vel Low] [Rad. Hi] [Rad. Low]
pwilson39 0:21c11d775cc4 19 const char DriveDirect = 145; // 4: [Right Hi] [Right Low] [Left Hi] [Left Low]
pwilson39 0:21c11d775cc4 20 const char Demo = 136; // 2: Run Demo x
pwilson39 0:21c11d775cc4 21 const char Sensors = 142; // 1: Sensor Packet ID
pwilson39 0:21c11d775cc4 22 const char CoverandDock = 143; // 1: Return to Charger
pwilson39 0:21c11d775cc4 23 const char SensorStream = 148; // x+1: [# of packets requested] IDs of requested packets to stream
pwilson39 0:21c11d775cc4 24 const char QueryList = 149; // x+1: [# of packets requested] IDs of requested packets to stream
pwilson39 0:21c11d775cc4 25 const char StreamPause = 150; // 1: 0 = stop stream, 1 = start stream
pwilson39 0:21c11d775cc4 26 const char PlaySong = 141;
pwilson39 0:21c11d775cc4 27 const char Song = 140;
pwilson39 0:21c11d775cc4 28 /* iRobot Create Sensor IDs */
pwilson39 0:21c11d775cc4 29 const char BumpsandDrops = 7;
pwilson39 0:21c11d775cc4 30 const char Distance = 19;
pwilson39 0:21c11d775cc4 31 const char Angle = 20;
pwilson39 0:21c11d775cc4 32
pwilson39 0:21c11d775cc4 33 int speed_left = 200;
pwilson39 0:21c11d775cc4 34 int speed_right = 200;
pwilson39 0:21c11d775cc4 35 void start();
pwilson39 0:21c11d775cc4 36 void forward();
pwilson39 0:21c11d775cc4 37 void reverse();
pwilson39 0:21c11d775cc4 38 void left();
pwilson39 0:21c11d775cc4 39 void right();
pwilson39 0:21c11d775cc4 40 void stop();
pwilson39 0:21c11d775cc4 41 void playsong();
pwilson39 0:21c11d775cc4 42 void charger();
pwilson39 0:21c11d775cc4 43 void testDistance();
pwilson39 0:21c11d775cc4 44
pwilson39 0:21c11d775cc4 45 // Demo to move around using basic commands
pwilson39 0:21c11d775cc4 46 int main() {
pwilson39 0:21c11d775cc4 47 // wait for Create to power up to accept serial commands
pwilson39 0:21c11d775cc4 48 wait(5);
pwilson39 0:21c11d775cc4 49 // set baud rate for Create factory default
pwilson39 0:21c11d775cc4 50 device.baud(57600);
pwilson39 0:21c11d775cc4 51 // Start command mode and select sensor data to send back
pwilson39 0:21c11d775cc4 52 start();
pwilson39 0:21c11d775cc4 53 wait(.5);
pwilson39 0:21c11d775cc4 54
pwilson39 0:21c11d775cc4 55 forward();
pwilson39 0:21c11d775cc4 56 wait(.5);
pwilson39 0:21c11d775cc4 57 stop();
pwilson39 0:21c11d775cc4 58
pwilson39 0:21c11d775cc4 59 testDistance();
pwilson39 0:21c11d775cc4 60 wait(10);
pwilson39 0:21c11d775cc4 61 playsong();
pwilson39 0:21c11d775cc4 62
pwilson39 0:21c11d775cc4 63 // Move around with motor commands
pwilson39 0:21c11d775cc4 64 //forward();
pwilson39 0:21c11d775cc4 65 // wait(.5);
pwilson39 0:21c11d775cc4 66 // stop();
pwilson39 0:21c11d775cc4 67 // wait(.1);
pwilson39 0:21c11d775cc4 68 // reverse();
pwilson39 0:21c11d775cc4 69 // wait(.5);
pwilson39 0:21c11d775cc4 70 // left();
pwilson39 0:21c11d775cc4 71 // wait(1);
pwilson39 0:21c11d775cc4 72 // stop();
pwilson39 0:21c11d775cc4 73 // wait(.1);
pwilson39 0:21c11d775cc4 74 // right();
pwilson39 0:21c11d775cc4 75 // wait(1);
pwilson39 0:21c11d775cc4 76 // stop();
pwilson39 0:21c11d775cc4 77 // wait(.5);
pwilson39 0:21c11d775cc4 78 // Play a song
pwilson39 0:21c11d775cc4 79 // playsong();
pwilson39 0:21c11d775cc4 80 // wait(10);
pwilson39 0:21c11d775cc4 81 // Search for battery charger IR beacon
pwilson39 0:21c11d775cc4 82 charger();
pwilson39 0:21c11d775cc4 83 }
pwilson39 0:21c11d775cc4 84
pwilson39 0:21c11d775cc4 85
pwilson39 0:21c11d775cc4 86 // Start - send start and safe mode, start streaming sensor data
pwilson39 0:21c11d775cc4 87 void start() {
pwilson39 0:21c11d775cc4 88 // device.printf("%c%c", Start, SafeMode);
pwilson39 0:21c11d775cc4 89 device.putc(Start);
pwilson39 0:21c11d775cc4 90 device.putc(SafeMode);
pwilson39 0:21c11d775cc4 91 wait(.5);
pwilson39 0:21c11d775cc4 92 // device.printf("%c%c%c", SensorStream, char(1), BumpsandDrops);
pwilson39 0:21c11d775cc4 93 device.putc(SensorStream);
pwilson39 0:21c11d775cc4 94 device.putc(1);
pwilson39 0:21c11d775cc4 95 device.putc(BumpsandDrops);
pwilson39 0:21c11d775cc4 96 wait(.5);
pwilson39 0:21c11d775cc4 97 }
pwilson39 0:21c11d775cc4 98 // Stop - turn off drive motors
pwilson39 0:21c11d775cc4 99 void stop() {
pwilson39 0:21c11d775cc4 100 device.printf("%c%c%c%c%c", DriveDirect, char(0), char(0), char(0), char(0));
pwilson39 0:21c11d775cc4 101 }
pwilson39 0:21c11d775cc4 102 // Forward - turn on drive motors
pwilson39 0:21c11d775cc4 103 void forward() {
pwilson39 0:21c11d775cc4 104 device.printf("%c%c%c%c%c", DriveDirect, char((speed_right>>8)&0xFF), char(speed_right&0xFF),
pwilson39 0:21c11d775cc4 105 char((speed_left>>8)&0xFF), char(speed_left&0xFF));
pwilson39 0:21c11d775cc4 106
pwilson39 0:21c11d775cc4 107 }
pwilson39 0:21c11d775cc4 108 // Reverse - reverse drive motors
pwilson39 0:21c11d775cc4 109 void reverse() {
pwilson39 0:21c11d775cc4 110 device.printf("%c%c%c%c%c", DriveDirect, char(((-speed_right)>>8)&0xFF), char((-speed_right)&0xFF),
pwilson39 0:21c11d775cc4 111 char(((-speed_left)>>8)&0xFF), char((-speed_left)&0xFF));
pwilson39 0:21c11d775cc4 112
pwilson39 0:21c11d775cc4 113 }
pwilson39 0:21c11d775cc4 114 // Left - drive motors set to rotate to left
pwilson39 0:21c11d775cc4 115 void left() {
pwilson39 0:21c11d775cc4 116 device.printf("%c%c%c%c%c", DriveDirect, char((speed_right>>8)&0xFF), char(speed_right&0xFF),
pwilson39 0:21c11d775cc4 117 char(((-speed_left)>>8)&0xFF), char((-speed_left)&0xFF));
pwilson39 0:21c11d775cc4 118 }
pwilson39 0:21c11d775cc4 119 // Right - drive motors set to rotate to right
pwilson39 0:21c11d775cc4 120 void right() {
pwilson39 0:21c11d775cc4 121 device.printf("%c%c%c%c%c", DriveDirect, char(((-speed_right)>>8)&0xFF), char((-speed_right)&0xFF),
pwilson39 0:21c11d775cc4 122 char((speed_left>>8)&0xFF), char(speed_left&0xFF));
pwilson39 0:21c11d775cc4 123
pwilson39 0:21c11d775cc4 124 }
pwilson39 0:21c11d775cc4 125 // Charger - search and return to charger using IR beacons (if found)
pwilson39 0:21c11d775cc4 126 void charger() {
pwilson39 0:21c11d775cc4 127 device.printf("%c%c", Demo, char(1));
pwilson39 0:21c11d775cc4 128 }
pwilson39 0:21c11d775cc4 129 // Play Song - define and play a song
pwilson39 0:21c11d775cc4 130 void playsong() { // Send out notes & duration to define song and then play song
pwilson39 0:21c11d775cc4 131
pwilson39 0:21c11d775cc4 132 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",
pwilson39 0:21c11d775cc4 133 Song, char(0), char(16), char(91), char(24), char(89), char(12), char(87), char(36), char(87),
pwilson39 0:21c11d775cc4 134 char(24), char(89), char(12), char(91), char(24), char(91), char(12), char(91), char(12), char(89),
pwilson39 0:21c11d775cc4 135 char(12),char(87), char(12), char(89), char(12), char(91), char(12), char(89), char(12), char(87),
pwilson39 0:21c11d775cc4 136 char(24), char(86), char(12), char(87), char(48));
pwilson39 0:21c11d775cc4 137
pwilson39 0:21c11d775cc4 138 wait(.2);
pwilson39 0:21c11d775cc4 139 device.printf("%c%c", PlaySong, char(0));
pwilson39 0:21c11d775cc4 140 }
pwilson39 0:21c11d775cc4 141
pwilson39 0:21c11d775cc4 142 void testDistance() {
pwilson39 0:21c11d775cc4 143 int timer = 0;
pwilson39 0:21c11d775cc4 144 while(timer < 100000000) {
pwilson39 0:21c11d775cc4 145 if(sensor > 0.75) {
pwilson39 0:21c11d775cc4 146 led1 = 1;
pwilson39 0:21c11d775cc4 147 led2 = 1;
pwilson39 0:21c11d775cc4 148 led3 = 1;
pwilson39 0:21c11d775cc4 149 led4 = 1;
pwilson39 0:21c11d775cc4 150 forward();
pwilson39 0:21c11d775cc4 151 } else if(sensor > 0.50){
pwilson39 0:21c11d775cc4 152 led1 = 1;
pwilson39 0:21c11d775cc4 153 led2 = 1;
pwilson39 0:21c11d775cc4 154 led3 = 1;
pwilson39 0:21c11d775cc4 155 led4 = 0;
pwilson39 0:21c11d775cc4 156 left();
pwilson39 0:21c11d775cc4 157 wait(1);
pwilson39 0:21c11d775cc4 158 forward();
pwilson39 0:21c11d775cc4 159 wait(1);
pwilson39 0:21c11d775cc4 160 stop();
pwilson39 0:21c11d775cc4 161 } else if(sensor > 0.25){
pwilson39 0:21c11d775cc4 162 led1 = 1;
pwilson39 0:21c11d775cc4 163 led2 = 1;
pwilson39 0:21c11d775cc4 164 led3 = 0;
pwilson39 0:21c11d775cc4 165 led4 = 0;
pwilson39 0:21c11d775cc4 166 right();
pwilson39 0:21c11d775cc4 167 wait(1);
pwilson39 0:21c11d775cc4 168 forward();
pwilson39 0:21c11d775cc4 169 wait(1);
pwilson39 0:21c11d775cc4 170 stop();
pwilson39 0:21c11d775cc4 171 } else if(sensor > 0.1){
pwilson39 0:21c11d775cc4 172 led1 = 1;
pwilson39 0:21c11d775cc4 173 led2 = 0;
pwilson39 0:21c11d775cc4 174 led3 = 0;
pwilson39 0:21c11d775cc4 175 led4 = 0;
pwilson39 0:21c11d775cc4 176 // forward();
pwilson39 0:21c11d775cc4 177 stop();
pwilson39 0:21c11d775cc4 178 } else {
pwilson39 0:21c11d775cc4 179 led1 = 0;
pwilson39 0:21c11d775cc4 180 led2 = 0;
pwilson39 0:21c11d775cc4 181 led3 = 0;
pwilson39 0:21c11d775cc4 182 led4 = 0;
pwilson39 0:21c11d775cc4 183 stop();
pwilson39 0:21c11d775cc4 184 }
pwilson39 0:21c11d775cc4 185 wait(1);
pwilson39 0:21c11d775cc4 186 timer = timer + 1;
pwilson39 0:21c11d775cc4 187 timer = timer - 1;
pwilson39 0:21c11d775cc4 188 }
pwilson39 0:21c11d775cc4 189 }