2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
main.cpp@10:1f0cf0182067, 2013-04-05 (annotated)
- Committer:
- twighk
- Date:
- Fri Apr 05 16:40:52 2013 +0000
- Revision:
- 10:1f0cf0182067
- Parent:
- 7:4340355261f9
- Parent:
- 8:69bdf20cb525
- Child:
- 11:bbddc908c78c
merge fix;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
twighk | 8:69bdf20cb525 | 1 | //#pragma Otime // Compiler Optimisations |
twighk | 0:200635fa1b08 | 2 | |
twighk | 0:200635fa1b08 | 3 | // Eurobot13 main.cpp |
twighk | 0:200635fa1b08 | 4 | |
madcowswe | 6:995b3679155f | 5 | |
madcowswe | 6:995b3679155f | 6 | |
madcowswe | 6:995b3679155f | 7 | /* |
madcowswe | 6:995b3679155f | 8 | PINOUT Sensors |
madcowswe | 6:995b3679155f | 9 | 5: RF:SDI |
madcowswe | 6:995b3679155f | 10 | 6 SDO |
madcowswe | 6:995b3679155f | 11 | 7 SCK |
madcowswe | 6:995b3679155f | 12 | 8 NCS |
madcowswe | 6:995b3679155f | 13 | 9 NIRQ |
madcowswe | 6:995b3679155f | 14 | 10-15 6 echo pins |
madcowswe | 6:995b3679155f | 15 | 16 trig |
madcowswe | 6:995b3679155f | 16 | 17 IRin |
madcowswe | 6:995b3679155f | 17 | 18-20 unused |
madcowswe | 6:995b3679155f | 18 | 21 stepper step |
madcowswe | 6:995b3679155f | 19 | 22-27 unused |
madcowswe | 6:995b3679155f | 20 | 28 Serial TX |
madcowswe | 6:995b3679155f | 21 | 29-30 unused |
madcowswe | 6:995b3679155f | 22 | |
madcowswe | 6:995b3679155f | 23 | |
madcowswe | 6:995b3679155f | 24 | PINOUT Main |
madcowswe | 6:995b3679155f | 25 | 5: Lower arm servo |
madcowswe | 6:995b3679155f | 26 | 6: Upper arm servo |
madcowswe | 6:995b3679155f | 27 | |
madcowswe | 6:995b3679155f | 28 | 14: Serial RX |
madcowswe | 6:995b3679155f | 29 | 15: Distance sensor |
madcowswe | 6:995b3679155f | 30 | |
madcowswe | 6:995b3679155f | 31 | 20: color sensor in |
madcowswe | 6:995b3679155f | 32 | 21-24: Motors PWM IN 1-4 |
madcowswe | 6:995b3679155f | 33 | 25-26: Encoders |
madcowswe | 6:995b3679155f | 34 | 27-28: Encoders |
madcowswe | 6:995b3679155f | 35 | 29: Color sensor RED LED |
madcowswe | 6:995b3679155f | 36 | 30: Color sensor BLUE LED |
madcowswe | 6:995b3679155f | 37 | |
madcowswe | 6:995b3679155f | 38 | */ |
madcowswe | 6:995b3679155f | 39 | |
madcowswe | 6:995b3679155f | 40 | #define P_SERVO_LOWER_ARM p5 |
madcowswe | 6:995b3679155f | 41 | #define P_SERVO_UPPER_ARM p6 |
madcowswe | 6:995b3679155f | 42 | |
madcowswe | 6:995b3679155f | 43 | #define P_SERIAL_RX p14 |
madcowswe | 6:995b3679155f | 44 | #define P_DISTANCE_SENSOR p15 |
madcowswe | 6:995b3679155f | 45 | |
madcowswe | 6:995b3679155f | 46 | #define P_COLOR_SENSOR_IN p20 |
madcowswe | 6:995b3679155f | 47 | |
madcowswe | 6:995b3679155f | 48 | #define P_MOT_RIGHT_A p21 |
madcowswe | 6:995b3679155f | 49 | #define P_MOT_RIGHT_B p22 |
madcowswe | 6:995b3679155f | 50 | #define P_MOT_LEFT_A p23 |
madcowswe | 6:995b3679155f | 51 | #define P_MOT_LEFT_B p24 |
madcowswe | 6:995b3679155f | 52 | |
madcowswe | 6:995b3679155f | 53 | #define P_ENC_RIGHT_A p28 |
madcowswe | 6:995b3679155f | 54 | #define P_ENC_RIGHT_B p27 |
madcowswe | 6:995b3679155f | 55 | #define P_ENC_LEFT_A p25 |
madcowswe | 6:995b3679155f | 56 | #define P_ENC_LEFT_B p26 |
madcowswe | 6:995b3679155f | 57 | |
madcowswe | 6:995b3679155f | 58 | #define P_COLOR_SENSOR_RED p29 |
madcowswe | 6:995b3679155f | 59 | #define P_COLOR_SENSOR_BLUE p30 |
madcowswe | 6:995b3679155f | 60 | |
madcowswe | 6:995b3679155f | 61 | |
madcowswe | 6:995b3679155f | 62 | |
twighk | 0:200635fa1b08 | 63 | #include "mbed.h" |
twighk | 3:717de74f6ebd | 64 | Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 65 | |
twighk | 4:1be0f6c6ceae | 66 | #include "Actuators/Arms/Arm.h" |
twighk | 1:8119211eae14 | 67 | #include "Actuators/MainMotors/MainMotor.h" |
twighk | 1:8119211eae14 | 68 | #include "Sensors/Encoders/Encoder.h" |
twighk | 4:1be0f6c6ceae | 69 | #include "Sensors/Colour/Colour.h" |
twighk | 8:69bdf20cb525 | 70 | #include "Sensors/CakeSensor/CakeSensor.h" |
twighk | 0:200635fa1b08 | 71 | |
twighk | 2:45da48fab346 | 72 | |
twighk | 0:200635fa1b08 | 73 | |
twighk | 0:200635fa1b08 | 74 | void motortest(); |
twighk | 0:200635fa1b08 | 75 | void encodertest(); |
twighk | 0:200635fa1b08 | 76 | void motorencodetest(); |
twighk | 0:200635fa1b08 | 77 | void motorencodetestline(); |
twighk | 0:200635fa1b08 | 78 | void motorsandservostest(); |
twighk | 1:8119211eae14 | 79 | void armtest(); |
twighk | 2:45da48fab346 | 80 | void motortestline(); |
twighk | 3:717de74f6ebd | 81 | void ledtest(); |
twighk | 3:717de74f6ebd | 82 | void phototransistortest(); |
twighk | 3:717de74f6ebd | 83 | void ledphototransistortest(); |
twighk | 3:717de74f6ebd | 84 | void colourtest(); |
twighk | 8:69bdf20cb525 | 85 | void cakesensortest(); |
twighk | 0:200635fa1b08 | 86 | |
twighk | 0:200635fa1b08 | 87 | int main() { |
twighk | 2:45da48fab346 | 88 | |
twighk | 3:717de74f6ebd | 89 | /***************** |
twighk | 3:717de74f6ebd | 90 | * Test Code * |
twighk | 3:717de74f6ebd | 91 | *****************/ |
twighk | 0:200635fa1b08 | 92 | //motortest(); |
twighk | 0:200635fa1b08 | 93 | //encodertest(); |
twighk | 8:69bdf20cb525 | 94 | //motorencodetest(); |
twighk | 1:8119211eae14 | 95 | //motorencodetestline(); |
twighk | 0:200635fa1b08 | 96 | //motorsandservostest(); |
twighk | 3:717de74f6ebd | 97 | //armtest(); |
twighk | 2:45da48fab346 | 98 | //motortestline(); |
madcowswe | 7:4340355261f9 | 99 | ledtest(); |
twighk | 3:717de74f6ebd | 100 | //phototransistortest(); |
twighk | 3:717de74f6ebd | 101 | //ledphototransistortest(); |
madcowswe | 5:56a5fdd373c9 | 102 | //colourtest(); // Red SnR too low |
twighk | 8:69bdf20cb525 | 103 | cakesensortest(); |
twighk | 8:69bdf20cb525 | 104 | } |
twighk | 8:69bdf20cb525 | 105 | |
twighk | 8:69bdf20cb525 | 106 | void cakesensortest(){ |
twighk | 8:69bdf20cb525 | 107 | wait(1); |
twighk | 8:69bdf20cb525 | 108 | pc.printf("cakesensortest"); |
twighk | 8:69bdf20cb525 | 109 | |
twighk | 8:69bdf20cb525 | 110 | CakeSensor cs(p20); |
twighk | 8:69bdf20cb525 | 111 | while(true){ |
twighk | 8:69bdf20cb525 | 112 | wait(0.1); |
twighk | 8:69bdf20cb525 | 113 | pc.printf("distance: %f\t %f\r\n", cs.Distance(),cs.Distanceincm()); |
twighk | 8:69bdf20cb525 | 114 | } |
twighk | 3:717de74f6ebd | 115 | } |
twighk | 3:717de74f6ebd | 116 | |
twighk | 3:717de74f6ebd | 117 | void colourtest(){ |
madcowswe | 7:4340355261f9 | 118 | Colour c(P_COLOR_SENSOR_BLUE, P_COLOR_SENSOR_RED, P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 119 | c.Calibrate(); |
twighk | 3:717de74f6ebd | 120 | while(true){ |
twighk | 3:717de74f6ebd | 121 | wait(0.1); |
twighk | 3:717de74f6ebd | 122 | ColourEnum ce = c.getColour(); |
twighk | 3:717de74f6ebd | 123 | switch(ce){ |
twighk | 3:717de74f6ebd | 124 | case BLUE : |
twighk | 3:717de74f6ebd | 125 | pc.printf("BLUE\n\r"); |
twighk | 3:717de74f6ebd | 126 | break; |
twighk | 3:717de74f6ebd | 127 | case RED: |
twighk | 3:717de74f6ebd | 128 | pc.printf("RED\n\r"); |
twighk | 3:717de74f6ebd | 129 | break; |
twighk | 3:717de74f6ebd | 130 | case WHITE: |
twighk | 3:717de74f6ebd | 131 | pc.printf("WHITE\n\r"); |
twighk | 3:717de74f6ebd | 132 | break; |
twighk | 3:717de74f6ebd | 133 | case INCONCLUSIVE: |
twighk | 3:717de74f6ebd | 134 | pc.printf("INCONCLUSIVE\n\r"); |
twighk | 3:717de74f6ebd | 135 | break; |
twighk | 3:717de74f6ebd | 136 | default: |
twighk | 3:717de74f6ebd | 137 | pc.printf("BUG\n\r"); |
twighk | 3:717de74f6ebd | 138 | } |
twighk | 2:45da48fab346 | 139 | } |
twighk | 0:200635fa1b08 | 140 | |
twighk | 3:717de74f6ebd | 141 | } |
twighk | 3:717de74f6ebd | 142 | |
twighk | 3:717de74f6ebd | 143 | |
twighk | 3:717de74f6ebd | 144 | void ledphototransistortest(){ |
madcowswe | 7:4340355261f9 | 145 | DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED); |
madcowswe | 7:4340355261f9 | 146 | AnalogIn pt(P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 147 | Serial pc(USBTX, USBRX); |
twighk | 3:717de74f6ebd | 148 | |
twighk | 3:717de74f6ebd | 149 | while(true){ |
madcowswe | 7:4340355261f9 | 150 | blue = 1; red = 0; |
twighk | 3:717de74f6ebd | 151 | for(int i = 0; i != 5; i++){ |
twighk | 3:717de74f6ebd | 152 | wait(0.1); |
twighk | 3:717de74f6ebd | 153 | pc.printf("Phototransistor Analog is (blue): %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 154 | } |
madcowswe | 7:4340355261f9 | 155 | blue = 0; red = 1; |
twighk | 3:717de74f6ebd | 156 | for(int i = 0; i != 5; i++){ |
twighk | 3:717de74f6ebd | 157 | wait(0.1); |
twighk | 3:717de74f6ebd | 158 | pc.printf("Phototransistor Analog is (red ): %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 159 | } |
twighk | 3:717de74f6ebd | 160 | } |
twighk | 3:717de74f6ebd | 161 | } |
twighk | 3:717de74f6ebd | 162 | |
twighk | 3:717de74f6ebd | 163 | void phototransistortest(){ |
madcowswe | 7:4340355261f9 | 164 | AnalogIn pt(P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 165 | while(true){ |
twighk | 3:717de74f6ebd | 166 | wait(0.1); |
twighk | 3:717de74f6ebd | 167 | pc.printf("Phototransistor Analog is: %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 168 | } |
twighk | 3:717de74f6ebd | 169 | |
twighk | 3:717de74f6ebd | 170 | } |
twighk | 3:717de74f6ebd | 171 | |
twighk | 3:717de74f6ebd | 172 | void ledtest(){ |
madcowswe | 7:4340355261f9 | 173 | DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED); |
twighk | 3:717de74f6ebd | 174 | while(true){ |
madcowswe | 7:4340355261f9 | 175 | blue = 1; red = 0; |
twighk | 3:717de74f6ebd | 176 | wait(0.2); |
madcowswe | 7:4340355261f9 | 177 | blue = 0; red = 1; |
twighk | 3:717de74f6ebd | 178 | wait(0.2); |
twighk | 3:717de74f6ebd | 179 | |
twighk | 3:717de74f6ebd | 180 | } |
twighk | 3:717de74f6ebd | 181 | } |
twighk | 3:717de74f6ebd | 182 | |
twighk | 1:8119211eae14 | 183 | void armtest(){ |
twighk | 3:717de74f6ebd | 184 | Arm white(p26), black(p25, false, 0.0005, 180); |
twighk | 3:717de74f6ebd | 185 | while(true){ |
twighk | 1:8119211eae14 | 186 | white(0); |
twighk | 1:8119211eae14 | 187 | black(0); |
twighk | 1:8119211eae14 | 188 | wait(1); |
twighk | 1:8119211eae14 | 189 | white(1); |
twighk | 1:8119211eae14 | 190 | black(1); |
twighk | 1:8119211eae14 | 191 | wait(1); |
twighk | 1:8119211eae14 | 192 | } |
twighk | 1:8119211eae14 | 193 | } |
twighk | 1:8119211eae14 | 194 | |
twighk | 1:8119211eae14 | 195 | |
twighk | 0:200635fa1b08 | 196 | void motorsandservostest(){ |
twighk | 0:200635fa1b08 | 197 | Encoder Eleft(p27, p28), Eright(p30, p29); |
twighk | 0:200635fa1b08 | 198 | MainMotor mleft(p24,p23), mright(p21,p22); |
twighk | 1:8119211eae14 | 199 | Arm sTop(p25), sBottom(p26); |
twighk | 4:1be0f6c6ceae | 200 | //Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 201 | const float speed = 0.0; |
twighk | 0:200635fa1b08 | 202 | const float dspeed = 0.0; |
twighk | 0:200635fa1b08 | 203 | |
twighk | 0:200635fa1b08 | 204 | Timer servoTimer; |
twighk | 0:200635fa1b08 | 205 | mleft(speed); mright(speed); |
twighk | 0:200635fa1b08 | 206 | servoTimer.start(); |
twighk | 0:200635fa1b08 | 207 | while (true){ |
twighk | 0:200635fa1b08 | 208 | pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint()); |
twighk | 0:200635fa1b08 | 209 | if (Eleft.getPoint() < Eright.getPoint()){ |
twighk | 0:200635fa1b08 | 210 | mleft(speed); |
twighk | 0:200635fa1b08 | 211 | mright(speed - dspeed); |
twighk | 0:200635fa1b08 | 212 | } else { |
twighk | 0:200635fa1b08 | 213 | mright(speed); |
twighk | 0:200635fa1b08 | 214 | mleft(speed - dspeed); |
twighk | 0:200635fa1b08 | 215 | } |
twighk | 0:200635fa1b08 | 216 | if (servoTimer.read() < 1){ |
twighk | 0:200635fa1b08 | 217 | sTop.clockwise(); |
twighk | 0:200635fa1b08 | 218 | } else if (servoTimer.read() < 4) { |
twighk | 2:45da48fab346 | 219 | sTop.halt(); |
twighk | 0:200635fa1b08 | 220 | } else if (servoTimer.read() < 5) { |
twighk | 0:200635fa1b08 | 221 | sBottom.anticlockwise(); |
twighk | 0:200635fa1b08 | 222 | //Led=1; |
twighk | 0:200635fa1b08 | 223 | } else if (servoTimer.read() < 6) { |
twighk | 0:200635fa1b08 | 224 | sBottom.clockwise(); |
twighk | 0:200635fa1b08 | 225 | //Led=0; |
twighk | 0:200635fa1b08 | 226 | } else if (servoTimer.read() < 7) { |
twighk | 2:45da48fab346 | 227 | sBottom.halt(); |
twighk | 0:200635fa1b08 | 228 | }else { |
twighk | 0:200635fa1b08 | 229 | sTop.anticlockwise(); |
twighk | 0:200635fa1b08 | 230 | } |
twighk | 0:200635fa1b08 | 231 | if (servoTimer.read() >= 9) servoTimer.reset(); |
twighk | 0:200635fa1b08 | 232 | } |
twighk | 0:200635fa1b08 | 233 | } |
twighk | 0:200635fa1b08 | 234 | |
twighk | 2:45da48fab346 | 235 | void motortestline(){ |
twighk | 2:45da48fab346 | 236 | MainMotor mleft(p24,p23), mright(p21,p22); |
twighk | 2:45da48fab346 | 237 | const float speed = 0.2; |
twighk | 2:45da48fab346 | 238 | mleft(speed); mright(speed); |
twighk | 2:45da48fab346 | 239 | while(true) wait(1); |
twighk | 2:45da48fab346 | 240 | } |
twighk | 2:45da48fab346 | 241 | |
twighk | 0:200635fa1b08 | 242 | void motorencodetestline(){ |
twighk | 0:200635fa1b08 | 243 | Encoder Eleft(p27, p28), Eright(p30, p29); |
twighk | 0:200635fa1b08 | 244 | MainMotor mleft(p24,p23), mright(p21,p22); |
twighk | 4:1be0f6c6ceae | 245 | //Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 246 | const float speed = 0.2; |
twighk | 0:200635fa1b08 | 247 | const float dspeed = 0.1; |
twighk | 0:200635fa1b08 | 248 | |
twighk | 0:200635fa1b08 | 249 | mleft(speed); mright(speed); |
twighk | 0:200635fa1b08 | 250 | while (true){ |
twighk | 0:200635fa1b08 | 251 | //left 27 cm = 113 -> 0.239 cm/pulse |
twighk | 0:200635fa1b08 | 252 | //right 27 cm = 72 -> 0.375 cm/pulse |
twighk | 0:200635fa1b08 | 253 | pc.printf("Position is: %i \t %i \n\r", (int)(Eleft.getPoint()*0.239), (int)(Eright.getPoint()*0.375)); |
twighk | 0:200635fa1b08 | 254 | if (Eleft.getPoint()*0.239 < Eright.getPoint()*0.375){ |
twighk | 0:200635fa1b08 | 255 | mright(speed - dspeed); |
twighk | 0:200635fa1b08 | 256 | } else { |
twighk | 0:200635fa1b08 | 257 | mright(speed + dspeed); |
twighk | 0:200635fa1b08 | 258 | } |
twighk | 0:200635fa1b08 | 259 | } |
twighk | 0:200635fa1b08 | 260 | |
twighk | 0:200635fa1b08 | 261 | } |
twighk | 0:200635fa1b08 | 262 | |
twighk | 0:200635fa1b08 | 263 | void motorencodetest(){ |
madcowswe | 7:4340355261f9 | 264 | Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B); |
madcowswe | 7:4340355261f9 | 265 | MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B); |
twighk | 0:200635fa1b08 | 266 | Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 267 | |
twighk | 0:200635fa1b08 | 268 | const float speed = -0.3; |
twighk | 0:200635fa1b08 | 269 | const int enc = -38; |
twighk | 0:200635fa1b08 | 270 | while(true){ |
twighk | 0:200635fa1b08 | 271 | mleft(speed); mright(0); |
twighk | 0:200635fa1b08 | 272 | while(Eleft.getPoint()>enc){ |
twighk | 0:200635fa1b08 | 273 | pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint()); |
twighk | 0:200635fa1b08 | 274 | } |
twighk | 0:200635fa1b08 | 275 | Eleft.reset(); Eright.reset(); |
twighk | 0:200635fa1b08 | 276 | mleft(0); mright(speed); |
twighk | 0:200635fa1b08 | 277 | while(Eright.getPoint()>enc){ |
twighk | 0:200635fa1b08 | 278 | pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint()); |
twighk | 0:200635fa1b08 | 279 | } |
twighk | 0:200635fa1b08 | 280 | Eleft.reset(); Eright.reset(); |
twighk | 0:200635fa1b08 | 281 | } |
twighk | 0:200635fa1b08 | 282 | } |
twighk | 0:200635fa1b08 | 283 | |
twighk | 0:200635fa1b08 | 284 | void encodertest(){ |
twighk | 0:200635fa1b08 | 285 | Encoder E1(p28, p27); |
twighk | 0:200635fa1b08 | 286 | Encoder E2(p29, p30); |
twighk | 0:200635fa1b08 | 287 | Serial pc(USBTX, USBRX); |
twighk | 3:717de74f6ebd | 288 | while(true){ |
twighk | 0:200635fa1b08 | 289 | wait(0.1); |
twighk | 0:200635fa1b08 | 290 | pc.printf("Position is: %i \t %i \n\r", E1.getPoint(), E2.getPoint()); |
twighk | 0:200635fa1b08 | 291 | } |
twighk | 0:200635fa1b08 | 292 | |
twighk | 0:200635fa1b08 | 293 | } |
twighk | 0:200635fa1b08 | 294 | void motortest(){ |
twighk | 0:200635fa1b08 | 295 | MainMotor mright(p22,p21), mleft(p23,p24); |
twighk | 3:717de74f6ebd | 296 | while(true) { |
twighk | 0:200635fa1b08 | 297 | wait(1); |
twighk | 0:200635fa1b08 | 298 | mleft(0.8); mright(0.8); |
twighk | 0:200635fa1b08 | 299 | wait(1); |
twighk | 0:200635fa1b08 | 300 | mleft(-0.2); mright(0.2); |
twighk | 0:200635fa1b08 | 301 | wait(1); |
twighk | 0:200635fa1b08 | 302 | mleft(0); mright(0); |
twighk | 0:200635fa1b08 | 303 | } |
twighk | 0:200635fa1b08 | 304 | } |