![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
vf
Dependencies: Servo ServoArm mbed
Fork of PES_Official by
Sources/main.cpp@11:292bdbd85a9c, 2017-05-06 (annotated)
- Committer:
- beacon
- Date:
- Sat May 06 13:33:23 2017 +0000
- Revision:
- 11:292bdbd85a9c
- Parent:
- 10:f76476943a6c
- Child:
- 12:c0bcb95885dd
Nenne dieses Programm "PES_Official" und l?sche das alte.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beacon | 0:d267b248eff4 | 1 | #include "mbed.h" |
beacon | 0:d267b248eff4 | 2 | #include "Robot.h" |
beacon | 4:67d7177c213f | 3 | #include "Declarations.h" |
beacon | 0:d267b248eff4 | 4 | |
beacon | 0:d267b248eff4 | 5 | #include <cstdlib> |
beacon | 0:d267b248eff4 | 6 | |
beacon | 4:67d7177c213f | 7 | //DistanceSensors related bottom: |
beacon | 0:d267b248eff4 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
beacon | 0:d267b248eff4 | 9 | |
beacon | 0:d267b248eff4 | 10 | AnalogIn sensorVoltage(PB_1); |
beacon | 0:d267b248eff4 | 11 | DigitalOut enable(PC_1); |
beacon | 0:d267b248eff4 | 12 | DigitalOut bit0(PH_1); |
beacon | 0:d267b248eff4 | 13 | DigitalOut bit1(PC_2); |
beacon | 0:d267b248eff4 | 14 | DigitalOut bit2(PC_3); |
beacon | 1:388c915756f5 | 15 | |
beacon | 4:67d7177c213f | 16 | //DistanceSensors top: |
beacon | 4:67d7177c213f | 17 | AnalogIn frontS(A1); |
beacon | 4:67d7177c213f | 18 | AnalogIn leftS(A2); |
beacon | 4:67d7177c213f | 19 | AnalogIn rightS(A3); |
beacon | 4:67d7177c213f | 20 | |
beacon | 1:388c915756f5 | 21 | //Leds related: |
beacon | 1:388c915756f5 | 22 | DigitalOut leds[6] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 }; |
beacon | 0:d267b248eff4 | 23 | |
beacon | 1:388c915756f5 | 24 | //motor related: |
beacon | 1:388c915756f5 | 25 | PwmOut left(PA_8); |
beacon | 1:388c915756f5 | 26 | PwmOut right(PA_9); |
beacon | 0:d267b248eff4 | 27 | |
beacon | 1:388c915756f5 | 28 | DigitalOut powerSignal(PB_2); |
beacon | 10:f76476943a6c | 29 | DigitalIn errorMotor(PB_14); |
beacon | 1:388c915756f5 | 30 | DigitalIn overtemperatur(PB_15); |
beacon | 1:388c915756f5 | 31 | |
beacon | 4:67d7177c213f | 32 | //Arm: |
beacon | 10:f76476943a6c | 33 | ServoArm servoArm(PA_6); |
beacon | 10:f76476943a6c | 34 | |
beacon | 10:f76476943a6c | 35 | //Greifer: |
beacon | 11:292bdbd85a9c | 36 | Servo servoGreifer(PB_7); |
beacon | 4:67d7177c213f | 37 | |
beacon | 1:388c915756f5 | 38 | //Farbsensor: |
beacon | 1:388c915756f5 | 39 | AnalogIn FarbVoltage(A0); |
beacon | 1:388c915756f5 | 40 | |
beacon | 10:f76476943a6c | 41 | Robot sam( &left, &right, &powerSignal, leds, &FarbVoltage, &frontS, &leftS, &rightS, &servoArm, &servoGreifer ); //Implement the Farbsensor into the Robot init function!! |
beacon | 10:f76476943a6c | 42 | |
beacon | 11:292bdbd85a9c | 43 | void initializeDistanceSensors() |
beacon | 11:292bdbd85a9c | 44 | { |
beacon | 10:f76476943a6c | 45 | for( int ii = 0; ii<9; ++ii) { |
beacon | 10:f76476943a6c | 46 | sam.sensors[ii].init(&sensorVoltage, &frontS, &leftS, &rightS, &bit0, &bit1, &bit2, ii); |
beacon | 10:f76476943a6c | 47 | |
beacon | 11:292bdbd85a9c | 48 | enable = 1; |
beacon | 10:f76476943a6c | 49 | } |
beacon | 10:f76476943a6c | 50 | } |
beacon | 1:388c915756f5 | 51 | |
beacon | 0:d267b248eff4 | 52 | |
beacon | 10:f76476943a6c | 53 | /* */ |
beacon | 11:292bdbd85a9c | 54 | int main() |
beacon | 11:292bdbd85a9c | 55 | { |
beacon | 4:67d7177c213f | 56 | initializeDistanceSensors(); //Initialises IR-Sensors. |
beacon | 11:292bdbd85a9c | 57 | //int counter = 0; //Counts how many times the robot has turned, before driving |
beacon | 4:67d7177c213f | 58 | int timer = 0; //Is used, to reset the robot. Returns time in [0.1s] |
beacon | 4:67d7177c213f | 59 | //int lastFun = -1; //Is used, to check if the same action in Robot::search() was made multiple times. |
beacon | 11:292bdbd85a9c | 60 | //int found = 0; //0:= no block available, 1 := a lego is ready to be picked up |
beacon | 11:292bdbd85a9c | 61 | //int done = 0; |
beacon | 11:292bdbd85a9c | 62 | |
beacon | 11:292bdbd85a9c | 63 | enum states { search = 0, forward, downward, down, upward, color, backwardDrop, readyDrop, backward }; |
beacon | 11:292bdbd85a9c | 64 | |
beacon | 11:292bdbd85a9c | 65 | int state = search; |
beacon | 11:292bdbd85a9c | 66 | |
beacon | 11:292bdbd85a9c | 67 | while( 1 ) { |
beacon | 10:f76476943a6c | 68 | |
beacon | 11:292bdbd85a9c | 69 | if ( timer > TIMEOUT ) { |
beacon | 4:67d7177c213f | 70 | NVIC_SystemReset(); //Resets Sam. |
beacon | 4:67d7177c213f | 71 | } |
beacon | 11:292bdbd85a9c | 72 | |
beacon | 11:292bdbd85a9c | 73 | switch( state ) { |
beacon | 11:292bdbd85a9c | 74 | case search: |
beacon | 11:292bdbd85a9c | 75 | if( sam.search(&timer) ){ |
beacon | 11:292bdbd85a9c | 76 | state = forward; |
beacon | 11:292bdbd85a9c | 77 | timer = 0; |
beacon | 11:292bdbd85a9c | 78 | } |
beacon | 11:292bdbd85a9c | 79 | |
beacon | 11:292bdbd85a9c | 80 | break; |
beacon | 11:292bdbd85a9c | 81 | |
beacon | 11:292bdbd85a9c | 82 | case forward: |
beacon | 11:292bdbd85a9c | 83 | if( sam.Arm.backToCollect() ){ |
beacon | 11:292bdbd85a9c | 84 | state = downward; |
beacon | 11:292bdbd85a9c | 85 | timer = 0; |
beacon | 11:292bdbd85a9c | 86 | } |
beacon | 11:292bdbd85a9c | 87 | |
beacon | 11:292bdbd85a9c | 88 | break; |
beacon | 11:292bdbd85a9c | 89 | |
beacon | 11:292bdbd85a9c | 90 | case downward: |
beacon | 11:292bdbd85a9c | 91 | if( sam.Arm.collectToDown() ){ |
beacon | 11:292bdbd85a9c | 92 | state = down; |
beacon | 11:292bdbd85a9c | 93 | timer = 0; |
beacon | 11:292bdbd85a9c | 94 | } |
beacon | 11:292bdbd85a9c | 95 | |
beacon | 11:292bdbd85a9c | 96 | break; |
beacon | 11:292bdbd85a9c | 97 | |
beacon | 11:292bdbd85a9c | 98 | case down: |
beacon | 11:292bdbd85a9c | 99 | if( sam.Greifer.take() ) { |
beacon | 11:292bdbd85a9c | 100 | state = upward; |
beacon | 11:292bdbd85a9c | 101 | timer = 0; |
beacon | 11:292bdbd85a9c | 102 | } |
beacon | 11:292bdbd85a9c | 103 | |
beacon | 11:292bdbd85a9c | 104 | break; |
beacon | 11:292bdbd85a9c | 105 | |
beacon | 11:292bdbd85a9c | 106 | case upward: |
beacon | 11:292bdbd85a9c | 107 | if( sam.Arm.downToCollect() ){ |
beacon | 11:292bdbd85a9c | 108 | state = color; |
beacon | 11:292bdbd85a9c | 109 | timer = 0; |
beacon | 11:292bdbd85a9c | 110 | } |
beacon | 11:292bdbd85a9c | 111 | |
beacon | 11:292bdbd85a9c | 112 | break; |
beacon | 11:292bdbd85a9c | 113 | |
beacon | 11:292bdbd85a9c | 114 | case color: { |
beacon | 11:292bdbd85a9c | 115 | int color = sam.FarbVoltage.read(); |
beacon | 11:292bdbd85a9c | 116 | |
beacon | 11:292bdbd85a9c | 117 | if( color == -1){ |
beacon | 11:292bdbd85a9c | 118 | //Do nothing |
beacon | 11:292bdbd85a9c | 119 | } |
beacon | 11:292bdbd85a9c | 120 | |
beacon | 11:292bdbd85a9c | 121 | else if( color == 0 || color == GREEN ){ |
beacon | 11:292bdbd85a9c | 122 | state = backwardDrop; |
beacon | 11:292bdbd85a9c | 123 | timer = 0; |
beacon | 11:292bdbd85a9c | 124 | } |
beacon | 11:292bdbd85a9c | 125 | |
beacon | 11:292bdbd85a9c | 126 | else if( color == RED ){ |
beacon | 11:292bdbd85a9c | 127 | state = readyDrop; |
beacon | 11:292bdbd85a9c | 128 | timer = 0; |
beacon | 11:292bdbd85a9c | 129 | } |
beacon | 11:292bdbd85a9c | 130 | |
beacon | 11:292bdbd85a9c | 131 | else{ |
beacon | 11:292bdbd85a9c | 132 | //Shit... |
beacon | 11:292bdbd85a9c | 133 | } |
beacon | 11:292bdbd85a9c | 134 | break; |
beacon | 11:292bdbd85a9c | 135 | } |
beacon | 11:292bdbd85a9c | 136 | |
beacon | 11:292bdbd85a9c | 137 | case backwardDrop: |
beacon | 11:292bdbd85a9c | 138 | if( sam.Arm.collectToBack() ){ |
beacon | 11:292bdbd85a9c | 139 | state = readyDrop; |
beacon | 11:292bdbd85a9c | 140 | timer = 0; |
beacon | 11:292bdbd85a9c | 141 | } |
beacon | 11:292bdbd85a9c | 142 | |
beacon | 11:292bdbd85a9c | 143 | break; |
beacon | 11:292bdbd85a9c | 144 | |
beacon | 11:292bdbd85a9c | 145 | case readyDrop: |
beacon | 11:292bdbd85a9c | 146 | if( sam.Greifer.leave() ){ |
beacon | 11:292bdbd85a9c | 147 | state = backward; |
beacon | 11:292bdbd85a9c | 148 | timer = 0; |
beacon | 11:292bdbd85a9c | 149 | } |
beacon | 11:292bdbd85a9c | 150 | |
beacon | 11:292bdbd85a9c | 151 | break; |
beacon | 11:292bdbd85a9c | 152 | |
beacon | 11:292bdbd85a9c | 153 | case backward: |
beacon | 11:292bdbd85a9c | 154 | if( sam.Arm.collectToBack() ){ |
beacon | 11:292bdbd85a9c | 155 | state = search; |
beacon | 11:292bdbd85a9c | 156 | timer = 0; |
beacon | 11:292bdbd85a9c | 157 | } |
beacon | 11:292bdbd85a9c | 158 | |
beacon | 11:292bdbd85a9c | 159 | break; |
beacon | 4:67d7177c213f | 160 | } |
beacon | 1:388c915756f5 | 161 | |
beacon | 11:292bdbd85a9c | 162 | timer++; |
beacon | 11:292bdbd85a9c | 163 | wait(0.1f); |
beacon | 11:292bdbd85a9c | 164 | } |
beacon | 11:292bdbd85a9c | 165 | |
beacon | 11:292bdbd85a9c | 166 | return 0; |
beacon | 11:292bdbd85a9c | 167 | } |
beacon | 11:292bdbd85a9c | 168 | |
beacon | 11:292bdbd85a9c | 169 | |
beacon | 11:292bdbd85a9c | 170 | |
beacon | 11:292bdbd85a9c | 171 | /* |
beacon | 11:292bdbd85a9c | 172 | if (sam.search(&counter, &timer)) { |
beacon | 11:292bdbd85a9c | 173 | |
beacon | 11:292bdbd85a9c | 174 | while(!done) { |
beacon | 11:292bdbd85a9c | 175 | sam.Arm.collectToDown(&done); |
beacon | 11:292bdbd85a9c | 176 | wait(0.1f); |
beacon | 11:292bdbd85a9c | 177 | } |
beacon | 11:292bdbd85a9c | 178 | done = 0; |
beacon | 11:292bdbd85a9c | 179 | |
beacon | 11:292bdbd85a9c | 180 | sam.Greifer.take(); |
beacon | 11:292bdbd85a9c | 181 | |
beacon | 11:292bdbd85a9c | 182 | while(!done) { |
beacon | 11:292bdbd85a9c | 183 | sam.Arm.downToCollect(&done); |
beacon | 11:292bdbd85a9c | 184 | sam.leds[1] = 1; |
beacon | 11:292bdbd85a9c | 185 | wait(0.1f); |
beacon | 11:292bdbd85a9c | 186 | } |
beacon | 11:292bdbd85a9c | 187 | done = 0; |
beacon | 11:292bdbd85a9c | 188 | |
beacon | 11:292bdbd85a9c | 189 | sam.Greifer.leave(); |
beacon | 11:292bdbd85a9c | 190 | |
beacon | 11:292bdbd85a9c | 191 | found = 0; |
beacon | 11:292bdbd85a9c | 192 | } |
beacon | 11:292bdbd85a9c | 193 | |
beacon | 11:292bdbd85a9c | 194 | |
beacon | 1:388c915756f5 | 195 | wait( 0.1f ); |
beacon | 0:d267b248eff4 | 196 | } |
beacon | 0:d267b248eff4 | 197 | return 0; |
beacon | 4:67d7177c213f | 198 | } |
beacon | 11:292bdbd85a9c | 199 | */ |
beacon | 4:67d7177c213f | 200 | |
beacon | 4:67d7177c213f | 201 | /* * / |
beacon | 4:67d7177c213f | 202 | int main(){ |
beacon | 11:292bdbd85a9c | 203 | |
beacon | 4:67d7177c213f | 204 | initializeDistanceSensors(); |
beacon | 4:67d7177c213f | 205 | sam.stop(); |
beacon | 11:292bdbd85a9c | 206 | |
beacon | 4:67d7177c213f | 207 | while ( 1 ){ |
beacon | 4:67d7177c213f | 208 | for (int i=0; i<6; i++){ |
beacon | 4:67d7177c213f | 209 | sam.sensors[i] < NEAR ? sam.leds[i] = 1 : sam.leds[i] = 0; |
beacon | 4:67d7177c213f | 210 | } |
beacon | 11:292bdbd85a9c | 211 | |
beacon | 4:67d7177c213f | 212 | sam.turnLeftS(); |
beacon | 11:292bdbd85a9c | 213 | |
beacon | 4:67d7177c213f | 214 | while (sam.sensors[FWD] < NEAR){ |
beacon | 4:67d7177c213f | 215 | wait(0.05f); |
beacon | 4:67d7177c213f | 216 | sam.stop(); |
beacon | 4:67d7177c213f | 217 | } |
beacon | 11:292bdbd85a9c | 218 | |
beacon | 4:67d7177c213f | 219 | } |
beacon | 4:67d7177c213f | 220 | return 0; |
beacon | 4:67d7177c213f | 221 | } |
beacon | 4:67d7177c213f | 222 | /* */ |
beacon | 4:67d7177c213f | 223 | |
beacon | 4:67d7177c213f | 224 | /* * / |
beacon | 4:67d7177c213f | 225 | int main(){ |
beacon | 4:67d7177c213f | 226 | for(float p=0; p<1.0; p += 0.1) { |
beacon | 4:67d7177c213f | 227 | // arm = p; |
beacon | 4:67d7177c213f | 228 | wait(0.2); |
beacon | 4:67d7177c213f | 229 | } |
beacon | 4:67d7177c213f | 230 | } |
beacon | 6:ba26dd3251b3 | 231 | /* */ |
beacon | 6:ba26dd3251b3 | 232 | |
beacon | 10:f76476943a6c | 233 | /* * / |
beacon | 6:ba26dd3251b3 | 234 | int main(){ |
beacon | 6:ba26dd3251b3 | 235 | sam.stop(); |
beacon | 6:ba26dd3251b3 | 236 | int done = 0; //1:= finished process; 0:= not finished |
beacon | 6:ba26dd3251b3 | 237 | int fun = 0; //just to test. |
beacon | 11:292bdbd85a9c | 238 | int start = 0; |
beacon | 11:292bdbd85a9c | 239 | |
beacon | 11:292bdbd85a9c | 240 | |
beacon | 6:ba26dd3251b3 | 241 | while(1){ |
beacon | 6:ba26dd3251b3 | 242 | if(fun == 0){ |
beacon | 6:ba26dd3251b3 | 243 | done = 0; |
beacon | 6:ba26dd3251b3 | 244 | sam.Arm.collecttoback(&done); |
beacon | 11:292bdbd85a9c | 245 | done == 0 ? fun = 0 : fun = 1; |
beacon | 6:ba26dd3251b3 | 246 | } |
beacon | 6:ba26dd3251b3 | 247 | else if(fun == 1){ |
beacon | 6:ba26dd3251b3 | 248 | done = 0; |
beacon | 6:ba26dd3251b3 | 249 | sam.Arm.backtocollect(&done); |
beacon | 6:ba26dd3251b3 | 250 | done == 0 ? fun = 1 : fun = 2; |
beacon | 6:ba26dd3251b3 | 251 | } |
beacon | 6:ba26dd3251b3 | 252 | else if(fun == 2){ |
beacon | 6:ba26dd3251b3 | 253 | done = 0; |
beacon | 6:ba26dd3251b3 | 254 | sam.Arm.collecttodown(&done); |
beacon | 6:ba26dd3251b3 | 255 | done == 0 ? fun = 2 : fun = 3; |
beacon | 6:ba26dd3251b3 | 256 | } |
beacon | 6:ba26dd3251b3 | 257 | else if(fun == 3){ |
beacon | 6:ba26dd3251b3 | 258 | done = 0; |
beacon | 6:ba26dd3251b3 | 259 | sam.Arm.downtocollect(&done); |
beacon | 6:ba26dd3251b3 | 260 | done == 0 ? fun = 3 : fun = 0; |
beacon | 6:ba26dd3251b3 | 261 | } |
beacon | 6:ba26dd3251b3 | 262 | wait(0.1); |
beacon | 6:ba26dd3251b3 | 263 | } |
beacon | 10:f76476943a6c | 264 | } |
beacon | 10:f76476943a6c | 265 | |
beacon | 10:f76476943a6c | 266 | */ |