Colour sensors calibrated

Dependencies:   mbed-rtos mbed Servo QEI

Fork of ICRSEurobot13 by Thomas Branch

Committer:
twighk
Date:
Wed Apr 03 17:54:53 2013 +0000
Revision:
4:1be0f6c6ceae
Parent:
3:717de74f6ebd
Child:
5:56a5fdd373c9
State on 3rd of april

Who changed what in which revision?

UserRevisionLine numberNew contents of line
twighk 3:717de74f6ebd 1 #pragma Otime // Compiler Optimisations
twighk 0:200635fa1b08 2
twighk 0:200635fa1b08 3 // Eurobot13 main.cpp
twighk 0:200635fa1b08 4
twighk 0:200635fa1b08 5 #include "mbed.h"
twighk 3:717de74f6ebd 6 Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 7
twighk 4:1be0f6c6ceae 8 #include "Actuators/Arms/Arm.h"
twighk 1:8119211eae14 9 #include "Actuators/MainMotors/MainMotor.h"
twighk 4:1be0f6c6ceae 10 #include "Others/EmergencyStop/EmergencyStop.h"
twighk 1:8119211eae14 11 #include "Sensors/Encoders/Encoder.h"
twighk 4:1be0f6c6ceae 12 #include "Sensors/Colour/Colour.h"
twighk 3:717de74f6ebd 13 #include "Sensors/Colour/Led.h"
twighk 3:717de74f6ebd 14 #include "Sensors/Colour/Phototransistor.h"
twighk 0:200635fa1b08 15
twighk 2:45da48fab346 16
twighk 0:200635fa1b08 17
twighk 0:200635fa1b08 18 void motortest();
twighk 0:200635fa1b08 19 void encodertest();
twighk 0:200635fa1b08 20 void motorencodetest();
twighk 0:200635fa1b08 21 void motorencodetestline();
twighk 0:200635fa1b08 22 void motorsandservostest();
twighk 1:8119211eae14 23 void armtest();
twighk 2:45da48fab346 24 void motortestline();
twighk 3:717de74f6ebd 25 void ledtest();
twighk 3:717de74f6ebd 26 void phototransistortest();
twighk 3:717de74f6ebd 27 void ledphototransistortest();
twighk 3:717de74f6ebd 28 void colourtest();
twighk 0:200635fa1b08 29
twighk 0:200635fa1b08 30 int main() {
twighk 3:717de74f6ebd 31 /* Setup EmergencyStop button for actuators.
twighk 3:717de74f6ebd 32 Derive all actuators from the pure virtual actuator class!
twighk 2:45da48fab346 33 */ EmergencyStop EStop(p8);
twighk 2:45da48fab346 34
twighk 3:717de74f6ebd 35 /*****************
twighk 3:717de74f6ebd 36 * Test Code *
twighk 3:717de74f6ebd 37 *****************/
twighk 0:200635fa1b08 38 //motortest();
twighk 0:200635fa1b08 39 //encodertest();
twighk 0:200635fa1b08 40 //motorencodetest();
twighk 1:8119211eae14 41 //motorencodetestline();
twighk 0:200635fa1b08 42 //motorsandservostest();
twighk 3:717de74f6ebd 43 //armtest();
twighk 2:45da48fab346 44 //motortestline();
twighk 3:717de74f6ebd 45 //ledtest();
twighk 3:717de74f6ebd 46 //phototransistortest();
twighk 3:717de74f6ebd 47 //ledphototransistortest();
twighk 3:717de74f6ebd 48 colourtest(); // Red SnR too low
twighk 3:717de74f6ebd 49 }
twighk 3:717de74f6ebd 50
twighk 3:717de74f6ebd 51 void colourtest(){
twighk 3:717de74f6ebd 52 Colour c(p9,p10,p20);
twighk 3:717de74f6ebd 53 c.Calibrate();
twighk 3:717de74f6ebd 54 while(true){
twighk 3:717de74f6ebd 55 wait(0.1);
twighk 3:717de74f6ebd 56 ColourEnum ce = c.getColour();
twighk 3:717de74f6ebd 57 switch(ce){
twighk 3:717de74f6ebd 58 case BLUE :
twighk 3:717de74f6ebd 59 pc.printf("BLUE\n\r");
twighk 3:717de74f6ebd 60 break;
twighk 3:717de74f6ebd 61 case RED:
twighk 3:717de74f6ebd 62 pc.printf("RED\n\r");
twighk 3:717de74f6ebd 63 break;
twighk 3:717de74f6ebd 64 case WHITE:
twighk 3:717de74f6ebd 65 pc.printf("WHITE\n\r");
twighk 3:717de74f6ebd 66 break;
twighk 3:717de74f6ebd 67 case INCONCLUSIVE:
twighk 3:717de74f6ebd 68 pc.printf("INCONCLUSIVE\n\r");
twighk 3:717de74f6ebd 69 break;
twighk 3:717de74f6ebd 70 default:
twighk 3:717de74f6ebd 71 pc.printf("BUG\n\r");
twighk 3:717de74f6ebd 72 }
twighk 2:45da48fab346 73 }
twighk 0:200635fa1b08 74
twighk 3:717de74f6ebd 75 }
twighk 3:717de74f6ebd 76
twighk 3:717de74f6ebd 77
twighk 3:717de74f6ebd 78 void ledphototransistortest(){
twighk 3:717de74f6ebd 79 Led blue(p9), red(p10);
twighk 3:717de74f6ebd 80 Phototransistor pt(p20);
twighk 3:717de74f6ebd 81 Serial pc(USBTX, USBRX);
twighk 3:717de74f6ebd 82
twighk 3:717de74f6ebd 83 while(true){
twighk 3:717de74f6ebd 84 blue.on();red.off();
twighk 3:717de74f6ebd 85 for(int i = 0; i != 5; i++){
twighk 3:717de74f6ebd 86 wait(0.1);
twighk 3:717de74f6ebd 87 pc.printf("Phototransistor Analog is (blue): %f \n\r", pt.read());
twighk 3:717de74f6ebd 88 }
twighk 3:717de74f6ebd 89 blue.off();red.on();
twighk 3:717de74f6ebd 90 for(int i = 0; i != 5; i++){
twighk 3:717de74f6ebd 91 wait(0.1);
twighk 3:717de74f6ebd 92 pc.printf("Phototransistor Analog is (red ): %f \n\r", pt.read());
twighk 3:717de74f6ebd 93 }
twighk 3:717de74f6ebd 94 }
twighk 3:717de74f6ebd 95 }
twighk 3:717de74f6ebd 96
twighk 3:717de74f6ebd 97 void phototransistortest(){
twighk 3:717de74f6ebd 98 Phototransistor pt(p20);
twighk 3:717de74f6ebd 99 while(true){
twighk 3:717de74f6ebd 100 wait(0.1);
twighk 3:717de74f6ebd 101 pc.printf("Phototransistor Analog is: %f \n\r", pt.read());
twighk 3:717de74f6ebd 102 }
twighk 3:717de74f6ebd 103
twighk 3:717de74f6ebd 104 }
twighk 3:717de74f6ebd 105
twighk 3:717de74f6ebd 106 void ledtest(){
twighk 3:717de74f6ebd 107 Led blue(p9), red(p10);
twighk 3:717de74f6ebd 108 while(true){
twighk 3:717de74f6ebd 109 blue.on();red.off();
twighk 3:717de74f6ebd 110 wait(0.2);
twighk 3:717de74f6ebd 111 blue.off();red.on();
twighk 3:717de74f6ebd 112 wait(0.2);
twighk 3:717de74f6ebd 113
twighk 3:717de74f6ebd 114 }
twighk 3:717de74f6ebd 115 }
twighk 3:717de74f6ebd 116
twighk 1:8119211eae14 117 void armtest(){
twighk 3:717de74f6ebd 118 Arm white(p26), black(p25, false, 0.0005, 180);
twighk 3:717de74f6ebd 119 while(true){
twighk 1:8119211eae14 120 white(0);
twighk 1:8119211eae14 121 black(0);
twighk 1:8119211eae14 122 wait(1);
twighk 1:8119211eae14 123 white(1);
twighk 1:8119211eae14 124 black(1);
twighk 1:8119211eae14 125 wait(1);
twighk 1:8119211eae14 126 }
twighk 1:8119211eae14 127 }
twighk 1:8119211eae14 128
twighk 1:8119211eae14 129
twighk 0:200635fa1b08 130 void motorsandservostest(){
twighk 0:200635fa1b08 131 Encoder Eleft(p27, p28), Eright(p30, p29);
twighk 0:200635fa1b08 132 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 1:8119211eae14 133 Arm sTop(p25), sBottom(p26);
twighk 4:1be0f6c6ceae 134 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 135 const float speed = 0.0;
twighk 0:200635fa1b08 136 const float dspeed = 0.0;
twighk 0:200635fa1b08 137
twighk 0:200635fa1b08 138 Timer servoTimer;
twighk 0:200635fa1b08 139 mleft(speed); mright(speed);
twighk 0:200635fa1b08 140 servoTimer.start();
twighk 0:200635fa1b08 141 while (true){
twighk 0:200635fa1b08 142 pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
twighk 0:200635fa1b08 143 if (Eleft.getPoint() < Eright.getPoint()){
twighk 0:200635fa1b08 144 mleft(speed);
twighk 0:200635fa1b08 145 mright(speed - dspeed);
twighk 0:200635fa1b08 146 } else {
twighk 0:200635fa1b08 147 mright(speed);
twighk 0:200635fa1b08 148 mleft(speed - dspeed);
twighk 0:200635fa1b08 149 }
twighk 0:200635fa1b08 150 if (servoTimer.read() < 1){
twighk 0:200635fa1b08 151 sTop.clockwise();
twighk 0:200635fa1b08 152 } else if (servoTimer.read() < 4) {
twighk 2:45da48fab346 153 sTop.halt();
twighk 0:200635fa1b08 154 } else if (servoTimer.read() < 5) {
twighk 0:200635fa1b08 155 sBottom.anticlockwise();
twighk 0:200635fa1b08 156 //Led=1;
twighk 0:200635fa1b08 157 } else if (servoTimer.read() < 6) {
twighk 0:200635fa1b08 158 sBottom.clockwise();
twighk 0:200635fa1b08 159 //Led=0;
twighk 0:200635fa1b08 160 } else if (servoTimer.read() < 7) {
twighk 2:45da48fab346 161 sBottom.halt();
twighk 0:200635fa1b08 162 }else {
twighk 0:200635fa1b08 163 sTop.anticlockwise();
twighk 0:200635fa1b08 164 }
twighk 0:200635fa1b08 165 if (servoTimer.read() >= 9) servoTimer.reset();
twighk 0:200635fa1b08 166 }
twighk 0:200635fa1b08 167 }
twighk 0:200635fa1b08 168
twighk 2:45da48fab346 169 void motortestline(){
twighk 2:45da48fab346 170 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 2:45da48fab346 171 const float speed = 0.2;
twighk 2:45da48fab346 172 mleft(speed); mright(speed);
twighk 2:45da48fab346 173 while(true) wait(1);
twighk 2:45da48fab346 174 }
twighk 2:45da48fab346 175
twighk 0:200635fa1b08 176 void motorencodetestline(){
twighk 0:200635fa1b08 177 Encoder Eleft(p27, p28), Eright(p30, p29);
twighk 0:200635fa1b08 178 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 4:1be0f6c6ceae 179 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 180 const float speed = 0.2;
twighk 0:200635fa1b08 181 const float dspeed = 0.1;
twighk 0:200635fa1b08 182
twighk 0:200635fa1b08 183 mleft(speed); mright(speed);
twighk 0:200635fa1b08 184 while (true){
twighk 0:200635fa1b08 185 //left 27 cm = 113 -> 0.239 cm/pulse
twighk 0:200635fa1b08 186 //right 27 cm = 72 -> 0.375 cm/pulse
twighk 0:200635fa1b08 187 pc.printf("Position is: %i \t %i \n\r", (int)(Eleft.getPoint()*0.239), (int)(Eright.getPoint()*0.375));
twighk 0:200635fa1b08 188 if (Eleft.getPoint()*0.239 < Eright.getPoint()*0.375){
twighk 0:200635fa1b08 189 mright(speed - dspeed);
twighk 0:200635fa1b08 190 } else {
twighk 0:200635fa1b08 191 mright(speed + dspeed);
twighk 0:200635fa1b08 192 }
twighk 0:200635fa1b08 193 }
twighk 0:200635fa1b08 194
twighk 0:200635fa1b08 195 }
twighk 0:200635fa1b08 196
twighk 0:200635fa1b08 197 void motorencodetest(){
twighk 0:200635fa1b08 198 Encoder Eleft(p28, p27), Eright(p29, p30);
twighk 0:200635fa1b08 199 MainMotor mleft(p23,p24), mright(p22,p21);
twighk 0:200635fa1b08 200 Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 201
twighk 0:200635fa1b08 202 const float speed = -0.3;
twighk 0:200635fa1b08 203 const int enc = -38;
twighk 0:200635fa1b08 204 while(true){
twighk 0:200635fa1b08 205 mleft(speed); mright(0);
twighk 0:200635fa1b08 206 while(Eleft.getPoint()>enc){
twighk 0:200635fa1b08 207 pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
twighk 0:200635fa1b08 208 }
twighk 0:200635fa1b08 209 Eleft.reset(); Eright.reset();
twighk 0:200635fa1b08 210 mleft(0); mright(speed);
twighk 0:200635fa1b08 211 while(Eright.getPoint()>enc){
twighk 0:200635fa1b08 212 pc.printf("Position is: %i \t %i \n\r", Eleft.getPoint(), Eright.getPoint());
twighk 0:200635fa1b08 213 }
twighk 0:200635fa1b08 214 Eleft.reset(); Eright.reset();
twighk 0:200635fa1b08 215 }
twighk 0:200635fa1b08 216 }
twighk 0:200635fa1b08 217
twighk 0:200635fa1b08 218 void encodertest(){
twighk 0:200635fa1b08 219 Encoder E1(p28, p27);
twighk 0:200635fa1b08 220 Encoder E2(p29, p30);
twighk 0:200635fa1b08 221 Serial pc(USBTX, USBRX);
twighk 3:717de74f6ebd 222 while(true){
twighk 0:200635fa1b08 223 wait(0.1);
twighk 0:200635fa1b08 224 pc.printf("Position is: %i \t %i \n\r", E1.getPoint(), E2.getPoint());
twighk 0:200635fa1b08 225 }
twighk 0:200635fa1b08 226
twighk 0:200635fa1b08 227 }
twighk 0:200635fa1b08 228 void motortest(){
twighk 0:200635fa1b08 229 MainMotor mright(p22,p21), mleft(p23,p24);
twighk 3:717de74f6ebd 230 while(true) {
twighk 0:200635fa1b08 231 wait(1);
twighk 0:200635fa1b08 232 mleft(0.8); mright(0.8);
twighk 0:200635fa1b08 233 wait(1);
twighk 0:200635fa1b08 234 mleft(-0.2); mright(0.2);
twighk 0:200635fa1b08 235 wait(1);
twighk 0:200635fa1b08 236 mleft(0); mright(0);
twighk 0:200635fa1b08 237 }
twighk 0:200635fa1b08 238 }