30 static const uint8_t FRONT_LEFT_MOTOR_CHIP_SELECT = 0;
31 static const uint8_t FRONT_RIGHT_MOTOR_CHIP_SELECT = 3;
32 static const uint8_t BACK_LEFT_MOTOR_CHIP_SELECT = 1;
33 static const uint8_t BACK_RIGHT_MOTOR_CHIP_SELECT = 2;
55 TbotsProto::MotorStatus
poll(
const TbotsProto::MotorControl& motor_control,
56 double time_elapsed_since_last_poll_s);
68 uint8_t tmc6100ReadWriteByte(uint8_t motor, uint8_t data, uint8_t last_transfer);
84 void startEncoderCalibration(uint8_t motor);
85 void endEncoderCalibration(uint8_t motor);
123 std::unordered_set<TbotsProto::MotorFault> motor_faults;
138 std::unordered_set<TbotsProto::MotorFault>& motor_faults)
139 : drive_enabled(drive_enabled), motor_faults(motor_faults)
189 int control_loop_frequency_hz);
197 void startDriver(uint8_t motor);
198 void startController(uint8_t motor,
bool dribbler);
218 void configurePWM(uint8_t motor);
219 void configureDribblerPI(uint8_t motor);
220 void configureDrivePI(uint8_t motor);
221 void configureADC(uint8_t motor);
222 void configureEncoder(uint8_t motor);
223 void configureHall(uint8_t motor);
238 void writeToControllerOrDieTrying(uint8_t motor, uint8_t address, int32_t value);
239 void writeToDriverOrDieTrying(uint8_t motor, uint8_t address, int32_t value);
251 void spiTransfer(
int fd, uint8_t
const* tx, uint8_t
const* rx,
unsigned len,
265 void readThenWriteSpiTransfer(
int fd,
const uint8_t* read_tx, uint8_t
const* write_tx,
266 uint8_t
const* read_rx, uint32_t spi_speed);
279 int32_t tmc4671ReadThenWriteValue(uint8_t motor, uint8_t read_addr,
280 uint8_t write_addr, int32_t write_data);
292 uint8_t readWriteByte(uint8_t motor, uint8_t data, uint8_t last_transfer,
303 void checkEncoderConnections();
322 TbotsProto::MotorStatus updateMotorStatus(
double front_left_velocity_mps,
323 double front_right_velocity_mps,
324 double back_left_velocity_mps,
325 double back_right_velocity_mps,
326 double dribbler_rpm);
338 template <
typename T>
339 static std::unique_ptr<Gpio> setupGpio(
const T& gpio_number, GpioDirection direction,
340 GpioState initial_state);
350 bool requiresMotorReinit(uint8_t motor);
357 static constexpr double MECHANICAL_MPS_PER_ELECTRICAL_RPM = 0.000111;
358 static constexpr double ELECTRICAL_RPM_PER_MECHANICAL_MPS =
359 1 / MECHANICAL_MPS_PER_ELECTRICAL_RPM;
362 bool is_initialized_ =
false;
365 std::unique_ptr<Gpio> spi_demux_select_0_;
366 std::unique_ptr<Gpio> spi_demux_select_1_;
369 std::unique_ptr<Gpio> driver_control_enable_gpio_;
370 std::unique_ptr<Gpio> reset_gpio_;
373 uint8_t tx_[5] = {0};
374 uint8_t rx_[5] = {0};
377 uint8_t write_tx_[5] = {0};
378 uint8_t read_tx_[5] = {0};
379 uint8_t read_rx_[5] = {0};
382 bool transfer_started_ =
false;
383 bool currently_writing_ =
false;
384 bool currently_reading_ =
false;
385 uint8_t position_ = 0;
388 std::unordered_map<int, int> file_descriptors_;
394 std::unordered_map<int, bool> encoder_calibrated_;
395 std::unordered_map<int, MotorFaultIndicator> cached_motor_faults_;
398 WheelSpace_t prev_wheel_velocities_;
400 int front_left_target_rpm = 0;
401 int front_right_target_rpm = 0;
402 int back_left_target_rpm = 0;
403 int back_right_target_rpm = 0;
406 uint8_t motor_fault_detector_;
408 static const int NUM_CALIBRATION_ATTEMPTS = 10;
410 int dribbler_ramp_rpm_;
412 std::optional<std::chrono::time_point<std::chrono::system_clock>>
413 tracked_motor_fault_start_time_;
414 int num_tracked_motor_resets_;
416 static const uint8_t DRIBBLER_MOTOR_CHIP_SELECT = 4;
418 static const uint8_t NUM_DRIVE_MOTORS = 4;
419 static const uint8_t NUM_MOTORS = NUM_DRIVE_MOTORS + 1;
421 static const int MOTOR_FAULT_TIME_THRESHOLD_S = 60;
422 static const int MOTOR_FAULT_THRESHOLD_COUNT = 3;
425 static constexpr const char* SPI_PATHS[] = {
"/dev/spidev0.0",
"/dev/spidev0.1",
426 "/dev/spidev0.2",
"/dev/spidev0.3",
430 static constexpr const char* MOTOR_NAMES[] = {
"front_left",
"back_left",
"back_right",
431 "front_right",
"dribbler"};