Tetrapod Project
MotorControl Class Reference

#include <motor_driver.h>

Public Member Functions

 MotorControl ()
 Default constructor needed for initializing array. More...
 
 MotorControl (uint8_t _id, uint8_t _can_port_id, int _number_of_inner_motor_rotations, double _position_offset)
 Constructor for a MotorControl object. Motor ID and CAN port are set. More...
 
bool readPIDParameters ()
 Read the motor PI parameters and update the private PI values. More...
 
bool writePIDParametersToRAM (double _kp_pos, double _ki_pos, double _kp_speed, double _ki_speed, double _kp_torque, double _ki_torque)
 Set the motor PI parameters and store them temporary in RAM. More...
 
void setPositionReference (double _angle)
 Set the desired multi turn motor angle. More...
 
void setSpeedReference (double _speed)
 Set the desired motor speed. More...
 
void setTorqueReference (double _torque)
 Set the desired motor torque. More...
 
void setTorqueCurrent (int _torque_current)
 Set the desired motor torque current. Scales linearly with torque. More...
 
void readMotorControlCommandReply (unsigned char *_can_message)
 The state variables position, velocity, and torque are updated based on the latest information from the motor. More...
 
bool stopMotor ()
 The motor stops exerting a control output. The motor will remain idle until a new control command or motor resume command is received. More...
 
bool turnOffMotor ()
 The motor stops exerting a control output. The motor's operating state and previously received control commands are cleared. If a new control command is received the motor will start moving again. More...
 
bool readMultiTurnAngle ()
 The multi turn angle of the motor updated. More...
 
bool readMotorStatus ()
 A request is sent to the motor asking it to send back its state. The private state varibles position, velocity, and torque are then updated. More...
 
void requestMotorStatus ()
 A request is sent to the motor asking it to send back its state, position, velocity, and torque. More...
 
bool readCompleteEncoderPosition ()
 All the encoder data from the motor is read and printed. This includes the encoder value, raw encoder value, and encoder offset. This is mainly for debugging. More...
 
uint8_t get_id ()
 Return the ID of the MotorClass object. More...
 
double getPosition ()
 Return the position of the MotorClass object. More...
 
double getVelocity ()
 Return the velocity of the MotorClass object. More...
 
double getTorque ()
 Return the torque of the MotorClass object. More...
 
double getEncoderValue ()
 Return the latest received encoder value from the motor. More...
 
double getMultiTurnAngle ()
 Return the latest received multi turn angle from the motor. More...
 
double getMultiTurnAngleRaw ()
 Return the raw int64_t 0.01 degree multi turn angle. More...
 
void getPIDParameters (double &_kp_pos, double &_ki_pos, double &_kp_speed, double &_ki_speed, double &_kp_torque, double &_ki_torque)
 The input parameters are updated to match the PI parameters of the motor. More...
 
void printPIDParameters ()
 The function prints the PI parameters of the motor. More...
 
void printState ()
 The ID, encoder value, multi turn angle, position, speed and torque of the motor are printed. More...
 
void printTorqueCurrents ()
 

Public Attributes

double raw_position_reference
 

Private Member Functions

double innerMotorTurnCompleted (uint16_t _previous_encoder_value, uint16_t _new_encoder_value)
 This function checks if the inner motor completed a turn. Depending on the difference between the new and previous encoder value we can determine if the motor completed a turn a direction or not. More...
 
void sendMessage (CAN_message_t _can_message)
 This function ensures that the message is sent to the correct CAN port. More...
 
int readMessage (CAN_message_t &_can_message)
 This function ensures that the message on the correct CAN port is read. More...
 
void errorMessage ()
 

Private Attributes

uint8_t id
 Motor ID set through the Serial configurator [1 - 32]. More...
 
uint16_t address
 Motor address = ID + 0x140. Declared for convenience. More...
 
uint8_t can_port_id
 Used to decide which CAN port to use. Possible values {1, 2}. More...
 
double kp_pos
 Motor PID Parameters. More...
 
double ki_pos
 
double kp_speed
 
