2014 Eurobot fork
Dependencies: mbed-rtos mbed QEI
main.cpp@31:ada943ecaceb, 2013-04-10 (annotated)
- Committer:
- madcowswe
- Date:
- Wed Apr 10 18:04:47 2013 +0000
- Revision:
- 31:ada943ecaceb
- Parent:
- 28:4e20b44251c6
- Child:
- 32:e3f633620816
Made calibration more robust
Who changed what in which revision?
User | Revision | Line number | New 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" |
madcowswe | 20:70d651156779 | 16 | |
twighk | 0:200635fa1b08 | 17 | void motortest(); |
twighk | 0:200635fa1b08 | 18 | void encodertest(); |
twighk | 0:200635fa1b08 | 19 | void motorencodetest(); |
twighk | 0:200635fa1b08 | 20 | void motorencodetestline(); |
twighk | 0:200635fa1b08 | 21 | void motorsandservostest(); |
twighk | 1:8119211eae14 | 22 | void armtest(); |
twighk | 2:45da48fab346 | 23 | void motortestline(); |
twighk | 3:717de74f6ebd | 24 | void ledtest(); |
twighk | 3:717de74f6ebd | 25 | void phototransistortest(); |
twighk | 3:717de74f6ebd | 26 | void ledphototransistortest(); |
twighk | 3:717de74f6ebd | 27 | void colourtest(); |
twighk | 8:69bdf20cb525 | 28 | void cakesensortest(); |
twighk | 13:d4b5851742a3 | 29 | void printingtestthread(void const*); |
twighk | 13:d4b5851742a3 | 30 | void printingtestthread2(void const*); |
madcowswe | 12:76c9915db820 | 31 | void feedbacktest(); |
twighk | 0:200635fa1b08 | 32 | |
twighk | 0:200635fa1b08 | 33 | int main() { |
twighk | 2:45da48fab346 | 34 | |
twighk | 3:717de74f6ebd | 35 | /***************** |
twighk | 3:717de74f6ebd | 36 | * Test Code * |
twighk | 3:717de74f6ebd | 37 | *****************/ |
twighk | 0:200635fa1b08 | 38 | //motortest(); |
twighk | 0:200635fa1b08 | 39 | //encodertest(); |
twighk | 8:69bdf20cb525 | 40 | //motorencodetest(); |
twighk | 1:8119211eae14 | 41 | //motorencodetestline(); |
twighk | 0:200635fa1b08 | 42 | //motorsandservostest(); |
twighk | 3:717de74f6ebd | 43 | //armtest(); |
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(); |
madcowswe | 15:9c5aaeda36dc | 51 | |
madcowswe | 20:70d651156779 | 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 | 25:b16f1045108f | 63 | Serial pc(USBTX, USBRX); |
madcowswe | 25:b16f1045108f | 64 | pc.baud(115200); |
madcowswe | 25:b16f1045108f | 65 | |
rsavitski | 24:50805ef8c499 | 66 | using AI::current_waypoint; |
madcowswe | 25:b16f1045108f | 67 | |
rsavitski | 24:50805ef8c499 | 68 | current_waypoint = new Waypoint; |
rsavitski | 24:50805ef8c499 | 69 | current_waypoint->x = 0.5; |
rsavitski | 24:50805ef8c499 | 70 | current_waypoint->y = 0.7; |
rsavitski | 24:50805ef8c499 | 71 | current_waypoint->theta = 0.0; |
rsavitski | 24:50805ef8c499 | 72 | current_waypoint->pos_threshold = 0.02; |
rsavitski | 24:50805ef8c499 | 73 | current_waypoint->angle_threshold = 0.09; |
madcowswe | 20:70d651156779 | 74 | |
madcowswe | 20:70d651156779 | 75 | InitSerial(); |
madcowswe | 20:70d651156779 | 76 | //while(1) |
madcowswe | 20:70d651156779 | 77 | // printbuff(); |
madcowswe | 20:70d651156779 | 78 | wait(1); |
madcowswe | 20:70d651156779 | 79 | Kalman::KalmanInit(); |
madcowswe | 20:70d651156779 | 80 | |
madcowswe | 20:70d651156779 | 81 | Thread predictthread(Kalman::predictloop, NULL, osPriorityNormal, 2084);//512); //temp 2k |
madcowswe | 20:70d651156779 | 82 | Kalman::start_predict_ticker(&predictthread); |
rsavitski | 24:50805ef8c499 | 83 | |
madcowswe | 31:ada943ecaceb | 84 | Thread updatethread(Kalman::updateloop, NULL, osPriorityNormal, 2084); |
madcowswe | 31:ada943ecaceb | 85 | |
madcowswe | 31:ada943ecaceb | 86 | //Ticker motorcontroltestticker; |
madcowswe | 31:ada943ecaceb | 87 | //motorcontroltestticker.attach(MotorControl::motor_control_isr, 0.05); |
madcowswe | 22:6e3218cf75f8 | 88 | |
rsavitski | 24:50805ef8c499 | 89 | // motion layer periodic callback |
rsavitski | 24:50805ef8c499 | 90 | RtosTimer motion_timer(motion::motionlayer, osTimerPeriodic); |
rsavitski | 24:50805ef8c499 | 91 | motion_timer.start(50); |
madcowswe | 25:b16f1045108f | 92 | |
madcowswe | 21:167dacfe0b14 | 93 | Thread printingThread(Printing::printingloop, NULL, osPriorityLow, 2048); |
madcowswe | 25:b16f1045108f | 94 | |
madcowswe | 28:4e20b44251c6 | 95 | //measureCPUidle(); //repurpose thread for idle measurement |
madcowswe | 21:167dacfe0b14 | 96 | Thread::wait(osWaitForever); |
madcowswe | 25:b16f1045108f | 97 | |
madcowswe | 12:76c9915db820 | 98 | } |
madcowswe | 12:76c9915db820 | 99 | |
twighk | 13:d4b5851742a3 | 100 | #include <cstdlib> |
twighk | 13:d4b5851742a3 | 101 | using namespace std; |
twighk | 13:d4b5851742a3 | 102 | |
twighk | 13:d4b5851742a3 | 103 | void printingtestthread(void const*){ |
twighk | 13:d4b5851742a3 | 104 | const char ID = 1; |
twighk | 13:d4b5851742a3 | 105 | float buffer[3] = {ID}; |
madcowswe | 21:167dacfe0b14 | 106 | Printing::registerID(ID,sizeof(buffer)/sizeof(buffer[0])); |
twighk | 13:d4b5851742a3 | 107 | while (true){ |
twighk | 13:d4b5851742a3 | 108 | for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){ |
madcowswe | 20:70d651156779 | 109 | buffer[i] = ID ; |
twighk | 13:d4b5851742a3 | 110 | } |
madcowswe | 21:167dacfe0b14 | 111 | Printing::updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0])); |
twighk | 13:d4b5851742a3 | 112 | Thread::wait(200); |
twighk | 13:d4b5851742a3 | 113 | } |
twighk | 13:d4b5851742a3 | 114 | } |
madcowswe | 14:c638d4b9ee94 | 115 | |
twighk | 13:d4b5851742a3 | 116 | void printingtestthread2(void const*){ |
twighk | 13:d4b5851742a3 | 117 | const char ID = 2; |
twighk | 13:d4b5851742a3 | 118 | float buffer[5] = {ID}; |
madcowswe | 21:167dacfe0b14 | 119 | Printing::registerID(ID,sizeof(buffer)/sizeof(buffer[0])); |
twighk | 13:d4b5851742a3 | 120 | while (true){ |
twighk | 13:d4b5851742a3 | 121 | for(size_t i = 1; i != sizeof(buffer)/sizeof(buffer[0]); ++i){ |
twighk | 13:d4b5851742a3 | 122 | buffer[i] = ID; |
twighk | 13:d4b5851742a3 | 123 | } |
madcowswe | 21:167dacfe0b14 | 124 | Printing::updateval(ID, buffer, sizeof(buffer)/sizeof(buffer[0])); |
twighk | 13:d4b5851742a3 | 125 | Thread::wait(500); |
twighk | 13:d4b5851742a3 | 126 | } |
twighk | 8:69bdf20cb525 | 127 | } |
twighk | 8:69bdf20cb525 | 128 | |
madcowswe | 25:b16f1045108f | 129 | |
rsavitski | 24:50805ef8c499 | 130 | /* |
madcowswe | 12:76c9915db820 | 131 | void feedbacktest(){ |
madcowswe | 20:70d651156779 | 132 | //Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B); |
madcowswe | 12:76c9915db820 | 133 | MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B); |
madcowswe | 12:76c9915db820 | 134 | |
madcowswe | 20:70d651156779 | 135 | Kalman::State state; |
madcowswe | 20:70d651156779 | 136 | |
madcowswe | 20:70d651156779 | 137 | float Pgain = -0.01; |
madcowswe | 15:9c5aaeda36dc | 138 | float fwdspeed = -400/3.0f; |
madcowswe | 12:76c9915db820 | 139 | Timer timer; |
madcowswe | 12:76c9915db820 | 140 | timer.start(); |
madcowswe | 12:76c9915db820 | 141 | |
madcowswe | 12:76c9915db820 | 142 | while(true){ |
madcowswe | 12:76c9915db820 | 143 | float expecdist = fwdspeed * timer.read(); |
madcowswe | 20:70d651156779 | 144 | state = Kalman::getState(); |
madcowswe | 20:70d651156779 | 145 | float errleft = left_encoder.getTicks() - (expecdist); |
madcowswe | 20:70d651156779 | 146 | float errright = right_encoder.getTicks() - expecdist; |
madcowswe | 12:76c9915db820 | 147 | |
madcowswe | 12:76c9915db820 | 148 | mleft(max(min(errleft*Pgain, 0.4f), -0.4f)); |
madcowswe | 12:76c9915db820 | 149 | mright(max(min(errright*Pgain, 0.4f), -0.4f)); |
madcowswe | 12:76c9915db820 | 150 | } |
twighk | 8:69bdf20cb525 | 151 | } |
rsavitski | 24:50805ef8c499 | 152 | */ |
madcowswe | 25:b16f1045108f | 153 | |
twighk | 8:69bdf20cb525 | 154 | void cakesensortest(){ |
twighk | 8:69bdf20cb525 | 155 | wait(1); |
madcowswe | 20:70d651156779 | 156 | printf("cakesensortest"); |
twighk | 8:69bdf20cb525 | 157 | |
twighk | 11:bbddc908c78c | 158 | CakeSensor cs(P_COLOR_SENSOR_IN); |
twighk | 8:69bdf20cb525 | 159 | while(true){ |
twighk | 8:69bdf20cb525 | 160 | wait(0.1); |
madcowswe | 20:70d651156779 | 161 | printf("distance: %f\t %f\r\n", cs.Distance(),cs.Distanceincm()); |
twighk | 8:69bdf20cb525 | 162 | } |
twighk | 3:717de74f6ebd | 163 | } |
twighk | 3:717de74f6ebd | 164 | |
twighk | 3:717de74f6ebd | 165 | void colourtest(){ |
madcowswe | 7:4340355261f9 | 166 | Colour c(P_COLOR_SENSOR_BLUE, P_COLOR_SENSOR_RED, P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 167 | c.Calibrate(); |
twighk | 3:717de74f6ebd | 168 | while(true){ |
twighk | 3:717de74f6ebd | 169 | wait(0.1); |
twighk | 3:717de74f6ebd | 170 | ColourEnum ce = c.getColour(); |
twighk | 3:717de74f6ebd | 171 | switch(ce){ |
twighk | 3:717de74f6ebd | 172 | case BLUE : |
madcowswe | 20:70d651156779 | 173 | printf("BLUE\n\r"); |
twighk | 3:717de74f6ebd | 174 | break; |
twighk | 3:717de74f6ebd | 175 | case RED: |
madcowswe | 20:70d651156779 | 176 | printf("RED\n\r"); |
twighk | 3:717de74f6ebd | 177 | break; |
twighk | 3:717de74f6ebd | 178 | case WHITE: |
madcowswe | 20:70d651156779 | 179 | printf("WHITE\n\r"); |
twighk | 3:717de74f6ebd | 180 | break; |
twighk | 3:717de74f6ebd | 181 | case INCONCLUSIVE: |
madcowswe | 20:70d651156779 | 182 | printf("INCONCLUSIVE\n\r"); |
twighk | 3:717de74f6ebd | 183 | break; |
twighk | 3:717de74f6ebd | 184 | default: |
madcowswe | 20:70d651156779 | 185 | printf("BUG\n\r"); |
twighk | 3:717de74f6ebd | 186 | } |
twighk | 2:45da48fab346 | 187 | } |
twighk | 0:200635fa1b08 | 188 | |
twighk | 3:717de74f6ebd | 189 | } |
twighk | 3:717de74f6ebd | 190 | |
twighk | 3:717de74f6ebd | 191 | |
twighk | 3:717de74f6ebd | 192 | void ledphototransistortest(){ |
madcowswe | 7:4340355261f9 | 193 | DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED); |
madcowswe | 7:4340355261f9 | 194 | AnalogIn pt(P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 195 | Serial pc(USBTX, USBRX); |
twighk | 3:717de74f6ebd | 196 | |
twighk | 3:717de74f6ebd | 197 | while(true){ |
twighk | 11:bbddc908c78c | 198 | blue = 0; red = 0; |
twighk | 11:bbddc908c78c | 199 | for(int i = 0; i != 5; i++){ |
twighk | 11:bbddc908c78c | 200 | wait(0.1); |
madcowswe | 20:70d651156779 | 201 | printf("Phototransistor Analog is (none): %f \n\r", pt.read()); |
twighk | 11:bbddc908c78c | 202 | } |
twighk | 11:bbddc908c78c | 203 | |
madcowswe | 7:4340355261f9 | 204 | blue = 1; red = 0; |
twighk | 3:717de74f6ebd | 205 | for(int i = 0; i != 5; i++){ |
twighk | 3:717de74f6ebd | 206 | wait(0.1); |
madcowswe | 20:70d651156779 | 207 | printf("Phototransistor Analog is (blue): %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 208 | } |
madcowswe | 7:4340355261f9 | 209 | blue = 0; red = 1; |
twighk | 3:717de74f6ebd | 210 | for(int i = 0; i != 5; i++){ |
twighk | 3:717de74f6ebd | 211 | wait(0.1); |
madcowswe | 20:70d651156779 | 212 | printf("Phototransistor Analog is (red ): %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 213 | } |
twighk | 11:bbddc908c78c | 214 | blue = 1; red = 1; |
twighk | 11:bbddc908c78c | 215 | for(int i = 0; i != 5; i++){ |
twighk | 11:bbddc908c78c | 216 | wait(0.1); |
madcowswe | 20:70d651156779 | 217 | printf("Phototransistor Analog is (both): %f \n\r", pt.read()); |
twighk | 11:bbddc908c78c | 218 | } |
twighk | 3:717de74f6ebd | 219 | } |
twighk | 3:717de74f6ebd | 220 | } |
twighk | 3:717de74f6ebd | 221 | |
twighk | 3:717de74f6ebd | 222 | void phototransistortest(){ |
madcowswe | 7:4340355261f9 | 223 | AnalogIn pt(P_COLOR_SENSOR_IN); |
twighk | 3:717de74f6ebd | 224 | while(true){ |
twighk | 3:717de74f6ebd | 225 | wait(0.1); |
madcowswe | 20:70d651156779 | 226 | printf("Phototransistor Analog is: %f \n\r", pt.read()); |
twighk | 3:717de74f6ebd | 227 | } |
twighk | 3:717de74f6ebd | 228 | |
twighk | 3:717de74f6ebd | 229 | } |
twighk | 3:717de74f6ebd | 230 | |
twighk | 3:717de74f6ebd | 231 | void ledtest(){ |
madcowswe | 7:4340355261f9 | 232 | DigitalOut blue(P_COLOR_SENSOR_BLUE), red(P_COLOR_SENSOR_RED); |
twighk | 3:717de74f6ebd | 233 | while(true){ |
madcowswe | 7:4340355261f9 | 234 | blue = 1; red = 0; |
twighk | 3:717de74f6ebd | 235 | wait(0.2); |
madcowswe | 7:4340355261f9 | 236 | blue = 0; red = 1; |
twighk | 3:717de74f6ebd | 237 | wait(0.2); |
twighk | 3:717de74f6ebd | 238 | |
twighk | 3:717de74f6ebd | 239 | } |
twighk | 3:717de74f6ebd | 240 | } |
twighk | 3:717de74f6ebd | 241 | |
twighk | 1:8119211eae14 | 242 | void armtest(){ |
twighk | 3:717de74f6ebd | 243 | Arm white(p26), black(p25, false, 0.0005, 180); |
twighk | 3:717de74f6ebd | 244 | while(true){ |
twighk | 1:8119211eae14 | 245 | white(0); |
twighk | 1:8119211eae14 | 246 | black(0); |
twighk | 1:8119211eae14 | 247 | wait(1); |
twighk | 1:8119211eae14 | 248 | white(1); |
twighk | 1:8119211eae14 | 249 | black(1); |
twighk | 1:8119211eae14 | 250 | wait(1); |
twighk | 1:8119211eae14 | 251 | } |
twighk | 1:8119211eae14 | 252 | } |
twighk | 1:8119211eae14 | 253 | |
twighk | 1:8119211eae14 | 254 | |
twighk | 0:200635fa1b08 | 255 | void motorsandservostest(){ |
twighk | 0:200635fa1b08 | 256 | Encoder Eleft(p27, p28), Eright(p30, p29); |
twighk | 0:200635fa1b08 | 257 | MainMotor mleft(p24,p23), mright(p21,p22); |
twighk | 1:8119211eae14 | 258 | Arm sTop(p25), sBottom(p26); |
twighk | 4:1be0f6c6ceae | 259 | //Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 260 | const float speed = 0.0; |
twighk | 0:200635fa1b08 | 261 | const float dspeed = 0.0; |
twighk | 0:200635fa1b08 | 262 | |
twighk | 0:200635fa1b08 | 263 | Timer servoTimer; |
twighk | 0:200635fa1b08 | 264 | mleft(speed); mright(speed); |
twighk | 0:200635fa1b08 | 265 | servoTimer.start(); |
twighk | 0:200635fa1b08 | 266 | while (true){ |
madcowswe | 20:70d651156779 | 267 | printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks()); |
madcowswe | 20:70d651156779 | 268 | if (Eleft.getTicks() < Eright.getTicks()){ |
twighk | 0:200635fa1b08 | 269 | mleft(speed); |
twighk | 0:200635fa1b08 | 270 | mright(speed - dspeed); |
twighk | 0:200635fa1b08 | 271 | } else { |
twighk | 0:200635fa1b08 | 272 | mright(speed); |
twighk | 0:200635fa1b08 | 273 | mleft(speed - dspeed); |
twighk | 0:200635fa1b08 | 274 | } |
twighk | 0:200635fa1b08 | 275 | if (servoTimer.read() < 1){ |
twighk | 0:200635fa1b08 | 276 | sTop.clockwise(); |
twighk | 0:200635fa1b08 | 277 | } else if (servoTimer.read() < 4) { |
twighk | 2:45da48fab346 | 278 | sTop.halt(); |
twighk | 0:200635fa1b08 | 279 | } else if (servoTimer.read() < 5) { |
twighk | 0:200635fa1b08 | 280 | sBottom.anticlockwise(); |
twighk | 0:200635fa1b08 | 281 | //Led=1; |
twighk | 0:200635fa1b08 | 282 | } else if (servoTimer.read() < 6) { |
twighk | 0:200635fa1b08 | 283 | sBottom.clockwise(); |
twighk | 0:200635fa1b08 | 284 | //Led=0; |
twighk | 0:200635fa1b08 | 285 | } else if (servoTimer.read() < 7) { |
twighk | 2:45da48fab346 | 286 | sBottom.halt(); |
twighk | 0:200635fa1b08 | 287 | }else { |
twighk | 0:200635fa1b08 | 288 | sTop.anticlockwise(); |
twighk | 0:200635fa1b08 | 289 | } |
twighk | 0:200635fa1b08 | 290 | if (servoTimer.read() >= 9) servoTimer.reset(); |
twighk | 0:200635fa1b08 | 291 | } |
twighk | 0:200635fa1b08 | 292 | } |
twighk | 0:200635fa1b08 | 293 | |
twighk | 2:45da48fab346 | 294 | void motortestline(){ |
twighk | 2:45da48fab346 | 295 | MainMotor mleft(p24,p23), mright(p21,p22); |
twighk | 2:45da48fab346 | 296 | const float speed = 0.2; |
twighk | 2:45da48fab346 | 297 | mleft(speed); mright(speed); |
twighk | 2:45da48fab346 | 298 | while(true) wait(1); |
twighk | 2:45da48fab346 | 299 | } |
twighk | 2:45da48fab346 | 300 | |
twighk | 0:200635fa1b08 | 301 | void motorencodetestline(){ |
madcowswe | 12:76c9915db820 | 302 | Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B); |
madcowswe | 12:76c9915db820 | 303 | MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B); |
twighk | 4:1be0f6c6ceae | 304 | //Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 305 | const float speed = 0.2; |
twighk | 0:200635fa1b08 | 306 | const float dspeed = 0.1; |
twighk | 0:200635fa1b08 | 307 | |
twighk | 0:200635fa1b08 | 308 | mleft(speed); mright(speed); |
twighk | 0:200635fa1b08 | 309 | while (true){ |
twighk | 0:200635fa1b08 | 310 | //left 27 cm = 113 -> 0.239 cm/pulse |
twighk | 0:200635fa1b08 | 311 | //right 27 cm = 72 -> 0.375 cm/pulse |
madcowswe | 20:70d651156779 | 312 | printf("Position is: %i \t %i \n\r", (int)(Eleft.getTicks()*0.239), (int)(Eright.getTicks()*0.375)); |
madcowswe | 20:70d651156779 | 313 | if (Eleft.getTicks()*0.239 < Eright.getTicks()*0.375){ |
twighk | 0:200635fa1b08 | 314 | mright(speed - dspeed); |
twighk | 0:200635fa1b08 | 315 | } else { |
twighk | 0:200635fa1b08 | 316 | mright(speed + dspeed); |
twighk | 0:200635fa1b08 | 317 | } |
twighk | 0:200635fa1b08 | 318 | } |
twighk | 0:200635fa1b08 | 319 | |
twighk | 0:200635fa1b08 | 320 | } |
twighk | 0:200635fa1b08 | 321 | |
twighk | 0:200635fa1b08 | 322 | void motorencodetest(){ |
madcowswe | 7:4340355261f9 | 323 | Encoder Eright(P_ENC_RIGHT_A, P_ENC_RIGHT_B), Eleft(P_ENC_LEFT_A, P_ENC_LEFT_B); |
madcowswe | 7:4340355261f9 | 324 | MainMotor mright(P_MOT_RIGHT_A, P_MOT_RIGHT_B), mleft(P_MOT_LEFT_A, P_MOT_LEFT_B); |
twighk | 0:200635fa1b08 | 325 | Serial pc(USBTX, USBRX); |
twighk | 0:200635fa1b08 | 326 | |
twighk | 0:200635fa1b08 | 327 | const float speed = -0.3; |
twighk | 0:200635fa1b08 | 328 | const int enc = -38; |
twighk | 0:200635fa1b08 | 329 | while(true){ |
twighk | 0:200635fa1b08 | 330 | mleft(speed); mright(0); |
madcowswe | 20:70d651156779 | 331 | while(Eleft.getTicks()>enc){ |
madcowswe | 20:70d651156779 | 332 | printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks()); |
twighk | 0:200635fa1b08 | 333 | } |
twighk | 0:200635fa1b08 | 334 | Eleft.reset(); Eright.reset(); |
twighk | 0:200635fa1b08 | 335 | mleft(0); mright(speed); |
madcowswe | 20:70d651156779 | 336 | while(Eright.getTicks()>enc){ |
madcowswe | 20:70d651156779 | 337 | printf("Position is: %i \t %i \n\r", Eleft.getTicks(), Eright.getTicks()); |
twighk | 0:200635fa1b08 | 338 | } |
twighk | 0:200635fa1b08 | 339 | Eleft.reset(); Eright.reset(); |
twighk | 0:200635fa1b08 | 340 | } |
twighk | 0:200635fa1b08 | 341 | } |
twighk | 0:200635fa1b08 | 342 | |
twighk | 0:200635fa1b08 | 343 | void encodertest(){ |
madcowswe | 15:9c5aaeda36dc | 344 | Encoder E1(P_ENC_LEFT_A, P_ENC_LEFT_B); |
madcowswe | 15:9c5aaeda36dc | 345 | //Encoder E2(P_ENC_RIGHT_A, P_ENC_RIGHT_B); |
twighk | 0:200635fa1b08 | 346 | Serial pc(USBTX, USBRX); |
twighk | 3:717de74f6ebd | 347 | while(true){ |
twighk | 0:200635fa1b08 | 348 | wait(0.1); |
madcowswe | 20:70d651156779 | 349 | printf("Position is: %i \t %i \n\r", E1.getTicks(), 0);//E2.getTicks()); |
twighk | 0:200635fa1b08 | 350 | } |
twighk | 0:200635fa1b08 | 351 | |
twighk | 0:200635fa1b08 | 352 | } |
twighk | 0:200635fa1b08 | 353 | void motortest(){ |
twighk | 0:200635fa1b08 | 354 | MainMotor mright(p22,p21), mleft(p23,p24); |
twighk | 3:717de74f6ebd | 355 | while(true) { |
twighk | 0:200635fa1b08 | 356 | wait(1); |
twighk | 0:200635fa1b08 | 357 | mleft(0.8); mright(0.8); |
twighk | 0:200635fa1b08 | 358 | wait(1); |
twighk | 0:200635fa1b08 | 359 | mleft(-0.2); mright(0.2); |
twighk | 0:200635fa1b08 | 360 | wait(1); |
twighk | 0:200635fa1b08 | 361 | mleft(0); mright(0); |
twighk | 0:200635fa1b08 | 362 | } |
twighk | 0:200635fa1b08 | 363 | } |