2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Fri Apr 12 21:34:34 2013 +0000
Revision:
51:bc261eae004b
Parent:
50:937e860f4621
Child:
59:63609922579c
Child:
60:5058465904e0
cleaned up main

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