2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
rsavitski
Date:
Sat Apr 13 22:42:01 2013 +0000
Revision:
59:63609922579c
Parent:
51:bc261eae004b
Child:
65:4709ff6c753c
tmp

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>
rsavitski 24:50805ef8c499 13 #include "motion.h"
madcowswe 25:b16f1045108f 14 #include "MotorControl.h"
madcowswe 28:4e20b44251c6 15 #include "system.h"
rsavitski 30:791739422122 16 #include "ai.h"
madcowswe 20:70d651156779 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 colourtest();
twighk 8:69bdf20cb525 26 void cakesensortest();
twighk 13:d4b5851742a3 27 void printingtestthread(void const*);
twighk 13:d4b5851742a3 28 void printingtestthread2(void const*);
madcowswe 12:76c9915db820 29 void feedbacktest();
twighk 0:200635fa1b08 30
xiaxia686 43:c592bf6a6a2d 31 int main()
xiaxia686 43:c592bf6a6a2d 32 {
xiaxia686 43:c592bf6a6a2d 33
xiaxia686 43:c592bf6a6a2d 34 /*****************
xiaxia686 43:c592bf6a6a2d 35 * Test Code *
xiaxia686 43:c592bf6a6a2d 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();
rsavitski 59:63609922579c 42 armtest();
rsavitski 59:63609922579c 43 while(1);
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();
xiaxia686 43:c592bf6a6a2d 51
xiaxia686 43:c592bf6a6a2d 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 51:bc261eae004b 63 Serial pc(USBTX, USBRX);
madcowswe 25:b16f1045108f 64 pc.baud(115200);
xiaxia686 46:adcd57a5e402 65 wait(2);
xiaxia686 46:adcd57a5e402 66 InitSerial();
xiaxia686 46:adcd57a5e402 67 wait(3);
xiaxia686 46:adcd57a5e402 68 Kalman::KalmanInit();
madcowswe 20:70d651156779 69
xiaxia686 46:adcd57a5e402 70 Thread predictthread(Kalman::predictloop, NULL, osPriorityNormal, 2084);//512); //temp 2k
xiaxia686 46:adcd57a5e402 71 Kalman::start_predict_ticker(&predictthread);
madcowswe 20:70d651156779 72
xiaxia686 46:adcd57a5e402 73 Thread updatethread(Kalman::updateloop, NULL, osPriorityNormal, 2084);
rsavitski 24:50805ef8c499 74
xiaxia686 46:adcd57a5e402 75 Ticker motorcontroltestticker;
xiaxia686 46:adcd57a5e402 76 motorcontroltestticker.attach(MotorControl::motor_control_isr, 0.05);
rsavitski 30:791739422122 77 // ai layer thread
xiaxia686 46:adcd57a5e402 78 Thread aithread(AI::ailayer, NULL, osPriorityNormal, 2048);
madcowswe 22:6e3218cf75f8 79
rsavitski 24:50805ef8c499 80 // motion layer periodic callback
xiaxia686 46:adcd57a5e402 81 RtosTimer motion_timer(motion::motionlayer, osTimerPeriodic);
xiaxia686 46:adcd57a5e402 82 motion_timer.start(50);
xiaxia686 43:c592bf6a6a2d 83
xiaxia686 46:adcd57a5e402 84 Thread printingThread(Printing::printingloop, NULL, osPriorityLow, 2048);
xiaxia686 43:c592bf6a6a2d 85
xiaxia686 46:adcd57a5e402 86 measureCPUidle(); //repurpose thread for idle measurement
madcowswe 21:167dacfe0b14 87 Thread::wait(osWaitForever);
madcowswe 51:bc261eae004b 88
madcowswe 12:76c9915db820 89 }
madcowswe 12:76c9915db820 90
twighk 13:d4b5851742a3 91 #include <cstdlib>
twighk 13:d4b5851742a3 92 using namespace std;
twighk 13:d4b5851742a3 93
xiaxia686 43:c592bf6a6a2d 94 void printingtestthread(void const*)
xiaxia686 43:c592bf6a6a2d 95 {
twighk 13:d4b5851742a3 96 const char ID = 1;
twighk 13:d4b5851742a3 97 float buffer[3] = {ID};
madcowswe 21:167dacfe0b14 98 Printing::registerID(ID,sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 99 while (true){
twighk 13:d4b5851742a3 100 for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){
madcowswe 20:70d651156779 101 buffer[i] = ID ;
twighk 13:d4b5851742a3 102 }
madcowswe 21:167dacfe0b14 103 Printing::updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 104 Thread::wait(200);
twighk 13:d4b5851742a3 105 }
twighk 13:d4b5851742a3 106 }
madcowswe 14:c638d4b9ee94 107
xiaxia686 43:c592bf6a6a2d 108 void printingtestthread2(void const*)
xiaxia686 43:c592bf6a6a2d 109 {
twighk 13:d4b5851742a3 110 const char ID = 2;
twighk 13:d4b5851742a3 111 float buffer[5] = {ID};
madcowswe 21:167dacfe0b14 112 Printing::registerID(ID,sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 113 while (true){
twighk 13:d4b5851742a3 114 for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){
twighk 13:d4b5851742a3 115 buffer[i] = ID;
twighk 13:d4b5851742a3 116 }
madcowswe 21:167dacfe0b14 117 Printing::updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0]));
twighk 13:d4b5851742a3 118 Thread::wait(500);
twighk 13:d4b5851742a3 119 }
twighk 8:69bdf20cb525 120 }
twighk 8:69bdf20cb525 121
madcowswe 14:c638d4b9ee94 122
rsavitski 24:50805ef8c499 123 /*
madcowswe 12:76c9915db820 124 void feedbacktest(){
madcowswe 20:70d651156779 125 //Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 12:76c9915db820 126 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
xiaxia686 43:c592bf6a6a2d 127
madcowswe 20:70d651156779 128 Kalman::State state;
xiaxia686 43:c592bf6a6a2d 129
madcowswe 20:70d651156779 130 float Pgain = -0.01;
madcowswe 15:9c5aaeda36dc 131 float fwdspeed = -400/3.0f;
madcowswe 12:76c9915db820 132 Timer timer;
madcowswe 12:76c9915db820 133 timer.start();
xiaxia686 43:c592bf6a6a2d 134
xiaxia686 43:c592bf6a6a2d 135 while(true) {
madcowswe 12:76c9915db820 136 float expecdist = fwdspeed * timer.read();
madcowswe 20:70d651156779 137 state = Kalman::getState();
madcowswe 20:70d651156779 138 float errleft = left_encoder.getTicks() - (expecdist);
madcowswe 20:70d651156779 139 float errright = right_encoder.getTicks() - expecdist;
xiaxia686 43:c592bf6a6a2d 140
madcowswe 12:76c9915db820 141 mleft(max(min(errleft*Pgain, 0.4f), -0.4f));
madcowswe 12:76c9915db820 142 mright(max(min(errright*Pgain, 0.4f), -0.4f));
madcowswe 12:76c9915db820 143 }
twighk 8:69bdf20cb525 144 }
rsavitski 24:50805ef8c499 145 */
twighk 8:69bdf20cb525 146
xiaxia686 43:c592bf6a6a2d 147 void cakesensortest()
xiaxia686 43:c592bf6a6a2d 148 {
twighk 8:69bdf20cb525 149 wait(1);
madcowswe 20:70d651156779 150 printf("cakesensortest");
xiaxia686 43:c592bf6a6a2d 151
xiaxia686 43:c592bf6a6a2d 152 CakeSensor cs(P_DISTANCE_SENSOR);
xiaxia686 43:c592bf6a6a2d 153 while(true) {
twighk 8:69bdf20cb525 154 wait(0.1);
madcowswe 20:70d651156779 155 printf("distance: %f\t %f\r\n", cs.Distance(),cs.Distanceincm());
xiaxia686 43:c592bf6a6a2d 156 }
twighk 3:717de74f6ebd 157 }
twighk 3:717de74f6ebd 158
xiaxia686 43:c592bf6a6a2d 159 void colourtest()
xiaxia686 43:c592bf6a6a2d 160 {
xiaxia686 45:77cf6375348a 161 Colour c_upper(P_COLOR_SENSOR_BLUE_UPPER, P_COLOR_SENSOR_RED_UPPER, P_COLOR_SENSOR_IN_UPPER,UPPER);
xiaxia686 45:77cf6375348a 162 Colour c_lower(P_COLOR_SENSOR_BLUE_LOWER, P_COLOR_SENSOR_RED_LOWER, P_COLOR_SENSOR_IN_LOWER,LOWER);
xiaxia686 43:c592bf6a6a2d 163
xiaxia686 43:c592bf6a6a2d 164 while(true) {
twighk 3:717de74f6ebd 165 wait(0.1);
xiaxia686 43:c592bf6a6a2d 166
xiaxia686 45:77cf6375348a 167 switch(c_lower.getColour()) {
twighk 3:717de74f6ebd 168 case BLUE :
xiaxia686 43:c592bf6a6a2d 169 printf("BLUE\n");
twighk 3:717de74f6ebd 170 break;
twighk 3:717de74f6ebd 171 case RED:
xiaxia686 43:c592bf6a6a2d 172 printf("RED\n");
twighk 3:717de74f6ebd 173 break;
twighk 3:717de74f6ebd 174 case WHITE:
xiaxia686 43:c592bf6a6a2d 175 printf("WHITE\n");
twighk 3:717de74f6ebd 176 break;
xiaxia686 43:c592bf6a6a2d 177 case BLACK:
xiaxia686 43:c592bf6a6a2d 178 printf("BLACK\n");
twighk 3:717de74f6ebd 179 break;
twighk 3:717de74f6ebd 180 default:
xiaxia686 43:c592bf6a6a2d 181 printf("BUG\n");
twighk 3:717de74f6ebd 182 }
xiaxia686 43:c592bf6a6a2d 183
twighk 2:45da48fab346 184 }
twighk 0:200635fa1b08 185
twighk 3:717de74f6ebd 186 }
twighk 3:717de74f6ebd 187
madcowswe 51:bc261eae004b 188 /*
madcowswe 51:bc261eae004b 189
xiaxia686 43:c592bf6a6a2d 190 void pt_test()
xiaxia686 43:c592bf6a6a2d 191 {
xiaxia686 43:c592bf6a6a2d 192 DigitalOut _blue_led(p10);
xiaxia686 43:c592bf6a6a2d 193 DigitalOut _red_led(p11);
xiaxia686 45:77cf6375348a 194 AnalogIn _pt(p18);
xiaxia686 43:c592bf6a6a2d 195
xiaxia686 43:c592bf6a6a2d 196 bytepack_t datapackage;
xiaxia686 43:c592bf6a6a2d 197 // first 3 bytes of header is used for alignment
xiaxia686 43:c592bf6a6a2d 198 datapackage.data.header[0] = 0xFF;
xiaxia686 43:c592bf6a6a2d 199 datapackage.data.header[1] = 0xFF;
xiaxia686 43:c592bf6a6a2d 200 datapackage.data.header[2] = 0xFF;
xiaxia686 43:c592bf6a6a2d 201 while(true) {
xiaxia686 43:c592bf6a6a2d 202 //toggles leds for the next state
xiaxia686 43:c592bf6a6a2d 203 _blue_led = 1;
xiaxia686 43:c592bf6a6a2d 204 _red_led = 0;
xiaxia686 43:c592bf6a6a2d 205 wait(0.01);
xiaxia686 43:c592bf6a6a2d 206 volatile float blue_temp = _pt.read();
twighk 3:717de74f6ebd 207
xiaxia686 43:c592bf6a6a2d 208
xiaxia686 43:c592bf6a6a2d 209 datapackage.data.ID = (unsigned char)0;
xiaxia686 43:c592bf6a6a2d 210 datapackage.data.value = blue_temp;
xiaxia686 43:c592bf6a6a2d 211 datapackage.data.aux = 0;
xiaxia686 43:c592bf6a6a2d 212 for (int i = 0; i < sizeof(datapackage); i++) {
xiaxia686 43:c592bf6a6a2d 213 //mbed_serial.putc(datapackage.type_char[i]);
xiaxia686 43:c592bf6a6a2d 214 pc.putc(datapackage.type_char[i]);
twighk 3:717de74f6ebd 215 }
xiaxia686 43:c592bf6a6a2d 216
xiaxia686 43:c592bf6a6a2d 217 _blue_led = 0;
xiaxia686 43:c592bf6a6a2d 218 _red_led = 1;
xiaxia686 43:c592bf6a6a2d 219 wait(0.01);
xiaxia686 43:c592bf6a6a2d 220 volatile float red_temp = _pt.read();
xiaxia686 43:c592bf6a6a2d 221
xiaxia686 43:c592bf6a6a2d 222
xiaxia686 43:c592bf6a6a2d 223 datapackage.data.ID = (unsigned char)1;
xiaxia686 43:c592bf6a6a2d 224 datapackage.data.value = red_temp;
xiaxia686 43:c592bf6a6a2d 225 datapackage.data.aux = 0;
xiaxia686 43:c592bf6a6a2d 226 for (int i = 0; i < sizeof(datapackage); i++) {
xiaxia686 43:c592bf6a6a2d 227 //mbed_serial.putc(datapackage.type_char[i]);
xiaxia686 43:c592bf6a6a2d 228 pc.putc(datapackage.type_char[i]);
twighk 11:bbddc908c78c 229 }
twighk 3:717de74f6ebd 230
xiaxia686 43:c592bf6a6a2d 231 _blue_led = 0;
xiaxia686 43:c592bf6a6a2d 232 _red_led = 0;
xiaxia686 43:c592bf6a6a2d 233 wait(0.01);
xiaxia686 43:c592bf6a6a2d 234 volatile float noise_temp = _pt.read();
twighk 3:717de74f6ebd 235
twighk 3:717de74f6ebd 236
xiaxia686 43:c592bf6a6a2d 237 datapackage.data.ID = (unsigned char)2;
xiaxia686 43:c592bf6a6a2d 238 datapackage.data.value = noise_temp;
xiaxia686 43:c592bf6a6a2d 239 datapackage.data.aux = 0;
xiaxia686 43:c592bf6a6a2d 240 for (int i = 0; i < sizeof(datapackage); i++) {
xiaxia686 43:c592bf6a6a2d 241 //mbed_serial.putc(datapackage.type_char[i]);
xiaxia686 43:c592bf6a6a2d 242 pc.putc(datapackage.type_char[i]);
xiaxia686 43:c592bf6a6a2d 243 }
xiaxia686 43:c592bf6a6a2d 244
twighk 3:717de74f6ebd 245 }
twighk 3:717de74f6ebd 246 }
madcowswe 51:bc261eae004b 247 */
xiaxia686 43:c592bf6a6a2d 248
xiaxia686 43:c592bf6a6a2d 249
xiaxia686 43:c592bf6a6a2d 250 void armtest()
xiaxia686 43:c592bf6a6a2d 251 {
twighk 3:717de74f6ebd 252 Arm white(p26), black(p25, false, 0.0005, 180);
xiaxia686 43:c592bf6a6a2d 253 while(true) {
twighk 1:8119211eae14 254 white(0);
rsavitski 59:63609922579c 255 black(0.2);
twighk 1:8119211eae14 256 wait(1);
twighk 1:8119211eae14 257 white(1);
rsavitski 59:63609922579c 258 black(0.8);
twighk 1:8119211eae14 259 wait(1);
twighk 1:8119211eae14 260 }
twighk 1:8119211eae14 261 }
twighk 1:8119211eae14 262
twighk 1:8119211eae14 263
xiaxia686 43:c592bf6a6a2d 264 void motorsandservostest()
xiaxia686 43:c592bf6a6a2d 265 {
twighk 0:200635fa1b08 266 Encoder Eleft(p27, p28), Eright(p30, p29);
twighk 0:200635fa1b08 267 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 1:8119211eae14 268 Arm sTop(p25), sBottom(p26);
twighk 4:1be0f6c6ceae 269 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 270 const float speed = 0.0;
twighk 0:200635fa1b08 271 const float dspeed = 0.0;
xiaxia686 43:c592bf6a6a2d 272
twighk 0:200635fa1b08 273 Timer servoTimer;
xiaxia686 43:c592bf6a6a2d 274 mleft(speed);
xiaxia686 43:c592bf6a6a2d 275 mright(speed);
twighk 0:200635fa1b08 276 servoTimer.start();
xiaxia686 43:c592bf6a6a2d 277 while (true) {
madcowswe 20:70d651156779 278 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
xiaxia686 43:c592bf6a6a2d 279 if (Eleft.getTicks() < Eright.getTicks()) {
twighk 0:200635fa1b08 280 mleft(speed);
twighk 0:200635fa1b08 281 mright(speed - dspeed);
twighk 0:200635fa1b08 282 } else {
twighk 0:200635fa1b08 283 mright(speed);
twighk 0:200635fa1b08 284 mleft(speed - dspeed);
twighk 0:200635fa1b08 285 }
xiaxia686 43:c592bf6a6a2d 286 if (servoTimer.read() < 1) {
twighk 0:200635fa1b08 287 sTop.clockwise();
twighk 0:200635fa1b08 288 } else if (servoTimer.read() < 4) {
twighk 2:45da48fab346 289 sTop.halt();
twighk 0:200635fa1b08 290 } else if (servoTimer.read() < 5) {
twighk 0:200635fa1b08 291 sBottom.anticlockwise();
twighk 0:200635fa1b08 292 //Led=1;
twighk 0:200635fa1b08 293 } else if (servoTimer.read() < 6) {
twighk 0:200635fa1b08 294 sBottom.clockwise();
twighk 0:200635fa1b08 295 //Led=0;
twighk 0:200635fa1b08 296 } else if (servoTimer.read() < 7) {
twighk 2:45da48fab346 297 sBottom.halt();
xiaxia686 43:c592bf6a6a2d 298 } else {
twighk 0:200635fa1b08 299 sTop.anticlockwise();
twighk 0:200635fa1b08 300 }
twighk 0:200635fa1b08 301 if (servoTimer.read() >= 9) servoTimer.reset();
twighk 0:200635fa1b08 302 }
twighk 0:200635fa1b08 303 }
twighk 0:200635fa1b08 304
xiaxia686 43:c592bf6a6a2d 305 void motortestline()
xiaxia686 43:c592bf6a6a2d 306 {
twighk 2:45da48fab346 307 MainMotor mleft(p24,p23), mright(p21,p22);
twighk 2:45da48fab346 308 const float speed = 0.2;
xiaxia686 43:c592bf6a6a2d 309 mleft(speed);
xiaxia686 43:c592bf6a6a2d 310 mright(speed);
twighk 2:45da48fab346 311 while(true) wait(1);
twighk 2:45da48fab346 312 }
twighk 2:45da48fab346 313
xiaxia686 43:c592bf6a6a2d 314 void motorencodetestline()
xiaxia686 43:c592bf6a6a2d 315 {
madcowswe 12:76c9915db820 316 Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 12:76c9915db820 317 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
twighk 4:1be0f6c6ceae 318 //Serial pc(USBTX, USBRX);
twighk 0:200635fa1b08 319 const float speed = 0.2;
twighk 0:200635fa1b08 320 const float dspeed = 0.1;
twighk 0:200635fa1b08 321
xiaxia686 43:c592bf6a6a2d 322 mleft(speed);
xiaxia686 43:c592bf6a6a2d 323 mright(speed);
xiaxia686 43:c592bf6a6a2d 324 while (true) {
xiaxia686 43:c592bf6a6a2d 325 //left 27 cm = 113 -> 0.239 cm/pulse
xiaxia686 43:c592bf6a6a2d 326 //right 27 cm = 72 -> 0.375 cm/pulse
madcowswe 20:70d651156779 327 printf("Position is: %i \t %i \n\r", (int)(Eleft.getTicks()*0.239), (int)(Eright.getTicks()*0.375));
xiaxia686 43:c592bf6a6a2d 328 if (Eleft.getTicks()*0.239 < Eright.getTicks()*0.375) {
twighk 0:200635fa1b08 329 mright(speed - dspeed);
twighk 0:200635fa1b08 330 } else {
twighk 0:200635fa1b08 331 mright(speed + dspeed);
twighk 0:200635fa1b08 332 }
twighk 0:200635fa1b08 333 }
twighk 0:200635fa1b08 334
twighk 0:200635fa1b08 335 }
twighk 0:200635fa1b08 336
xiaxia686 43:c592bf6a6a2d 337 void motorencodetest()
xiaxia686 43:c592bf6a6a2d 338 {
madcowswe 7:4340355261f9 339 Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 7:4340355261f9 340 MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B);
twighk 0:200635fa1b08 341 Serial pc(USBTX, USBRX);
xiaxia686 43:c592bf6a6a2d 342
twighk 0:200635fa1b08 343 const float speed = -0.3;
twighk 0:200635fa1b08 344 const int enc = -38;
xiaxia686 43:c592bf6a6a2d 345 while(true) {
xiaxia686 43:c592bf6a6a2d 346 mleft(speed);
xiaxia686 43:c592bf6a6a2d 347 mright(0);
xiaxia686 43:c592bf6a6a2d 348 while(Eleft.getTicks()>enc) {
madcowswe 20:70d651156779 349 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
twighk 0:200635fa1b08 350 }
xiaxia686 43:c592bf6a6a2d 351 Eleft.reset();
xiaxia686 43:c592bf6a6a2d 352 Eright.reset();
xiaxia686 43:c592bf6a6a2d 353 mleft(0);
xiaxia686 43:c592bf6a6a2d 354 mright(speed);
xiaxia686 43:c592bf6a6a2d 355 while(Eright.getTicks()>enc) {
madcowswe 20:70d651156779 356 printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks());
twighk 0:200635fa1b08 357 }
xiaxia686 43:c592bf6a6a2d 358 Eleft.reset();
xiaxia686 43:c592bf6a6a2d 359 Eright.reset();
twighk 0:200635fa1b08 360 }
twighk 0:200635fa1b08 361 }
twighk 0:200635fa1b08 362
xiaxia686 43:c592bf6a6a2d 363 void encodertest()
xiaxia686 43:c592bf6a6a2d 364 {
madcowswe 15:9c5aaeda36dc 365 Encoder E1(P_ENC_LEFT_A, P_ENC_LEFT_B);
madcowswe 15:9c5aaeda36dc 366 //Encoder E2(P_ENC_RIGHT_A, P_ENC_RIGHT_B);
twighk 0:200635fa1b08 367 Serial pc(USBTX, USBRX);
xiaxia686 43:c592bf6a6a2d 368 while(true) {
twighk 0:200635fa1b08 369 wait(0.1);
madcowswe 20:70d651156779 370 printf("Position is: %i \t %i \n\r", E1.getTicks(), 0);//E2.getTicks());
twighk 0:200635fa1b08 371 }
twighk 0:200635fa1b08 372
twighk 0:200635fa1b08 373 }
xiaxia686 43:c592bf6a6a2d 374 void motortest()
xiaxia686 43:c592bf6a6a2d 375 {
twighk 0:200635fa1b08 376 MainMotor mright(p22,p21), mleft(p23,p24);
twighk 3:717de74f6ebd 377 while(true) {
twighk 0:200635fa1b08 378 wait(1);
xiaxia686 43:c592bf6a6a2d 379 mleft(0.8);
xiaxia686 43:c592bf6a6a2d 380 mright(0.8);
twighk 0:200635fa1b08 381 wait(1);
xiaxia686 43:c592bf6a6a2d 382 mleft(-0.2);
xiaxia686 43:c592bf6a6a2d 383 mright(0.2);
twighk 0:200635fa1b08 384 wait(1);
xiaxia686 43:c592bf6a6a2d 385 mleft(0);
xiaxia686 43:c592bf6a6a2d 386 mright(0);
twighk 0:200635fa1b08 387 }
twighk 0:200635fa1b08 388 }