2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Tue Apr 09 15:33:36 2013 +0000
Revision:
20:70d651156779
Parent:
19:4b993a9a156e
Child:
21:167dacfe0b14
Child:
24:50805ef8c499
Child:
43:c592bf6a6a2d
Predict loop running, update loop not done.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
madcowswe 20:70d651156779 1 #include "globals.h"
madcowswe 20:70d651156779 2 #include "Kalman.h"
twighk 0:200635fa1b08 3 #include "mbed.h"
twighk 13:d4b5851742a3 4 #include "rtos.h"
twighk 4:1be0f6c6ceae 5 #include "Actuators/Arms/Arm.h"
twighk 1:8119211eae14 6 #include "Actuators/MainMotors/MainMotor.h"
twighk 1:8119211eae14 7 #include "Sensors/Encoders/Encoder.h"
twighk 4:1be0f6c6ceae 8 #include "Sensors/Colour/Colour.h"
twighk 8:69bdf20cb525 9 #include "Sensors/CakeSensor/CakeSensor.h"
twighk 13:d4b5851742a3 10 #include "Processes/Printing/Printing.h"
madcowswe 20:70d651156779 11 #include "coprocserial.h"
madcowswe 12:76c9915db820 12 #include <algorithm>
twighk 0:200635fa1b08 13
madcowswe 20:70d651156779 14 pos beaconpos[] = {{0,1}, {3,0}, {3,2}};
madcowswe 20:70d651156779 15
twighk 0:200635fa1b08 16 void motortest();
twighk 0:200635fa1b08 17 void encodertest();
twighk 0:200635fa1b08 18 void motorencodetest();
twighk 0:200635fa1b08 19 void motorencodetestline();
twighk 0:200635fa1b08 20 void motorsandservostest();
twighk 1:8119211eae14 21 void armtest();
twighk 2:45da48fab346 22 void motortestline();
twighk 3:717de74f6ebd 23 void ledtest();
twighk 3:717de74f6ebd 24 void phototransistortest();
twighk 3:717de74f6ebd 25 void ledphototransistortest();
twighk 3:717de74f6ebd 26 void colourtest();
twighk 8:69bdf20cb525 27 void cakesensortest();
twighk 13:d4b5851742a3 28 void printingtestthread(void const*);
twighk 13:d4b5851742a3 29 void printingtestthread2(void const*);
madcowswe 12:76c9915db820 30 void feedbacktest();
twighk 0:200635fa1b08 31
twighk 0:200635fa1b08 32 int main() {
twighk 2:45da48fab346 33
twighk 3:717de74f6ebd 34 /*****************
twighk 3:717de74f6ebd 35 * Test Code *
twighk 3:717de74f6ebd 36 *****************/
twighk 0:200635fa1b08 37 //motortest();
twighk 0:200635fa1b08 38 //encodertest();
twighk 8:69bdf20cb525 39 //motorencodetest();
twighk 1:8119211eae14 40 //motorencodetestline();
twighk 0:200635fa1b08 41 //motorsandservostest();
twighk 3:717de74f6ebd 42 //armtest();
twighk 2:45da48fab346 43 //motortestline();
twighk 11:bbddc908c78c 44 //ledtest();
twighk 3:717de74f6ebd 45 //phototransistortest();
madcowswe 12:76c9915db820 46 //ledphototransistortest();
madcowswe 5:56a5fdd373c9 47 //colourtest(); // Red SnR too low
madcowswe 12:76c9915db820 48 //cakesensortest();
madcowswe 20:70d651156779 49 //feedbacktest();
madcowswe 15:9c5aaeda36dc 50
madcowswe 20:70d651156779 51 /*
twighk 13:d4b5851742a3 52 DigitalOut l1(LED1);
twighk 13:d4b5851742a3 53 Thread p(printingThread, NULL, osPriorityNormal, 2048);
twighk 13:d4b5851742a3 54 l1=1;
twighk 13:d4b5851742a3 55 Thread a(printingtestthread, NULL, osPriorityNormal, 1024);
twighk 13:d4b5851742a3 56 Thread b(printingtestthread2, NULL, osPriorityNormal, 1024);
twighk 13:d4b5851742a3 57 Thread::wait(osWaitForever);
madcowswe 15:9c5aaeda36dc 58 */
madcowswe 16:52250d8d8fce 59
madcowswe 20:70d651156779 60
madcowswe 20:70d651156779 61 InitSerial();
madcowswe 20:70d651156779 62 //while(1)
madcowswe 20:70d651156779 63 // printbuff();
madcowswe 20:70d651156779 64 wait(1);
madcowswe 20:70d651156779 65 Kalman::KalmanInit();
madcowswe 20:70d651156779 66
madcowswe 20:70d651156779 67 Thread predictthread(Kalman::predictloop, NULL, osPriorityNormal, 2084);//512); //temp 2k
madcowswe 20:70d651156779 68
madcowswe 20:70d651156779 69 Kalman::start_predict_ticker(&predictthread);
madcowswe 20:70d651156779 70 //Thread::wait(osWaitForever);
madcowswe 20:70d651156779 71 feedbacktest();
madcowswe 20:70d651156779 72
madcowswe 12:76c9915db820 73 }
madcowswe 12:76c9915db820 74
twighk 13:d4b5851742a3 75 #include <cstdlib>
twighk 13:d4b5851742a3 76 using namespace std;
twighk 13:d4b5851742a3 77
twighk 13:d4b5851742a3 78 void printingtestthread(void const*){
twighk 13:d4b5851742a3 79 const char ID = 1;
twighk 13:d4b5851742a3 80 float buffer[3] = {ID};
twighk 13:d4b5851742a3 81 registerID(ID,sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 82 while (true){
twighk 13:d4b5851742a3 83 for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){
madcowswe 20:70d651156779 84 buffer[i] = ID ;
twighk 13:d4b5851742a3 85 }
twighk 13:d4b5851742a3 86 updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 87 Thread::wait(200);
twighk 13:d4b5851742a3 88 }
twighk 13:d4b5851742a3 89 }
madcowswe 14:c638d4b9ee94 90
twighk 13:d4b5851742a3 91 void printingtestthread2(void const*){
twighk 13:d4b5851742a3 92 const char ID = 2;
twighk 13:d4b5851742a3 93 float buffer[5] = {ID};
twighk 13:d4b5851742a3 94 registerID(ID,sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 95 while (true){
twighk 13:d4b5851742a3 96 for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){
twighk 13:d4b5851742a3 97 buffer[i] = ID;
twighk 13:d4b5851742a3 98 }
twighk 13:d4b5851742a3 99 updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 100 Thread::wait(500);
twighk 13:d4b5851742a3 101 }
twighk 8:69bdf20cb525 102 }
twighk 8:69bdf20cb525 103
madcowswe 14:c638d4b9ee94 104
madcowswe 12:76c9915db820 105 void feedbacktest(){
madcowswe 20:70d651156779 106 //Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 12:76c9915db820 107 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
madcowswe 12:76c9915db820 108
madcowswe 20:70d651156779 109 Kalman::State state;
madcowswe 20:70d651156779 110
madcowswe 20:70d651156779 111 float Pgain = -0.01;
madcowswe 15:9c5aaeda36dc 112 float fwdspeed = -400/3.0f;
madcowswe 12:76c9915db820 113 Timer timer;
madcowswe 12:76c9915db820 114 timer.start();
madcowswe 12:76c9915db820 115
madcowswe 12:76c9915db820 116 while(true){
madcowswe 12:76c9915db820 117 float expecdist = fwdspeed * timer.read();
madcowswe 20:70d651156779 118 state = Kalman::getState();
madcowswe 20:70d651156779 119 float errleft = left_encoder.getTicks() - (expecdist);
madcowswe 20:70d651156779 120 float errright = right_encoder.getTicks() - expecdist;
madcowswe 12:76c9915db820 121
madcowswe 12:76c9915db820 122 mleft(max(min(errleft*Pgain, 0.4f), -0.4f));
madcowswe 12:76c9915db820 123 mright(max(min(errright*Pgain, 0.4f), -0.4f));
madcowswe 12:76c9915db820 124 }
twighk 8:69bdf20cb525 125 }
twighk 8:69bdf20cb525 126
twighk 8:69bdf20cb525 127 void cakesensortest(){
twighk 8:69bdf20cb525 128 wait(1);
madcowswe 20:70d651156779 129 printf("cakesensortest");
twighk 8:69bdf20cb525 130
twighk 11:bbddc908c78c 131 CakeSensor cs(P_COLOR_SENSOR_IN);
twighk 8:69bdf20cb525 132 while(true){
twighk 8:69bdf20cb525 133 wait(0.1);
madcowswe 20:70d651156779 134 printf("distance: %f\t %f\r\n", cs.Distance(),cs.Distanceincm());
twighk 8:69bdf20cb525 135 }
twighk 3:717de74f6ebd 136 }
twighk 3:717de74f6ebd 137
twighk 3:717de74f6ebd 138 void colourtest(){
madcowswe 7:4340355261f9 139 Colour c(P_COLOR_SENSOR_BLUE, P_COLOR_SENSOR_RED, P_COLOR_SENSOR_IN);
twighk 3:717de74f6ebd 140 c.Calibrate();
twighk 3:717de74f6ebd 141 while(true){
twighk 3:717de74f6ebd 142 wait(0.1);
twighk 3:717de74f6ebd 143 ColourEnum ce = c.getColour();
twighk 3:717de74f6ebd 144 switch(ce){
twighk 3:717de74f6ebd 145 case BLUE :
madcowswe 20:70d651156779 146 printf("BLUE\n\r");
twighk 3:717de74f6ebd 147 break;
twighk 3:717de74f6ebd 148 case RED:
madcowswe 20:70d651156779 149 printf("RED\n\r");
twighk 3:717de74f6ebd 150 break;
twighk 3:717de74f6ebd 151 case WHITE:
madcowswe 20:70d651156779 152 printf("WHITE\n\r");
twighk 3:717de74f6ebd 153 break;
twighk 3:717de74f6ebd 154 case INCONCLUSIVE:
madcowswe 20:70d651156779 155 printf("INCONCLUSIVE\n\r");
twighk 3:717de74f6ebd 156 break;
twighk 3:717de74f6ebd 157 default:
madcowswe 20:70d651156779 158 printf("BUG\n\r");
twighk 3:717de74f6ebd 159 }
twighk 2:45da48fab346 160 }
twighk 0:200635fa1b08 161
twighk 3:717de74f6ebd 162 }
twighk 3:717de74f6ebd 163
twighk 3:717de74f6ebd 164
twighk 3:717de74f6ebd 165 void ledphototransistortest(){
madcowswe 7:4340355261f9 166 DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED);
madcowswe 7:4340355261f9 167 AnalogIn pt(P_COLOR_SENSOR_IN);
twighk 3:717de74f6ebd 168 Serial pc(USBTX, USBRX);
twighk 3:717de74f6ebd 169
twighk 3:717de74f6ebd 170 while(true){
twighk 11:bbddc908c78c 171 blue = 0; red = 0;
twighk 11:bbddc908c78c 172 for(int i = 0; i != 5; i++){
twighk 11:bbddc908c78c 173 wait(0.1);
madcowswe 20:70d651156779 174 printf("Phototransistor Analog is (none): %f \n\r", pt.read());
twighk 11:bbddc908c78c 175 }
twighk 11:bbddc908c78c 176
madcowswe 7:4340355261f9 177 blue = 1; red = 0;
twighk 3:717de74f6ebd 178 for(int i = 0; i != 5; i++){
twighk 3:717de74f6ebd 179 wait(0.1);
madcowswe 20:70d651156779 180 printf("Phototransistor Analog is (blue): %f \n\r", pt.read());
twighk 3:717de74f6ebd 181 }
madcowswe 7:4340355261f9 182 blue = 0; red = 1;
twighk 3:717de74f6ebd 183 for(int i = 0; i != 5; i++){
twighk 3:717de74f6ebd 184 wait(0.1);
madcowswe 20:70d651156779 185 printf("Phototransistor Analog is (red ): %f \n\r", pt.read());
twighk 3:717de74f6ebd 186 }
twighk 11:bbddc908c78c 187 blue = 1; red = 1;
twighk 11:bbddc908c78c 188 for(int i = 0; i != 5; i++){
twighk 11:bbddc908c78c 189 wait(0.1);
madcowswe 20:70d651156779 190 printf("Phototransistor Analog is (both): %f \n\r", pt.read());
twighk 11:bbddc908c78c 191 }
twighk 3:717de74f6ebd 192 }
twighk 3:717de74f6ebd 193 }
twighk 3:717de74f6ebd 194
twighk 3:717de74f6ebd 195 void phototransistortest(){
madcowswe 7:4340355261f9 196 AnalogIn pt(P_COLOR_SENSOR_IN);
twighk 3:717de74f6ebd 197 while(true){
twighk 3:717de74f6ebd 198 wait(0.1);
madcowswe 20:70d651156779 199 printf("Phototransistor Analog is: %f \n\r", pt.read());
twighk 3:717de74f6ebd 200 }
twighk 3:717de74f6ebd 201
twighk 3:717de74f6ebd 202 }
twighk 3:717de74f6ebd 203
twighk 3:717de74f6ebd 204 void ledtest(){
madcowswe 7:4340355261f9 205 DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED);
twighk 3:717de74f6ebd 206 while(true){
madcowswe 7:4340355261f9 207 blue = 1; red = 0;
twighk 3:717de74f6ebd 208 wait(0.2);
madcowswe 7:4340355261f9 209 blue = 0; red = 1;
twighk 3:717de74f6ebd 210 wait(0.2);
twighk 3:717de74f6ebd 211
twighk 3:717de74f6ebd 212 }
twighk 3:717de74f6ebd 213 }
twighk 3:717de74f6ebd 214
twighk 1:8119211eae14 215 void armtest(){
twighk 3:717de74f6ebd 216 Arm white(p26), black(p25, false, 0.0005, 180);
twighk 3:717de74f6ebd 217 while(true){
twighk 1:8119211eae14 218 white(0);
twighk 1:8119211eae14 219 black(0);
twighk 1:8119211eae14 220 wait(1);
twighk 1:8119211eae14 221 white(1);
twighk 1:8119211eae14 222 black(1);
twighk 1:8119211eae14 223 wait(1);
twighk 1:8119211eae14 224 }
twighk 1:8119211eae14 225 }
twighk 1:8119211eae14 226
twighk 1:8119211eae14 227
twighk 0:200635fa1b08 228 void motorsandservostest(){
twighk 0:200635fa1b08 229 Encoder Eleft(p27, p28), Eright(p30, p29);
twighk 0:200635fa1b08 230 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 1:8119211eae14 231 Arm sTop(p25), sBottom(p26);
twighk 4:1be0f6c6ceae 232 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 233 const float speed = 0.0;
twighk 0:200635fa1b08 234 const float dspeed = 0.0;
twighk 0:200635fa1b08 235
twighk 0:200635fa1b08 236 Timer servoTimer;
twighk 0:200635fa1b08 237 mleft(speed); mright(speed);
twighk 0:200635fa1b08 238 servoTimer.start();
twighk 0:200635fa1b08 239 while (true){
madcowswe 20:70d651156779 240 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
madcowswe 20:70d651156779 241 if (Eleft.getTicks() < Eright.getTicks()){
twighk 0:200635fa1b08 242 mleft(speed);
twighk 0:200635fa1b08 243 mright(speed - dspeed);
twighk 0:200635fa1b08 244 } else {
twighk 0:200635fa1b08 245 mright(speed);
twighk 0:200635fa1b08 246 mleft(speed - dspeed);
twighk 0:200635fa1b08 247 }
twighk 0:200635fa1b08 248 if (servoTimer.read() < 1){
twighk 0:200635fa1b08 249 sTop.clockwise();
twighk 0:200635fa1b08 250 } else if (servoTimer.read() < 4) {
twighk 2:45da48fab346 251 sTop.halt();
twighk 0:200635fa1b08 252 } else if (servoTimer.read() < 5) {
twighk 0:200635fa1b08 253 sBottom.anticlockwise();
twighk 0:200635fa1b08 254 //Led=1;
twighk 0:200635fa1b08 255 } else if (servoTimer.read() < 6) {
twighk 0:200635fa1b08 256 sBottom.clockwise();
twighk 0:200635fa1b08 257 //Led=0;
twighk 0:200635fa1b08 258 } else if (servoTimer.read() < 7) {
twighk 2:45da48fab346 259 sBottom.halt();
twighk 0:200635fa1b08 260 }else {
twighk 0:200635fa1b08 261 sTop.anticlockwise();
twighk 0:200635fa1b08 262 }
twighk 0:200635fa1b08 263 if (servoTimer.read() >= 9) servoTimer.reset();
twighk 0:200635fa1b08 264 }
twighk 0:200635fa1b08 265 }
twighk 0:200635fa1b08 266
twighk 2:45da48fab346 267 void motortestline(){
twighk 2:45da48fab346 268 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 2:45da48fab346 269 const float speed = 0.2;
twighk 2:45da48fab346 270 mleft(speed); mright(speed);
twighk 2:45da48fab346 271 while(true) wait(1);
twighk 2:45da48fab346 272 }
twighk 2:45da48fab346 273
twighk 0:200635fa1b08 274 void motorencodetestline(){
madcowswe 12:76c9915db820 275 Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 12:76c9915db820 276 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
twighk 4:1be0f6c6ceae 277 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 278 const float speed = 0.2;
twighk 0:200635fa1b08 279 const float dspeed = 0.1;
twighk 0:200635fa1b08 280
twighk 0:200635fa1b08 281 mleft(speed); mright(speed);
twighk 0:200635fa1b08 282 while (true){
twighk 0:200635fa1b08 283 //left 27 cm = 113 -> 0.239 cm/pulse
twighk 0:200635fa1b08 284 //right 27 cm = 72 -> 0.375 cm/pulse
madcowswe 20:70d651156779 285 printf("Position is: %i \t %i \n\r", (int)(Eleft.getTicks()*0.239), (int)(Eright.getTicks()*0.375));
madcowswe 20:70d651156779 286 if (Eleft.getTicks()*0.239 < Eright.getTicks()*0.375){
twighk 0:200635fa1b08 287 mright(speed - dspeed);
twighk 0:200635fa1b08 288 } else {
twighk 0:200635fa1b08 289 mright(speed + dspeed);
twighk 0:200635fa1b08 290 }
twighk 0:200635fa1b08 291 }
twighk 0:200635fa1b08 292
twighk 0:200635fa1b08 293 }
twighk 0:200635fa1b08 294
twighk 0:200635fa1b08 295 void motorencodetest(){
madcowswe 7:4340355261f9 296 Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 7:4340355261f9 297 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
twighk 0:200635fa1b08 298 Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 299
twighk 0:200635fa1b08 300 const float speed = -0.3;
twighk 0:200635fa1b08 301 const int enc = -38;
twighk 0:200635fa1b08 302 while(true){
twighk 0:200635fa1b08 303 mleft(speed); mright(0);
madcowswe 20:70d651156779 304 while(Eleft.getTicks()>enc){
madcowswe 20:70d651156779 305 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
twighk 0:200635fa1b08 306 }
twighk 0:200635fa1b08 307 Eleft.reset(); Eright.reset();
twighk 0:200635fa1b08 308 mleft(0); mright(speed);
madcowswe 20:70d651156779 309 while(Eright.getTicks()>enc){
madcowswe 20:70d651156779 310 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
twighk 0:200635fa1b08 311 }
twighk 0:200635fa1b08 312 Eleft.reset(); Eright.reset();
twighk 0:200635fa1b08 313 }
twighk 0:200635fa1b08 314 }
twighk 0:200635fa1b08 315
twighk 0:200635fa1b08 316 void encodertest(){
madcowswe 15:9c5aaeda36dc 317 Encoder E1(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 15:9c5aaeda36dc 318 //Encoder E2(P_ENC_RIGHT_A, P_ENC_RIGHT_B);
twighk 0:200635fa1b08 319 Serial pc(USBTX, USBRX);
twighk 3:717de74f6ebd 320 while(true){
twighk 0:200635fa1b08 321 wait(0.1);
madcowswe 20:70d651156779 322 printf("Position is: %i \t %i \n\r", E1.getTicks(), 0);//E2.getTicks());
twighk 0:200635fa1b08 323 }
twighk 0:200635fa1b08 324
twighk 0:200635fa1b08 325 }
twighk 0:200635fa1b08 326 void motortest(){
twighk 0:200635fa1b08 327 MainMotor mright(p22,p21), mleft(p23,p24);
twighk 3:717de74f6ebd 328 while(true) {
twighk 0:200635fa1b08 329 wait(1);
twighk 0:200635fa1b08 330 mleft(0.8); mright(0.8);
twighk 0:200635fa1b08 331 wait(1);
twighk 0:200635fa1b08 332 mleft(-0.2); mright(0.2);
twighk 0:200635fa1b08 333 wait(1);
twighk 0:200635fa1b08 334 mleft(0); mright(0);
twighk 0:200635fa1b08 335 }
twighk 0:200635fa1b08 336 }