ICRS Eurobot 2013

Dependencies:   mbed mbed-rtos Servo QEI

Committer:
twighk
Date:
Fri Apr 05 17:47:06 2013 +0000
Revision:
11:bbddc908c78c
Parent:
10:1f0cf0182067
Child:
12:d4b5851742a3
Child:
13:76c9915db820
neater;

Who changed what in which revision?

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