Zachary Sunberg / Mbed 2 deprecated SAILORSbot

Dependencies:   mbed

Committer:
zsunberg
Date:
Sun Jun 28 08:43:51 2015 +0000
Revision:
1:a7aab5937375
Parent:
0:9f058fe8cab5
Child:
2:483c2e492a36
purty much works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zsunberg 0:9f058fe8cab5 1 #include "robot.h"
zsunberg 1:a7aab5937375 2 #include <sstream>
zsunberg 1:a7aab5937375 3
zsunberg 1:a7aab5937375 4 // std::stringstream command;
zsunberg 1:a7aab5937375 5
zsunberg 1:a7aab5937375 6 volatile double leftspeed;
zsunberg 1:a7aab5937375 7 volatile double rightspeed;
zsunberg 1:a7aab5937375 8
zsunberg 1:a7aab5937375 9 char received[80];
zsunberg 1:a7aab5937375 10 int r_index;
zsunberg 1:a7aab5937375 11
zsunberg 1:a7aab5937375 12 int sensors[5];
zsunberg 0:9f058fe8cab5 13
zsunberg 1:a7aab5937375 14 /*
zsunberg 1:a7aab5937375 15 parses a single command from the stream
zsunberg 1:a7aab5937375 16 */
zsunberg 1:a7aab5937375 17 // int parse_command(std::stringstream & cmd)
zsunberg 1:a7aab5937375 18 int parse_command(const char* cmd)
zsunberg 1:a7aab5937375 19 {
zsunberg 1:a7aab5937375 20 //xbee.printf("%s\n", cmd.str().c_str());
zsunberg 1:a7aab5937375 21 //xbee.printf("%s\n", cmd);
zsunberg 1:a7aab5937375 22 if(cmd[1]==':'){
zsunberg 1:a7aab5937375 23 if(cmd[0]=='l'){
zsunberg 1:a7aab5937375 24 leftspeed = atof(&cmd[2]);
zsunberg 1:a7aab5937375 25 }else if(cmd[0]=='r'){
zsunberg 1:a7aab5937375 26 rightspeed = atof(&cmd[2]);
zsunberg 1:a7aab5937375 27 }else{
zsunberg 1:a7aab5937375 28 xbee.printf("%s\n",cmd);
zsunberg 1:a7aab5937375 29 }
zsunberg 1:a7aab5937375 30 }else{
zsunberg 1:a7aab5937375 31 xbee.printf("%s\n",cmd);
zsunberg 1:a7aab5937375 32 }
zsunberg 1:a7aab5937375 33 return 0;
zsunberg 1:a7aab5937375 34 }
zsunberg 0:9f058fe8cab5 35
zsunberg 0:9f058fe8cab5 36 void Rx_interrupt()
zsunberg 0:9f058fe8cab5 37 {
zsunberg 1:a7aab5937375 38 // assume recursive interrupt is not possible
zsunberg 0:9f058fe8cab5 39 led2 = 1;
zsunberg 1:a7aab5937375 40 char read;
zsunberg 0:9f058fe8cab5 41
zsunberg 0:9f058fe8cab5 42 while(xbee.readable()){
zsunberg 1:a7aab5937375 43 read = xbee.getc();
zsunberg 1:a7aab5937375 44 if(read=='\n'){
zsunberg 1:a7aab5937375 45 received[r_index]='\0'; // put a null character at the end
zsunberg 1:a7aab5937375 46 parse_command(received);
zsunberg 1:a7aab5937375 47 r_index=0;
zsunberg 1:a7aab5937375 48 //command.str("");
zsunberg 0:9f058fe8cab5 49 } else {
zsunberg 1:a7aab5937375 50 if(r_index >= 80){
zsunberg 1:a7aab5937375 51 r_index=0;
zsunberg 1:a7aab5937375 52 }
zsunberg 1:a7aab5937375 53 received[r_index]=read;
zsunberg 1:a7aab5937375 54 r_index++;
zsunberg 1:a7aab5937375 55 //command << read;
zsunberg 0:9f058fe8cab5 56 }
zsunberg 0:9f058fe8cab5 57 }
zsunberg 0:9f058fe8cab5 58
zsunberg 1:a7aab5937375 59 led2=0;
zsunberg 1:a7aab5937375 60
zsunberg 0:9f058fe8cab5 61 return;
zsunberg 0:9f058fe8cab5 62 }
zsunberg 0:9f058fe8cab5 63
zsunberg 0:9f058fe8cab5 64 int main() {
zsunberg 0:9f058fe8cab5 65
zsunberg 0:9f058fe8cab5 66 xbee.attach(&Rx_interrupt, Serial::RxIrq);
zsunberg 0:9f058fe8cab5 67 xbeeReset = 0;
zsunberg 0:9f058fe8cab5 68 wait(2);
zsunberg 0:9f058fe8cab5 69 xbeeReset = 1;
zsunberg 1:a7aab5937375 70 pi.sensor_auto_calibrate();
zsunberg 1:a7aab5937375 71 r_index = 0;
zsunberg 1:a7aab5937375 72 received[0] = '\0';
zsunberg 0:9f058fe8cab5 73
zsunberg 0:9f058fe8cab5 74 while(1){
zsunberg 0:9f058fe8cab5 75 led1 = 1;
zsunberg 1:a7aab5937375 76 wait_ms(50);
zsunberg 1:a7aab5937375 77 pi.left_motor(leftspeed);
zsunberg 1:a7aab5937375 78 pi.right_motor(rightspeed);
zsunberg 0:9f058fe8cab5 79 led1 = 0;
zsunberg 1:a7aab5937375 80 wait_ms(50);
zsunberg 1:a7aab5937375 81 pi.sensor_reading(sensors);
zsunberg 1:a7aab5937375 82 int* s = sensors;
zsunberg 1:a7aab5937375 83 __disable_irq();
zsunberg 1:a7aab5937375 84 xbee.printf("s:%i,%i,%i,%i,%i\n", s[0], s[1], s[2], s[3], s[4]);
zsunberg 1:a7aab5937375 85 __enable_irq();
zsunberg 1:a7aab5937375 86 __disable_irq();
zsunberg 1:a7aab5937375 87 xbee.printf("p:%f\n", pi.line_position());
zsunberg 1:a7aab5937375 88 __enable_irq();
zsunberg 1:a7aab5937375 89 // xbee.printf("%s\n",received)
zsunberg 0:9f058fe8cab5 90 }
zsunberg 0:9f058fe8cab5 91
zsunberg 0:9f058fe8cab5 92 return 0;
zsunberg 0:9f058fe8cab5 93 }