Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SAILORSbot by
main.cpp@26:49945d96d461, 2015-08-05 (annotated)
- Committer:
- zsunberg
- Date:
- Wed Aug 05 05:46:23 2015 +0000
- Revision:
- 26:49945d96d461
- Parent:
- 25:c4577daa425a
- Child:
- 27:949a31d30439
seems to work fine :)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| zsunberg | 0:9f058fe8cab5 | 1 | #include "robot.h" |
| zsunberg | 23:55f8b1abbf99 | 2 | #include "my_functions.h" |
| zsunberg | 0:9f058fe8cab5 | 3 | |
| zsunberg | 1:a7aab5937375 | 4 | /* |
| zsunberg | 23:55f8b1abbf99 | 5 | * This function will be called at approximately 50 hz when the control mode is LINE_FOLLOW_MODE |
| Zachary Sunberg |
4:70fea94b29ae | 6 | */ |
| Zachary Sunberg |
5:70c86dbc8832 | 7 | void line_follow_loop(){ |
| Zachary Sunberg |
4:70fea94b29ae | 8 | led4 = 1; |
| laurennyg | 19:c900c40b270e | 9 | |
| laurennyg | 19:c900c40b270e | 10 | //varaibles to |
| laurennyg | 19:c900c40b270e | 11 | float prev_position = 0; |
| laurennyg | 19:c900c40b270e | 12 | float derivative, proportional, integral = 0; |
| laurennyg | 19:c900c40b270e | 13 | int sensors[5]; |
| laurennyg | 19:c900c40b270e | 14 | float position = 0; |
| laurennyg | 19:c900c40b270e | 15 | float power; //speed increase or decrease |
| laurennyg | 19:c900c40b270e | 16 | float speed = MAX; |
| Zachary Sunberg |
4:70fea94b29ae | 17 | |
| laurennyg | 19:c900c40b270e | 18 | |
| laurennyg | 19:c900c40b270e | 19 | //get sensors |
| laurennyg | 19:c900c40b270e | 20 | pi.sensor_reading(sensors); |
| laurennyg | 19:c900c40b270e | 21 | |
| laurennyg | 19:c900c40b270e | 22 | //line position |
| laurennyg | 19:c900c40b270e | 23 | position = pi.line_position(); |
| laurennyg | 19:c900c40b270e | 24 | proportional = position; |
| laurennyg | 19:c900c40b270e | 25 | |
| laurennyg | 19:c900c40b270e | 26 | //derivative |
| laurennyg | 19:c900c40b270e | 27 | derivative = position - prev_position; |
| laurennyg | 19:c900c40b270e | 28 | |
| laurennyg | 19:c900c40b270e | 29 | //integral |
| laurennyg | 19:c900c40b270e | 30 | integral += proportional; |
| laurennyg | 19:c900c40b270e | 31 | |
| laurennyg | 19:c900c40b270e | 32 | //last position |
| laurennyg | 19:c900c40b270e | 33 | prev_position = position; |
| laurennyg | 19:c900c40b270e | 34 | |
| laurennyg | 19:c900c40b270e | 35 | power = (proportional * k_p) + (integral * k_i) + (derivative * k_d); |
| laurennyg | 19:c900c40b270e | 36 | |
| laurennyg | 19:c900c40b270e | 37 | //new speeds |
| zsunberg | 20:f0ca65974329 | 38 | rightspeed = speed - power; |
| zsunberg | 20:f0ca65974329 | 39 | leftspeed = speed + power; |
| laurennyg | 19:c900c40b270e | 40 | |
| laurennyg | 19:c900c40b270e | 41 | if(rightspeed < MIN) |
| laurennyg | 19:c900c40b270e | 42 | rightspeed = MIN; |
| laurennyg | 19:c900c40b270e | 43 | else if(rightspeed > MAX) |
| laurennyg | 19:c900c40b270e | 44 | rightspeed = MAX; |
| laurennyg | 19:c900c40b270e | 45 | |
| laurennyg | 19:c900c40b270e | 46 | if(leftspeed < MIN) |
| laurennyg | 19:c900c40b270e | 47 | leftspeed = MIN; |
| laurennyg | 19:c900c40b270e | 48 | else if(leftspeed > MAX) |
| laurennyg | 19:c900c40b270e | 49 | leftspeed = MAX; |
| laurennyg | 19:c900c40b270e | 50 | |
| laurennyg | 19:c900c40b270e | 51 | //pi.left_motor(leftspeed); |
| laurennyg | 19:c900c40b270e | 52 | // pi.right_motor(rightspeed); |
| laurennyg | 19:c900c40b270e | 53 | |
| laurennyg | 19:c900c40b270e | 54 | if(sensors[0] < 1200 && sensors[1] < 1200 && sensors[2] < 1200 && sensors[3] < 1200 && sensors[4] < 1200) |
| laurennyg | 19:c900c40b270e | 55 | { |
| laurennyg | 19:c900c40b270e | 56 | pi.stop(); |
| laurennyg | 19:c900c40b270e | 57 | //dead end |
| laurennyg | 19:c900c40b270e | 58 | mode = MANUAL_MODE; |
| laurennyg | 19:c900c40b270e | 59 | } |
| laurennyg | 19:c900c40b270e | 60 | else if(sensors[0] > 1800 || sensors[4] > 1800) |
| laurennyg | 19:c900c40b270e | 61 | { |
| laurennyg | 19:c900c40b270e | 62 | //intersection |
| laurennyg | 19:c900c40b270e | 63 | mode = MANUAL_MODE; |
| laurennyg | 19:c900c40b270e | 64 | } |
| Zachary Sunberg |
4:70fea94b29ae | 65 | |
| Zachary Sunberg |
4:70fea94b29ae | 66 | // set mode to MANUAL_MODE when the end is detected |
| Zachary Sunberg |
4:70fea94b29ae | 67 | // mode = MANUAL_MODE |
| zsunberg | 11:f0df5dea322d | 68 | |
| laurennyg | 19:c900c40b270e | 69 | // wait_ms(10); |
| Zachary Sunberg |
4:70fea94b29ae | 70 | |
| Zachary Sunberg |
4:70fea94b29ae | 71 | led4 = 0; |
| Zachary Sunberg |
4:70fea94b29ae | 72 | } |
| Zachary Sunberg |
4:70fea94b29ae | 73 | |
| Zachary Sunberg |
4:70fea94b29ae | 74 | |
| Zachary Sunberg |
4:70fea94b29ae | 75 | // INPUT COMMANDS |
| Zachary Sunberg |
4:70fea94b29ae | 76 | // l:<float> set left wheel speed (only effective in MANUAL_MODE) |
| Zachary Sunberg |
4:70fea94b29ae | 77 | // r:<float> set right wheel speed (only effective in MANUAL_MODE) |
| Zachary Sunberg |
4:70fea94b29ae | 78 | // c:<int> change mode |
| Zachary Sunberg |
12:f96af1d15e9e | 79 | // g:<p|i|d>:<float> |
| Zachary Sunberg |
12:f96af1d15e9e | 80 | // change gains |
| zsunberg | 22:dae192ffca90 | 81 | // b: check battery |
| Zachary Sunberg |
4:70fea94b29ae | 82 | |
| Zachary Sunberg |
4:70fea94b29ae | 83 | // OUTPUT MESSAGES |
| Zachary Sunberg |
4:70fea94b29ae | 84 | // p:<float> line position |
| Zachary Sunberg |
4:70fea94b29ae | 85 | // s:<int>,<int>,<int>,<int>,<int> |
| Zachary Sunberg |
4:70fea94b29ae | 86 | // light sensor values |
| Zachary Sunberg |
4:70fea94b29ae | 87 | // m:<int> mode |
| zsunberg | 23:55f8b1abbf99 | 88 | // a:<message> acknowledge |
| zsunberg | 22:dae192ffca90 | 89 | void communicate() |
| zsunberg | 22:dae192ffca90 | 90 | { |
| zsunberg | 22:dae192ffca90 | 91 | led1 = 1; |
| Zachary Sunberg |
25:c4577daa425a | 92 | __disable_irq(); |
| zsunberg | 22:dae192ffca90 | 93 | pi.sensor_reading(sensors); |
| Zachary Sunberg |
25:c4577daa425a | 94 | __enable_irq(); |
| zsunberg | 22:dae192ffca90 | 95 | int* s = sensors; // just to make the next line more compact |
| zsunberg | 22:dae192ffca90 | 96 | xbee.printf("s:%i,%i,%i,%i,%i\n", s[0], s[1], s[2], s[3], s[4]); |
| zsunberg | 23:55f8b1abbf99 | 97 | __disable_irq(); |
| zsunberg | 22:dae192ffca90 | 98 | xbee.printf("p:%f\n", pi.line_position()); |
| Zachary Sunberg |
25:c4577daa425a | 99 | __enable_irq(); |
| zsunberg | 22:dae192ffca90 | 100 | xbee.printf("m:%d\n", mode); |
| zsunberg | 22:dae192ffca90 | 101 | led1 = 0; |
| zsunberg | 22:dae192ffca90 | 102 | } |
| Zachary Sunberg |
4:70fea94b29ae | 103 | |
| Zachary Sunberg |
25:c4577daa425a | 104 | void signal_comm(){ |
| Zachary Sunberg |
25:c4577daa425a | 105 | comm_time = true; |
| Zachary Sunberg |
25:c4577daa425a | 106 | } |
| Zachary Sunberg |
25:c4577daa425a | 107 | |
| zsunberg | 1:a7aab5937375 | 108 | int parse_command(const char* cmd) |
| zsunberg | 1:a7aab5937375 | 109 | { |
| zsunberg | 1:a7aab5937375 | 110 | if(cmd[1]==':'){ |
| Zachary Sunberg |
12:f96af1d15e9e | 111 | // left motor |
| Zachary Sunberg |
9:7f94942cd50d | 112 | if(cmd[0]=='l'){ |
| Zachary Sunberg |
9:7f94942cd50d | 113 | if(mode==MANUAL_MODE){ |
| Zachary Sunberg |
9:7f94942cd50d | 114 | leftspeed = atof(&cmd[2]); |
| Zachary Sunberg |
9:7f94942cd50d | 115 | } |
| Zachary Sunberg |
12:f96af1d15e9e | 116 | // right motor |
| Zachary Sunberg |
9:7f94942cd50d | 117 | }else if(cmd[0]=='r'){ |
| Zachary Sunberg |
9:7f94942cd50d | 118 | if(mode==MANUAL_MODE){ |
| Zachary Sunberg |
9:7f94942cd50d | 119 | rightspeed = atof(&cmd[2]); |
| Zachary Sunberg |
9:7f94942cd50d | 120 | } |
| Zachary Sunberg |
12:f96af1d15e9e | 121 | // mode |
| Zachary Sunberg |
4:70fea94b29ae | 122 | }else if(cmd[0]=='c'){ |
| Zachary Sunberg |
4:70fea94b29ae | 123 | mode = atoi(&cmd[2]); |
| zsunberg | 23:55f8b1abbf99 | 124 | xbee.printf("a:c:%d\n", mode);// acknowledge the mode change |
| zsunberg | 21:18b585a44155 | 125 | // xbee.printf("mode set to %d\n", mode); |
| Zachary Sunberg |
12:f96af1d15e9e | 126 | // gains |
| Zachary Sunberg |
12:f96af1d15e9e | 127 | }else if(cmd[0]=='g'){ |
| Zachary Sunberg |
12:f96af1d15e9e | 128 | if(cmd[2]=='p'){ |
| Zachary Sunberg |
12:f96af1d15e9e | 129 | k_p = atof(&cmd[4]); |
| Zachary Sunberg |
12:f96af1d15e9e | 130 | }else if(cmd[2]=='i'){ |
| Zachary Sunberg |
12:f96af1d15e9e | 131 | k_i = atof(&cmd[4]); |
| Zachary Sunberg |
12:f96af1d15e9e | 132 | }else if(cmd[2]=='d'){ |
| Zachary Sunberg |
12:f96af1d15e9e | 133 | k_d = atof(&cmd[4]); |
| Zachary Sunberg |
12:f96af1d15e9e | 134 | } |
| Zachary Sunberg |
16:94857d61e839 | 135 | xbee.printf("gains p:%f, i:%f, d:%f\n", k_p, k_i, k_d); |
| zsunberg | 23:55f8b1abbf99 | 136 | |
| zsunberg | 22:dae192ffca90 | 137 | // battery |
| zsunberg | 22:dae192ffca90 | 138 | }else if(cmd[0]=='b'){ |
| zsunberg | 23:55f8b1abbf99 | 139 | __disable_irq(); |
| zsunberg | 22:dae192ffca90 | 140 | xbee.printf("battery voltage: %f\n", pi.battery()); |
| zsunberg | 23:55f8b1abbf99 | 141 | __enable_irq(); |
| zsunberg | 1:a7aab5937375 | 142 | }else{ |
| zsunberg | 23:55f8b1abbf99 | 143 | xbee.printf("%s\n",cmd); |
| zsunberg | 1:a7aab5937375 | 144 | } |
| zsunberg | 1:a7aab5937375 | 145 | }else{ |
| zsunberg | 1:a7aab5937375 | 146 | xbee.printf("%s\n",cmd); |
| zsunberg | 1:a7aab5937375 | 147 | } |
| zsunberg | 1:a7aab5937375 | 148 | return 0; |
| zsunberg | 1:a7aab5937375 | 149 | } |
| zsunberg | 0:9f058fe8cab5 | 150 | |
| Zachary Sunberg |
25:c4577daa425a | 151 | void check_incoming(){ |
| zsunberg | 0:9f058fe8cab5 | 152 | led2 = 1; |
| zsunberg | 26:49945d96d461 | 153 | char read; |
| zsunberg | 26:49945d96d461 | 154 | |
| zsunberg | 0:9f058fe8cab5 | 155 | while(xbee.readable()){ |
| zsunberg | 1:a7aab5937375 | 156 | read = xbee.getc(); |
| zsunberg | 1:a7aab5937375 | 157 | if(read=='\n'){ |
| zsunberg | 1:a7aab5937375 | 158 | received[r_index]='\0'; // put a null character at the end |
| zsunberg | 1:a7aab5937375 | 159 | parse_command(received); |
| zsunberg | 1:a7aab5937375 | 160 | r_index=0; |
| zsunberg | 0:9f058fe8cab5 | 161 | } else { |
| zsunberg | 1:a7aab5937375 | 162 | if(r_index >= 80){ |
| zsunberg | 1:a7aab5937375 | 163 | r_index=0; |
| zsunberg | 1:a7aab5937375 | 164 | } |
| zsunberg | 1:a7aab5937375 | 165 | received[r_index]=read; |
| zsunberg | 1:a7aab5937375 | 166 | r_index++; |
| zsunberg | 0:9f058fe8cab5 | 167 | } |
| zsunberg | 0:9f058fe8cab5 | 168 | } |
| zsunberg | 1:a7aab5937375 | 169 | led2=0; |
| zsunberg | 0:9f058fe8cab5 | 170 | } |
| zsunberg | 0:9f058fe8cab5 | 171 | |
| zsunberg | 20:f0ca65974329 | 172 | void control() |
| zsunberg | 20:f0ca65974329 | 173 | { |
| zsunberg | 20:f0ca65974329 | 174 | if(mode==LINE_FOLLOW_MODE){ |
| zsunberg | 20:f0ca65974329 | 175 | line_follow_loop(); |
| zsunberg | 20:f0ca65974329 | 176 | } |
| Zachary Sunberg |
25:c4577daa425a | 177 | pi.left_motor(rightspeed); |
| Zachary Sunberg |
25:c4577daa425a | 178 | pi.right_motor(leftspeed); |
| zsunberg | 20:f0ca65974329 | 179 | } |
| zsunberg | 20:f0ca65974329 | 180 | |
| zsunberg | 0:9f058fe8cab5 | 181 | int main() { |
| zsunberg | 0:9f058fe8cab5 | 182 | |
| zsunberg | 26:49945d96d461 | 183 | // xbee.attach(&Rx_interrupt, Serial::RxIrq); |
| zsunberg | 0:9f058fe8cab5 | 184 | xbeeReset = 0; |
| zsunberg | 0:9f058fe8cab5 | 185 | wait(2); |
| zsunberg | 0:9f058fe8cab5 | 186 | xbeeReset = 1; |
| zsunberg | 1:a7aab5937375 | 187 | pi.sensor_auto_calibrate(); |
| zsunberg | 20:f0ca65974329 | 188 | leftspeed = 0.0; |
| zsunberg | 20:f0ca65974329 | 189 | rightspeed = 0.0; |
| zsunberg | 26:49945d96d461 | 190 | |
| zsunberg | 1:a7aab5937375 | 191 | r_index = 0; |
| zsunberg | 1:a7aab5937375 | 192 | received[0] = '\0'; |
| zsunberg | 7:860856cead84 | 193 | mode = MANUAL_MODE; |
| zsunberg | 20:f0ca65974329 | 194 | |
| Zachary Sunberg |
25:c4577daa425a | 195 | communication.attach(&signal_comm, 0.1); |
| zsunberg | 21:18b585a44155 | 196 | controls.attach(&control, 0.02); |
| zsunberg | 0:9f058fe8cab5 | 197 | |
| Zachary Sunberg |
25:c4577daa425a | 198 | // main loop |
| zsunberg | 0:9f058fe8cab5 | 199 | while(1){ |
| zsunberg | 20:f0ca65974329 | 200 | led3 = mode; |
| Zachary Sunberg |
8:9792935abfc3 | 201 | |
| Zachary Sunberg |
25:c4577daa425a | 202 | check_incoming(); |
| Zachary Sunberg |
25:c4577daa425a | 203 | |
| Zachary Sunberg |
25:c4577daa425a | 204 | if(comm_time){ |
| Zachary Sunberg |
25:c4577daa425a | 205 | communicate(); |
| Zachary Sunberg |
25:c4577daa425a | 206 | comm_time = false; |
| Zachary Sunberg |
25:c4577daa425a | 207 | } |
| Zachary Sunberg |
4:70fea94b29ae | 208 | |
| zsunberg | 0:9f058fe8cab5 | 209 | } |
| zsunberg | 0:9f058fe8cab5 | 210 | |
| zsunberg | 0:9f058fe8cab5 | 211 | return 0; |
| zsunberg | 0:9f058fe8cab5 | 212 | } |