double ki_speed
 
double kp_torque
 
double ki_torque
 
double initial_number_of_inner_motor_rotations
 Number of rotations of the inner motor during startup This is needed to create position commands. More...
 
double number_of_inner_motor_rotations
 Number of rotations of the built-in motor, not the output-shaft. More...
 
uint16_t previous_encoder_value
 Keep track of previous encoder position. This way you can detect turns. See innerMotorTurnCompleted(). More...
 
int32_t multi_turn_angle_32_bit = 0
 This variable contains the multi turn angle read by the motor in degrees. More...
 
int32_t multi_turn_angle_32_bit_001lsb = 0
 This variable contains the 32 bit representation of the raw 0.01 degree multi turn angle. More...
 
int64_t multi_turn_angle_64_bit = 0
 This is the true 64 bit representation of the multi turn angle in degrees. More...
 
int64_t multi_turn_angle_001lsb = 0
 This is the 64 bit representation of the raw int64_t multi turn angle in 0.01 degrees. More...
 
double target_position_offset = 0.0
 If the motor is initialized in certain positions the setPosition function will have zero offset of 60 degrees.
More...
 
double position_offset = 0.0
 position offset = true position - position More...
 
double position_center_offset = M_PI/6.0
 This is used to center the output shaft zero position. More...
 
double position
 Latest measured position of the shaft in radians. More...
 
double speed
 Latest measured speed of the shaft in radians/second. More...
 
double torque
 Latest measured shaft torque in Nm. More...
 
double temperature
 Latest measured temperature of the motor in degree Celsius. More...
 
int16_t torque_current_reference = 0
 The torque current reference set for the motor. More...
 
int16_t torque_current_measured = 0
 The measured torque current of the motor. More...
 
CAN_message_t can_message
 CAN message for this motor. Only buf field is set for every message sent. More...
 
CAN_message_t received_can_message
 CAN message object used to receive incomming CAN messages. More...
 
double max_torque
 The torque in Newton meter has to lie in the interval [-max_torque, max_torque]. More...
 
int max_torque_current
 All torque values are scaled to the interval [-max_torque_current, max_torque_current]. More...
 
uint16_t max_encoder_value
 The 16 bit encoder measures the position of the inner DC motor, not the shaft. More...
 
uint16_t encoder_turn_threshold
 If the change in encoder value is larger than this it is safe to assume that the inner motor completed a turn. More...
 

Detailed Description

Definition at line 13 of file motor_driver.h.

Constructor & Destructor Documentation

◆ MotorControl() [1/2]

MotorControl::MotorControl ( )
inline

Default constructor needed for initializing array.

Definition at line 17 of file motor_driver.h.

◆ MotorControl() [2/2]

MotorControl::MotorControl ( uint8_t  _id,
uint8_t  _can_port_id,
int  _number_of_inner_motor_rotations,
double  _position_offset 
)

Constructor for a MotorControl object. Motor ID and CAN port are set.

Parameters
[in]_idID of the motor [1 - 32]

Definition at line 3 of file motor_driver.cpp.

Member Function Documentation

◆ readPIDParameters()

bool MotorControl::readPIDParameters ( )

Read the motor PI parameters and update the private PI values.

Returns
If the PI parameters were successfully read, return true. Otherwise return false.

Definition at line 75 of file motor_driver.cpp.

◆ writePIDParametersToRAM()

bool MotorControl::writePIDParametersToRAM ( double  _kp_pos,
double  _ki_pos,
double  _kp_speed,
double  _ki_speed,
double  _kp_torque,
double  _ki_torque 
)

Set the motor PI parameters and store them temporary in RAM.

Parameters
[in]_kp_posDesired proportional position gain
[in]_ki_posDesired integral position gain
[in]_kp_speedDesired proportional speed gain
[in]_ki_speedDesired integral speed gain
[in]_kp_torqueDesired proportional torque gain
[in]_ki_torqueDesired integral torque gain
Returns
If the PI parameters were successfully set true is returned. Otherwise false is returned.

Definition at line 124 of file motor_driver.cpp.

