Tetrapod Project
|
#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... | |
Definition at line 13 of file motor_driver.h.
|
inline |
Default constructor needed for initializing array.
Definition at line 17 of file motor_driver.h.
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.
[in] | _id | ID of the motor [1 - 32] |
Definition at line 3 of file motor_driver.cpp.
bool MotorControl::readPIDParameters | ( | ) |
Read the motor PI parameters and update the private PI values.
Definition at line 75 of file motor_driver.cpp.
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.
[in] | _kp_pos | Desired proportional position gain |
[in] | _ki_pos | Desired integral position gain |
[in] | _kp_speed | Desired proportional speed gain |
[in] | _ki_speed | Desired integral speed gain |
[in] | _kp_torque | Desired proportional torque gain |
[in] | _ki_torque | Desired integral torque gain |
Definition at line 124 of file motor_driver.cpp.
void MotorControl::setPositionReference | ( | double | _angle | ) |
Set the desired multi turn motor angle.
[in] | _angle | Setpoint motor angle in radians |
Definition at line 190 of file motor_driver.cpp.
void MotorControl::setSpeedReference | ( | double | _speed | ) |
Set the desired motor speed.
[in] | _speed | Setpoint motor speed in radians/second |
Definition at line 205 of file motor_driver.cpp.
void MotorControl::setTorqueReference | ( | double | _torque | ) |
Set the desired motor torque.
[in] | _torque | Setpoint motor torque in Nm |
Definition at line 220 of file motor_driver.cpp.
void MotorControl::setTorqueCurrent | ( | int | _torque_current | ) |
Set the desired motor torque current. Scales linearly with torque.
[in] | _torque_current | Setpoint torque current |
Definition at line 562 of file motor_driver.cpp.
void MotorControl::readMotorControlCommandReply | ( | unsigned char * | _can_message | ) |
The state variables position, velocity, and torque are updated based on the latest information from the motor.
[in] | _can_message | An 8 dimensional array containing the data of a CAN message |
Definition at line 247 of file motor_driver.cpp.
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.
Definition at line 278 of file motor_driver.cpp.
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.
Definition at line 318 of file motor_driver.cpp.
bool MotorControl::readMultiTurnAngle | ( | ) |
The multi turn angle of the motor updated.
Definition at line 358 of file motor_driver.cpp.
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.
Definition at line 423 of file motor_driver.cpp.
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.
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.
Definition at line 476 of file motor_driver.cpp.
|
inline |
Return the ID of the MotorClass object.
Definition at line 101 of file motor_driver.h.
|
inline |
Return the position of the MotorClass object.
Definition at line 105 of file motor_driver.h.
|
inline |
Return the velocity of the MotorClass object.
Definition at line 109 of file motor_driver.h.
|
inline |
Return the torque of the MotorClass object.
Definition at line 113 of file motor_driver.h.
|
inline |
Return the latest received encoder value from the motor.
Definition at line 117 of file motor_driver.h.
|
inline |
Return the latest received multi turn angle from the motor.
Definition at line 121 of file motor_driver.h.
|
inline |
Return the raw int64_t 0.01 degree multi turn angle.
Definition at line 124 of file motor_driver.h.
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.
[in] | _kp_pos | Proportional position gain |
[in] | _ki_pos | Integral position gain |
[in] | _kp_speed | Proportional speed gain |
[in] | _ki_speed | Integral speed gain |
[in] | _kp_torque | Proportional torque gain |
[in] | _ki_torque | Integral torque gain |
Definition at line 518 of file motor_driver.cpp.
void MotorControl::printPIDParameters | ( | ) |
The function prints the PI parameters of the motor.
Definition at line 534 of file motor_driver.cpp.
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.
void MotorControl::printTorqueCurrents | ( | ) |
|
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.
[in] | _previous_encoder_value | The previously measured encoder value |
[in] | _new_encoder_value | The newly measured encoder value |
Definition at line 582 of file motor_driver.cpp.
|
private |
This function ensures that the message is sent to the correct CAN port.
Definition at line 601 of file motor_driver.cpp.
|
private |
This function ensures that the message on the correct CAN port is read.
Definition at line 622 of file motor_driver.cpp.
|
private |
Definition at line 641 of file motor_driver.cpp.
double MotorControl::raw_position_reference |
Definition at line 150 of file motor_driver.h.
|
private |
Motor ID set through the Serial configurator [1 - 32].
Definition at line 153 of file motor_driver.h.
|
private |
Motor address = ID + 0x140. Declared for convenience.
Definition at line 156 of file motor_driver.h.
|
private |
Used to decide which CAN port to use. Possible values {1, 2}.
Definition at line 159 of file motor_driver.h.
|
private |
Motor PID Parameters.
Definition at line 162 of file motor_driver.h.
|
private |
Definition at line 163 of file motor_driver.h.
|
private |
Definition at line 164 of file motor_driver.h.
|
private |
Definition at line 165 of file motor_driver.h.
|
private |
Definition at line 166 of file motor_driver.h.
|
private |
Definition at line 167 of file motor_driver.h.
|
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.
|
private |
Number of rotations of the built-in motor, not the output-shaft.
Definition at line 175 of file motor_driver.h.
|
private |
Keep track of previous encoder position. This way you can detect turns. See innerMotorTurnCompleted().
Definition at line 179 of file motor_driver.h.
|
private |
This variable contains the multi turn angle read by the motor in degrees.
Definition at line 182 of file motor_driver.h.
|
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.
|
private |
This is the true 64 bit representation of the multi turn angle in degrees.
Definition at line 188 of file motor_driver.h.
|
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.
|
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.
|
private |
position offset = true position - position
Definition at line 199 of file motor_driver.h.
|
private |
This is used to center the output shaft zero position.
Definition at line 202 of file motor_driver.h.
|
private |
Latest measured position of the shaft in radians.
Definition at line 205 of file motor_driver.h.
|
private |
Latest measured speed of the shaft in radians/second.
Definition at line 208 of file motor_driver.h.
|
private |
Latest measured shaft torque in Nm.
Definition at line 211 of file motor_driver.h.
|
private |
Latest measured temperature of the motor in degree Celsius.
Definition at line 214 of file motor_driver.h.
|
private |
The torque current reference set for the motor.
Definition at line 217 of file motor_driver.h.
|
private |
The measured torque current of the motor.
Definition at line 220 of file motor_driver.h.
|
private |
CAN message for this motor. Only buf field is set for every message sent.
Definition at line 224 of file motor_driver.h.
|
private |
CAN message object used to receive incomming CAN messages.
Definition at line 227 of file motor_driver.h.
|
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.
|
private |
All torque values are scaled to the interval [-max_torque_current, max_torque_current].
Definition at line 237 of file motor_driver.h.
|
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.
|
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.