Tetrapod Project
single_leg_controller.h
Go to the documentation of this file.
1 #ifndef single_leg_controller_h
2 #define single_leg_controller_h
3 
4 // ROS
5 #include "ros/ros.h"
6 #include "std_msgs/Empty.h"
7 #include "std_msgs/Bool.h"
8 #include "std_msgs/Float64.h"
9 #include "std_msgs/Float64MultiArray.h"
10 #include "sensor_msgs/JointState.h"
11 #include "eigen_conversions/eigen_msg.h"
12 
13 // Eigen
14 #include <Eigen/Core>
15 
16 // Standard Library
17 #include <thread>
18 #include <iostream>
19 #include <iomanip>
20 
21 // Kinematics
22 #include <kinematics/kinematics.h>
23 
24 // Utilities
26 
28 {
30  public: enum GaitPhase {stance = 1, swing = 2, uninitialized = 3};
31 
32  /*** Constructor/Destructor ***/
33 
35  public: SingleLegController(double _publish_frequency);
36 
38  public: virtual ~SingleLegController(){};
39 
40  /*** ROS FUNCTIONS ***/
41 
43  public: void initROS();
44 
47  public: void generalizedCoordinatesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg);
48 
51  public: void generalizedVelocitiesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg);
52 
55  public: void generalizedForcesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg);
56 
60  private: void jointStateCallback(const sensor_msgs::JointStatePtr &_msg);
61 
64  public: void readyToProceedCallback(const std_msgs::Bool &_msg);
65 
68  private: void jointSetpointCallback(const std_msgs::Float64MultiArrayConstPtr &_msg);
69 
73  private: void motorConfirmationCallback(const std_msgs::Bool &_msg);
74 
75  /*** CONTROL FUNCTIONS ***/
76 
83  private: Eigen::Matrix<double, 3, 1> calculateSwingLegHeightTrajectory(double _percentage, double _period, double _max_swing_height, double _hip_height);
84 
92  private: void calculateSingleAxisTrajectory
93  (
94  const double &_percentage,
95  const double &_period,
96  const double &_max_travel,
97  double &_axis_pos,
98  double &_axis_vel,
99  double &_axis_acc
100  );
101 
103  public: void updateSwingFootPositionTrajectory();
104 
106  public: void updateStanceFootPositionTrajectory();
107 
109  public: void updateJointReferences();
110 
117  public: void updateJointTorqueCommands
118  (
119  const Eigen::Matrix<double, 3, 1> &_joint_pos_ref,
120  const Eigen::Matrix<double, 3, 1> &_joint_vel_ref,
121  const Eigen::Matrix<double, 3, 1> &_joint_acc_ref,
122  const Eigen::Matrix<double, 3, 1> &_joint_pos,
123  const Eigen::Matrix<double, 3, 1> &_joint_vel
124  );
125 
127  public: void updateJointTorqueCommands();
128 
130  public: void updateClosedLoopTorqueCommands();
131 
134  public: void updateJointVelocityCommands();
135 
137  public: void sendJointPositionCommands();
138 
140  public: void sendJointVelocityCommands();
141 
143  public: void sendJointTorqueCommands();
144 
146  public: void increaseIterator();
147 
149  public: bool updateGaitPhase();
150 
152  public: void updateFootTrajectoryReference();
153 
156 
158  public: void setPoseGoal(Eigen::Matrix<double, 3, 1> _foot_pos);
159 
161  public: void setMotorGains();
162 
166  public: bool moveFootToPosition(Eigen::Matrix<double, 3, 1> _foot_goal_pos);
167 
169  public: bool moveJointsToSetpoints();
170 
172  public: bool moveFootToNominalPosition();
173 
174 
175 
176  /*** HELPER FUNCTIONS ***/
177 
179  public: void checkForNewMessages();
180 
183  public: bool readyToProceed(){return ready_to_proceed;}
184 
186  public: void resetReadyToProceed(){ready_to_proceed = false;}
187 
190  public: bool isTargetPositionReached();
191 
193  public: bool isJointVelocitySmall();
194 
196  public: bool isInitialStateReceived();
197 
199  public: void printTorqueReferences();
200 
202  public: void printSpatialTrajectories();
203 
205  public: void printJointTrajectories();
206 
208  public: void printAllStates();
209 
211  public: void writeToLog();
212 
215  public: GaitPhase getGaitPhase(){return gait_phase;}
216 
217 
218  /*** STATE VARIABLES ***/
219 
221  private: Eigen::Matrix<double, 3, 1> foot_pos_ref = Eigen::Matrix<double, 3, 1>::Zero();
222 
224  private: Eigen::Matrix<double, 3, 1> foot_vel_ref = Eigen::Matrix<double, 3, 1>::Zero();
225 
227  private: Eigen::Matrix<double, 3, 1> foot_acc_ref = Eigen::Matrix<double, 3, 1>::Zero();
228 
230  private: Eigen::Matrix<double, 3, 1> foot_pos_goal = Eigen::Matrix<double, 3, 1>::Zero();
231 
233  private: Eigen::Matrix<double, 3, 1> joint_pos_goal = Eigen::Matrix<double, 3, 1>::Zero();
234 
236  private: Eigen::Matrix<double, 3, 1> joint_pos_initial = Eigen::Matrix<double, 3, 1>::Zero();
237 
239  private: Eigen::Matrix<double, 3, 1> joint_pos = Eigen::Matrix<double, 3, 1>::Zero();
240 
242  private: Eigen::Matrix<double, 3, 1> joint_vel = Eigen::Matrix<double, 3, 1>::Zero();
243 
245  private: Eigen::Matrix<double, 3, 1> joint_torque = Eigen::Matrix<double, 3, 1>::Zero();
246 
248  private: Eigen::Matrix<double, 3, 1> joint_pos_ref = Eigen::Matrix<double, 3, 1>::Zero();
249 
251  private: Eigen::Matrix<double, 3, 1> joint_vel_ref = Eigen::Matrix<double, 3, 1>::Zero();
252 
254  private: Eigen::Matrix<double, 3, 1> joint_acc_ref = Eigen::Matrix<double, 3, 1>::Zero();
255 
257  private: Eigen::Matrix<double, 3, 1> joint_torque_ref = Eigen::Matrix<double, 3, 1>::Zero();
258 
260  private: Eigen::Matrix<double, 3, 1> joint_vel_commands = Eigen::Matrix<double, 3, 1>::Zero();
261 
263  private: Eigen::Matrix<double, 3, 1> joint_torque_commands = Eigen::Matrix<double, 3, 1>::Zero();
264 
266  private: GaitPhase gait_phase = GaitPhase::stance;
267 
270  private: bool ready_to_proceed = false;
271 
274  private: double current_iteration = 0;
275 
277  private: double final_iteration = 400.0;
278 
280  private: double current_pose_iteration = 0;
281 
283  private: double final_pose_iteration = 100;
284 
286  private: bool gains_set = false;
287 
288 
289 
290  /*** PARAMETERS ***/
291 
293  private: const int NUMBER_OF_MOTORS = 3;
294 
296  private: const double UNINITIALIZED_STATE = 100.0;
297 
299  private: const double POSITION_CONVERGENCE_CRITERIA = 0.01; // Cirka 1 degree error for all joints
300 
302  private: const double POSITION_COMMAND = 1.0;
303 
305  private: const double VELOCITY_COMMAND = 2.0;
306 
308  private: const double TORQUE_COMMAND = 3.0;
309 
311  private: Eigen::Matrix<double, 3, 3> K_p = Eigen::Matrix<double, 3, 3>::Zero();
312 
314  private: Eigen::Matrix<double, 3, 3> K_d = Eigen::Matrix<double, 3, 3>::Zero();
315 
317  private: Eigen::Matrix<double, 3, 3> K_pos_error_vel_control = Eigen::Matrix<double, 3, 3>::Zero();
318 
321  private: double publish_frequency = 200.0;
322 
324  private: double phase_period = 0.5;
325 
327  private: double hip_height = 0.35;
328 
330  private: double x_nominal = 0.1;
331 
333  private: double y_nominal = 0.3;
334 
336  private: double x_step_distance = 0.2;
337 
339  private: double y_step_distance = 0.0;
340 
342  private: double max_swing_height = 0.12;
343 
345  private: double pose_period;
346 
348  private: double max_pose_vel = math_utils::degToRad(50);
349 
351  double k_p_pos_hy;
352 
354  double k_i_pos_hy;
355 
357  double k_p_pos_hp;
358 
360  double k_i_pos_hp;
361 
363  double k_p_pos_kp;
364 
366  double k_i_pos_kp;
367 
369  double k_p_vel_hy;
370 
372  double k_i_vel_hy;
373 
375  double k_p_vel_hp;
376 
378  double k_i_vel_hp;
379 
381  double k_p_vel_kp;
382 
384  double k_i_vel_kp;
385 
388 
391 
394 
397 
400 
403 
404 
405 
406  /*** ROS VARIABLES ***/
407 
409  private: std::unique_ptr<ros::NodeHandle> node_handle;
410 
412  private: ros::Subscriber generalized_coordinates_subscriber;
413 
415  private: ros::Subscriber generalized_velocities_subscriber;
416 
418  private: ros::Subscriber generalized_forces_subscriber;
419 
421  private: ros::Subscriber joint_state_subscriber;
422 
424  private: ros::Subscriber ready_to_proceed_subscriber;
425 
427  private: ros::Subscriber joint_setpoint_subscriber;
428 
430  private: ros::Subscriber motor_confirmation_subscriber;
431 
433  private: ros::Publisher joint_state_publisher;
434 
436  private: ros::Publisher motor_gain_publisher;
437 
439  private: sensor_msgs::JointState motor_command_msg;
440 
442  private: ros::Publisher log_joint_states_publisher;
443 
445  private: sensor_msgs::JointState joint_state_log_msg;
446 
448  private: ros::Publisher log_joint_references_publisher;
449 
451  private: sensor_msgs::JointState joint_reference_log_msg;
452 
453  /*** UTILITY VARIABLES ***/
454 
457 };
458 
459 #endif
A class for analytical Kinematics Solving.
Definition: kinematics.h:54
const int NUMBER_OF_MOTORS
The number of motors used in single leg control tests.
Eigen::Matrix< double, 3, 1 > foot_pos_goal
When using setpointTrajectory control these are the joint angles we want to reach eventually.
double k_p_torque_kp
Knee pitch motor torque control proportional gain.
ros::Subscriber motor_confirmation_subscriber
Subscribes to confirmation messages from the motor.
double max_swing_height
The maximum height above the ground to lift the foot.
void writeToLog()
Write the joint references and estimated states to the log.
ros::Subscriber generalized_forces_subscriber
Subscribes to generalized forces messages.
std::unique_ptr< ros::NodeHandle > node_handle
ROS node handle.
double k_i_torque_hp
Hip pitch motor torque control integral gain.
GaitPhase getGaitPhase()
Return the leg's phase state.
void jointSetpointCallback(const std_msgs::Float64MultiArrayConstPtr &_msg)
Listens to joint setpoints messages.
double max_pose_vel
Max pose control joint angle velocity [rad/s].
Eigen::Matrix< double, 3, 1 > joint_torque_ref
The reference joint torques for the motors.
void updateJointVelocityCommands()
Updates the joint velocity control commands based on the joint velocity reference and the joint posit...
virtual ~SingleLegController()
Destructor.
Eigen::Matrix< double, 3, 1 > joint_vel_ref
The reference joint velocities for the motors.
void setMotorGains()
Tries to set the control gains in the motors. The function loops until it succeeds.
double k_p_torque_hp
Hip pitch motor torque control proportional gain.
void printJointTrajectories()
Print the joint trajectory references.
double k_p_torque_hy
Hip yaw motor torque control proportional gain.
ros::Publisher log_joint_references_publisher
Publishes joint position references for logging.
sensor_msgs::JointState motor_command_msg
The joint state message that is sent to the Teensy.
void printAllStates()
Print various information about all joints.
double publish_frequency
The expected publish frequency of the robot. This is used to calculate the maximum number of iteratio...
void updateFootTrajectoryReference()
Update the foot trajectory based on the leg state.
const double POSITION_CONVERGENCE_CRITERIA
Convergence crieria for position control test.
Eigen::Matrix< double, 3, 1 > foot_acc_ref
The foot acceleration reference relative to the hip.
double k_p_vel_hy
Hip yaw joint motor velocity control proportional gain.
void updateClosedLoopTorqueCommands()
A non model based torque controller.
void printSpatialTrajectories()
Print the foot trajectory reference.
Eigen::Matrix< double, 3, 1 > joint_torque
The estimated joint torques of the motors.
Eigen::Matrix< double, 3, 1 > joint_pos_goal
The joint angles we want to eventually reach when using pose trajectory control.
Eigen::Matrix< double, 3, 1 > joint_torque_commands
The joint torque command being sent to the motors.
double k_i_vel_hp
Hip pitch joint motor velocity control integral gain.
double k_i_vel_hy
Hip yaw joint motor velocity control integral gain.
double k_i_torque_kp
Knee pitch motor torque control integral gain.
double final_pose_iteration
The final iteration used for pose control.
bool moveFootToNominalPosition()
The function tries to move the foot to the nominal trajectory position.
Eigen::Matrix< double, 3, 1 > joint_pos
The estimated joint angles of the motors.
Eigen::Matrix< double, 3, 1 > joint_acc_ref
The reference joint accelerations for the motors.
ros::Subscriber ready_to_proceed_subscriber
Subscribes to messages deciding whether or not we should proceed in the script.
void updateStanceFootPositionTrajectory()
Update the stance foot tractory for the leg.
ros::Publisher log_joint_states_publisher
Publishes joint positions for logging.
Eigen::Matrix< double, 3, 1 > joint_pos_ref
The reference joint angles for the motors.
const double POSITION_COMMAND
The identifier to be used for position command messages.
double k_p_vel_hp
Hip pitch joint motor velocity control proportional gain.
ros::Subscriber generalized_velocities_subscriber
Subscribes to generalized velocities messages.
ros::Publisher joint_state_publisher
Publishes velocity commands to the teensy to control the motors.
Kinematics kinematics
Kinematics object.
void printTorqueReferences()
Print the joint torque references.
ros::Subscriber generalized_coordinates_subscriber
Subscribes to generalized coordinates messages.
double k_p_pos_kp
Knee pitch joint motor position control proportional gain.
void generalizedForcesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg)
Listens to joint force messages from the simulated leg.
double phase_period
The duration of a phase period in seconds.
bool gains_set
Keeps track of whether or not the motor gains have been set.
double k_p_vel_kp
Knee pitch joint motor velocity control proportional gain.
ros::Subscriber joint_setpoint_subscriber
Subscribes to joint setpoint messages.
Eigen::Matrix< double, 3, 3 > K_d
A diagonal matrix of derivative gains for the closed loop torque controller.
Eigen::Matrix< double, 3, 1 > joint_vel
The estimated joint velocities of the motors.
void jointStateCallback(const sensor_msgs::JointStatePtr &_msg)
Listens to joint state messages from the motors The messages contains joint angles,...
void resetReadyToProceed()
Set the ready_to_proceed flag to false.
Eigen::Matrix< double, 3, 1 > foot_pos_ref
The foot position reference relative to the hip.
void initROS()
This function initilizes ROS.
SingleLegController(double _publish_frequency)
Constructor.
void sendJointVelocityCommands()
Updates the joint velocity commands and send them to the motors.
void sendJointPositionCommands()
Sends a joint position commands to the motors.
double x_step_distance
The step distance in the x direction.
bool updatePoseControlJointTrajectoryReference()
Update the pose control trajectory reference for the single leg.
bool isInitialStateReceived()
Check if the uninitalized joint state of the robot has been overridden by measurements.
double current_pose_iteration
The current iteration used for pose control.
double k_p_pos_hp
Hip pitch joint motor position control proportional gain.
double hip_height
The standard height of the hips above the ground.
Eigen::Matrix< double, 3, 1 > calculateSwingLegHeightTrajectory(double _percentage, double _period, double _max_swing_height, double _hip_height)
This function calculates the swing leg height trajectory based on various gait cycle parameters.
const double TORQUE_COMMAND
The identifier to be used for torque command messages.
void motorConfirmationCallback(const std_msgs::Bool &_msg)
Listens to a confirmation message from the motors which is used to indicate if the motor gains have b...
double final_iteration
The final iteration of a gait phase.
bool isJointVelocitySmall()
Checks if the sum of squared joint velocities are smaller than a certain threshold.
void updateSwingFootPositionTrajectory()
Update the swing foot trajectory for the leg.
void setPoseGoal(Eigen::Matrix< double, 3, 1 > _foot_pos)
Set the final and initial states for pose control trajectories.
double y_step_distance
The step distance in the y direction.
sensor_msgs::JointState joint_state_log_msg
The joint state message that is used to log states.
double k_i_pos_hy
Hip yaw joint motor position control integral gain.
double k_i_torque_hy
Hip yaw motor torque control integral gain.
Eigen::Matrix< double, 3, 1 > foot_vel_ref
The foot velocity reference relative to the hip.
void updateJointReferences()
Update The joint trajectory reference based on the spatial foot trajectory.
ros::Subscriber joint_state_subscriber
Subscribes to joint state messages from the motors.
double current_iteration
The current iteration in a gait phase. This should be incremented for every control loop.
void generalizedVelocitiesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg)
Listens to joint velocity messages from the simulated leg.
Eigen::Matrix< double, 3, 3 > K_p
A diagonal matrix of proportional gains for the closed loop torque controller.
ros::Publisher motor_gain_publisher
Publishes desired motor gains to the motors.
bool moveJointsToSetpoints()
The function tries to move the foot to the foot position given by the input parameters.
Eigen::Matrix< double, 3, 1 > joint_pos_initial
The joint angles at the beginning of a trajectory.
const double VELOCITY_COMMAND
The identifier to be used for velocity command messages.
bool ready_to_proceed
A variable used to control the flow of the program. It can be set throuh a boolean message received b...
sensor_msgs::JointState joint_reference_log_msg
The joint state message that is used to log references.
bool updateGaitPhase()
Increments the gait cycle iterator and updates the leg state.
bool moveFootToPosition(Eigen::Matrix< double, 3, 1 > _foot_goal_pos)
Creates a trajectory from the current foot position to the goal foot position and moves the foot to t...
void increaseIterator()
Increment the gait cycle iterator. It stops iterating when the final iteration is reached.
double k_p_pos_hy
Hip yaw joint motor position control proportional gain.
void readyToProceedCallback(const std_msgs::Bool &_msg)
The ready_to_proceed parameter is changed based on the incomming message.
void sendJointTorqueCommands()
Updates the joint torque control commands based on the desired foot position.
void checkForNewMessages()
Function used to check if any new ROS messages has been received.
void updateJointTorqueCommands()
Overloaded function that uses the current joint references and joint states.
double y_nominal
The nominal y position of the foot relative to the hip.
void generalizedCoordinatesCallback(const std_msgs::Float64MultiArrayConstPtr &_msg)
Listens to joint angle messages from the simulated leg.
double pose_period
The time used to change from one pose to the next.
bool isTargetPositionReached()
Check if the squared error between the current joint angles and target joint angles is smaller than s...
double k_i_pos_kp
Knee pitch joint motor position control integral gain.
const double UNINITIALIZED_STATE
The value of an uninitialized state.
Eigen::Matrix< double, 3, 1 > joint_vel_commands
The velocity commands sent to the motors.
Eigen::Matrix< double, 3, 3 > K_pos_error_vel_control
A digonal matrix for of gains used for the position error in the closed loop velocity controller.
double k_i_pos_hp
Hip pitch joint motor position control integral gain.
GaitPhase gait_phase
The current gait phase of the leg.
void calculateSingleAxisTrajectory(const double &_percentage, const double &_period, const double &_max_travel, double &_axis_pos, double &_axis_vel, double &_axis_acc)
This functions calculates a smooth trajectory along a single linear axis. Please see the report for d...
bool readyToProceed()
Check whether or not a ready to proceed message has been received.
double x_nominal
The nominal x position of the foot relative to the hip.
double k_i_vel_kp
Knee pitch joint motor velocity control integral gain.
double degToRad(const double &_deg)
The degToRad function converts degrees to radians.
Definition: angle_utils.cpp:33