Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1 | #include "motors/core/kinetis.h" |
| 2 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 3 | #include <inttypes.h> |
Brian Silverman | dabdf90 | 2017-10-21 15:34:40 -0400 | [diff] [blame] | 4 | #include <stdio.h> |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 5 | |
| 6 | #include <atomic> |
| 7 | #include <cmath> |
| 8 | |
Brian Silverman | dabdf90 | 2017-10-21 15:34:40 -0400 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/integral_haptic_trigger.h" |
| 10 | #include "frc971/control_loops/drivetrain/integral_haptic_wheel.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 11 | #include "motors/core/time.h" |
| 12 | #include "motors/motor.h" |
| 13 | #include "motors/peripheral/adc.h" |
| 14 | #include "motors/peripheral/can.h" |
| 15 | #include "motors/pistol_grip/motor_controls.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame^] | 16 | #include "motors/print/print.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 17 | #include "motors/util.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 18 | |
| 19 | #define MOTOR0_PWM_FTM FTM3 |
| 20 | #define MOTOR0_ENCODER_FTM FTM2 |
| 21 | #define MOTOR1_PWM_FTM FTM0 |
| 22 | #define MOTOR1_ENCODER_FTM FTM1 |
| 23 | |
| 24 | extern const float kWheelCoggingTorque[4096]; |
| 25 | extern const float kTriggerCoggingTorque[4096]; |
| 26 | |
| 27 | namespace frc971 { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 28 | namespace motors { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticTriggerPlant; |
| 32 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticTriggerObserver; |
| 33 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelPlant; |
| 34 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelObserver; |
| 35 | |
Brian Silverman | 9ed2cf1 | 2018-05-12 13:06:38 -0700 | [diff] [blame] | 36 | struct SmallAdcReadings { |
| 37 | uint16_t currents[3]; |
| 38 | }; |
| 39 | |
| 40 | struct SmallInitReadings { |
| 41 | uint16_t motor0_abs; |
| 42 | uint16_t motor1_abs; |
| 43 | uint16_t wheel_abs; |
| 44 | }; |
| 45 | |
| 46 | void AdcInitSmall() { |
| 47 | AdcInitCommon(); |
| 48 | |
| 49 | // M0_CH0F ADC1_SE17 |
| 50 | PORTA_PCR17 = PORT_PCR_MUX(0); |
| 51 | |
| 52 | // M0_CH1F ADC1_SE14 |
| 53 | PORTB_PCR10 = PORT_PCR_MUX(0); |
| 54 | |
| 55 | // M0_CH2F ADC1_SE15 |
| 56 | PORTB_PCR11 = PORT_PCR_MUX(0); |
| 57 | |
| 58 | // M0_ABS ADC0_SE5b |
| 59 | PORTD_PCR1 = PORT_PCR_MUX(0); |
| 60 | |
| 61 | // M1_CH0F ADC0_SE13 |
| 62 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 63 | |
| 64 | // M1_CH1F ADC0_SE12 |
| 65 | PORTB_PCR2 = PORT_PCR_MUX(0); |
| 66 | |
| 67 | // M1_CH2F ADC0_SE14 |
| 68 | PORTC_PCR0 = PORT_PCR_MUX(0); |
| 69 | |
| 70 | // M1_ABS ADC0_SE17 |
| 71 | PORTE_PCR24 = PORT_PCR_MUX(0); |
| 72 | |
| 73 | // WHEEL_ABS ADC0_SE18 |
| 74 | PORTE_PCR25 = PORT_PCR_MUX(0); |
| 75 | |
| 76 | // VIN ADC1_SE5B |
| 77 | PORTC_PCR9 = PORT_PCR_MUX(0); |
| 78 | } |
| 79 | |
| 80 | SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) { |
| 81 | SmallAdcReadings r; |
| 82 | |
| 83 | ADC1_SC1A = 17; |
| 84 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 85 | } |
| 86 | ADC1_SC1A = 14; |
| 87 | r.currents[0] = ADC1_RA; |
| 88 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 89 | } |
| 90 | ADC1_SC1A = 15; |
| 91 | r.currents[1] = ADC1_RA; |
| 92 | while (!(ADC1_SC1A & ADC_SC1_COCO)) { |
| 93 | } |
| 94 | r.currents[2] = ADC1_RA; |
| 95 | |
| 96 | return r; |
| 97 | } |
| 98 | |
| 99 | SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) { |
| 100 | SmallAdcReadings r; |
| 101 | |
| 102 | ADC0_SC1A = 13; |
| 103 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 104 | } |
| 105 | ADC0_SC1A = 12; |
| 106 | r.currents[0] = ADC0_RA; |
| 107 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 108 | } |
| 109 | ADC0_SC1A = 14; |
| 110 | r.currents[1] = ADC0_RA; |
| 111 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 112 | } |
| 113 | r.currents[2] = ADC0_RA; |
| 114 | |
| 115 | return r; |
| 116 | } |
| 117 | |
| 118 | SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) { |
| 119 | SmallInitReadings r; |
| 120 | |
| 121 | ADC0_SC1A = 5; |
| 122 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 123 | } |
| 124 | ADC0_SC1A = 17; |
| 125 | r.motor0_abs = ADC0_RA; |
| 126 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 127 | } |
| 128 | ADC0_SC1A = 18; |
| 129 | r.motor1_abs = ADC0_RA; |
| 130 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 131 | } |
| 132 | r.wheel_abs = ADC0_RA; |
| 133 | |
| 134 | return r; |
| 135 | } |
| 136 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 137 | constexpr float kHapticWheelCurrentLimit = static_cast<float>( |
| 138 | ::frc971::control_loops::drivetrain::kHapticWheelCurrentLimit); |
| 139 | constexpr float kHapticTriggerCurrentLimit = static_cast<float>( |
| 140 | ::frc971::control_loops::drivetrain::kHapticTriggerCurrentLimit); |
| 141 | |
| 142 | ::std::atomic<Motor *> global_motor0{nullptr}, global_motor1{nullptr}; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 143 | |
| 144 | // Angle last time the current loop ran. |
| 145 | ::std::atomic<float> global_wheel_angle{0.0f}; |
| 146 | ::std::atomic<float> global_trigger_angle{0.0f}; |
| 147 | |
| 148 | // Wheel observer/plant. |
| 149 | ::std::atomic<StateFeedbackObserver<3, 1, 1, float> *> global_wheel_observer{ |
| 150 | nullptr}; |
| 151 | ::std::atomic<StateFeedbackPlant<3, 1, 1, float> *> global_wheel_plant{nullptr}; |
| 152 | // Throttle observer/plant. |
| 153 | ::std::atomic<StateFeedbackObserver<3, 1, 1, float> *> global_trigger_observer{ |
| 154 | nullptr}; |
| 155 | ::std::atomic<StateFeedbackPlant<3, 1, 1, float> *> global_trigger_plant{ |
| 156 | nullptr}; |
| 157 | |
| 158 | // Torques for the current loop to apply. |
| 159 | ::std::atomic<float> global_wheel_current{0.0f}; |
| 160 | ::std::atomic<float> global_trigger_torque{0.0f}; |
| 161 | |
| 162 | constexpr int kSwitchingDivisor = 2; |
| 163 | |
| 164 | float analog_ratio(uint16_t reading) { |
| 165 | static constexpr uint16_t kMin = 260, kMax = 3812; |
| 166 | return static_cast<float>(::std::max(::std::min(reading, kMax), kMin) - |
| 167 | kMin) / |
| 168 | static_cast<float>(kMax - kMin); |
| 169 | } |
| 170 | |
| 171 | constexpr float InterpolateFloat(float x1, float x0, float y1, float y0, float x) { |
| 172 | return (x - x0) * (y1 - y0) / (x1 - x0) + y0; |
| 173 | } |
| 174 | |
| 175 | float absolute_wheel(float wheel_position) { |
| 176 | if (wheel_position < 0.43f) { |
| 177 | wheel_position += 1.0f; |
| 178 | } |
| 179 | wheel_position -= 0.462f + 0.473f; |
| 180 | return wheel_position; |
| 181 | } |
| 182 | |
| 183 | extern "C" { |
| 184 | |
| 185 | void *__stack_chk_guard = (void *)0x67111971; |
| 186 | void __stack_chk_fail() { |
| 187 | while (true) { |
| 188 | GPIOC_PSOR = (1 << 5); |
| 189 | printf("Stack corruption detected\n"); |
| 190 | delay(1000); |
| 191 | GPIOC_PCOR = (1 << 5); |
| 192 | delay(1000); |
| 193 | } |
| 194 | } |
| 195 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 196 | extern uint32_t __bss_ram_start__[], __bss_ram_end__[]; |
| 197 | extern uint32_t __data_ram_start__[], __data_ram_end__[]; |
| 198 | extern uint32_t __heap_start__[], __heap_end__[]; |
| 199 | extern uint32_t __stack_end__[]; |
| 200 | |
| 201 | } // extern "C" |
| 202 | |
| 203 | constexpr float kWheelMaxExtension = 1.0f; |
| 204 | constexpr float kWheelFrictionMax = 0.2f; |
| 205 | float WheelCenteringCurrent(float scalar, float angle, float velocity) { |
| 206 | float friction_goal_current = -angle * 10.0f; |
| 207 | if (friction_goal_current > kWheelFrictionMax) { |
| 208 | friction_goal_current = kWheelFrictionMax; |
| 209 | } else if (friction_goal_current < -kWheelFrictionMax) { |
| 210 | friction_goal_current = -kWheelFrictionMax; |
| 211 | } |
| 212 | |
| 213 | constexpr float kWheelSpringNonlinearity = 0.45f; |
| 214 | |
| 215 | float goal_current = -((1.0f - kWheelSpringNonlinearity) * angle + |
| 216 | kWheelSpringNonlinearity * angle * angle * angle) * |
| 217 | 6.0f - |
| 218 | velocity * 0.04f; |
| 219 | if (goal_current > 5.0f - scalar) { |
| 220 | goal_current = 5.0f - scalar; |
| 221 | } else if (goal_current < -5.0f + scalar) { |
| 222 | goal_current = -5.0f + scalar; |
| 223 | } |
| 224 | |
| 225 | return goal_current * scalar + friction_goal_current; |
| 226 | } |
| 227 | |
| 228 | extern "C" void ftm0_isr() { |
| 229 | SmallAdcReadings readings; |
| 230 | { |
| 231 | DisableInterrupts disable_interrupts; |
| 232 | readings = AdcReadSmall1(disable_interrupts); |
| 233 | } |
| 234 | uint32_t encoder = |
| 235 | global_motor1.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 236 | int32_t absolute_encoder = global_motor1.load(::std::memory_order_relaxed) |
| 237 | ->absolute_encoder(encoder); |
| 238 | |
| 239 | const float angle = absolute_encoder / static_cast<float>((15320 - 1488) / 2); |
| 240 | global_wheel_angle.store(angle); |
| 241 | |
| 242 | float goal_current = -global_wheel_current.load(::std::memory_order_relaxed) + |
| 243 | kWheelCoggingTorque[encoder]; |
| 244 | |
| 245 | global_motor1.load(::std::memory_order_relaxed)->SetGoalCurrent(goal_current); |
| 246 | global_motor1.load(::std::memory_order_relaxed) |
| 247 | ->HandleInterrupt(BalanceSimpleReadings(readings.currents), encoder); |
| 248 | } |
| 249 | |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 250 | constexpr float kTriggerMaxExtension = -0.70f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 251 | constexpr float kTriggerCenter = 0.0f; |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 252 | constexpr float kCenteringStiffness = 0.15f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 253 | float TriggerCenteringCurrent(float trigger_angle) { |
| 254 | float goal_current = (kTriggerCenter - trigger_angle) * 3.0f; |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 255 | float knotch_goal_current = (kTriggerCenter - trigger_angle) * 8.0f; |
| 256 | if (knotch_goal_current < -kCenteringStiffness) { |
| 257 | knotch_goal_current = -kCenteringStiffness; |
| 258 | } else if (knotch_goal_current > kCenteringStiffness) { |
| 259 | knotch_goal_current = kCenteringStiffness; |
| 260 | } |
| 261 | |
| 262 | goal_current += knotch_goal_current; |
| 263 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 264 | if (goal_current < -1.0f) { |
| 265 | goal_current = -1.0f; |
| 266 | } else if (goal_current > 1.0f) { |
| 267 | goal_current = 1.0f; |
| 268 | if (trigger_angle < kTriggerMaxExtension) { |
| 269 | goal_current -= (30.0f * (trigger_angle - kTriggerMaxExtension)); |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 270 | if (goal_current > 4.0f) { |
| 271 | goal_current = 4.0f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | return goal_current; |
| 276 | } |
| 277 | |
| 278 | extern "C" void ftm3_isr() { |
| 279 | SmallAdcReadings readings; |
| 280 | { |
| 281 | DisableInterrupts disable_interrupts; |
| 282 | readings = AdcReadSmall0(disable_interrupts); |
| 283 | } |
| 284 | uint32_t encoder = |
| 285 | global_motor0.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 286 | int32_t absolute_encoder = global_motor0.load(::std::memory_order_relaxed) |
| 287 | ->absolute_encoder(encoder); |
| 288 | |
| 289 | float trigger_angle = absolute_encoder / 1370.f; |
| 290 | |
| 291 | const float goal_current = |
| 292 | -global_trigger_torque.load(::std::memory_order_relaxed) + |
| 293 | kTriggerCoggingTorque[encoder]; |
| 294 | |
| 295 | global_motor0.load(::std::memory_order_relaxed)->SetGoalCurrent(goal_current); |
| 296 | global_motor0.load(::std::memory_order_relaxed) |
| 297 | ->HandleInterrupt(BalanceSimpleReadings(readings.currents), encoder); |
| 298 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 299 | global_trigger_angle.store(trigger_angle); |
| 300 | } |
| 301 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 302 | int ConvertFloat16(float val) { |
| 303 | int result = static_cast<int>(val * 32768.0f) + 32768; |
| 304 | if (result > 0xffff) { |
| 305 | result = 0xffff; |
| 306 | } else if (result < 0) { |
| 307 | result = 0; |
| 308 | } |
| 309 | return result; |
| 310 | } |
| 311 | int ConvertFloat14(float val) { |
| 312 | int result = static_cast<int>(val * 8192.0f) + 8192; |
| 313 | if (result > 0x3fff) { |
| 314 | result = 0x3fff; |
| 315 | } else if (result < 0) { |
| 316 | result = 0; |
| 317 | } |
| 318 | return result; |
| 319 | } |
| 320 | |
| 321 | extern "C" void pit3_isr() { |
| 322 | PIT_TFLG3 = 1; |
| 323 | const float absolute_trigger_angle = |
| 324 | global_trigger_angle.load(::std::memory_order_relaxed); |
| 325 | const float absolute_wheel_angle = |
| 326 | global_wheel_angle.load(::std::memory_order_relaxed); |
| 327 | |
| 328 | // Force a barrier here so we sample everything guaranteed at the beginning. |
| 329 | __asm__("" ::: "memory"); |
| 330 | const float absolute_wheel_angle_radians = |
| 331 | absolute_wheel_angle * static_cast<float>(M_PI) * (338.16f / 360.0f); |
| 332 | const float absolute_trigger_angle_radians = |
| 333 | absolute_trigger_angle * static_cast<float>(M_PI) * (45.0f / 360.0f); |
| 334 | |
| 335 | static uint32_t last_command_time = 0; |
| 336 | static float trigger_goal_position = 0.0f; |
| 337 | static float trigger_goal_velocity = 0.0f; |
| 338 | static float trigger_haptic_current = 0.0f; |
| 339 | static bool trigger_centering = true; |
| 340 | static bool trigger_haptics = false; |
| 341 | { |
| 342 | uint8_t data[8]; |
| 343 | int length; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 344 | can_receive(data, &length, 0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 345 | if (length > 0) { |
| 346 | last_command_time = micros(); |
| 347 | trigger_goal_position = |
| 348 | static_cast<float>( |
| 349 | static_cast<int32_t>(static_cast<uint32_t>(data[0]) | |
| 350 | (static_cast<uint32_t>(data[1]) << 8)) - |
| 351 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 352 | static_cast<float>(32768.0 * M_PI / 8.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 353 | trigger_goal_velocity = |
| 354 | static_cast<float>( |
| 355 | static_cast<int32_t>(static_cast<uint32_t>(data[2]) | |
| 356 | (static_cast<uint32_t>(data[3]) << 8)) - |
| 357 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 358 | static_cast<float>(32768.0 * 4.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 359 | |
| 360 | trigger_haptic_current = |
| 361 | static_cast<float>( |
| 362 | static_cast<int32_t>(static_cast<uint32_t>(data[4]) | |
| 363 | (static_cast<uint32_t>(data[5]) << 8)) - |
| 364 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 365 | static_cast<float>(32768.0 * 2.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 366 | if (trigger_haptic_current > kHapticTriggerCurrentLimit) { |
| 367 | trigger_haptic_current = kHapticTriggerCurrentLimit; |
| 368 | } else if (trigger_haptic_current < -kHapticTriggerCurrentLimit) { |
| 369 | trigger_haptic_current = -kHapticTriggerCurrentLimit; |
| 370 | } |
| 371 | trigger_centering = !!(data[7] & 0x01); |
| 372 | trigger_haptics = !!(data[7] & 0x02); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static float wheel_goal_position = 0.0f; |
| 377 | static float wheel_goal_velocity = 0.0f; |
| 378 | static float wheel_haptic_current = 0.0f; |
| 379 | static float wheel_kp = 0.0f; |
| 380 | static bool wheel_centering = true; |
| 381 | static float wheel_centering_scalar = 0.25f; |
| 382 | { |
| 383 | uint8_t data[8]; |
| 384 | int length; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 385 | can_receive(data, &length, 1); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 386 | if (length == 8) { |
| 387 | last_command_time = micros(); |
| 388 | wheel_goal_position = |
| 389 | static_cast<float>( |
| 390 | static_cast<int32_t>(static_cast<uint32_t>(data[0]) | |
| 391 | (static_cast<uint32_t>(data[1]) << 8)) - |
| 392 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 393 | static_cast<float>(32768.0 * M_PI); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 394 | wheel_goal_velocity = |
| 395 | static_cast<float>( |
| 396 | static_cast<int32_t>(static_cast<uint32_t>(data[2]) | |
| 397 | (static_cast<uint32_t>(data[3]) << 8)) - |
| 398 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 399 | static_cast<float>(32768.0 * 10.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 400 | |
| 401 | wheel_haptic_current = |
| 402 | static_cast<float>( |
| 403 | static_cast<int32_t>(static_cast<uint32_t>(data[4]) | |
| 404 | (static_cast<uint32_t>(data[5]) << 8)) - |
| 405 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 406 | static_cast<float>(32768.0 * 2.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 407 | if (wheel_haptic_current > kHapticWheelCurrentLimit) { |
| 408 | wheel_haptic_current = kHapticWheelCurrentLimit; |
| 409 | } else if (wheel_haptic_current < -kHapticWheelCurrentLimit) { |
| 410 | wheel_haptic_current = -kHapticWheelCurrentLimit; |
| 411 | } |
| 412 | wheel_kp = static_cast<float>(data[6]) * 30.0f / 255.0f; |
| 413 | wheel_centering = !!(data[7] & 0x01); |
| 414 | wheel_centering_scalar = ((data[7] >> 1) & 0x7f) / 127.0f; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | static constexpr uint32_t kTimeout = 100000; |
| 419 | if (!time_after(time_add(last_command_time, kTimeout), micros())) { |
| 420 | last_command_time = time_subtract(micros(), kTimeout); |
| 421 | trigger_goal_position = 0.0f; |
| 422 | trigger_goal_velocity = 0.0f; |
| 423 | trigger_haptic_current = 0.0f; |
| 424 | trigger_centering = true; |
| 425 | trigger_haptics = false; |
| 426 | |
| 427 | wheel_goal_position = 0.0f; |
| 428 | wheel_goal_velocity = 0.0f; |
| 429 | wheel_haptic_current = 0.0f; |
| 430 | wheel_centering = true; |
| 431 | wheel_centering_scalar = 0.25f; |
Brian Silverman | 17ffa8c | 2018-03-09 18:27:29 -0800 | [diff] [blame] | 432 | // Avoid wrapping back into the valid range. |
| 433 | last_command_time = time_subtract(micros(), kTimeout); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | StateFeedbackPlant<3, 1, 1, float> *const trigger_plant = |
| 437 | global_trigger_plant.load(::std::memory_order_relaxed); |
| 438 | StateFeedbackObserver<3, 1, 1, float> *const trigger_observer = |
| 439 | global_trigger_observer.load(::std::memory_order_relaxed); |
| 440 | ::Eigen::Matrix<float, 1, 1> trigger_Y; |
| 441 | trigger_Y << absolute_trigger_angle_radians; |
| 442 | trigger_observer->Correct(*trigger_plant, |
| 443 | ::Eigen::Matrix<float, 1, 1>::Zero(), trigger_Y); |
| 444 | |
| 445 | StateFeedbackPlant<3, 1, 1, float> *const wheel_plant = |
| 446 | global_wheel_plant.load(::std::memory_order_relaxed); |
| 447 | StateFeedbackObserver<3, 1, 1, float> *const wheel_observer = |
| 448 | global_wheel_observer.load(::std::memory_order_relaxed); |
| 449 | ::Eigen::Matrix<float, 1, 1> wheel_Y; |
| 450 | wheel_Y << absolute_wheel_angle_radians; |
| 451 | wheel_observer->Correct(*wheel_plant, ::Eigen::Matrix<float, 1, 1>::Zero(), |
| 452 | wheel_Y); |
| 453 | |
| 454 | float kWheelD = (wheel_kp - 10.0f) * (0.25f - 0.20f) / 5.0f + 0.20f; |
| 455 | if (wheel_kp < 0.5f) { |
| 456 | kWheelD = wheel_kp * 0.05f / 0.5f; |
| 457 | } else if (wheel_kp < 1.0f) { |
| 458 | kWheelD = InterpolateFloat(1.0f, 0.5f, 0.06f, 0.05f, wheel_kp); |
| 459 | } else if (wheel_kp < 2.0f) { |
| 460 | kWheelD = InterpolateFloat(2.0f, 1.0f, 0.08f, 0.06f, wheel_kp); |
| 461 | } else if (wheel_kp < 3.0f) { |
| 462 | kWheelD = InterpolateFloat(3.0f, 2.0f, 0.10f, 0.08f, wheel_kp); |
| 463 | } else if (wheel_kp < 5.0f) { |
| 464 | kWheelD = InterpolateFloat(5.0f, 3.0f, 0.13f, 0.10f, wheel_kp); |
| 465 | } else if (wheel_kp < 10.0f) { |
| 466 | kWheelD = InterpolateFloat(10.0f, 5.0f, 0.20f, 0.13f, wheel_kp); |
| 467 | } |
| 468 | |
| 469 | float wheel_goal_current = wheel_haptic_current; |
| 470 | |
| 471 | wheel_goal_current += |
| 472 | (wheel_goal_position - absolute_wheel_angle_radians) * wheel_kp + |
| 473 | (wheel_goal_velocity - wheel_observer->X_hat()(1, 0)) * kWheelD; |
| 474 | |
| 475 | // Compute the torques to apply to each motor. |
| 476 | if (wheel_centering) { |
| 477 | wheel_goal_current += |
| 478 | WheelCenteringCurrent(wheel_centering_scalar, absolute_wheel_angle, |
| 479 | wheel_observer->X_hat()(1, 0)); |
| 480 | } |
| 481 | |
| 482 | if (wheel_goal_current > kHapticWheelCurrentLimit) { |
| 483 | wheel_goal_current = kHapticWheelCurrentLimit; |
| 484 | } else if (wheel_goal_current < -kHapticWheelCurrentLimit) { |
| 485 | wheel_goal_current = -kHapticWheelCurrentLimit; |
| 486 | } |
| 487 | global_wheel_current.store(wheel_goal_current, ::std::memory_order_relaxed); |
| 488 | |
| 489 | constexpr float kTriggerP = |
| 490 | static_cast<float>(::frc971::control_loops::drivetrain::kHapticTriggerP); |
| 491 | constexpr float kTriggerD = |
| 492 | static_cast<float>(::frc971::control_loops::drivetrain::kHapticTriggerD); |
| 493 | float trigger_goal_current = trigger_haptic_current; |
| 494 | if (trigger_haptics) { |
| 495 | trigger_goal_current += |
| 496 | (trigger_goal_position - absolute_trigger_angle_radians) * kTriggerP + |
| 497 | (trigger_goal_velocity - trigger_observer->X_hat()(1, 0)) * kTriggerD; |
| 498 | } |
| 499 | |
| 500 | if (trigger_centering) { |
| 501 | trigger_goal_current += TriggerCenteringCurrent(absolute_trigger_angle); |
| 502 | } |
| 503 | |
| 504 | if (trigger_goal_current > kHapticTriggerCurrentLimit) { |
| 505 | trigger_goal_current = kHapticTriggerCurrentLimit; |
| 506 | } else if (trigger_goal_current < -kHapticTriggerCurrentLimit) { |
| 507 | trigger_goal_current = -kHapticTriggerCurrentLimit; |
| 508 | } |
| 509 | global_trigger_torque.store(trigger_goal_current, |
| 510 | ::std::memory_order_relaxed); |
| 511 | |
| 512 | uint8_t buttons = 0; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 513 | if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 14)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 514 | buttons |= 0x1; |
| 515 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 516 | if (!PERIPHERAL_BITBAND(GPIOE_PDIR, 26)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 517 | buttons |= 0x2; |
| 518 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 519 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 7)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 520 | buttons |= 0x4; |
| 521 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 522 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 0)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 523 | buttons |= 0x8; |
| 524 | } |
| 525 | |
| 526 | float trigger_angle = absolute_trigger_angle; |
| 527 | |
| 528 | // Adjust the trigger range for reporting back. |
| 529 | // TODO(austin): We'll likely need to make this symmetric for the controls to |
| 530 | // work out well. |
| 531 | if (trigger_angle > kTriggerCenter) { |
| 532 | trigger_angle = (trigger_angle - kTriggerCenter) / (1.0f - kTriggerCenter); |
| 533 | } else { |
| 534 | trigger_angle = (trigger_angle - kTriggerCenter) / |
| 535 | (kTriggerCenter - kTriggerMaxExtension); |
| 536 | } |
| 537 | |
| 538 | // TODO(austin): Class + fns. This is a mess. |
| 539 | // TODO(austin): Move this to a separate file. It's too big. |
| 540 | int can_trigger = ConvertFloat16(absolute_trigger_angle); |
| 541 | int can_trigger_velocity = |
| 542 | ConvertFloat16(trigger_observer->X_hat()(1, 0) / 50.0f); |
| 543 | int can_trigger_torque = |
| 544 | ConvertFloat16(trigger_observer->X_hat()(2, 0) * 2.0f); |
| 545 | int can_trigger_current = ConvertFloat14(trigger_goal_current / 10.0f); |
| 546 | |
| 547 | int can_wheel = ConvertFloat16(absolute_wheel_angle); |
| 548 | int can_wheel_velocity = |
| 549 | ConvertFloat16(wheel_observer->X_hat()(1, 0) / 50.0f); |
| 550 | int can_wheel_torque = ConvertFloat16(wheel_observer->X_hat()(2, 0) * 2.0f); |
| 551 | int can_wheel_current = ConvertFloat14(wheel_goal_current / 10.0f); |
| 552 | |
| 553 | { |
| 554 | const uint8_t trigger_joystick_values[8] = { |
| 555 | static_cast<uint8_t>(can_trigger & 0xff), |
| 556 | static_cast<uint8_t>((can_trigger >> 8) & 0xff), |
| 557 | static_cast<uint8_t>(can_trigger_velocity & 0xff), |
| 558 | static_cast<uint8_t>((can_trigger_velocity >> 8) & 0xff), |
| 559 | static_cast<uint8_t>(can_trigger_torque & 0xff), |
| 560 | static_cast<uint8_t>((can_trigger_torque >> 8) & 0xff), |
| 561 | static_cast<uint8_t>(can_trigger_current & 0xff), |
| 562 | static_cast<uint8_t>(((buttons & 0x3) << 6) | |
| 563 | (can_trigger_current >> 8))}; |
| 564 | const uint8_t wheel_joystick_values[8] = { |
| 565 | static_cast<uint8_t>(can_wheel & 0xff), |
| 566 | static_cast<uint8_t>((can_wheel >> 8) & 0xff), |
| 567 | static_cast<uint8_t>(can_wheel_velocity & 0xff), |
| 568 | static_cast<uint8_t>((can_wheel_velocity >> 8) & 0xff), |
| 569 | static_cast<uint8_t>(can_wheel_torque & 0xff), |
| 570 | static_cast<uint8_t>((can_wheel_torque >> 8) & 0xff), |
| 571 | static_cast<uint8_t>(can_wheel_current & 0xff), |
| 572 | static_cast<uint8_t>(((buttons & 0xc) << 4) | |
| 573 | (can_wheel_current >> 8))}; |
| 574 | |
| 575 | can_send(0, trigger_joystick_values, 8, 2); |
| 576 | can_send(1, wheel_joystick_values, 8, 3); |
| 577 | } |
| 578 | |
| 579 | ::Eigen::Matrix<float, 1, 1> trigger_U; |
| 580 | trigger_U << trigger_goal_current; |
| 581 | ::Eigen::Matrix<float, 1, 1> wheel_U; |
| 582 | wheel_U << wheel_goal_current; |
| 583 | trigger_observer->Predict(trigger_plant, trigger_U, |
| 584 | ::std::chrono::milliseconds(1)); |
| 585 | wheel_observer->Predict(wheel_plant, wheel_U, ::std::chrono::milliseconds(1)); |
| 586 | } |
| 587 | |
| 588 | void ConfigurePwmFtm(BigFTM *pwm_ftm) { |
| 589 | // Put them all into combine active-high mode, and all the low ones staying |
| 590 | // off all the time by default. We'll then use only the low ones. |
| 591 | pwm_ftm->C0SC = FTM_CSC_ELSB; |
| 592 | pwm_ftm->C0V = 0; |
| 593 | pwm_ftm->C1SC = FTM_CSC_ELSB; |
| 594 | pwm_ftm->C1V = 0; |
| 595 | pwm_ftm->C2SC = FTM_CSC_ELSB; |
| 596 | pwm_ftm->C2V = 0; |
| 597 | pwm_ftm->C3SC = FTM_CSC_ELSB; |
| 598 | pwm_ftm->C3V = 0; |
| 599 | pwm_ftm->C4SC = FTM_CSC_ELSB; |
| 600 | pwm_ftm->C4V = 0; |
| 601 | pwm_ftm->C5SC = FTM_CSC_ELSB; |
| 602 | pwm_ftm->C5V = 0; |
| 603 | pwm_ftm->C6SC = FTM_CSC_ELSB; |
| 604 | pwm_ftm->C6V = 0; |
| 605 | pwm_ftm->C7SC = FTM_CSC_ELSB; |
| 606 | pwm_ftm->C7V = 0; |
| 607 | |
| 608 | pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ | |
| 609 | FTM_COMBINE_COMP3 /* Make them complementary */ | |
| 610 | FTM_COMBINE_COMBINE3 /* Combine the channels */ | |
| 611 | FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ | |
| 612 | FTM_COMBINE_COMP2 /* Make them complementary */ | |
| 613 | FTM_COMBINE_COMBINE2 /* Combine the channels */ | |
| 614 | FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ | |
| 615 | FTM_COMBINE_COMP1 /* Make them complementary */ | |
| 616 | FTM_COMBINE_COMBINE1 /* Combine the channels */ | |
| 617 | FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ | |
| 618 | FTM_COMBINE_COMP0 /* Make them complementary */ | |
| 619 | FTM_COMBINE_COMBINE0 /* Combine the channels */; |
| 620 | } |
| 621 | |
| 622 | bool CountValid(uint32_t count) { |
| 623 | static constexpr int kMaxMovement = 1; |
| 624 | return count <= kMaxMovement || count >= (4096 - kMaxMovement); |
| 625 | } |
| 626 | |
| 627 | bool ZeroMotors(uint16_t *motor0_offset, uint16_t *motor1_offset, |
| 628 | uint16_t *wheel_offset) { |
| 629 | static constexpr int kNumberSamples = 1024; |
| 630 | static_assert(UINT16_MAX * kNumberSamples <= UINT32_MAX, "Too many samples"); |
| 631 | uint32_t motor0_sum = 0, motor1_sum = 0, wheel_sum = 0; |
| 632 | |
| 633 | // First clear both encoders. |
| 634 | MOTOR0_ENCODER_FTM->CNT = MOTOR1_ENCODER_FTM->CNT = 0; |
| 635 | for (int i = 0; i < kNumberSamples; ++i) { |
| 636 | delay(1); |
| 637 | |
| 638 | if (!CountValid(MOTOR0_ENCODER_FTM->CNT)) { |
| 639 | printf("Motor 0 moved too much\n"); |
| 640 | return false; |
| 641 | } |
| 642 | if (!CountValid(MOTOR1_ENCODER_FTM->CNT)) { |
| 643 | printf("Motor 1 moved too much\n"); |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | DisableInterrupts disable_interrupts; |
| 648 | const SmallInitReadings readings = AdcReadSmallInit(disable_interrupts); |
| 649 | motor0_sum += readings.motor0_abs; |
| 650 | motor1_sum += readings.motor1_abs; |
| 651 | wheel_sum += readings.wheel_abs; |
| 652 | } |
| 653 | |
| 654 | *motor0_offset = (motor0_sum + kNumberSamples / 2) / kNumberSamples; |
| 655 | *motor1_offset = (motor1_sum + kNumberSamples / 2) / kNumberSamples; |
| 656 | *wheel_offset = (wheel_sum + kNumberSamples / 2) / kNumberSamples; |
| 657 | |
| 658 | return true; |
| 659 | } |
| 660 | |
Brian Silverman | 22e491f | 2018-09-16 17:03:24 -0700 | [diff] [blame] | 661 | // Returns an identifier for the processor we're running on. |
| 662 | // This isn't guaranteed to be unique, but it should be close enough. |
| 663 | uint8_t ProcessorIdentifier() { |
| 664 | // This XORs together all the bytes of the unique identifier provided by the |
| 665 | // hardware. |
| 666 | uint8_t r = 0; |
| 667 | for (uint8_t uid : {SIM_UIDH, SIM_UIDMH, SIM_UIDML, SIM_UIDL}) { |
| 668 | r = r ^ ((uid >> 0) & 0xFF); |
| 669 | r = r ^ ((uid >> 8) & 0xFF); |
| 670 | r = r ^ ((uid >> 16) & 0xFF); |
| 671 | r = r ^ ((uid >> 24) & 0xFF); |
| 672 | } |
| 673 | return r; |
| 674 | } |
| 675 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 676 | } // namespace |
| 677 | |
| 678 | extern "C" int main() { |
| 679 | // for background about this startup delay, please see these conversations |
| 680 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 681 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 682 | delay(400); |
| 683 | |
| 684 | // Set all interrupts to the second-lowest priority to start with. |
| 685 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 686 | |
| 687 | // Now set priorities for all the ones we care about. They only have meaning |
| 688 | // relative to each other, which means centralizing them here makes it a lot |
| 689 | // more manageable. |
| 690 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 691 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3); |
| 692 | NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0x3); |
| 693 | NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5); |
| 694 | |
| 695 | // Set the LED's pin to output mode. |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 696 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 697 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 698 | |
| 699 | // Set up the CAN pins. |
| 700 | PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 701 | PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 702 | |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 703 | // .1ms filter time. |
| 704 | PORTA_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000; |
| 705 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 706 | // BTN0 |
| 707 | PORTC_PCR7 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 708 | PORTC_DFER |= 1 << 7; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 709 | // BTN1 |
| 710 | PORTE_PCR26 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 711 | PORTE_DFER |= 1 << 26; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 712 | // BTN2 |
| 713 | PORTA_PCR14 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 714 | PORTA_DFER |= 1 << 14; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 715 | // BTN3 |
| 716 | PORTD_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 717 | PORTD_DFER |= 1 << 0; |
| 718 | // BTN4 |
| 719 | PORTD_PCR7 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 720 | PORTD_DFER |= 1 << 7; |
| 721 | // BTN5 (only new revision) |
| 722 | PORTA_PCR15 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 723 | PORTA_DFER |= 1 << 15; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 724 | |
| 725 | PORTA_PCR5 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 726 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 727 | DMA.CR = M_DMA_EMLM; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 728 | |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame^] | 729 | PrintingParameters printing_parameters; |
| 730 | printing_parameters.dedicated_usb = true; |
| 731 | const ::std::unique_ptr<PrintingImplementation> printing = |
| 732 | CreatePrinting(printing_parameters); |
| 733 | printing->Initialize(); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 734 | |
| 735 | AdcInitSmall(); |
| 736 | MathInit(); |
| 737 | delay(100); |
| 738 | can_init(2, 3); |
| 739 | |
| 740 | GPIOD_PCOR = 1 << 3; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 741 | PERIPHERAL_BITBAND(GPIOD_PDDR, 3) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 742 | PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 743 | GPIOD_PSOR = 1 << 3; |
| 744 | |
| 745 | GPIOC_PCOR = 1 << 4; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 746 | PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 747 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 748 | GPIOC_PSOR = 1 << 4; |
| 749 | |
| 750 | LittleMotorControlsImplementation controls0, controls1; |
| 751 | |
| 752 | delay(100); |
| 753 | |
| 754 | // M0_EA = FTM1_QD_PHB |
| 755 | PORTB_PCR19 = PORT_PCR_MUX(6); |
| 756 | // M0_EB = FTM1_QD_PHA |
| 757 | PORTB_PCR18 = PORT_PCR_MUX(6); |
| 758 | |
| 759 | // M1_EA = FTM1_QD_PHA |
| 760 | PORTB_PCR0 = PORT_PCR_MUX(6); |
| 761 | // M1_EB = FTM1_QD_PHB |
| 762 | PORTB_PCR1 = PORT_PCR_MUX(6); |
| 763 | |
| 764 | // M0_CH0 = FTM3_CH4 |
| 765 | PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 766 | // M0_CH1 = FTM3_CH2 |
| 767 | PORTD_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 768 | // M0_CH2 = FTM3_CH6 |
| 769 | PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 770 | |
| 771 | // M1_CH0 = FTM0_CH0 |
| 772 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 773 | // M1_CH1 = FTM0_CH2 |
| 774 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 775 | // M1_CH2 = FTM0_CH4 |
| 776 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 777 | |
| 778 | Motor motor0( |
| 779 | MOTOR0_PWM_FTM, MOTOR0_ENCODER_FTM, &controls0, |
| 780 | {&MOTOR0_PWM_FTM->C4V, &MOTOR0_PWM_FTM->C2V, &MOTOR0_PWM_FTM->C6V}); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame^] | 781 | motor0.set_printing_implementation(printing.get()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 782 | motor0.set_switching_divisor(kSwitchingDivisor); |
| 783 | Motor motor1( |
| 784 | MOTOR1_PWM_FTM, MOTOR1_ENCODER_FTM, &controls1, |
| 785 | {&MOTOR1_PWM_FTM->C0V, &MOTOR1_PWM_FTM->C2V, &MOTOR1_PWM_FTM->C4V}); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame^] | 786 | motor1.set_printing_implementation(printing.get()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 787 | motor1.set_switching_divisor(kSwitchingDivisor); |
| 788 | ConfigurePwmFtm(MOTOR0_PWM_FTM); |
| 789 | ConfigurePwmFtm(MOTOR1_PWM_FTM); |
| 790 | motor0.Init(); |
| 791 | motor1.Init(); |
| 792 | global_motor0.store(&motor0, ::std::memory_order_relaxed); |
| 793 | global_motor1.store(&motor1, ::std::memory_order_relaxed); |
| 794 | |
| 795 | SIM_SCGC6 |= SIM_SCGC6_PIT; |
Brian Silverman | b0de240 | 2018-03-24 03:48:28 -0400 | [diff] [blame] | 796 | // Workaround for errata e7914. |
| 797 | (void)PIT_MCR; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 798 | PIT_MCR = 0; |
Brian Silverman | b0de240 | 2018-03-24 03:48:28 -0400 | [diff] [blame] | 799 | PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 1000) - 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 800 | PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN; |
| 801 | |
| 802 | // Have them both wait for the GTB signal. |
| 803 | FTM0->CONF = FTM3->CONF = |
| 804 | FTM_CONF_GTBEEN | FTM_CONF_NUMTOF(kSwitchingDivisor - 1); |
| 805 | // Make FTM3's period half of what it should be so we can get it a half-cycle |
| 806 | // out of phase. |
| 807 | const uint32_t original_mod = FTM3->MOD; |
| 808 | FTM3->MOD = ((original_mod + 1) / 2) - 1; |
| 809 | FTM3->SYNC |= FTM_SYNC_SWSYNC; |
| 810 | |
| 811 | // Output triggers to things like the PDBs on initialization. |
| 812 | FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN; |
| 813 | FTM3_EXTTRIG = FTM_EXTTRIG_INITTRIGEN; |
| 814 | // Don't let any memory accesses sneak past here, because we actually |
| 815 | // need everything to be starting up. |
| 816 | __asm__("" ::: "memory"); |
| 817 | |
| 818 | // Give everything a chance to get going. |
| 819 | delay(100); |
| 820 | |
| 821 | printf("BSS: %p-%p\n", __bss_ram_start__, __bss_ram_end__); |
| 822 | printf("data: %p-%p\n", __data_ram_start__, __data_ram_end__); |
| 823 | printf("heap start: %p\n", __heap_start__); |
| 824 | printf("stack start: %p\n", __stack_end__); |
| 825 | |
Brian Silverman | 22e491f | 2018-09-16 17:03:24 -0700 | [diff] [blame] | 826 | printf("Zeroing motors for %x\n", (unsigned int)ProcessorIdentifier()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 827 | uint16_t motor0_offset, motor1_offset, wheel_offset; |
| 828 | while (!ZeroMotors(&motor0_offset, &motor1_offset, &wheel_offset)) { |
| 829 | } |
| 830 | printf("Done zeroing\n"); |
| 831 | |
| 832 | const float motor0_offset_scaled = -analog_ratio(motor0_offset); |
| 833 | const float motor1_offset_scaled = analog_ratio(motor1_offset); |
| 834 | // Good for the initial trigger. |
| 835 | { |
| 836 | constexpr float kZeroOffset0 = 0.27f; |
| 837 | const int motor0_starting_point = static_cast<int>( |
| 838 | (motor0_offset_scaled + (kZeroOffset0 / 7.0f)) * 4096.0f); |
| 839 | printf("Motor 0 starting at %d\n", motor0_starting_point); |
| 840 | motor0.set_encoder_calibration_offset(motor0_starting_point); |
| 841 | motor0.set_encoder_multiplier(-1); |
| 842 | |
| 843 | // Calibrate neutral here. |
| 844 | motor0.set_encoder_offset(motor0.encoder_offset() - 2065 + 20); |
| 845 | |
| 846 | uint32_t new_encoder = motor0.wrapped_encoder(); |
| 847 | int32_t absolute_encoder = motor0.absolute_encoder(new_encoder); |
| 848 | printf("Motor 0 encoder %d absolute %d\n", static_cast<int>(new_encoder), |
| 849 | static_cast<int>(absolute_encoder)); |
| 850 | } |
| 851 | |
| 852 | { |
| 853 | constexpr float kZeroOffset1 = 0.26f; |
| 854 | const int motor1_starting_point = static_cast<int>( |
| 855 | (motor1_offset_scaled + (kZeroOffset1 / 7.0f)) * 4096.0f); |
| 856 | printf("Motor 1 starting at %d\n", motor1_starting_point); |
| 857 | motor1.set_encoder_calibration_offset(motor1_starting_point); |
| 858 | motor1.set_encoder_multiplier(-1); |
| 859 | |
| 860 | float wheel_position = absolute_wheel(analog_ratio(wheel_offset)); |
| 861 | |
| 862 | uint32_t encoder = motor1.wrapped_encoder(); |
| 863 | |
| 864 | printf("Wheel starting at %d, encoder %" PRId32 "\n", |
| 865 | static_cast<int>(wheel_position * 1000.0f), encoder); |
| 866 | |
| 867 | constexpr float kWheelGearRatio = (1.25f + 0.02f) / 0.35f; |
| 868 | constexpr float kWrappedWheelAtZero = 0.6586310546875f; |
| 869 | |
| 870 | const int encoder_wraps = |
| 871 | static_cast<int>(lround(wheel_position * kWheelGearRatio - |
| 872 | (encoder / 4096.f) + kWrappedWheelAtZero)); |
| 873 | |
| 874 | printf("Wraps: %d\n", encoder_wraps); |
| 875 | motor1.set_encoder_offset(4096 * encoder_wraps + motor1.encoder_offset() - |
| 876 | static_cast<int>(kWrappedWheelAtZero * 4096)); |
| 877 | printf("Wheel encoder now at %d\n", |
| 878 | static_cast<int>(1000.f / 4096.f * |
| 879 | motor1.absolute_encoder(motor1.wrapped_encoder()))); |
| 880 | } |
| 881 | |
| 882 | // Turn an LED on for Austin. |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 883 | PERIPHERAL_BITBAND(GPIOC_PDDR, 6) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 884 | GPIOC_PCOR = 1 << 6; |
| 885 | PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 886 | |
| 887 | // M0_THW |
| 888 | PORTC_PCR11 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 889 | // M0_FAULT |
| 890 | PORTD_PCR6 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 891 | // M1_THW |
| 892 | PORTC_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 893 | // M1_FAULT |
| 894 | PORTD_PCR5 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 895 | |
| 896 | motor0.Start(); |
| 897 | motor1.Start(); |
| 898 | { |
| 899 | // We rely on various things happening faster than the timer period, so make |
| 900 | // sure slow USB or whatever interrupts don't prevent that. |
| 901 | DisableInterrupts disable_interrupts; |
| 902 | |
| 903 | // First clear the overflow flag. |
| 904 | FTM3->SC &= ~FTM_SC_TOF; |
| 905 | |
| 906 | // Now poke the GTB to actually start both timers. |
| 907 | FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_GTBEOUT | |
| 908 | FTM_CONF_NUMTOF(kSwitchingDivisor - 1); |
| 909 | |
| 910 | // Wait for it to overflow twice. For some reason, just once doesn't work. |
| 911 | while (!(FTM3->SC & FTM_SC_TOF)) { |
| 912 | } |
| 913 | FTM3->SC &= ~FTM_SC_TOF; |
| 914 | while (!(FTM3->SC & FTM_SC_TOF)) { |
| 915 | } |
| 916 | |
| 917 | // Now put the MOD value back to what it was. |
| 918 | FTM3->MOD = original_mod; |
| 919 | FTM3->PWMLOAD = FTM_PWMLOAD_LDOK; |
| 920 | |
| 921 | // And then clear the overflow flags before enabling interrupts so we |
| 922 | // actually wait until the next overflow to start doing interrupts. |
| 923 | FTM0->SC &= ~FTM_SC_TOF; |
| 924 | FTM3->SC &= ~FTM_SC_TOF; |
| 925 | NVIC_ENABLE_IRQ(IRQ_FTM0); |
| 926 | NVIC_ENABLE_IRQ(IRQ_FTM3); |
| 927 | } |
| 928 | global_trigger_plant.store( |
| 929 | new StateFeedbackPlant<3, 1, 1, float>(MakeIntegralHapticTriggerPlant())); |
| 930 | global_trigger_observer.store(new StateFeedbackObserver<3, 1, 1, float>( |
| 931 | MakeIntegralHapticTriggerObserver())); |
| 932 | global_trigger_observer.load(::std::memory_order_relaxed) |
| 933 | ->Reset(global_trigger_plant.load(::std::memory_order_relaxed)); |
| 934 | |
| 935 | global_wheel_plant.store( |
| 936 | new StateFeedbackPlant<3, 1, 1, float>(MakeIntegralHapticWheelPlant())); |
| 937 | global_wheel_observer.store(new StateFeedbackObserver<3, 1, 1, float>( |
| 938 | MakeIntegralHapticWheelObserver())); |
| 939 | global_wheel_observer.load(::std::memory_order_relaxed) |
| 940 | ->Reset(global_wheel_plant.load(::std::memory_order_relaxed)); |
| 941 | |
| 942 | delay(1000); |
| 943 | |
| 944 | NVIC_ENABLE_IRQ(IRQ_PIT_CH3); |
| 945 | |
| 946 | // TODO(Brian): Use SLEEPONEXIT to reduce interrupt latency? |
| 947 | while (true) { |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 948 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 11)) { |
| 949 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 950 | printf("M0_THW\n"); |
| 951 | } |
| 952 | GPIOC_PSOR = 1 << 5; |
| 953 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 954 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 6)) { |
| 955 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 956 | printf("M0_FAULT\n"); |
| 957 | } |
| 958 | GPIOC_PSOR = 1 << 5; |
| 959 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 960 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 2)) { |
| 961 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 962 | printf("M1_THW\n"); |
| 963 | } |
| 964 | GPIOC_PSOR = 1 << 5; |
| 965 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 966 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 5)) { |
| 967 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 968 | printf("M1_FAULT\n"); |
| 969 | } |
| 970 | GPIOC_PSOR = 1 << 5; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 977 | } // namespace motors |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 978 | } // namespace frc971 |