Class MotorChannel

Class Documentation

class MotorChannel

Provides control and management for each of the MotorGo brushless DC (BLDC) motor channels.

This class encapsulates the functionality for controlling and managing BLDC motors using the MotorGo hardware. It provides functions for setting up motors and encoders, running control loops, and setting control modes.

Motor Initialization

Functions for initializing motors and encoders with various settings.

void init(ChannelConfiguration channel_config)

Initializes motor and encoder with default settings. Calibration is automatically loaded.

Parameters:

channel_configChannelConfiguration structure containing the configuration for the motor channel.

void init(ChannelConfiguration channel_config, bool should_calibrate)

Initializes motor and encoder with the option of calibration. If should_calibrate is false, calibration procedure will not run. Calibration will be loaded from flash if available, else the channel will only run open loop control modes.

Parameters:
  • channel_configChannelConfiguration structure containing the configuration for the motor channel.

  • should_calibrate – If true, performs calibration on startup.

PID Controller Management

Functions for managing PID controllers, including getting and setting parameters.

PIDParameters get_torque_controller()

Retrieves the PID parameters for the torque controller motor.

Returns:

PIDParameters structure containing the current settings of the torque controller.

PIDParameters get_velocity_controller()

Retrieves the PID parameters for the velocity controller.

Returns:

PIDParameters structure containing the current settings of the velocity controller.

PIDParameters get_position_controller()

Retrieves the PID parameters for the position controller.

Returns:

PIDParameters structure containing the current settings of the position controller.

void set_torque_controller(PIDParameters params)

Sets the PID parameters for the torque controller.

Parameters:

params – The PIDParameters structure to configure the torque controller.

void set_velocity_controller(PIDParameters params)

Sets the PID parameters for the velocity controller.

Parameters:

params – The PIDParameters structure to configure the velocity controller.

void set_position_controller(PIDParameters params)

Sets the PID parameters for the position controller.

Parameters:

params – The PIDParameters structure to configure the position controller.

float get_torque_limit()

Get the user-specified torque limit.

Returns:

float

float get_velocity_limit()

Get the user-specified velocity limit.

Returns:

float

float get_position_limit_low()

Get the user-specified lower position limit.

Returns:

float

float get_position_limit_high()

Get the user-specified upper position limit.

Returns:

float

float get_voltage_limit()

Get the user-specified voltage limit.

Returns:

float

void set_torque_limit(float limit)

Sets the maximum torque that can be commanded to the torque controller.

Note

This limit is only enforced if the motor is in torque control mode.

Parameters:

limit – The maximum torque in N*m that can be commanded to the motor.

void set_velocity_limit(float limit)

Sets the maximum velocity that can be commanded to the velocity controller.

Note

This limit is only enforced if the motor is in velocity control mode.

Parameters:

limit – The maximum velocity in rad/s that can be commanded to the motor.

void set_position_limit(float low, float high)

Sets the position limits for the position controller.

Note

This limit is only enforced if the motor is in position control mode.

Parameters:
  • low – The lower position limit in radians.

  • high – The upper position limit in radians.

void set_voltage_limit(float limit)

Sets the maximum voltage that can be commanded to the motor.

Note

This limit is only enforced if the motor is in voltage control mode.

Parameters:

limit – The maximum voltage in V that can be commanded to the motor.

void reset_torque_controller()

Resets the internal state of the torque controller. This clears the integral term and sets the output to zero.

void reset_velocity_controller()

Resets the internal state of the velocity controller. This clears the integral term and sets the output to zero.

void reset_position_controller()

Resets the internal state of the position controller. This clears the integral term and sets the output to zero.

void save_torque_controller()

Saves the current torque controller settings to memory.

void save_velocity_controller()

Saves the current velocity controller settings to memory.

void save_position_controller()

Saves the current position controller settings to memory.

void load_torque_controller()

Loads the torque controller settings from memory.

void load_velocity_controller()

Loads the velocity controller settings from memory.

void load_position_controller()

Loads the position controller settings from memory.

Motor Command

Functions for basic motor command operations like enable, disable, and setting control modes.

void enable()

Enables the motor, it will run the currently set command.

void disable()

Disables the motor, it will stop running and ignore commands.

void set_control_mode(ControlMode mode)

Sets the control mode for the motor.

Parameters:

mode – The desired ControlMode (e.g., Velocity, Position) for the motor.

void set_target_torque(float target)

Sets the target torque for the motor.

Parameters:

target – The desired target torque in N*m for the motor.

void set_target_velocity(float target)

Sets the target velocity for the motor.

Parameters:

target – The desired target velocity in rad/s for the motor.

void set_target_position(float target)

Sets the target position for the motor.

Parameters:

target – The desired target position in rad for the motor.

void set_target_voltage(float target)

Sets the target voltage for the motor.

Parameters:

target – The desired target voltage in V for the motor.

State Retrieval

Functions for retrieving the current state of the motors.

void zero_position()

Sets the current position of the motor to zero. This function resets the motor’s position value to zero.

float get_torque()

Retrieves the current torque of the motor.

Returns:

The motor’s torque in N*m.

float get_velocity()

Retrieves the current velocity of the motor.

Returns:

The motor’s velocity in rad/s.

float get_position()

Retrieves the current position of the motor.

Returns:

The motor’s position in radians.

float get_voltage()

Retrieves the current voltage of the motor.

Returns:

The motor’s voltage in V.

Public Functions

MotorChannel(BLDCChannelParameters params, const char *name)

Constructor for the MotorChannel class.

Parameters:
  • paramsBLDCChannelParameters structure containing the pin configuration for the motor channel.

  • name – The name of the motor channel, used for saving calibration parameters to EEPROM.

MotorChannel(const MotorChannel&) = delete
MotorChannel &operator=(const MotorChannel&) = delete
void loop()

Runs the control loop for the motor channel and updates encoder data. This should be run as fast as possible, without delays.