Zachary Sunberg / Mbed 2 deprecated SAILORSbot_student

Dependencies:   mbed

Fork of SAILORSbot by Zachary Sunberg

Committer:
Zachary Sunberg
Date:
Mon Jul 20 20:19:47 2015 -0700
Revision:
2:483c2e492a36
Parent:
1:a7aab5937375
Child:
3:47cd6b9e4dbb
got rid of some comments

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