2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Sun Apr 14 12:57:04 2013 +0000
Revision:
62:78d99b781f02
Parent:
60:5058465904e0
Child:
63:c2c6269767b8
Basic fwd dc power feed forward working

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