Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 1 | // This file has the main for the Teensy on the simple receiver board. |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | #include <atomic> |
| 6 | #include <cmath> |
| 7 | |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 8 | #include "frc971/control_loops/drivetrain/polydrivetrain.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 9 | #include "motors/core/kinetis.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 10 | #include "motors/core/time.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 11 | #include "motors/peripheral/adc.h" |
| 12 | #include "motors/peripheral/can.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 13 | #include "motors/peripheral/configuration.h" |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 14 | #include "motors/seems_reasonable/drivetrain_dog_motor_plant.h" |
| 15 | #include "motors/seems_reasonable/polydrivetrain_dog_motor_plant.h" |
| 16 | #include "motors/seems_reasonable/spring.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 17 | #include "motors/usb/cdc.h" |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 18 | #include "motors/usb/usb.h" |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 19 | #include "motors/util.h" |
| 20 | |
| 21 | namespace frc971 { |
| 22 | namespace motors { |
| 23 | namespace { |
| 24 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 25 | using ::frc971::control_loops::drivetrain::DrivetrainConfig; |
| 26 | using ::frc971::control_loops::drivetrain::PolyDrivetrain; |
| 27 | using ::frc971::constants::ShifterHallEffect; |
| 28 | using ::frc971::control_loops::DrivetrainQueue_Goal; |
| 29 | using ::frc971::control_loops::DrivetrainQueue_Output; |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 30 | using ::motors::seems_reasonable::Spring; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 31 | |
| 32 | const ShifterHallEffect kThreeStateDriveShifter{0.0, 0.0, 0.25, 0.75}; |
| 33 | |
| 34 | const DrivetrainConfig<float> &GetDrivetrainConfig() { |
| 35 | static DrivetrainConfig<float> kDrivetrainConfig{ |
| 36 | ::frc971::control_loops::drivetrain::ShifterType::NO_SHIFTER, |
| 37 | ::frc971::control_loops::drivetrain::LoopType::OPEN_LOOP, |
| 38 | ::frc971::control_loops::drivetrain::GyroType::SPARTAN_GYRO, |
| 39 | ::frc971::control_loops::drivetrain::IMUType::IMU_X, |
| 40 | |
| 41 | ::motors::seems_reasonable::MakeDrivetrainLoop, |
| 42 | ::motors::seems_reasonable::MakeVelocityDrivetrainLoop, |
| 43 | ::std::function<StateFeedbackLoop<7, 2, 4, float>()>(), |
| 44 | |
| 45 | ::motors::seems_reasonable::kDt, ::motors::seems_reasonable::kRobotRadius, |
| 46 | ::motors::seems_reasonable::kWheelRadius, ::motors::seems_reasonable::kV, |
| 47 | |
| 48 | ::motors::seems_reasonable::kHighGearRatio, |
| 49 | ::motors::seems_reasonable::kLowGearRatio, kThreeStateDriveShifter, |
| 50 | kThreeStateDriveShifter, true /* default_high_gear */, |
| 51 | 0 /* down_offset if using constants use |
| 52 | constants::GetValues().down_error */, 0.8 /* wheel_non_linearity */, |
| 53 | 1.2 /* quickturn_wheel_multiplier */, 1.5 /* wheel_multiplier */, |
| 54 | }; |
| 55 | |
| 56 | return kDrivetrainConfig; |
| 57 | }; |
| 58 | |
| 59 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 60 | ::std::atomic<teensy::AcmTty *> global_stdout{nullptr}; |
| 61 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 62 | ::std::atomic<PolyDrivetrain<float> *> global_polydrivetrain{nullptr}; |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 63 | ::std::atomic<Spring *> global_spring{nullptr}; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 64 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 65 | // Last width we received on each channel. |
| 66 | uint16_t pwm_input_widths[5]; |
| 67 | // When we received a pulse on each channel in milliseconds. |
| 68 | uint32_t pwm_input_times[5]; |
| 69 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 70 | constexpr int kChannelTimeout = 100; |
| 71 | |
| 72 | bool lost_channel(int channel) { |
| 73 | DisableInterrupts disable_interrupts; |
| 74 | if (time_after(millis(), |
| 75 | time_add(pwm_input_times[channel], kChannelTimeout))) { |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 81 | // Returns the most recently captured value for the specified input channel |
| 82 | // scaled from -1 to 1, or 0 if it was captured over 100ms ago. |
| 83 | float convert_input_width(int channel) { |
| 84 | uint16_t width; |
| 85 | { |
| 86 | DisableInterrupts disable_interrupts; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 87 | if (time_after(millis(), |
| 88 | time_add(pwm_input_times[channel], kChannelTimeout))) { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | width = pwm_input_widths[channel]; |
| 93 | } |
| 94 | |
| 95 | // Values measured with a channel mapped to a button. |
| 96 | static constexpr uint16_t kMinWidth = 4133; |
| 97 | static constexpr uint16_t kMaxWidth = 7177; |
| 98 | if (width < kMinWidth) { |
| 99 | width = kMinWidth; |
| 100 | } else if (width > kMaxWidth) { |
| 101 | width = kMaxWidth; |
| 102 | } |
| 103 | return (static_cast<float>(2 * (width - kMinWidth)) / |
| 104 | static_cast<float>(kMaxWidth - kMinWidth)) - |
| 105 | 1.0f; |
| 106 | } |
| 107 | |
| 108 | // Sends a SET_RPM command to the specified VESC. |
| 109 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 110 | // bandwidth. |
| 111 | void vesc_set_rpm(int vesc_id, float rpm) { |
| 112 | const int32_t rpm_int = rpm; |
| 113 | uint32_t id = CAN_EFF_FLAG; |
| 114 | id |= vesc_id; |
| 115 | id |= (0x03 /* SET_RPM */) << 8; |
| 116 | uint8_t data[4] = { |
| 117 | static_cast<uint8_t>((rpm_int >> 24) & 0xFF), |
| 118 | static_cast<uint8_t>((rpm_int >> 16) & 0xFF), |
| 119 | static_cast<uint8_t>((rpm_int >> 8) & 0xFF), |
| 120 | static_cast<uint8_t>((rpm_int >> 0) & 0xFF), |
| 121 | }; |
| 122 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 123 | } |
| 124 | |
| 125 | // Sends a SET_CURRENT command to the specified VESC. |
| 126 | // current is in amps. |
| 127 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 128 | // bandwidth. |
| 129 | void vesc_set_current(int vesc_id, float current) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 130 | constexpr float kMaxCurrent = 80.0f; |
| 131 | const int32_t current_int = |
| 132 | ::std::max(-kMaxCurrent, ::std::min(kMaxCurrent, current)) * 1000.0f; |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 133 | uint32_t id = CAN_EFF_FLAG; |
| 134 | id |= vesc_id; |
| 135 | id |= (0x01 /* SET_CURRENT */) << 8; |
| 136 | uint8_t data[4] = { |
| 137 | static_cast<uint8_t>((current_int >> 24) & 0xFF), |
| 138 | static_cast<uint8_t>((current_int >> 16) & 0xFF), |
| 139 | static_cast<uint8_t>((current_int >> 8) & 0xFF), |
| 140 | static_cast<uint8_t>((current_int >> 0) & 0xFF), |
| 141 | }; |
| 142 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 143 | } |
| 144 | |
Brian Silverman | 4d1e527 | 2018-03-26 03:18:42 -0400 | [diff] [blame] | 145 | // Sends a SET_DUTY command to the specified VESC. |
| 146 | // duty is from -1 to 1. |
| 147 | // Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN |
| 148 | // bandwidth. |
| 149 | void vesc_set_duty(int vesc_id, float duty) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 150 | constexpr int32_t kMaxDuty = 99999; |
| 151 | const int32_t duty_int = ::std::max( |
| 152 | -kMaxDuty, ::std::min(kMaxDuty, static_cast<int32_t>(duty * 100000.0f))); |
Brian Silverman | 4d1e527 | 2018-03-26 03:18:42 -0400 | [diff] [blame] | 153 | uint32_t id = CAN_EFF_FLAG; |
| 154 | id |= vesc_id; |
| 155 | id |= (0x00 /* SET_DUTY */) << 8; |
| 156 | uint8_t data[4] = { |
| 157 | static_cast<uint8_t>((duty_int >> 24) & 0xFF), |
| 158 | static_cast<uint8_t>((duty_int >> 16) & 0xFF), |
| 159 | static_cast<uint8_t>((duty_int >> 8) & 0xFF), |
| 160 | static_cast<uint8_t>((duty_int >> 0) & 0xFF), |
| 161 | }; |
| 162 | can_send(id, data, sizeof(data), 2 + vesc_id); |
| 163 | } |
| 164 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 165 | void DoVescTest() { |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 166 | uint32_t time = micros(); |
| 167 | while (true) { |
| 168 | for (int i = 0; i < 6; ++i) { |
| 169 | const uint32_t end = time_add(time, 500000); |
| 170 | while (true) { |
| 171 | const bool done = time_after(micros(), end); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 172 | float current; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 173 | if (done) { |
| 174 | current = -6; |
| 175 | } else { |
| 176 | current = 6; |
| 177 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 178 | vesc_set_current(i, current); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 179 | if (done) { |
| 180 | break; |
| 181 | } |
| 182 | delay(5); |
| 183 | } |
| 184 | time = end; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 189 | void DoReceiverTest2() { |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 190 | static constexpr float kMaxRpm = 10000.0f; |
| 191 | while (true) { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 192 | const bool flip = convert_input_width(2) > 0; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 193 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 194 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 195 | const float value = convert_input_width(0); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 196 | |
| 197 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 198 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 199 | if (flip) { |
| 200 | rpm *= -1.0f; |
| 201 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 202 | vesc_set_rpm(0, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 206 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 207 | if (flip) { |
| 208 | rpm *= -1.0f; |
| 209 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 210 | vesc_set_rpm(1, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 214 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 215 | const float value = convert_input_width(1); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 216 | |
| 217 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 218 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 219 | if (flip) { |
| 220 | rpm *= -1.0f; |
| 221 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 222 | vesc_set_rpm(2, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 226 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 227 | if (flip) { |
| 228 | rpm *= -1.0f; |
| 229 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 230 | vesc_set_rpm(3, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 234 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 235 | const float value = convert_input_width(4); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 236 | |
| 237 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 238 | float rpm = ::std::min(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 239 | if (flip) { |
| 240 | rpm *= -1.0f; |
| 241 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 242 | vesc_set_rpm(4, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | { |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 246 | float rpm = ::std::max(0.0f, value) * kMaxRpm; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 247 | if (flip) { |
| 248 | rpm *= -1.0f; |
| 249 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 250 | vesc_set_rpm(5, rpm); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 253 | // Give the CAN frames a chance to go out. |
| 254 | delay(5); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | void SetupPwmFtm(BigFTM *ftm) { |
| 259 | ftm->MODE = FTM_MODE_WPDIS; |
| 260 | ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 261 | ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */; |
| 262 | |
| 263 | // Can't change MOD according to the reference manual ("The Dual Edge Capture |
| 264 | // mode must be used with ... the FTM counter in Free running counter."). |
| 265 | ftm->MOD = 0xFFFF; |
| 266 | |
| 267 | // Capturing rising edge. |
| 268 | ftm->C0SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 269 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 270 | ftm->C1SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 271 | |
| 272 | // Capturing rising edge. |
| 273 | ftm->C2SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 274 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 275 | ftm->C3SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 276 | |
| 277 | // Capturing rising edge. |
| 278 | ftm->C4SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 279 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 280 | ftm->C5SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 281 | |
| 282 | // Capturing rising edge. |
| 283 | ftm->C6SC = FTM_CSC_MSA | FTM_CSC_ELSA; |
| 284 | // Capturing falling edge. |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 285 | ftm->C7SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 286 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 287 | (void)ftm->STATUS; |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 288 | ftm->STATUS = 0x00; |
| 289 | |
| 290 | ftm->COMBINE = FTM_COMBINE_DECAP3 | FTM_COMBINE_DECAPEN3 | |
| 291 | FTM_COMBINE_DECAP2 | FTM_COMBINE_DECAPEN2 | |
| 292 | FTM_COMBINE_DECAP1 | FTM_COMBINE_DECAPEN1 | |
| 293 | FTM_COMBINE_DECAP0 | FTM_COMBINE_DECAPEN0; |
| 294 | |
| 295 | // 34.95ms max period before it starts wrapping and being weird. |
| 296 | ftm->SC = FTM_SC_CLKS(1) /* Use the system clock */ | |
| 297 | FTM_SC_PS(4) /* Prescaler=32 */; |
| 298 | |
| 299 | ftm->MODE &= ~FTM_MODE_WPDIS; |
| 300 | } |
| 301 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 302 | extern "C" void ftm0_isr() { |
| 303 | while (true) { |
| 304 | const uint32_t status = FTM0->STATUS; |
| 305 | if (status == 0) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if (status & (1 << 1)) { |
| 310 | const uint32_t start = FTM0->C0V; |
| 311 | const uint32_t end = FTM0->C1V; |
| 312 | pwm_input_widths[0] = (end - start) & 0xFFFF; |
| 313 | pwm_input_times[0] = millis(); |
| 314 | } |
| 315 | if (status & (1 << 7)) { |
| 316 | const uint32_t start = FTM0->C6V; |
| 317 | const uint32_t end = FTM0->C7V; |
| 318 | pwm_input_widths[1] = (end - start) & 0xFFFF; |
| 319 | pwm_input_times[1] = millis(); |
| 320 | } |
| 321 | if (status & (1 << 5)) { |
| 322 | const uint32_t start = FTM0->C4V; |
| 323 | const uint32_t end = FTM0->C5V; |
| 324 | pwm_input_widths[2] = (end - start) & 0xFFFF; |
| 325 | pwm_input_times[2] = millis(); |
| 326 | } |
| 327 | if (status & (1 << 3)) { |
| 328 | const uint32_t start = FTM0->C2V; |
| 329 | const uint32_t end = FTM0->C3V; |
| 330 | pwm_input_widths[4] = (end - start) & 0xFFFF; |
| 331 | pwm_input_times[4] = millis(); |
| 332 | } |
| 333 | |
| 334 | FTM0->STATUS = 0; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | extern "C" void ftm3_isr() { |
| 339 | while (true) { |
| 340 | const uint32_t status = FTM3->STATUS; |
| 341 | if (status == 0) { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (status & (1 << 3)) { |
| 346 | const uint32_t start = FTM3->C2V; |
| 347 | const uint32_t end = FTM3->C3V; |
| 348 | pwm_input_widths[3] = (end - start) & 0xFFFF; |
| 349 | pwm_input_times[3] = millis(); |
| 350 | } |
| 351 | |
| 352 | FTM3->STATUS = 0; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | float ConvertEncoderChannel(uint16_t reading) { |
| 357 | // Theoretical values based on the datasheet are 931 and 2917. |
| 358 | // With these values, the magnitude ranges from 0.99-1.03, which works fine |
| 359 | // (the encoder's output appears to get less accurate in one quadrant for some |
| 360 | // reason, hence the variation). |
| 361 | static constexpr uint16_t kMin = 802, kMax = 3088; |
| 362 | if (reading < kMin) { |
| 363 | reading = kMin; |
| 364 | } else if (reading > kMax) { |
| 365 | reading = kMax; |
| 366 | } |
| 367 | return (static_cast<float>(2 * (reading - kMin)) / |
| 368 | static_cast<float>(kMax - kMin)) - |
| 369 | 1.0f; |
| 370 | } |
| 371 | |
| 372 | struct EncoderReading { |
| 373 | EncoderReading(const salsa::SimpleAdcReadings &adc_readings) { |
| 374 | const float sin = ConvertEncoderChannel(adc_readings.sin); |
| 375 | const float cos = ConvertEncoderChannel(adc_readings.cos); |
| 376 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 377 | const float magnitude = hypot(sin, cos); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 378 | const float magnitude_error = ::std::abs(magnitude - 1.0f); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 379 | valid = magnitude_error < 0.30f; |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 380 | |
| 381 | angle = ::std::atan2(sin, cos); |
| 382 | } |
| 383 | |
| 384 | // Angle in radians, in [-pi, pi]. |
| 385 | float angle; |
| 386 | |
| 387 | bool valid; |
| 388 | }; |
| 389 | |
| 390 | extern "C" void pit3_isr() { |
| 391 | PIT_TFLG3 = 1; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 392 | PolyDrivetrain<float> *polydrivetrain = |
| 393 | global_polydrivetrain.load(::std::memory_order_acquire); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 394 | Spring *spring = global_spring.load(::std::memory_order_acquire); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 395 | |
| 396 | salsa::SimpleAdcReadings adc_readings; |
| 397 | { |
| 398 | DisableInterrupts disable_interrupts; |
| 399 | adc_readings = salsa::AdcReadSimple(disable_interrupts); |
| 400 | } |
| 401 | |
| 402 | EncoderReading encoder(adc_readings); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 403 | static float last_good_encoder = 0.0f; |
| 404 | static int invalid_encoder_count = 0; |
| 405 | if (encoder.valid) { |
| 406 | last_good_encoder = encoder.angle; |
| 407 | invalid_encoder_count = 0; |
| 408 | } else { |
| 409 | ++invalid_encoder_count; |
| 410 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 411 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 412 | const bool lost_any_channel = lost_channel(0) || lost_channel(1) || |
| 413 | lost_channel(2) || lost_channel(3) || |
| 414 | lost_channel(4) || |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 415 | (::std::abs(convert_input_width(4)) < 0.5f); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 416 | |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 417 | if (polydrivetrain != nullptr && spring != nullptr) { |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 418 | DrivetrainQueue_Goal goal; |
| 419 | goal.control_loop_driving = false; |
| 420 | if (lost_any_channel) { |
| 421 | goal.throttle = 0.0f; |
| 422 | goal.wheel = 0.0f; |
| 423 | } else { |
| 424 | goal.throttle = convert_input_width(1); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 425 | goal.wheel = -convert_input_width(0); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 426 | } |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 427 | goal.quickturn = ::std::abs(polydrivetrain->velocity()) < 0.25f; |
| 428 | |
| 429 | DrivetrainQueue_Output output; |
| 430 | |
| 431 | polydrivetrain->SetGoal(goal); |
| 432 | polydrivetrain->Update(); |
| 433 | polydrivetrain->SetOutput(&output); |
| 434 | |
| 435 | vesc_set_duty(0, -output.left_voltage / 12.0f); |
| 436 | vesc_set_duty(1, -output.left_voltage / 12.0f); |
| 437 | |
| 438 | vesc_set_duty(2, output.right_voltage / 12.0f); |
| 439 | vesc_set_duty(3, output.right_voltage / 12.0f); |
| 440 | |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 441 | bool prime = convert_input_width(2) > 0.1f; |
| 442 | bool fire = convert_input_width(3) > 0.1f; |
| 443 | |
| 444 | bool unload = lost_any_channel; |
| 445 | static bool was_lost = true; |
| 446 | bool force_reset = !lost_any_channel && was_lost; |
| 447 | was_lost = lost_any_channel; |
| 448 | |
| 449 | spring->Iterate(unload, prime, fire, force_reset, invalid_encoder_count <= 2, |
| 450 | last_good_encoder); |
| 451 | |
| 452 | float spring_output = spring->output(); |
| 453 | |
| 454 | vesc_set_duty(4, -spring_output); |
| 455 | vesc_set_duty(5, spring_output); |
| 456 | |
| 457 | /* |
| 458 | // Massive debug. Turn on for useful bits. |
| 459 | if (!encoder.valid) { |
| 460 | printf("Stuck encoder: ADC %" PRIu16 " %" PRIu16 |
| 461 | " enc %d/1000 %s mag %d\n", |
| 462 | adc_readings.sin, adc_readings.cos, (int)(encoder.angle * 1000), |
| 463 | encoder.valid ? "T" : "f", |
| 464 | (int)(hypot(ConvertEncoderChannel(adc_readings.sin), |
| 465 | ConvertEncoderChannel(adc_readings.cos)) * |
| 466 | 1000)); |
| 467 | } |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 468 | static int i = 0; |
| 469 | ++i; |
| 470 | if (i > 20) { |
| 471 | i = 0; |
| 472 | if (lost_any_channel) { |
| 473 | printf("200Hz loop, disabled %d %d %d %d %d\n", |
| 474 | (int)(convert_input_width(0) * 1000), |
| 475 | (int)(convert_input_width(1) * 1000), |
| 476 | (int)(convert_input_width(2) * 1000), |
| 477 | (int)(convert_input_width(3) * 1000), |
| 478 | (int)(convert_input_width(4) * 1000)); |
| 479 | } else { |
| 480 | printf( |
| 481 | "TODO(Austin): 200Hz loop %d %d %d %d %d, lr, %d, %d velocity %d " |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 482 | " state: %d, near %d angle %d goal %d to: %d ADC %" PRIu16 |
| 483 | " %" PRIu16 " enc %d/1000 %s from %d\n", |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 484 | (int)(convert_input_width(0) * 1000), |
| 485 | (int)(convert_input_width(1) * 1000), |
| 486 | (int)(convert_input_width(2) * 1000), |
| 487 | (int)(convert_input_width(3) * 1000), |
| 488 | (int)(convert_input_width(4) * 1000), |
| 489 | static_cast<int>(output.left_voltage * 100), |
| 490 | static_cast<int>(output.right_voltage * 100), |
| 491 | static_cast<int>(polydrivetrain->velocity() * 100), |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 492 | static_cast<int>(spring->state()), static_cast<int>(spring->Near()), |
| 493 | static_cast<int>(spring->angle() * 1000), |
| 494 | static_cast<int>(spring->goal() * 1000), |
| 495 | static_cast<int>(spring->timeout()), adc_readings.sin, |
| 496 | adc_readings.cos, (int)(encoder.angle * 1000), |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 497 | encoder.valid ? "T" : "f", |
| 498 | (int)(::std::sqrt(ConvertEncoderChannel(adc_readings.sin) * |
| 499 | ConvertEncoderChannel(adc_readings.sin) + |
| 500 | ConvertEncoderChannel(adc_readings.cos) * |
| 501 | ConvertEncoderChannel(adc_readings.cos)) * |
| 502 | 1000)); |
| 503 | } |
| 504 | } |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 505 | */ |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 506 | } |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 509 | } // namespace |
| 510 | |
| 511 | extern "C" { |
| 512 | |
| 513 | void *__stack_chk_guard = (void *)0x67111971; |
| 514 | |
| 515 | int _write(int /*file*/, char *ptr, int len) { |
| 516 | teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire); |
| 517 | if (tty != nullptr) { |
| 518 | return tty->Write(ptr, len); |
| 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | void __stack_chk_fail(void); |
| 524 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 525 | } // extern "C" |
| 526 | |
| 527 | extern "C" int main(void) { |
| 528 | // for background about this startup delay, please see these conversations |
| 529 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 530 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 531 | delay(400); |
| 532 | |
| 533 | // Set all interrupts to the second-lowest priority to start with. |
| 534 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 535 | |
| 536 | // Now set priorities for all the ones we care about. They only have meaning |
| 537 | // relative to each other, which means centralizing them here makes it a lot |
| 538 | // more manageable. |
| 539 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 540 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0xa); |
| 541 | NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0xa); |
| 542 | NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 543 | |
| 544 | // Builtin LED. |
| 545 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1; |
| 546 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1); |
| 547 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
| 548 | |
| 549 | // Set up the CAN pins. |
| 550 | PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 551 | PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 552 | |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 553 | // PWM_IN0 |
| 554 | // FTM0_CH0 |
| 555 | PORTC_PCR1 = PORT_PCR_MUX(4); |
| 556 | |
| 557 | // PWM_IN1 |
| 558 | // FTM0_CH6 |
| 559 | PORTD_PCR6 = PORT_PCR_MUX(4); |
| 560 | |
| 561 | // PWM_IN2 |
| 562 | // FTM0_CH4 |
| 563 | PORTD_PCR4 = PORT_PCR_MUX(4); |
| 564 | |
| 565 | // PWM_IN3 |
| 566 | // FTM3_CH2 |
| 567 | PORTD_PCR2 = PORT_PCR_MUX(4); |
| 568 | |
| 569 | // PWM_IN4 |
| 570 | // FTM0_CH2 |
| 571 | PORTC_PCR3 = PORT_PCR_MUX(4); |
| 572 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 573 | delay(100); |
| 574 | |
| 575 | teensy::UsbDevice usb_device(0, 0x16c0, 0x0492); |
| 576 | usb_device.SetManufacturer("Seems Reasonable LLC"); |
| 577 | usb_device.SetProduct("Simple Receiver Board"); |
| 578 | |
| 579 | teensy::AcmTty tty0(&usb_device); |
| 580 | global_stdout.store(&tty0, ::std::memory_order_release); |
| 581 | usb_device.Initialize(); |
| 582 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 583 | SIM_SCGC6 |= SIM_SCGC6_PIT; |
| 584 | // Workaround for errata e7914. |
| 585 | (void)PIT_MCR; |
| 586 | PIT_MCR = 0; |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 587 | PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 200) - 1; |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 588 | PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN; |
| 589 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 590 | can_init(0, 1); |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 591 | salsa::AdcInitSimple(); |
Brian Silverman | 4f8c6a7 | 2018-03-17 23:12:45 -0700 | [diff] [blame] | 592 | SetupPwmFtm(FTM0); |
| 593 | SetupPwmFtm(FTM3); |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 594 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 595 | PolyDrivetrain<float> polydrivetrain(GetDrivetrainConfig(), nullptr); |
| 596 | global_polydrivetrain.store(&polydrivetrain, ::std::memory_order_release); |
Austin Schuh | 4fae0fc | 2018-03-27 23:51:42 -0700 | [diff] [blame^] | 597 | Spring spring; |
| 598 | global_spring.store(&spring, ::std::memory_order_release); |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 599 | |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 600 | // Leave the LEDs on for a bit longer. |
| 601 | delay(300); |
| 602 | printf("Done starting up\n"); |
| 603 | |
| 604 | // Done starting up, now turn the LED off. |
| 605 | PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0; |
| 606 | |
Brian Silverman | a3a172b | 2018-03-24 03:53:32 -0400 | [diff] [blame] | 607 | NVIC_ENABLE_IRQ(IRQ_FTM0); |
| 608 | NVIC_ENABLE_IRQ(IRQ_FTM3); |
| 609 | NVIC_ENABLE_IRQ(IRQ_PIT_CH3); |
| 610 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 611 | while (true) { |
| 612 | } |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | void __stack_chk_fail(void) { |
| 618 | while (true) { |
| 619 | GPIOC_PSOR = (1 << 5); |
| 620 | printf("Stack corruption detected\n"); |
| 621 | delay(1000); |
| 622 | GPIOC_PCOR = (1 << 5); |
| 623 | delay(1000); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | } // namespace motors |
| 628 | } // namespace frc971 |