Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
can::motor Namespace Reference

Utilities for interacting with motor boards over a CAN interface. More...

Classes

struct  sensor_t
 The supported motor position sensors. More...
 

Enumerations

enum class  motormode_t { pwm = MOTOR_UNIT_MODE_PWM , pid = MOTOR_UNIT_MODE_PID }
 The possible motor modes.
 

Functions

void initEncoder (deviceserial_t serial, bool invertEncoder, bool zeroEncoder, int32_t pulsesPerJointRev, std::optional< std::chrono::milliseconds > telemetryPeriod)
 Initialize an encoder attached to the given motor.
 
void initPotentiometer (deviceserial_t serial, int32_t posLo, int32_t posHi, uint16_t adcLo, uint16_t adcHi, std::optional< std::chrono::milliseconds > telemetryPeriod)
 Initialize a potentiometer attached to the given motor.
 
void emergencyStopMotors ()
 Emergency stop all motors on the CAN bus.
 
void initMotor (deviceserial_t serial)
 Initialize a motor.
 
void setLimitSwitchLimits (deviceserial_t serial, int32_t lo, int32_t hi)
 Set the limits of the limit switch on a motor board.
 
void setMotorPIDConstants (deviceserial_t serial, int32_t kP, int32_t kI, int32_t kD)
 Set the PID constants for a motor board.
 
void setMotorMode (deviceserial_t serial, motormode_t mode)
 Set the mode of a motor board.
 
void setMotorPower (deviceserial_t serial, double power)
 Set the power output of a motor board.
 
void setMotorPower (deviceserial_t serial, int16_t power)
 Set the power output of a motor board.
 
void setMotorPIDTarget (deviceserial_t serial, int32_t target)
 Set the position PID target of a motor board.
 
void setServoPos (deviceserial_t serial, uint8_t servoNum, int32_t angle)
 Set the angle of the PCA servo.
 
DataPoint< int32_tgetMotorPosition (deviceserial_t serial)
 Get the last reported position of a motor.
 
void pullMotorPosition (deviceserial_t serial)
 Poll the position data from a motor board.
 
callbackid_t addLimitSwitchCallback (deviceserial_t serial, const std::function< void( deviceserial_t serial, robot::types::DataPoint< robot::types::LimitSwitchData > limitSwitchData)> &callback)
 Add a callback that is invoked when the limit switch is triggered for a motor board.
 
void removeLimitSwitchCallback (callbackid_t id)
 Remove a previously registered limit switch callback.
 

Detailed Description

Utilities for interacting with motor boards over a CAN interface.

These motor boards can either be AVR or PSoC boards.

Function Documentation

◆ addLimitSwitchCallback()

callbackid_t can::motor::addLimitSwitchCallback ( deviceserial_t serial,
const std::function< void(deviceserial_t serial, robot::types::DataPoint< robot::types::LimitSwitchData > limitSwitchData)> & callback )

Add a callback that is invoked when the limit switch is triggered for a motor board.

The event is only triggered when the limit switch is clicked, not released.

Parameters
serialThe CAN serial number of the motor board.
callbackThe callback to invoke when the limit switch is triggered.
Returns
callbackid_t An ID that refers to this callback. This can be passed to removeLimitSwitchCallback() to remove this callback.

◆ emergencyStopMotors()

void can::motor::emergencyStopMotors ( )

Emergency stop all motors on the CAN bus.

This cuts power to all motors and resets them.

◆ getMotorPosition()

robot::types::DataPoint< int32_t > can::motor::getMotorPosition ( deviceserial_t serial)

Get the last reported position of a motor.

This only reports the cached position, it does not poll the motor board for new data.

Parameters
serialThe serial number of the motor board.
Returns
robot::types::DataPoint<int32_t> The position data of the given motor, in millidegrees. If no position data has been received, returns an empty data point.

◆ initEncoder()

void can::motor::initEncoder ( deviceserial_t serial,
bool invertEncoder,
bool zeroEncoder,
int32_t pulsesPerJointRev,
std::optional< std::chrono::milliseconds > telemetryPeriod )

Initialize an encoder attached to the given motor.

For potentiometers, use initPotentiometer().

