44 Serial.println(
"Trying to read motor states");
52 Serial.println(
"Constructor - Trying read multi turn angle");
106 Serial.println(
"In the function readPIDParameters an incorrect answer was received");
117 Serial.println(
"In the function readPIDParameters no reply was received");
172 Serial.println(
"In the function writePIDParametersToRAM an incorrect reply was received");
183 Serial.println(
"In the function writePIDParametersToRAM no reply was received");
211 int32_t speed_001dps = (int)round(
GEAR_REDUCTION*_speed*100.0*180.0/M_PI);
250 uint16_t new_encoder_value = _can_message[7]*256 + _can_message[6];
264 int16_t speed_motor_dps = _can_message[5]*256 + _can_message[4];
270 int16_t torque_current = _can_message[3]*256 + _can_message[2];
300 Serial.println(
"In the function stopMotor an incorrect reply was received");
311 Serial.println(
"In the function stopMotor an incorrect reply was received");
340 Serial.println(
"In the function stopMotor an incorrect reply was received");
351 Serial.println(
"In the function turnOffMotor no reply was received");
405 Serial.println(
"In the function readMultiTurnAngle an incorrect reply was received");
416 Serial.println(
"In the function readMultiTurnAngle no reply was received");
449 Serial.println(
"In the function readMotorStatus an incorrect reply was received");
460 Serial.println(
"In the function readMotorStatus no reply was received");
501 Serial.println(
"getCompleteEncoderPosition - wrong reply received.");
511 Serial.println(
"getCompleteEncoderPosition - no reply received.");
537 Serial.print(
"Motor "); Serial.print(
id); Serial.print(
" - CAN "); Serial.print(
can_port_id); Serial.println(
" ");
538 Serial.print(
"kp_pos:\t"); Serial.println(
kp_pos);
539 Serial.print(
"ki_pos:\t"); Serial.println(
ki_pos);
540 Serial.print(
"kp_vel:\t"); Serial.println(
kp_speed);
541 Serial.print(
"kp_vel:\t"); Serial.println(
ki_speed);
542 Serial.print(
"kp_tor:\t"); Serial.println(
kp_torque);
543 Serial.print(
"kp_tor:\t"); Serial.println(
ki_torque);
550 Serial.print(
"Motor "); Serial.print(
id); Serial.print(
", CAN "); Serial.print(
can_port_id); Serial.print(
" - ");
551 Serial.print(
"Pos: "); Serial.print(
position); Serial.print(
"[rad]\t");
552 Serial.print(
"Vel: "); Serial.print(
speed); Serial.print(
"[rad/s]\t");
553 Serial.print(
"Tor: "); Serial.print(
torque); Serial.print(
"[Nm]\t");
564 _torque_current = -_torque_current;
615 Serial.println(
"In the function sendMessage, an invalid CAN port was selected");
636 Serial.println(
"In the function readMessage, an invalid CAN port was selected");
double initial_number_of_inner_motor_rotations
Number of rotations of the inner motor during startup This is needed to create position commands.
void setSpeedReference(double _speed)
Set the desired motor speed.
int16_t torque_current_measured
The measured torque current of the motor.
bool turnOffMotor()
The motor stops exerting a control output. The motor's operating state and previously received contro...
bool stopMotor()
The motor stops exerting a control output. The motor will remain idle until a new control command or ...
double max_torque
The torque in Newton meter has to lie in the interval [-max_torque, max_torque].
uint16_t address
Motor address = ID + 0x140. Declared for convenience.
CAN_message_t can_message
CAN message for this motor. Only buf field is set for every message sent.
void printPIDParameters()
The function prints the PI parameters of the motor.
bool readMotorStatus()
A request is sent to the motor asking it to send back its state. The private state varibles position,...
int64_t multi_turn_angle_001lsb
This is the 64 bit representation of the raw int64_t multi turn angle in 0.01 degrees.
uint16_t previous_encoder_value
Keep track of previous encoder position. This way you can detect turns. See innerMotorTurnCompleted()...
double number_of_inner_motor_rotations
Number of rotations of the built-in motor, not the output-shaft.
void setTorqueCurrent(int _torque_current)
Set the desired motor torque current. Scales linearly with torque.
uint8_t can_port_id
Used to decide which CAN port to use. Possible values {1, 2}.
void readMotorControlCommandReply(unsigned char *_can_message)
The state variables position, velocity, and torque are updated based on the latest information from t...
int readMessage(CAN_message_t &_can_message)
This function ensures that the message on the correct CAN port is read.
MotorControl()
Default constructor needed for initializing array.
int max_torque_current
All torque values are scaled to the interval [-max_torque_current, max_torque_current].
int16_t torque_current_reference
The torque current reference set for the motor.
bool readPIDParameters()
Read the motor PI parameters and update the private PI values.
double torque
Latest measured shaft torque in Nm.
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...
double speed
Latest measured speed of the shaft in radians/second.
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.
double raw_position_reference
void requestMotorStatus()
A request is sent to the motor asking it to send back its state, position, velocity,...
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.
int32_t multi_turn_angle_32_bit_001lsb
This variable contains the 32 bit representation of the raw 0.01 degree multi turn angle.
double position
Latest measured position of the shaft in radians.
double position_offset
position offset = true position - position
uint16_t encoder_turn_threshold
If the change in encoder value is larger than this it is safe to assume that the inner motor complete...
int64_t multi_turn_angle_64_bit
This is the true 64 bit representation of the multi turn angle in degrees.
bool readCompleteEncoderPosition()
All the encoder data from the motor is read and printed. This includes the encoder value,...
CAN_message_t received_can_message
CAN message object used to receive incomming CAN messages.
void setPositionReference(double _angle)
Set the desired multi turn motor angle.
void sendMessage(CAN_message_t _can_message)
This function ensures that the message is sent to the correct CAN port.
void printState()
The ID, encoder value, multi turn angle, position, speed and torque of the motor are printed.
void setTorqueReference(double _torque)
Set the desired motor torque.
bool readMultiTurnAngle()
The multi turn angle of the motor updated.
int32_t multi_turn_angle_32_bit
This variable contains the multi turn angle read by the motor in degrees.
double kp_pos
Motor PID Parameters.
uint16_t max_encoder_value
The 16 bit encoder measures the position of the inner DC motor, not the shaft.
#define MOTOR_ADDRESS_OFFSET
const int MAX_ENCODER_VALUE
#define MOTOR_COMMAND_MOTOR_STOP
#define MOTOR_COMMAND_MOTOR_OFF
#define MOTOR_COMMAND_READ_MULTI_TURN_ANGLE
const int MAX_TORQUE_CURRENT
#define MOTOR_COMMAND_READ_MOTOR_STATUS_2
#define MOTOR_COMMAND_READ_PID_PARAMETERS
#define MOTOR_COMMAND_WRITE_PID_PARAMETERS_TO_RAM
const double GEAR_REDUCTION
#define MOTOR_COMMAND_READ_ENCODER_POSITION
const double ROTATION_DISTANCE
void motorStop(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to stop its controller ...
void readMotorStatus2(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to send back a CAN mess...
void torqueCurrentControl(unsigned char *_can_message, int16_t _torque_current)
The function creates a CAN data message that can be used to set the motor's torque current....
void speedControl(unsigned char *_can_message, int32_t speed)
The function creates a CAN data message that can be used to set the motor's speed....
void positionControl1(unsigned char *_can_message, int32_t _multiturn_angle)
The function creates a CAN data message that can be used to set the motor's multi turn position....
void motorOff(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to turn off its control...
void readMultiTurnAngle(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to send back a CAN mess...
void readPIDParameters(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to send back a CAN mess...
void readEncoderPosition(unsigned char *_can_message)
The function creates a CAN data message that can be used to request the motor to send back a CAN mess...
void writePIDParametersToRAM(unsigned char *_can_message, unsigned char _k_p_position, unsigned char _k_i_position, unsigned char _k_p_speed, unsigned char _k_i_speed, unsigned char _k_p_torque, unsigned char _k_i_torque)
The function creates a CAN data message that can be used to set the PI parameters of the motor in RAM...
static FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can_port_1
static FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_16 > can_port_2
void delay_microseconds(double microsecond_delay)
Performs a nonblocking delay in the program Functions such as delay might block some interrupts.