◆ setPositionReference()

void MotorControl::setPositionReference ( double  _angle)

Set the desired multi turn motor angle.

Parameters
[in]_angleSetpoint motor angle in radians

Definition at line 190 of file motor_driver.cpp.

◆ setSpeedReference()

void MotorControl::setSpeedReference ( double  _speed)

Set the desired motor speed.

Parameters
[in]_speedSetpoint motor speed in radians/second

Definition at line 205 of file motor_driver.cpp.

◆ setTorqueReference()

void MotorControl::setTorqueReference ( double  _torque)

Set the desired motor torque.

Parameters
[in]_torqueSetpoint motor torque in Nm

Definition at line 220 of file motor_driver.cpp.

◆ setTorqueCurrent()

void MotorControl::setTorqueCurrent ( int  _torque_current)

Set the desired motor torque current. Scales linearly with torque.

Parameters
[in]_torque_currentSetpoint torque current

Definition at line 562 of file motor_driver.cpp.

◆ readMotorControlCommandReply()

void MotorControl::readMotorControlCommandReply ( unsigned char *  _can_message)

The state variables position, velocity, and torque are updated based on the latest information from the motor.

Parameters
[in]_can_messageAn 8 dimensional array containing the data of a CAN message

Definition at line 247 of file motor_driver.cpp.

◆ stopMotor()

bool MotorControl::stopMotor ( )

The motor stops exerting a control output. The motor will remain idle until a new control command or motor resume command is received.

Returns
If the motor is successfully stopped true is returned, otherwise false.

Definition at line 278 of file motor_driver.cpp.

◆ turnOffMotor()

bool MotorControl::turnOffMotor ( )

The motor stops exerting a control output. The motor's operating state and previously received control commands are cleared. If a new control command is received the motor will start moving again.

Returns
If the motor is successfully turned off true is returned, otherwise false.

Definition at line 318 of file motor_driver.cpp.

◆ readMultiTurnAngle()

bool MotorControl::readMultiTurnAngle ( )

The multi turn angle of the motor updated.

Returns
If the multi turn angle was successfully updated return true, otherwise false.

Definition at line 358 of file motor_driver.cpp.

◆ readMotorStatus()

bool MotorControl::readMotorStatus ( )

A request is sent to the motor asking it to send back its state. The private state varibles position, velocity, and torque are then updated.

Returns
If the private state variables were successfully updated return true, otherwise return false.

Definition at line 423 of file motor_driver.cpp.

◆ requestMotorStatus()

void MotorControl::requestMotorStatus ( )

A request is sent to the motor asking it to send back its state, position, velocity, and torque.

Definition at line 467 of file motor_driver.cpp.

◆ readCompleteEncoderPosition()

bool MotorControl::readCompleteEncoderPosition ( )

All the encoder data from the motor is read and printed. This includes the encoder value, raw encoder value, and encoder offset. This is mainly for debugging.

Returns
If the encoder data was successfully read true is returned, otherwise false.

Definition at line 476 of file motor_driver.cpp.

◆ get_id()

uint8_t MotorControl::get_id ( )
inline

Return the ID of the MotorClass object.

Returns
The ID of the MotorClass object.

Definition at line 101 of file motor_driver.h.

◆ getPosition()

double MotorControl::getPosition ( )
inline

Return the position of the MotorClass object.

Returns
The position of the MotorClass object.

Definition at line 105 of file motor_driver.h.

◆ getVelocity()

double MotorControl::getVelocity ( )
inline

Return the velocity of the MotorClass object.

Returns
The velocity of the MotorClass object.

Definition at line 109 of file motor_driver.h.

◆ getTorque()

double MotorControl::getTorque ( )
inline

Return the torque of the MotorClass object.

Returns
The torque of the MotorClass object.

Definition at line 113 of file motor_driver.h.

◆ getEncoderValue()

double MotorControl::getEncoderValue ( )
inline

Return the latest received encoder value from the motor.

Returns
The latest received encoder value from the motor

Definition at line 117 of file motor_driver.h.

