Tetrapod Project
motor_driver.h
Go to the documentation of this file.
1 #ifndef motor_driver_h
2 #define motor_driver_h
3 
4 #include "motor_constants.h"
5 #include "config_motor_driver.h"
6 #include "teensy_can_ports.h"
7 #include "make_can_msg.h"
8 #include "FlexCAN_T4.h"
9 #include <math.h>
10 #include "utilities.h"
11 #include "Arduino.h"
12 
14 {
15 public:
18 
22  MotorControl(uint8_t _id, uint8_t _can_port_id, int _number_of_inner_motor_rotations, double _position_offset);
23 
27  bool readPIDParameters();
28 
39  (
40  double _kp_pos,
41  double _ki_pos,
42  double _kp_speed,
43  double _ki_speed,
44  double _kp_torque,
45  double _ki_torque
46  );
47 
50  void setPositionReference(double _angle);
51 
54  void setSpeedReference(double _speed);
55 
58  void setTorqueReference(double _torque);
59 
62  void setTorqueCurrent(int _torque_current);
63 
67  void readMotorControlCommandReply(unsigned char* _can_message);
68 
72  bool stopMotor();
73 
78  bool turnOffMotor();
79 
82  bool readMultiTurnAngle();
83 
87  bool readMotorStatus();
88 
90  void requestMotorStatus();
91 
97 
98  // TODO change to camelcase - also in related functions
101  uint8_t get_id(){return id;}
102 
105  double getPosition(){return position;}
106 
109  double getVelocity(){return speed;}
110 
113  double getTorque(){return torque;}
114 
118 
122 
125 
133  void getPIDParameters(
134  double &_kp_pos,
135  double &_ki_pos,
136  double &_kp_speed,
137  double &_ki_speed,
138  double &_kp_torque,
139  double &_ki_torque);
140 
142  void printPIDParameters();
143 
146  void printState();
147 
149 
151 private:
153  uint8_t id;
154 
156  uint16_t address;
157 
159  uint8_t can_port_id;
160 
162  double kp_pos;
163  double ki_pos;
164  double kp_speed;
165  double ki_speed;
166  double kp_torque;
167  double ki_torque;
168 
172 
176 
180 
183 
186 
189 
192 
195  // This parameters keeps track of whether or not this is the case.
197 
199  double position_offset = 0.0;
200 
202  double position_center_offset = M_PI/6.0; //M_PI/6.0;
203 
205  double position;
206 
208  double speed;
209 
211  double torque;
212 
214  double temperature;
215 
218 
221 
224  CAN_message_t can_message;
225 
227  CAN_message_t received_can_message;
228 
229  // Motor specific parameters
230 
233  double max_torque;
234 
238 
242 
246 
253  double innerMotorTurnCompleted(uint16_t _previous_encoder_value, uint16_t _new_encoder_value);
254 
256  void sendMessage(CAN_message_t _can_message);
257 
259  int readMessage(CAN_message_t &_can_message);
260 
261  // TODO Remove
262  void errorMessage();
263 };
264 
265 #endif
double kp_speed
Definition: motor_driver.h:164
double initial_number_of_inner_motor_rotations
Number of rotations of the inner motor during startup This is needed to create position commands.
Definition: motor_driver.h:171
void setSpeedReference(double _speed)
Set the desired motor speed.
double getTorque()
Return the torque of the MotorClass object.
Definition: motor_driver.h:113
void errorMessage()
int16_t torque_current_measured
The measured torque current of the motor.
Definition: motor_driver.h:220
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].
Definition: motor_driver.h:233
double getMultiTurnAngleRaw()
Return the raw int64_t 0.01 degree multi turn angle.
Definition: motor_driver.h:124
uint16_t address
Motor address = ID + 0x140. Declared for convenience.
Definition: motor_driver.h:156
CAN_message_t can_message
CAN message for this motor. Only buf field is set for every message sent.
Definition: motor_driver.h:224
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.
Definition: motor_driver.h:191
uint16_t previous_encoder_value
Keep track of previous encoder position. This way you can detect turns. See innerMotorTurnCompleted()...
Definition: motor_driver.h:179
double ki_torque
Definition: motor_driver.h:167
double kp_torque
Definition: motor_driver.h:166
double temperature
Latest measured temperature of the motor in degree Celsius.
Definition: motor_driver.h:214
double getPosition()
Return the position of the MotorClass object.
Definition: motor_driver.h:105
double target_position_offset
If the motor is initialized in certain positions the setPosition function will have zero offset of 60...
Definition: motor_driver.h:196
double number_of_inner_motor_rotations
Number of rotations of the built-in motor, not the output-shaft.
Definition: motor_driver.h:175
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}.
Definition: motor_driver.h:159
void readMotorControlCommandReply(unsigned char *_can_message)
The state variables position, velocity, and torque are updated based on the latest information from t...
uint8_t id
Motor ID set through the Serial configurator [1 - 32].
Definition: motor_driver.h:153
int readMessage(CAN_message_t &_can_message)
This function ensures that the message on the correct CAN port is read.
double getVelocity()
Return the velocity of the MotorClass object.
Definition: motor_driver.h:109
MotorControl()
Default constructor needed for initializing array.
Definition: motor_driver.h:17
int max_torque_current
All torque values are scaled to the interval [-max_torque_current, max_torque_current].
Definition: motor_driver.h:237
int16_t torque_current_reference
The torque current reference set for the motor.
Definition: motor_driver.h:217
bool readPIDParameters()
Read the motor PI parameters and update the private PI values.
double torque
Latest measured shaft torque in Nm.
Definition: motor_driver.h:211
double position_center_offset
This is used to center the output shaft zero position.
Definition: motor_driver.h:202
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 getMultiTurnAngle()
Return the latest received multi turn angle from the motor.
Definition: motor_driver.h:121
double ki_speed
Definition: motor_driver.h:165
double speed
Latest measured speed of the shaft in radians/second.
Definition: motor_driver.h:208
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
Definition: motor_driver.h:150
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.
Definition: motor_driver.h:185
uint8_t get_id()
Return the ID of the MotorClass object.
Definition: motor_driver.h:101
double position
Latest measured position of the shaft in radians.
Definition: motor_driver.h:205
double position_offset
position offset = true position - position
Definition: motor_driver.h:199
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...
Definition: motor_driver.h:245
double getEncoderValue()
Return the latest received encoder value from the motor.
Definition: motor_driver.h:117
int64_t multi_turn_angle_64_bit
This is the true 64 bit representation of the multi turn angle in degrees.
Definition: motor_driver.h:188
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.
Definition: motor_driver.h:227
void printTorqueCurrents()
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.
Definition: motor_driver.h:182
double kp_pos
Motor PID Parameters.
Definition: motor_driver.h:162
uint16_t max_encoder_value
The 16 bit encoder measures the position of the inner DC motor, not the shaft.
Definition: motor_driver.h:241