der Roboter
/
BlueToothStuff
WORK IN PROGRESS
Fork of ThursWork by
main.cpp@2:cc7b01c5a9b0, 2014-05-27 (annotated)
- Committer:
- satyson
- Date:
- Tue May 27 12:49:21 2014 +0000
- Revision:
- 2:cc7b01c5a9b0
- Parent:
- 1:6402638c6f6d
- Child:
- 3:bf9a0cd9750c
Work In Progress
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmpeter | 0:386c250325ce | 1 | #include "mbed.h" |
mmpeter | 0:386c250325ce | 2 | #include "m3pi_ng.h" |
mmpeter | 0:386c250325ce | 3 | #include "cmath" |
mmpeter | 0:386c250325ce | 4 | #include "iostream" |
satyson | 1:6402638c6f6d | 5 | #include "btbee.h" |
mmpeter | 0:386c250325ce | 6 | |
mmpeter | 0:386c250325ce | 7 | using namespace std; |
mmpeter | 0:386c250325ce | 8 | |
mmpeter | 0:386c250325ce | 9 | m3pi thinggy; |
satyson | 1:6402638c6f6d | 10 | btbee btbee; |
satyson | 1:6402638c6f6d | 11 | |
satyson | 1:6402638c6f6d | 12 | DigitalOut mbed_led[] = {(LED1), (LED2),(LED3), (LED4)}; |
satyson | 1:6402638c6f6d | 13 | DigitalOut m3pi_led[] = {(p13), (p14), (p15), (p16), (p17), (p18), (p19), (p20)}; |
satyson | 1:6402638c6f6d | 14 | DigitalIn m3pi_pb(p21); |
mmpeter | 0:386c250325ce | 15 | |
mmpeter | 0:386c250325ce | 16 | int main() { |
mmpeter | 0:386c250325ce | 17 | |
satyson | 1:6402638c6f6d | 18 | //..................................................................................................\\ |
satyson | 1:6402638c6f6d | 19 | //......BlueTooth Communication.....................................................................\\ |
satyson | 1:6402638c6f6d | 20 | //..................................................................................................\\ |
satyson | 1:6402638c6f6d | 21 | |
satyson | 1:6402638c6f6d | 22 | // initialization stuff //////////////////////////////////////////////////////////////////////////////////////////////////// |
satyson | 1:6402638c6f6d | 23 | thinggy.locate(0,1); |
satyson | 1:6402638c6f6d | 24 | btbee.reset(); |
satyson | 1:6402638c6f6d | 25 | for (int i = 0; i <4; i++) { |
satyson | 1:6402638c6f6d | 26 | mbed_led[i] = 0; |
satyson | 1:6402638c6f6d | 27 | } |
satyson | 1:6402638c6f6d | 28 | for (int i = 0; i <8; i++) { |
satyson | 1:6402638c6f6d | 29 | m3pi_led[i]=0; |
satyson | 1:6402638c6f6d | 30 | } |
satyson | 1:6402638c6f6d | 31 | m3pi_pb.mode(PullUp); // expected would be 1 when pb is pressed, 0 when not, opposite is the case |
satyson | 1:6402638c6f6d | 32 | |
satyson | 1:6402638c6f6d | 33 | // end initialization stuff //////////////////////////////////////////////////////////////////////////////////////////////// |
satyson | 1:6402638c6f6d | 34 | |
satyson | 1:6402638c6f6d | 35 | thinggy.locate(0,0); |
satyson | 1:6402638c6f6d | 36 | wait(0.3); |
satyson | 1:6402638c6f6d | 37 | |
satyson | 1:6402638c6f6d | 38 | thinggy.locate(0,0); |
satyson | 2:cc7b01c5a9b0 | 39 | thinggy.printf("%s","Tell Me"); |
satyson | 1:6402638c6f6d | 40 | thinggy.locate(0,1); |
satyson | 2:cc7b01c5a9b0 | 41 | thinggy.printf("%s"," Why "); |
satyson | 1:6402638c6f6d | 42 | |
satyson | 1:6402638c6f6d | 43 | // wait for the user to push P21, should be pressed when the bt link is established (green led "link") |
satyson | 1:6402638c6f6d | 44 | while(m3pi_pb) { |
satyson | 1:6402638c6f6d | 45 | m3pi_led[0]=!m3pi_led[0]; |
satyson | 1:6402638c6f6d | 46 | wait(0.1); |
satyson | 1:6402638c6f6d | 47 | } |
satyson | 1:6402638c6f6d | 48 | |
satyson | 1:6402638c6f6d | 49 | int iline=1; |
satyson | 1:6402638c6f6d | 50 | char arr_read[30]; // this should be long enough to store any reply coming in over bt. |
satyson | 1:6402638c6f6d | 51 | int chars_read; // number of chars read in a bt reply |
satyson | 2:cc7b01c5a9b0 | 52 | bool go = true; |
satyson | 2:cc7b01c5a9b0 | 53 | while (go) { |
satyson | 1:6402638c6f6d | 54 | // this writes "Line 001\n" to "Line 005\n" and then "end\n" to the btbee |
satyson | 1:6402638c6f6d | 55 | if ( btbee.writeable() ) { |
satyson | 1:6402638c6f6d | 56 | if (iline==6) { |
satyson | 1:6402638c6f6d | 57 | btbee.printf("Your Turn!\n"); |
satyson | 1:6402638c6f6d | 58 | iline++; |
satyson | 1:6402638c6f6d | 59 | }//if |
satyson | 1:6402638c6f6d | 60 | else { |
satyson | 1:6402638c6f6d | 61 | if (iline <6){ |
satyson | 1:6402638c6f6d | 62 | btbee.printf("Line %0.3d \n",iline); |
satyson | 1:6402638c6f6d | 63 | thinggy.locate(0,0); |
satyson | 1:6402638c6f6d | 64 | thinggy.printf("Sent %0.3d",iline); |
satyson | 1:6402638c6f6d | 65 | iline++; |
satyson | 1:6402638c6f6d | 66 | } |
satyson | 1:6402638c6f6d | 67 | }//else |
satyson | 1:6402638c6f6d | 68 | }//if_write |
satyson | 2:cc7b01c5a9b0 | 69 | go = false; |
satyson | 2:cc7b01c5a9b0 | 70 | } // while_true |
satyson | 2:cc7b01c5a9b0 | 71 | |
satyson | 2:cc7b01c5a9b0 | 72 | go = true; |
satyson | 2:cc7b01c5a9b0 | 73 | |
satyson | 2:cc7b01c5a9b0 | 74 | |
satyson | 2:cc7b01c5a9b0 | 75 | /* |
satyson | 2:cc7b01c5a9b0 | 76 | while(go){ |
satyson | 2:cc7b01c5a9b0 | 77 | // wait for an input |
satyson | 2:cc7b01c5a9b0 | 78 | while ( !btbee.readable() ) { |
satyson | 2:cc7b01c5a9b0 | 79 | wait(0.01); |
satyson | 2:cc7b01c5a9b0 | 80 | thinggy.locate(0,0); |
satyson | 2:cc7b01c5a9b0 | 81 | thinggy.printf("I'm waiting"); |
satyson | 2:cc7b01c5a9b0 | 82 | }//while_notReadable |
satyson | 2:cc7b01c5a9b0 | 83 | */ |
satyson | 1:6402638c6f6d | 84 | // check for answers after each write /not write |
satyson | 1:6402638c6f6d | 85 | while ( btbee.readable() ) { |
satyson | 1:6402638c6f6d | 86 | btbee.read_all(arr_read, 30, &chars_read ); |
satyson | 2:cc7b01c5a9b0 | 87 | thinggy.locate(0,0); |
satyson | 2:cc7b01c5a9b0 | 88 | thinggy.cls(); |
satyson | 2:cc7b01c5a9b0 | 89 | thinggy.printf("IGotMsg!"); |
satyson | 2:cc7b01c5a9b0 | 90 | if (arr_read[0] == 'l'){ |
satyson | 2:cc7b01c5a9b0 | 91 | thinggy.locate(0,1); |
satyson | 2:cc7b01c5a9b0 | 92 | thinggy.cls(); |
satyson | 2:cc7b01c5a9b0 | 93 | thinggy.printf("TurningL"); |
satyson | 2:cc7b01c5a9b0 | 94 | // add code to turn left |
satyson | 2:cc7b01c5a9b0 | 95 | } |
satyson | 2:cc7b01c5a9b0 | 96 | else if (arr_read[0] == 'r'){ |
satyson | 2:cc7b01c5a9b0 | 97 | thinggy.locate(0,1); |
satyson | 2:cc7b01c5a9b0 | 98 | thinggy.cls(); |
satyson | 2:cc7b01c5a9b0 | 99 | thinggy.printf("TurningR"); |
satyson | 2:cc7b01c5a9b0 | 100 | // add code to turn right |
satyson | 2:cc7b01c5a9b0 | 101 | } |
satyson | 2:cc7b01c5a9b0 | 102 | else if (arr_read[0] == 'f'){ |
satyson | 2:cc7b01c5a9b0 | 103 | thinggy.locate(0,1); |
satyson | 2:cc7b01c5a9b0 | 104 | thinggy.cls(); |
satyson | 2:cc7b01c5a9b0 | 105 | thinggy.printf("GoingFwd"); |
satyson | 2:cc7b01c5a9b0 | 106 | // add code to go forward |
satyson | 2:cc7b01c5a9b0 | 107 | } |
satyson | 2:cc7b01c5a9b0 | 108 | |
satyson | 2:cc7b01c5a9b0 | 109 | |
satyson | 2:cc7b01c5a9b0 | 110 | //thinggy.print(arr_read,chars_read); |
satyson | 1:6402638c6f6d | 111 | }//while_readable |
satyson | 2:cc7b01c5a9b0 | 112 | |
satyson | 1:6402638c6f6d | 113 | wait(0.1); |
satyson | 1:6402638c6f6d | 114 | }//while_true |
satyson | 1:6402638c6f6d | 115 | |
satyson | 1:6402638c6f6d | 116 | //..................................................................................................\\ |
satyson | 1:6402638c6f6d | 117 | //......End BlueTooth Communication.................................................................\\ |
satyson | 1:6402638c6f6d | 118 | //..................................................................................................\\ |
satyson | 1:6402638c6f6d | 119 | |
satyson | 1:6402638c6f6d | 120 | /* |
satyson | 1:6402638c6f6d | 121 | |
satyson | 1:6402638c6f6d | 122 | |
satyson | 1:6402638c6f6d | 123 | |
mmpeter | 0:386c250325ce | 124 | float speed = 0.25; |
mmpeter | 0:386c250325ce | 125 | float correction; |
mmpeter | 0:386c250325ce | 126 | float k = -0.3; |
mmpeter | 0:386c250325ce | 127 | int sensor[5]; |
mmpeter | 0:386c250325ce | 128 | int returned; |
mmpeter | 0:386c250325ce | 129 | thinggy.locate(0,1); |
mmpeter | 0:386c250325ce | 130 | thinggy.printf("Villan"); |
mmpeter | 0:386c250325ce | 131 | thinggy.locate(0,0); |
mmpeter | 0:386c250325ce | 132 | thinggy.printf("Pimpin"); |
mmpeter | 0:386c250325ce | 133 | |
satyson | 1:6402638c6f6d | 134 | |
satyson | 1:6402638c6f6d | 135 | |
mmpeter | 0:386c250325ce | 136 | wait(1.0); |
mmpeter | 0:386c250325ce | 137 | |
mmpeter | 0:386c250325ce | 138 | thinggy.sensor_auto_calibrate(); |
mmpeter | 0:386c250325ce | 139 | |
mmpeter | 0:386c250325ce | 140 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 141 | returned = (sensor[1] + sensor[2] + sensor[3])/3; |
mmpeter | 0:386c250325ce | 142 | |
mmpeter | 0:386c250325ce | 143 | |
mmpeter | 0:386c250325ce | 144 | |
mmpeter | 0:386c250325ce | 145 | while(1) { |
mmpeter | 0:386c250325ce | 146 | // change "if" to while |
mmpeter | 0:386c250325ce | 147 | while(returned < 220){ |
mmpeter | 0:386c250325ce | 148 | while(sensor[0] < sensor[4] && thinggy.line_position() != 0){ |
mmpeter | 0:386c250325ce | 149 | thinggy.left_motor(.2); |
mmpeter | 0:386c250325ce | 150 | thinggy.right_motor(-.2); |
mmpeter | 0:386c250325ce | 151 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 152 | } |
mmpeter | 0:386c250325ce | 153 | while(sensor[4] > sensor[0] && thinggy.line_position() != 0){ |
mmpeter | 0:386c250325ce | 154 | thinggy.left_motor(-.2); |
mmpeter | 0:386c250325ce | 155 | thinggy.right_motor(.2); |
mmpeter | 0:386c250325ce | 156 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 157 | } |
mmpeter | 0:386c250325ce | 158 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 159 | returned = (sensor[1] + sensor[2] + sensor[3])/3; |
mmpeter | 0:386c250325ce | 160 | } |
mmpeter | 0:386c250325ce | 161 | |
mmpeter | 0:386c250325ce | 162 | // Curves and straightaways |
mmpeter | 0:386c250325ce | 163 | while(returned > 220){ |
mmpeter | 0:386c250325ce | 164 | |
mmpeter | 0:386c250325ce | 165 | float position = thinggy.line_position(); |
mmpeter | 0:386c250325ce | 166 | correction = k*(position); |
mmpeter | 0:386c250325ce | 167 | |
mmpeter | 0:386c250325ce | 168 | // -1.0 is far left, 1.0 is far right, 0.0 in the middle |
mmpeter | 0:386c250325ce | 169 | |
mmpeter | 0:386c250325ce | 170 | //speed limiting for right motor |
mmpeter | 0:386c250325ce | 171 | if(speed + correction > 1){ |
mmpeter | 0:386c250325ce | 172 | thinggy.right_motor(0.6); |
mmpeter | 0:386c250325ce | 173 | thinggy.left_motor(speed-correction); |
mmpeter | 0:386c250325ce | 174 | } |
mmpeter | 0:386c250325ce | 175 | else if(speed - correction > 1){ |
mmpeter | 0:386c250325ce | 176 | thinggy.right_motor(speed+correction); |
mmpeter | 0:386c250325ce | 177 | thinggy.left_motor(0.6); |
mmpeter | 0:386c250325ce | 178 | } |
mmpeter | 0:386c250325ce | 179 | else{ |
mmpeter | 0:386c250325ce | 180 | thinggy.left_motor(speed-correction); |
mmpeter | 0:386c250325ce | 181 | thinggy.right_motor(speed+correction); |
mmpeter | 0:386c250325ce | 182 | } |
mmpeter | 0:386c250325ce | 183 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 184 | returned = (sensor[1] + sensor[2] + sensor[3])/3; |
mmpeter | 0:386c250325ce | 185 | } |
mmpeter | 0:386c250325ce | 186 | thinggy.calibrated_sensor(sensor); |
mmpeter | 0:386c250325ce | 187 | returned = (sensor[1] + sensor[2] + sensor[3])/3; |
mmpeter | 0:386c250325ce | 188 | } |
satyson | 1:6402638c6f6d | 189 | */ |