Thunderbots Project
Loading...
Searching...
No Matches
MotorService Class Reference

#include <motor.h>

Classes

struct  MotorFaultIndicator
 

Public Member Functions

 MotorService (const RobotConstants_t &robot_constants, int control_loop_frequency_hz)
 
TbotsProto::MotorStatus poll (const TbotsProto::MotorControl &motor_control, double time_elapsed_since_last_poll_s)
 
uint8_t tmc4671ReadWriteByte (uint8_t motor, uint8_t data, uint8_t last_transfer)
 
uint8_t tmc6100ReadWriteByte (uint8_t motor, uint8_t data, uint8_t last_transfer)
 
void startEncoderCalibration (uint8_t motor)
 
void endEncoderCalibration (uint8_t motor)
 
void runOpenLoopCalibrationRoutine (uint8_t motor, size_t num_samples)
 
void resetMotorBoard ()
 
void setup ()
 
struct MotorFaultIndicator checkDriverFault (uint8_t motor)
 
void setUpDriveMotor (uint8_t motor)
 
void writeIntToTMC4671 (uint8_t motor, uint8_t address, int32_t value)
 
int readIntFromTMC4671 (uint8_t motor, uint8_t address)
 

Static Public Attributes

static const uint8_t FRONT_LEFT_MOTOR_CHIP_SELECT = 0
 
static const uint8_t FRONT_RIGHT_MOTOR_CHIP_SELECT = 3
 
static const uint8_t BACK_LEFT_MOTOR_CHIP_SELECT = 1
 
static const uint8_t BACK_RIGHT_MOTOR_CHIP_SELECT = 2
 

Detailed Description

A service that interacts with the motor.

It is responsible for:

  • Converting Euclidean velocities to wheel velocities
  • Communicating with the motor
  • Detecting and handling faults

Constructor & Destructor Documentation

◆ MotorService()

MotorService ( const RobotConstants_t robot_constants,
int  control_loop_frequency_hz 
)

Service that interacts with the motor board. Opens all the required ports and maintains them until destroyed.

Parameters
RobotConstants_tThe robot constants
control_loop_frequency_hzThe frequency the main loop will call poll at

Member Function Documentation

◆ checkDriverFault()

MotorService::MotorFaultIndicator checkDriverFault ( uint8_t  motor)

Log the driver fault in a human readable log msg

Parameters
motorThe motor to log the status for
Returns
a struct containing the motor faults and whether the motor was disabled due to the fault

◆ poll()

TbotsProto::MotorStatus poll ( const TbotsProto::MotorControl &  motor_control,
double  time_elapsed_since_last_poll_s 
)

When the motor service is polled with a DirectControlPrimitive msg, call the appropriate trinamic api function to spin the appropriate motor.

Parameters
motorThe motor msg to unpack and execute on the motors
time_elapsed_since_last_poll_sThe time since last poll was called in seconds
Returns
MotorStatus The status of all the drive units

◆ readIntFromTMC4671()

int readIntFromTMC4671 ( uint8_t  motor,
uint8_t  address 
)

Used for testing purposes:

Wrapper function that reads int from the TMC4671

Parameters
motordrive motor number
addressmotor address
Returns
read value

◆ resetMotorBoard()

void resetMotorBoard ( )

Reset the motor board by toggling the reset GPIO appropriately. Effectively stops the motors from moving.

◆ runOpenLoopCalibrationRoutine()

void runOpenLoopCalibrationRoutine ( uint8_t  motor,
size_t  num_samples 
)

Spin the motor in openloop mode (safe to run before encoder initialization)

Captures TMC4671_OPENLOOP_PHI and TMC4671_ABN_DECODER_PHI_E and stores it in encoder_calibration.csv

Captures adc_iv, adc_ux, adc_wy and pwm_iv, pwm_ux, pwm_wy.

WARNING: Make sure adc_iv is in phase with pwm_iv, adc_ux is in phase with pwm_ux, and adc_wy is in phase with pwm_wy.

If you dont do this, you can risk burning the motor.

Parameters
motorThe motor to spin in openloop mode
num_samplesThe number of samples to take on each

◆ setup()

void setup ( )

Clears previous faults, configures the motor and checks encoder connections.

◆ setUpDriveMotor()

void setUpDriveMotor ( uint8_t  motor)

Sets up motor as drive motor controllers

Parameters
motordrive motor number

◆ tmc4671ReadWriteByte()

uint8_t tmc4671ReadWriteByte ( uint8_t  motor,
uint8_t  data,
uint8_t  last_transfer 
)

Trinamic API binding, sets spi_demux_select_0|1 pins appropriately and calls readWriteByte. See C++ implementation file for more info

Parameters
motorWhich motor to talk to (in our case, the chip select)
dataThe data to send
last_transferThe last transfer of uint8_t data for this transaction.
Returns
A byte read from the trinamic chip

◆ writeIntToTMC4671()

void writeIntToTMC4671 ( uint8_t  motor,
uint8_t  address,
int32_t  value 
)

Used for testing purposes:

Wrapper function that writes int to the TMC4671

Parameters
motordrive motor number
addressmotor address
valuewrite value

The documentation for this class was generated from the following files: