2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Sun Apr 14 14:59:57 2013 +0000
Revision:
64:c979fb1cd3b5
Parent:
63:c2c6269767b8
Child:
67:be3ea5450cc7
Gains fairly tuned.

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