2014 Eurobot fork

Dependencies:   mbed-rtos mbed QEI

Committer:
madcowswe
Date:
Sat Apr 13 22:41:00 2013 +0000
Revision:
60:5058465904e0
Parent:
51:bc261eae004b
Child:
62:78d99b781f02
Added feed forward. Maybe positive feedback

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