![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Everything put together
Dependencies: FastAnalogIn MODSERIAL PID QEI RPCInterface Servo mbed-rtos mbed telemetry
main.cpp@4:947c3634b649, 2016-04-01 (annotated)
- Committer:
- vsutardja
- Date:
- Fri Apr 01 00:34:18 2016 +0000
- Revision:
- 4:947c3634b649
- Parent:
- 3:c57674c348bd
- Child:
- 5:7cba3ffc38bb
Faster Figure 8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsutardja | 0:fcf070a88ba0 | 1 | #include "mbed.h" |
vsutardja | 0:fcf070a88ba0 | 2 | #include "rtos.h" |
vsutardja | 0:fcf070a88ba0 | 3 | #include "Servo.h" |
vsutardja | 0:fcf070a88ba0 | 4 | #include "FastAnalogIn.h" |
vsutardja | 0:fcf070a88ba0 | 5 | #include "PID.h" |
vsutardja | 0:fcf070a88ba0 | 6 | #include "QEI.h" |
vsutardja | 0:fcf070a88ba0 | 7 | #include "telemetry.h" |
vsutardja | 0:fcf070a88ba0 | 8 | |
vsutardja | 0:fcf070a88ba0 | 9 | // ========= |
vsutardja | 0:fcf070a88ba0 | 10 | // Telemetry |
vsutardja | 0:fcf070a88ba0 | 11 | // ========= |
vsutardja | 0:fcf070a88ba0 | 12 | //MODSERIAL telemetry_serial(PTC4, PTC3); // TX, RX |
vsutardja | 0:fcf070a88ba0 | 13 | //telemetry::MbedHal telemetry_hal(telemetry_serial); // Hardware Abstraction Layer |
vsutardja | 0:fcf070a88ba0 | 14 | //telemetry::Telemetry telemetry_obj(telemetry_hal); // Telemetry |
vsutardja | 0:fcf070a88ba0 | 15 | |
vsutardja | 0:fcf070a88ba0 | 16 | |
vsutardja | 0:fcf070a88ba0 | 17 | // ============= |
vsutardja | 0:fcf070a88ba0 | 18 | // Communication |
vsutardja | 0:fcf070a88ba0 | 19 | // ============= |
vsutardja | 0:fcf070a88ba0 | 20 | Serial pc(USBTX, USBRX); // USB connection |
vsutardja | 0:fcf070a88ba0 | 21 | Serial bt(PTC4, PTC3); // BlueSMiRF connection |
vsutardja | 0:fcf070a88ba0 | 22 | //int idx = 0; |
vsutardja | 0:fcf070a88ba0 | 23 | char cmd; // Command |
vsutardja | 0:fcf070a88ba0 | 24 | char ch; |
vsutardja | 4:947c3634b649 | 25 | char in[5]; |
vsutardja | 0:fcf070a88ba0 | 26 | |
vsutardja | 0:fcf070a88ba0 | 27 | void communication(void const *args); // Communications |
vsutardja | 0:fcf070a88ba0 | 28 | |
vsutardja | 0:fcf070a88ba0 | 29 | // ===== |
vsutardja | 0:fcf070a88ba0 | 30 | // Motor |
vsutardja | 0:fcf070a88ba0 | 31 | // ===== |
vsutardja | 0:fcf070a88ba0 | 32 | PwmOut motor(PTA4); // Enable pin (PWM) |
vsutardja | 0:fcf070a88ba0 | 33 | int T = 25000; // Frequency |
vsutardja | 0:fcf070a88ba0 | 34 | float d = 0.0; // Duty cycle |
vsutardja | 0:fcf070a88ba0 | 35 | |
vsutardja | 0:fcf070a88ba0 | 36 | // ======= |
vsutardja | 0:fcf070a88ba0 | 37 | // Encoder |
vsutardja | 0:fcf070a88ba0 | 38 | // ======= |
vsutardja | 2:a8adff46eaca | 39 | const int MVG_AVG = 4; |
vsutardja | 0:fcf070a88ba0 | 40 | int ppr = 389; // Pulses per revolution |
vsutardja | 0:fcf070a88ba0 | 41 | QEI qei(PTD3, PTD2, NC, ppr, QEI::X4_ENCODING); // Quadrature encoder |
vsutardja | 0:fcf070a88ba0 | 42 | float c = 0.20106; // Wheel circumference |
vsutardja | 0:fcf070a88ba0 | 43 | int prev_pulses = 0; // Previous pulse count |
vsutardja | 0:fcf070a88ba0 | 44 | int curr_pulses = 0; // Current pulse count |
vsutardja | 0:fcf070a88ba0 | 45 | float velocity = 0; // Velocity |
vsutardja | 4:947c3634b649 | 46 | float vel = 0; |
vsutardja | 2:a8adff46eaca | 47 | float v_prev[MVG_AVG] = {0}; |
vsutardja | 0:fcf070a88ba0 | 48 | |
vsutardja | 0:fcf070a88ba0 | 49 | // ======== |
vsutardja | 0:fcf070a88ba0 | 50 | // Velocity |
vsutardja | 0:fcf070a88ba0 | 51 | // ======== |
vsutardja | 4:947c3634b649 | 52 | float Kp = 6.0; // Proportional factor |
vsutardja | 0:fcf070a88ba0 | 53 | float Ki = 0; // Integral factor |
vsutardja | 0:fcf070a88ba0 | 54 | float Kd = 0; // Derivative factor |
vsutardja | 0:fcf070a88ba0 | 55 | float interval = 0.01; // Sampling interval |
vsutardja | 4:947c3634b649 | 56 | float ref_v = 1.0; |
vsutardja | 0:fcf070a88ba0 | 57 | PID motor_ctrl(Kp, Ki, Kd, interval); // Motor controller |
vsutardja | 0:fcf070a88ba0 | 58 | |
vsutardja | 0:fcf070a88ba0 | 59 | // ===== |
vsutardja | 0:fcf070a88ba0 | 60 | // Servo |
vsutardja | 0:fcf070a88ba0 | 61 | // ===== |
vsutardja | 0:fcf070a88ba0 | 62 | Servo servo(PTA12); // Enable pin (PWM) |
vsutardja | 0:fcf070a88ba0 | 63 | float a = 88; // Angle |
vsutardja | 4:947c3634b649 | 64 | float Ks = 0.7; |
vsutardja | 0:fcf070a88ba0 | 65 | |
vsutardja | 0:fcf070a88ba0 | 66 | // ====== |
vsutardja | 0:fcf070a88ba0 | 67 | // Camera |
vsutardja | 0:fcf070a88ba0 | 68 | // ====== |
vsutardja | 0:fcf070a88ba0 | 69 | DigitalOut clk(PTD5); // Clock pin |
vsutardja | 0:fcf070a88ba0 | 70 | DigitalOut si(PTD0); // SI pin |
vsutardja | 0:fcf070a88ba0 | 71 | FastAnalogIn ao(PTC2); // AO pin |
vsutardja | 0:fcf070a88ba0 | 72 | Timeout camera_read; // Camera read timeout |
vsutardja | 0:fcf070a88ba0 | 73 | int t_int = 15000; // Exposure time |
vsutardja | 0:fcf070a88ba0 | 74 | int img[128]; // Image data |
vsutardja | 0:fcf070a88ba0 | 75 | |
vsutardja | 0:fcf070a88ba0 | 76 | void readCamera(); // Read data from camera |
vsutardja | 0:fcf070a88ba0 | 77 | |
vsutardja | 0:fcf070a88ba0 | 78 | // ================ |
vsutardja | 0:fcf070a88ba0 | 79 | // Image processing |
vsutardja | 0:fcf070a88ba0 | 80 | // ================ |
vsutardja | 0:fcf070a88ba0 | 81 | int max = -1; |
vsutardja | 3:c57674c348bd | 82 | float lum_bg = 0; |
vsutardja | 3:c57674c348bd | 83 | float contrast = 0; |
vsutardja | 0:fcf070a88ba0 | 84 | int argmax = 0; |
vsutardja | 0:fcf070a88ba0 | 85 | int argmin = 0; |
vsutardja | 0:fcf070a88ba0 | 86 | int temp[128]; |
vsutardja | 4:947c3634b649 | 87 | int center = 64; |
vsutardja | 0:fcf070a88ba0 | 88 | |
vsutardja | 0:fcf070a88ba0 | 89 | void track(); // Line-tracking steering |
vsutardja | 0:fcf070a88ba0 | 90 | |
vsutardja | 0:fcf070a88ba0 | 91 | // ================ |
vsutardja | 0:fcf070a88ba0 | 92 | // Functions |
vsutardja | 0:fcf070a88ba0 | 93 | // ================ |
vsutardja | 0:fcf070a88ba0 | 94 | |
vsutardja | 0:fcf070a88ba0 | 95 | // Communications |
vsutardja | 0:fcf070a88ba0 | 96 | void communication(void const *args) { |
vsutardja | 0:fcf070a88ba0 | 97 | while (true) { |
vsutardja | 0:fcf070a88ba0 | 98 | bt.printf("\r\nPress q to return to this prompt.\r\n"); |
vsutardja | 0:fcf070a88ba0 | 99 | bt.printf("Available diagnostics:\r\n"); |
vsutardja | 0:fcf070a88ba0 | 100 | bt.printf(" [0] Velocity\r\n"); |
vsutardja | 0:fcf070a88ba0 | 101 | bt.printf(" [1] Steering\r\n"); |
vsutardja | 2:a8adff46eaca | 102 | bt.printf(" [2] Change Kp\r\n"); |
vsutardja | 4:947c3634b649 | 103 | bt.printf(" [3] Change Ki\r\n"); |
vsutardja | 4:947c3634b649 | 104 | bt.printf(" [4] Change Kd\r\n"); |
vsutardja | 4:947c3634b649 | 105 | bt.printf(" [5] Change Ks\r\n"); |
vsutardja | 4:947c3634b649 | 106 | bt.printf(" [6] Change reference velocity\r\n"); |
vsutardja | 0:fcf070a88ba0 | 107 | cmd = bt.getc(); |
vsutardja | 0:fcf070a88ba0 | 108 | while (cmd != 'q') { |
vsutardja | 0:fcf070a88ba0 | 109 | switch(atoi(&cmd)){ |
vsutardja | 0:fcf070a88ba0 | 110 | case 0: |
vsutardja | 4:947c3634b649 | 111 | bt.printf("Duty cycle: %f, Pulse count: %d, Velocity: %f, Kp: %f, Ki: %f, Kd: %f\r\n", d, curr_pulses, velocity, Kp, Ki, Kd); |
vsutardja | 0:fcf070a88ba0 | 112 | break; |
vsutardja | 0:fcf070a88ba0 | 113 | case 1: |
vsutardja | 0:fcf070a88ba0 | 114 | bt.printf("Servo angle: %f, Track center: %d, t_int: %d\r\n", a, center, t_int); |
vsutardja | 0:fcf070a88ba0 | 115 | break; |
vsutardja | 2:a8adff46eaca | 116 | case 2: |
vsutardja | 4:947c3634b649 | 117 | bt.printf("Current: %f, New (5 digits): ", Kp); |
vsutardja | 4:947c3634b649 | 118 | for (int i = 0; i < 5; i++) { |
vsutardja | 4:947c3634b649 | 119 | in[i] = bt.getc(); |
vsutardja | 4:947c3634b649 | 120 | bt.putc(in[i]); |
vsutardja | 2:a8adff46eaca | 121 | } |
vsutardja | 2:a8adff46eaca | 122 | bt.printf("\r\n"); |
vsutardja | 2:a8adff46eaca | 123 | Kp = atof(in); |
vsutardja | 4:947c3634b649 | 124 | motor_ctrl.setTunings(Kp, Ki, Kd); |
vsutardja | 4:947c3634b649 | 125 | cmd = 'q'; |
vsutardja | 2:a8adff46eaca | 126 | break; |
vsutardja | 2:a8adff46eaca | 127 | case 3: |
vsutardja | 4:947c3634b649 | 128 | bt.printf("Current: %f, New (5 digits): ", Ki); |
vsutardja | 4:947c3634b649 | 129 | for (int i = 0; i < 5; i++) { |
vsutardja | 4:947c3634b649 | 130 | in[i] = bt.getc(); |
vsutardja | 4:947c3634b649 | 131 | bt.putc(in[i]); |
vsutardja | 4:947c3634b649 | 132 | } |
vsutardja | 4:947c3634b649 | 133 | bt.printf("\r\n"); |
vsutardja | 4:947c3634b649 | 134 | Ki = atof(in); |
vsutardja | 4:947c3634b649 | 135 | motor_ctrl.setTunings(Kp, Ki, Kd); |
vsutardja | 4:947c3634b649 | 136 | cmd = 'q'; |
vsutardja | 4:947c3634b649 | 137 | break; |
vsutardja | 4:947c3634b649 | 138 | case 4: |
vsutardja | 4:947c3634b649 | 139 | bt.printf("Current: %f, New (5 digits): ", Kd); |
vsutardja | 4:947c3634b649 | 140 | for (int i = 0; i < 5; i++) { |
vsutardja | 4:947c3634b649 | 141 | in[i] = bt.getc(); |
vsutardja | 4:947c3634b649 | 142 | bt.putc(in[i]); |
vsutardja | 2:a8adff46eaca | 143 | } |
vsutardja | 2:a8adff46eaca | 144 | bt.printf("\r\n"); |
vsutardja | 4:947c3634b649 | 145 | Kd = atof(in); |
vsutardja | 4:947c3634b649 | 146 | motor_ctrl.setTunings(Kp, Ki, Kd); |
vsutardja | 4:947c3634b649 | 147 | cmd = 'q'; |
vsutardja | 4:947c3634b649 | 148 | break; |
vsutardja | 4:947c3634b649 | 149 | case 5: |
vsutardja | 4:947c3634b649 | 150 | bt.printf("Current: %f, New (5 digits): ", Ks); |
vsutardja | 4:947c3634b649 | 151 | for (int i = 0; i < 5; i++) { |
vsutardja | 4:947c3634b649 | 152 | in[i] = bt.getc(); |
vsutardja | 4:947c3634b649 | 153 | bt.putc(in[i]); |
vsutardja | 4:947c3634b649 | 154 | } |
vsutardja | 4:947c3634b649 | 155 | bt.printf("\r\n"); |
vsutardja | 4:947c3634b649 | 156 | Ks = atof(in); |
vsutardja | 4:947c3634b649 | 157 | cmd = 'q'; |
vsutardja | 4:947c3634b649 | 158 | break; |
vsutardja | 4:947c3634b649 | 159 | case 6: |
vsutardja | 4:947c3634b649 | 160 | bt.printf("Current: %f, New (5 digits): ", ref_v); |
vsutardja | 4:947c3634b649 | 161 | for (int i = 0; i < 5; i++) { |
vsutardja | 4:947c3634b649 | 162 | in[i] = bt.getc(); |
vsutardja | 4:947c3634b649 | 163 | bt.putc(in[i]); |
vsutardja | 4:947c3634b649 | 164 | } |
vsutardja | 4:947c3634b649 | 165 | bt.printf("\r\n"); |
vsutardja | 4:947c3634b649 | 166 | ref_v = atof(in); |
vsutardja | 4:947c3634b649 | 167 | motor_ctrl.setSetPoint(ref_v); |
vsutardja | 4:947c3634b649 | 168 | cmd = 'q'; |
vsutardja | 2:a8adff46eaca | 169 | break; |
vsutardja | 0:fcf070a88ba0 | 170 | } |
vsutardja | 0:fcf070a88ba0 | 171 | if (bt.readable()) { |
vsutardja | 0:fcf070a88ba0 | 172 | cmd = bt.getc(); |
vsutardja | 0:fcf070a88ba0 | 173 | } |
vsutardja | 0:fcf070a88ba0 | 174 | } |
vsutardja | 0:fcf070a88ba0 | 175 | } |
vsutardja | 0:fcf070a88ba0 | 176 | } |
vsutardja | 0:fcf070a88ba0 | 177 | |
vsutardja | 0:fcf070a88ba0 | 178 | // Read data from camera |
vsutardja | 0:fcf070a88ba0 | 179 | void read_camera() { |
vsutardja | 0:fcf070a88ba0 | 180 | // Start data transfer |
vsutardja | 0:fcf070a88ba0 | 181 | si = 1; |
vsutardja | 0:fcf070a88ba0 | 182 | wait_us(1); |
vsutardja | 0:fcf070a88ba0 | 183 | clk = 1; |
vsutardja | 0:fcf070a88ba0 | 184 | wait_us(1); |
vsutardja | 0:fcf070a88ba0 | 185 | si = 0; |
vsutardja | 0:fcf070a88ba0 | 186 | wait_us(1); |
vsutardja | 0:fcf070a88ba0 | 187 | |
vsutardja | 0:fcf070a88ba0 | 188 | // Read camera data |
vsutardja | 0:fcf070a88ba0 | 189 | for (int i = 0; i < 128; i++) { |
vsutardja | 0:fcf070a88ba0 | 190 | clk = 0; |
vsutardja | 0:fcf070a88ba0 | 191 | img[i] = ao.read_u16(); |
vsutardja | 0:fcf070a88ba0 | 192 | clk = 1; |
vsutardja | 0:fcf070a88ba0 | 193 | wait_us(1); |
vsutardja | 0:fcf070a88ba0 | 194 | } |
vsutardja | 0:fcf070a88ba0 | 195 | clk = 0; |
vsutardja | 0:fcf070a88ba0 | 196 | |
vsutardja | 0:fcf070a88ba0 | 197 | // Update servo angle |
vsutardja | 0:fcf070a88ba0 | 198 | track(); |
vsutardja | 0:fcf070a88ba0 | 199 | |
vsutardja | 0:fcf070a88ba0 | 200 | // Set next frame exposure time |
vsutardja | 3:c57674c348bd | 201 | // camera_read.attach_us(&read_camera, t_int); |
vsutardja | 0:fcf070a88ba0 | 202 | } |
vsutardja | 0:fcf070a88ba0 | 203 | |
vsutardja | 0:fcf070a88ba0 | 204 | // Find line center from image |
vsutardja | 0:fcf070a88ba0 | 205 | // Take two-point moving average to smooth the data |
vsutardja | 0:fcf070a88ba0 | 206 | // Find indices where max and min of smoothed data are attained |
vsutardja | 0:fcf070a88ba0 | 207 | // Calculate and return midpoint of argmax and argmin |
vsutardja | 0:fcf070a88ba0 | 208 | void track() { |
vsutardja | 0:fcf070a88ba0 | 209 | max = -1; |
vsutardja | 3:c57674c348bd | 210 | lum_bg = 0; |
vsutardja | 0:fcf070a88ba0 | 211 | argmax = 0; |
vsutardja | 0:fcf070a88ba0 | 212 | argmin = 0; |
vsutardja | 0:fcf070a88ba0 | 213 | for (int i = 0; i < 107; i++) { |
vsutardja | 0:fcf070a88ba0 | 214 | if (img[i+11] > max) { |
vsutardja | 0:fcf070a88ba0 | 215 | max = img[i+11]; |
vsutardja | 0:fcf070a88ba0 | 216 | } |
vsutardja | 0:fcf070a88ba0 | 217 | if (i == 126) { |
vsutardja | 0:fcf070a88ba0 | 218 | temp[i-1] = (img[i+11] + img[i+1+11]) / 2 - temp[i-1]; |
vsutardja | 0:fcf070a88ba0 | 219 | if (temp[i-1] > temp[argmax]) { |
vsutardja | 0:fcf070a88ba0 | 220 | argmax = i - 1; |
vsutardja | 0:fcf070a88ba0 | 221 | } |
vsutardja | 0:fcf070a88ba0 | 222 | if (temp[i-1] < temp[argmin]) { |
vsutardja | 0:fcf070a88ba0 | 223 | argmin = i - 1; |
vsutardja | 0:fcf070a88ba0 | 224 | } |
vsutardja | 0:fcf070a88ba0 | 225 | } else { |
vsutardja | 0:fcf070a88ba0 | 226 | temp[i] = (img[i+11] + img[i+1+11]) / 2; |
vsutardja | 0:fcf070a88ba0 | 227 | if (i > 0) { |
vsutardja | 0:fcf070a88ba0 | 228 | temp[i-1] = temp[i] - temp[i-1]; |
vsutardja | 0:fcf070a88ba0 | 229 | if (temp[i-1] > temp[argmax]) { |
vsutardja | 0:fcf070a88ba0 | 230 | argmax = i - 1; |
vsutardja | 0:fcf070a88ba0 | 231 | } |
vsutardja | 0:fcf070a88ba0 | 232 | if (temp[i-1] < temp[argmin]) { |
vsutardja | 0:fcf070a88ba0 | 233 | argmin = i - 1; |
vsutardja | 0:fcf070a88ba0 | 234 | } |
vsutardja | 0:fcf070a88ba0 | 235 | } |
vsutardja | 0:fcf070a88ba0 | 236 | } |
vsutardja | 0:fcf070a88ba0 | 237 | } |
vsutardja | 0:fcf070a88ba0 | 238 | |
vsutardja | 4:947c3634b649 | 239 | for (int i = 0; i < 10; i++) { |
vsutardja | 4:947c3634b649 | 240 | lum_bg = lum_bg + img[64 - 4 - i] / 20.0 + img[64 + 4 + i] / 20.0; |
vsutardja | 3:c57674c348bd | 241 | } |
vsutardja | 3:c57674c348bd | 242 | |
vsutardja | 3:c57674c348bd | 243 | contrast = (max - lum_bg) / lum_bg; |
vsutardja | 3:c57674c348bd | 244 | |
vsutardja | 4:947c3634b649 | 245 | // if (contrast < 1.5) { |
vsutardja | 3:c57674c348bd | 246 | // Underexposed |
vsutardja | 4:947c3634b649 | 247 | if (max < 60000) { |
vsutardja | 4:947c3634b649 | 248 | t_int = t_int + 0.15 * (60000 - max); |
vsutardja | 3:c57674c348bd | 249 | } |
vsutardja | 3:c57674c348bd | 250 | // Overexposed |
vsutardja | 3:c57674c348bd | 251 | if (lum_bg > 25000) { |
vsutardja | 4:947c3634b649 | 252 | t_int = t_int - 0.15 * (lum_bg - 25000); |
vsutardja | 3:c57674c348bd | 253 | } |
vsutardja | 4:947c3634b649 | 254 | // } |
vsutardja | 3:c57674c348bd | 255 | |
vsutardja | 0:fcf070a88ba0 | 256 | if (max > 43253) { |
vsutardja | 0:fcf070a88ba0 | 257 | center = (argmax + argmin + 2 + 11) / 2; |
vsutardja | 4:947c3634b649 | 258 | a = 88 + (64 - center) * Ks; |
vsutardja | 0:fcf070a88ba0 | 259 | servo = a / 180; |
vsutardja | 0:fcf070a88ba0 | 260 | } |
vsutardja | 3:c57674c348bd | 261 | |
vsutardja | 3:c57674c348bd | 262 | camera_read.attach_us(&read_camera, t_int); |
vsutardja | 0:fcf070a88ba0 | 263 | } |
vsutardja | 0:fcf070a88ba0 | 264 | |
vsutardja | 0:fcf070a88ba0 | 265 | // ==== |
vsutardja | 0:fcf070a88ba0 | 266 | // Main |
vsutardja | 0:fcf070a88ba0 | 267 | // ==== |
vsutardja | 0:fcf070a88ba0 | 268 | int main() { |
vsutardja | 2:a8adff46eaca | 269 | |
vsutardja | 0:fcf070a88ba0 | 270 | // Initialize motor |
vsutardja | 0:fcf070a88ba0 | 271 | motor.period_us(T); |
vsutardja | 0:fcf070a88ba0 | 272 | motor = 1.0 - d; |
vsutardja | 0:fcf070a88ba0 | 273 | |
vsutardja | 0:fcf070a88ba0 | 274 | // Initialize servo |
vsutardja | 0:fcf070a88ba0 | 275 | servo.calibrate(0.001, 45.0); |
vsutardja | 0:fcf070a88ba0 | 276 | servo = a / 180.0; |
vsutardja | 0:fcf070a88ba0 | 277 | |
vsutardja | 0:fcf070a88ba0 | 278 | // Initialize & start camera |
vsutardja | 0:fcf070a88ba0 | 279 | clk = 0; |
vsutardja | 3:c57674c348bd | 280 | read_camera(); |
vsutardja | 0:fcf070a88ba0 | 281 | |
vsutardja | 0:fcf070a88ba0 | 282 | // Initialize motor controller |
vsutardja | 0:fcf070a88ba0 | 283 | motor_ctrl.setInputLimits(0.0, 10.0); |
vsutardja | 4:947c3634b649 | 284 | motor_ctrl.setOutputLimits(0.01, 0.5); |
vsutardja | 4:947c3634b649 | 285 | motor_ctrl.setSetPoint(ref_v); |
vsutardja | 0:fcf070a88ba0 | 286 | motor_ctrl.setBias(0.0); |
vsutardja | 0:fcf070a88ba0 | 287 | motor_ctrl.setMode(1); |
vsutardja | 0:fcf070a88ba0 | 288 | |
vsutardja | 0:fcf070a88ba0 | 289 | // Initialize bluetooth |
vsutardja | 0:fcf070a88ba0 | 290 | bt.baud(115200); |
vsutardja | 0:fcf070a88ba0 | 291 | |
vsutardja | 0:fcf070a88ba0 | 292 | // Initialize communications thread |
vsutardja | 0:fcf070a88ba0 | 293 | Thread communication_thread(communication); |
vsutardja | 0:fcf070a88ba0 | 294 | |
vsutardja | 0:fcf070a88ba0 | 295 | // Start motor controller |
vsutardja | 0:fcf070a88ba0 | 296 | while (true) { |
vsutardja | 0:fcf070a88ba0 | 297 | curr_pulses = qei.getPulses(); |
vsutardja | 4:947c3634b649 | 298 | //for (int i = 0; i < MVG_AVG - 1; i++) { |
vsutardja | 4:947c3634b649 | 299 | // v_prev[i] = abs(1.5 - 0.5 * (1.5 - v_prev[i+1])); |
vsutardja | 4:947c3634b649 | 300 | // } |
vsutardja | 4:947c3634b649 | 301 | // v_prev[MVG_AVG-1] = velocity; |
vsutardja | 4:947c3634b649 | 302 | velocity = (curr_pulses - prev_pulses)/ interval / ppr * c / 2.5; |
vsutardja | 4:947c3634b649 | 303 | //vel = velocity; |
vsutardja | 4:947c3634b649 | 304 | // for (int i = 0; i < MVG_AVG; i++) { |
vsutardja | 4:947c3634b649 | 305 | // velocity = velocity + v_prev[i]; |
vsutardja | 4:947c3634b649 | 306 | // } |
vsutardja | 4:947c3634b649 | 307 | // velocity = velocity / (MVG_AVG + 1.0); |
vsutardja | 0:fcf070a88ba0 | 308 | prev_pulses = curr_pulses; |
vsutardja | 0:fcf070a88ba0 | 309 | motor_ctrl.setProcessValue(velocity); |
vsutardja | 0:fcf070a88ba0 | 310 | d = motor_ctrl.compute(); |
vsutardja | 0:fcf070a88ba0 | 311 | motor = 1.0 - d; |
vsutardja | 0:fcf070a88ba0 | 312 | wait(interval); |
vsutardja | 4:947c3634b649 | 313 | pc.printf("Exposure: %d, Center: %d, Angle: %f\r\n", t_int, center, a); |
vsutardja | 0:fcf070a88ba0 | 314 | } |
vsutardja | 0:fcf070a88ba0 | 315 | } |