◆ getMultiTurnAngle()

double MotorControl::getMultiTurnAngle ( )
inline

Return the latest received multi turn angle from the motor.

Returns
The latest received received multi turn angle from the motor

Definition at line 121 of file motor_driver.h.

◆ getMultiTurnAngleRaw()

double MotorControl::getMultiTurnAngleRaw ( )
inline

Return the raw int64_t 0.01 degree multi turn angle.

Definition at line 124 of file motor_driver.h.

◆ getPIDParameters()

void MotorControl::getPIDParameters ( double &  _kp_pos,
double &  _ki_pos,
double &  _kp_speed,
double &  _ki_speed,
double &  _kp_torque,
double &  _ki_torque 
)

The input parameters are updated to match the PI parameters of the motor.

Parameters
[in]_kp_posProportional position gain
[in]_ki_posIntegral position gain
[in]_kp_speedProportional speed gain
[in]_ki_speedIntegral speed gain
[in]_kp_torqueProportional torque gain
[in]_ki_torqueIntegral torque gain

Definition at line 518 of file motor_driver.cpp.

◆ printPIDParameters()

void MotorControl::printPIDParameters ( )

The function prints the PI parameters of the motor.

Definition at line 534 of file motor_driver.cpp.

◆ printState()

void MotorControl::printState ( )

The ID, encoder value, multi turn angle, position, speed and torque of the motor are printed.

Definition at line 547 of file motor_driver.cpp.

◆ printTorqueCurrents()

void MotorControl::printTorqueCurrents ( )

◆ innerMotorTurnCompleted()

double MotorControl::innerMotorTurnCompleted ( uint16_t  _previous_encoder_value,
uint16_t  _new_encoder_value 
)
private

This function checks if the inner motor completed a turn. Depending on the difference between the new and previous encoder value we can determine if the motor completed a turn a direction or not.

Parameters
[in]_previous_encoder_valueThe previously measured encoder value
[in]_new_encoder_valueThe newly measured encoder value
Returns
If new << old return 1, if old << new return -1, else return 0

Definition at line 582 of file motor_driver.cpp.

◆ sendMessage()

void MotorControl::sendMessage ( CAN_message_t  _can_message)
private

This function ensures that the message is sent to the correct CAN port.

Definition at line 601 of file motor_driver.cpp.

◆ readMessage()

int MotorControl::readMessage ( CAN_message_t &  _can_message)
private

This function ensures that the message on the correct CAN port is read.

Definition at line 622 of file motor_driver.cpp.

◆ errorMessage()

void MotorControl::errorMessage ( )
private

Definition at line 641 of file motor_driver.cpp.

Member Data Documentation

◆ raw_position_reference

double MotorControl::raw_position_reference

Definition at line 150 of file motor_driver.h.

◆ id

uint8_t MotorControl::id
private

Motor ID set through the Serial configurator [1 - 32].

Definition at line 153 of file motor_driver.h.

◆ address

uint16_t MotorControl::address
private

Motor address = ID + 0x140. Declared for convenience.

Definition at line 156 of file motor_driver.h.

◆ can_port_id

uint8_t MotorControl::can_port_id
private

Used to decide which CAN port to use. Possible values {1, 2}.

Definition at line 159 of file motor_driver.h.

◆ kp_pos

double MotorControl::kp_pos
private

Motor PID Parameters.

Definition at line 162 of file motor_driver.h.

◆ ki_pos

double MotorControl::ki_pos
private

Definition at line 163 of file motor_driver.h.

◆ kp_speed

double MotorControl::kp_speed
private

Definition at line 164 of file motor_driver.h.

◆ ki_speed

double MotorControl::ki_speed
private

Definition at line 165 of file motor_driver.h.

◆ kp_torque

double MotorControl::kp_torque
private

Definition at line 166 of file motor_driver.h.

◆ ki_torque

double MotorControl::ki_torque
private

Definition at line 167 of file motor_driver.h.

◆ initial_number_of_inner_motor_rotations

double MotorControl::initial_number_of_inner_motor_rotations
private