Parameters
serialThe CAN serial number of the motor board.
invertEncoderIf true, invert the encoder direction. Use this to correct sensor phase.
zeroEncoderIf true, reset the encoder position to zero.
pulsesPerJointRevThe number of encoder pulses per revolution of the physical joint. Measure/calculate this using the gear ratios and encoder specs.
telemetryPeriodAn optional parameter specifying the telemetry period. The telemetry will be fetched at this period automatically. An empty optional disables this behavior, in which case the motor position must be explicitly pulled.

◆ initMotor()

void can::motor::initMotor ( deviceserial_t serial)

Initialize a motor.

This does not initialize any sensor or controller.

Parameters
serialThe CAN serial number of the motor to initialize.

◆ initPotentiometer()

void can::motor::initPotentiometer ( deviceserial_t serial,
int32_t posLo,
int32_t posHi,
uint16_t adcLo,
uint16_t adcHi,
std::optional< std::chrono::milliseconds > telemetryPeriod )

Initialize a potentiometer attached to the given motor.

Parameters
serialThe CAN serial number of the motor board.
posLoThe joint position that corresponds to adcLo
posHiThe joint position that corresponds to adcHi
adcLoThe ADC value when the joint is at posLo
adcHiThe ADC value when the joint is at posHi
telemetryPeriodAn optional parameter specifying the telemetry period. The telemetry will be fetched at this period automatically. An empty optional disables this behavior, in which case the motor position must be explicitly pulled.

◆ pullMotorPosition()

void can::motor::pullMotorPosition ( deviceserial_t serial)

Poll the position data from a motor board.

This may not be supported by every motor board implementation.

Parameters
serialThe CAN serial number of the motor board.

◆ removeLimitSwitchCallback()

void can::motor::removeLimitSwitchCallback ( callbackid_t id)

Remove a previously registered limit switch callback.

Parameters
idThe callback ID that was returned when the callback was registered with addLimitSwitchCallback().

◆ setLimitSwitchLimits()

void can::motor::setLimitSwitchLimits ( deviceserial_t serial,
int32_t lo,
int32_t hi )

Set the limits of the limit switch on a motor board.

When the corresponding limit switch is triggered, the encoder value is set to this value. Use this method for motorboard with both encoders and limit switches

Parameters
serialThe CAN serial number of the motor board.
loThe joint position in millidegrees of the low limit switch.
hiThe joint position in millidegrees of the high limit switch.

◆ setMotorMode()

void can::motor::setMotorMode ( deviceserial_t serial,
motormode_t mode )

Set the mode of a motor board.

Parameters
serialThe CAN serial number of the motor board.
modeThe mode to set.

◆ setMotorPIDConstants()

void can::motor::setMotorPIDConstants ( deviceserial_t serial,
int32_t kP,
int32_t kI,
int32_t kD )

Set the PID constants for a motor board.

Note that the PID constants are specified in units of 10000, so a 1 is interpreted as a

  1. This is because the controller operates on millidegrees.
Parameters
serialThe CAN serial number of the motor board.
kPThe P coefficient.
kIThe I coefficient.
kDThe D coefficient.

◆ setMotorPIDTarget()

void can::motor::setMotorPIDTarget ( deviceserial_t serial,
int32_t target )

Set the position PID target of a motor board.

The motor mode should have been set to motormode_t::pid. Additionally, both the sensor and the PID coefficients must have been initialized.

Parameters
serialThe CAN serial number of the motor board.
targetThe position in millidegrees to track with the PID controller.

◆ setMotorPower() [1/2]

void can::motor::setMotorPower ( deviceserial_t serial,
double power )

Set the power output of a motor board.

Parameters
serialThe CAN serial number of the motor board.
powerPercent power, in the range [-1,1].

◆ setMotorPower() [2/2]

void can::motor::setMotorPower ( deviceserial_t serial,
int16_t power )

Set the power output of a motor board.

The motor mode should have been set to motormode_t::pwm.

Parameters
serialThe CAN serial number of the motor board.
powerThe power to set. Any signed 16-bit integer is valid.

◆ setServoPos()

void can::motor::setServoPos ( deviceserial_t serial,
uint8_t servoNum,
int32_t angle )

Set the angle of the PCA servo.

Parameters
serialThe CAN serial number of the motor board.
servoNumthe servo number.
anglethe angle of the servo in millidegrees.