2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Tue Apr 09 20:41:22 2013 +0000
Revision:
22:6e3218cf75f8
Parent:
21:167dacfe0b14
Child:
23:5cfc4789e00b
MotorControl compiles

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