Number of rotations of the inner motor during startup This is needed to create position commands.

Definition at line 171 of file motor_driver.h.

◆ number_of_inner_motor_rotations

double MotorControl::number_of_inner_motor_rotations
private

Number of rotations of the built-in motor, not the output-shaft.

Definition at line 175 of file motor_driver.h.

◆ previous_encoder_value

uint16_t MotorControl::previous_encoder_value
private

Keep track of previous encoder position. This way you can detect turns. See innerMotorTurnCompleted().

Definition at line 179 of file motor_driver.h.

◆ multi_turn_angle_32_bit

int32_t MotorControl::multi_turn_angle_32_bit = 0
private

This variable contains the multi turn angle read by the motor in degrees.

Definition at line 182 of file motor_driver.h.

◆ multi_turn_angle_32_bit_001lsb

int32_t MotorControl::multi_turn_angle_32_bit_001lsb = 0
private

This variable contains the 32 bit representation of the raw 0.01 degree multi turn angle.

Definition at line 185 of file motor_driver.h.

◆ multi_turn_angle_64_bit

int64_t MotorControl::multi_turn_angle_64_bit = 0
private

This is the true 64 bit representation of the multi turn angle in degrees.

Definition at line 188 of file motor_driver.h.

◆ multi_turn_angle_001lsb

int64_t MotorControl::multi_turn_angle_001lsb = 0
private

This is the 64 bit representation of the raw int64_t multi turn angle in 0.01 degrees.

Definition at line 191 of file motor_driver.h.

◆ target_position_offset

double MotorControl::target_position_offset = 0.0
private

If the motor is initialized in certain positions the setPosition function will have zero offset of 60 degrees.

Definition at line 196 of file motor_driver.h.

◆ position_offset

double MotorControl::position_offset = 0.0
private

position offset = true position - position

Definition at line 199 of file motor_driver.h.

◆ position_center_offset

double MotorControl::position_center_offset = M_PI/6.0
private

This is used to center the output shaft zero position.

Definition at line 202 of file motor_driver.h.

◆ position

double MotorControl::position
private

Latest measured position of the shaft in radians.

Definition at line 205 of file motor_driver.h.

◆ speed

double MotorControl::speed
private

Latest measured speed of the shaft in radians/second.

Definition at line 208 of file motor_driver.h.

◆ torque

double MotorControl::torque
private

Latest measured shaft torque in Nm.

Definition at line 211 of file motor_driver.h.

◆ temperature

double MotorControl::temperature
private

Latest measured temperature of the motor in degree Celsius.

Definition at line 214 of file motor_driver.h.

◆ torque_current_reference

int16_t MotorControl::torque_current_reference = 0
private

The torque current reference set for the motor.

Definition at line 217 of file motor_driver.h.

◆ torque_current_measured

int16_t MotorControl::torque_current_measured = 0
private

The measured torque current of the motor.

Definition at line 220 of file motor_driver.h.

◆ can_message

CAN_message_t MotorControl::can_message
private

CAN message for this motor. Only buf field is set for every message sent.

Definition at line 224 of file motor_driver.h.

◆ received_can_message

CAN_message_t MotorControl::received_can_message
private

CAN message object used to receive incomming CAN messages.

Definition at line 227 of file motor_driver.h.

◆ max_torque

double MotorControl::max_torque
private

The torque in Newton meter has to lie in the interval [-max_torque, max_torque].

Definition at line 233 of file motor_driver.h.

◆ max_torque_current

int MotorControl::max_torque_current
private

All torque values are scaled to the interval [-max_torque_current, max_torque_current].

Definition at line 237 of file motor_driver.h.

◆ max_encoder_value

uint16_t MotorControl::max_encoder_value
private

The 16 bit encoder measures the position of the inner DC motor, not the shaft.

Definition at line 241 of file motor_driver.h.

◆ encoder_turn_threshold

uint16_t MotorControl::encoder_turn_threshold
private

If the change in encoder value is larger than this it is safe to assume that the inner motor completed a turn.

Definition at line 245 of file motor_driver.h.


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