Tetrapod Project
controller.h
Go to the documentation of this file.
1 // ROS
2 #include "ros/ros.h"
3 #include "std_msgs/Empty.h"
4 #include "std_msgs/Bool.h"
5 #include "std_msgs/Float64.h"
6 #include "std_msgs/Float64MultiArray.h"
7 #include "sensor_msgs/JointState.h"
8 #include "geometry_msgs/Twist.h"
9 #include "std_srvs/Empty.h"
10 #include "eigen_conversions/eigen_msg.h"
11 
12 // Eigen
13 #include <Eigen/Core>
14 
15 // Standard Library
16 #include <thread>
17 #include <functional>
18 #include <cmath>
19 #include <string>
20 // Kinematics
21 #include <kinematics/kinematics.h>
22 
23 #define UNINITIALIZED_JOINT_STATE 10.0
24 #define ROBOT_NAME "my_robot"
25 #define LEG_OFFSET_LENGTH 0.25
26 #define NUMBER_OF_LEGS 4
27 #define ACTUATORS_PER_LEG 3
28 
29 class Controller{
30  protected: using JointState = Eigen::Matrix<double, 12, 1>;
31 
32  public: Controller(int controller_freq);
33 
34  public: virtual ~Controller();
35 
36  /*** Functions ***/
37  public: bool RunStandUpSequence();
38 
39  public: void waitForReadyToProceed();
40 
41  public: virtual void UpdateFeetReferences() = 0;
42 
43  public: bool UpdateJointCommands();
44 
45  public: bool UpdateVelocityCommands();
46 
47  public: bool sendJointPositionCommands();
48 
49  public: void StandStill(const double &duration);
50 
51  public: virtual void setInitialConfiguration();
52 
53  public: void SetTwistCommand(double lin_vel_cmd_x, double lin_vel_cmd_y, double ang_vel_cmd_z);
54 
55  public: void initROS();
56 
57  public: void WaitForInitialState();
58 
59  public: bool MoveFeetToPositions(Eigen::Matrix<double, 3, 1> fl_goal_foot_pos,
60  Eigen::Matrix<double, 3, 1> fr_goal_foot_pos,
61  Eigen::Matrix<double, 3, 1> rl_goal_foot_pos,
62  Eigen::Matrix<double, 3, 1> rr_goal_foot_pos);
63 
64  public: bool MoveJointsToSetpoints(Eigen::Matrix<double, 12, 1> goal_joint_angles);
65 
66  public: bool MoveFootToPosition(const Kinematics::LegType &leg_type, const Eigen::Matrix<double, 3, 1> &goal_foot_position);
67 
68  public: void UpdateFeetPositions();
69 
70  protected: void jointStateCallback(const sensor_msgs::JointStatePtr &msg);
71 
72  protected: void readyToProceedCallback(const std_msgs::Bool &msg);
73 
75  protected: void TwistCommandCallback(const geometry_msgs::TwistConstPtr &_msg);
76 
77  protected: void TwistStateCallback(const geometry_msgs::TwistConstPtr &_msg);
78 
79  protected: void BasePoseStateCallback(const std_msgs::Float64MultiArrayConstPtr &msg);
80 
81  public: bool CheckReadyToProceed(){return ready_to_proceed;}
82 
83  public: void ResetReadyToProceedFlag(){ready_to_proceed = false;}
84 
85  public: void SetReadyToProceedFlag(){ready_to_proceed = true;}
86 
92  public: bool ReadyToProceed(std_srvs::Empty::Request &_req,
93  std_srvs::Empty::Response &_res);
94 
100  public: bool Shutdown(std_srvs::Empty::Request &_req,
101  std_srvs::Empty::Response &_res);
102 
103  /*** Variables ***/
104  private: bool ready_to_proceed = false;
105 
106  protected: std::string node_name = "controller_node";
107 
108  protected: std::string robot_name = ROBOT_NAME;
109 
110  protected: int controller_freq;
111 
112  protected: Kinematics kinematics;
113 
114  protected: ros::NodeHandlePtr nodeHandle;
115 
117  private: ros::ServiceServer shutdownService;
118 
120  private: ros::ServiceServer readyToProceedService;
121 
122  protected: ros::Publisher joint_command_publisher;
123 
124  protected: ros::Subscriber joint_state_subscriber;
125 
126  protected: ros::Subscriber base_pose_state_subscriber;
127 
128  protected: ros::Subscriber ready_to_proceed_subscriber;
129 
131  protected: ros::Subscriber twist_command_subscriber;
132 
133  protected: ros::Subscriber twist_state_subscriber;
134 
136 
138 
140 
142 
144 
146 
148 
150 
152 
154 
156 
158 
160 
162 
164 
166 
167  protected: JointState joint_angle_commands = JointState::Zero();
168 
169  protected: JointState joint_velocity_commands = JointState::Zero();
170 
171  protected: JointState joint_acceleration_commands = JointState::Zero();
172 
173  protected: JointState joint_torque_commands = JointState::Zero();
174 
175  protected: JointState joint_angles = JointState::Constant(UNINITIALIZED_JOINT_STATE);
176 
177  protected: JointState joint_velocities = JointState::Zero();
178 
179  protected: JointState joint_torques = JointState::Zero();
180 
181  protected: Eigen::Matrix<double, 6, 1> base_pose_state = Eigen::Matrix<double, 6, 1>::Zero();
182 
183  protected: Eigen::Matrix<double, 6, 1> base_pose_commands = Eigen::Matrix<double, 6, 1>::Zero();
184 
186  protected: double lin_vel_x = 0.0;
187 
189  protected: double lin_vel_y = 0.0;
190 
192  protected: double ang_vel_z = 0.0;
193 
194  protected: double _lin_vel_x_estimated = 0.0;
195 
196  protected: double _lin_vel_y_estimated = 0.0;
197 
198  protected: double _ang_vel_z_estimated = 0.0;
199 
200  protected: double _lin_vel_z_measured = 0.0;
201 
202  protected: double _ang_vel_x_measured = 0.0;
203 
204  protected: double _ang_vel_y_measured = 0.0;
205 
206  protected: double nominal_base_height = 0.35;
207 
208  // For logging
209 
210  public: void WriteToLog();
211 
212  private: ros::Publisher joint_state_logger;
213 
214  private: ros::Publisher joint_command_logger;
215 
216  private: ros::Publisher base_twist_state_logger;
217 
218  private: ros::Publisher base_pose_state_logger;
219 
220  private: ros::Publisher base_twist_command_logger;
221 
222  private: ros::Publisher base_pose_command_logger;
223 
224 };
Eigen::Vector3d fr_offset
Definition: controller.h:137
bool UpdateJointCommands()
Definition: controller.cpp:28
ros::NodeHandlePtr nodeHandle
Definition: controller.h:114
void TwistCommandCallback(const geometry_msgs::TwistConstPtr &_msg)
Converts twist command messages into desired linear and angular body velocities.
Definition: controller.cpp:254
virtual ~Controller()
Definition: controller.cpp:9
Eigen::Vector3d fl_acceleration_body
Definition: controller.h:159
bool ready_to_proceed
Definition: controller.h:104
double _ang_vel_x_measured
Definition: controller.h:202
double _lin_vel_x_estimated
Definition: controller.h:194
void jointStateCallback(const sensor_msgs::JointStatePtr &msg)
Definition: controller.cpp:237
double _lin_vel_y_estimated
Definition: controller.h:196
Eigen::Vector3d fl_offset
Definition: controller.h:135
double lin_vel_y
The desired linear robot velocity in the body frame's y direction.
Definition: controller.h:189
void SetReadyToProceedFlag()
Definition: controller.h:85
bool CheckReadyToProceed()
Definition: controller.h:81
JointState joint_angles
Definition: controller.h:175
ros::Publisher joint_state_logger
Definition: controller.h:212
Eigen::Vector3d fl_velocity_body
Definition: controller.h:151
ros::Subscriber ready_to_proceed_subscriber
Definition: controller.h:128
void BasePoseStateCallback(const std_msgs::Float64MultiArrayConstPtr &msg)
Definition: controller.cpp:272
Eigen::Vector3d rr_offset
Definition: controller.h:141
bool UpdateVelocityCommands()
Definition: controller.cpp:65
ros::Publisher base_twist_state_logger
Definition: controller.h:216
Eigen::Vector3d rl_position_body
Definition: controller.h:147
ros::Subscriber twist_command_subscriber
Subscribes to twist command messages from an external controller.
Definition: controller.h:131
double _ang_vel_y_measured
Definition: controller.h:204
ros::Subscriber base_pose_state_subscriber
Definition: controller.h:126
ros::Publisher joint_command_publisher
Definition: controller.h:122
void ResetReadyToProceedFlag()
Definition: controller.h:83
void StandStill(const double &duration)
Definition: controller.cpp:824
Controller(int controller_freq)
Definition: controller.cpp:3
Eigen::Vector3d rl_acceleration_body
Definition: controller.h:163
bool ReadyToProceed(std_srvs::Empty::Request &_req, std_srvs::Empty::Response &_res)
The ReadyToProceed function handles an incoming readyToProceed service request.
Definition: controller.cpp:842
JointState joint_velocity_commands
Definition: controller.h:169
void WaitForInitialState()
Definition: controller.cpp:376
void readyToProceedCallback(const std_msgs::Bool &msg)
Definition: controller.cpp:232
JointState joint_acceleration_commands
Definition: controller.h:171
Eigen::Matrix< double, 6, 1 > base_pose_commands
Definition: controller.h:183
Kinematics kinematics
Definition: controller.h:112
Eigen::Vector3d fl_position_body
Definition: controller.h:143
void TwistStateCallback(const geometry_msgs::TwistConstPtr &_msg)
Definition: controller.cpp:261
double _ang_vel_z_estimated
Definition: controller.h:198
virtual void UpdateFeetReferences()=0
int controller_freq
Definition: controller.h:110
Eigen::Vector3d rr_position_body
Definition: controller.h:149
double ang_vel_z
The desired angular robot velocity around the robot's z axis.
Definition: controller.h:192
Eigen::Matrix< double, 6, 1 > base_pose_state
Definition: controller.h:181
ros::Subscriber twist_state_subscriber
Definition: controller.h:133
void UpdateFeetPositions()
Definition: controller.cpp:712
ros::Publisher joint_command_logger
Definition: controller.h:214
Eigen::Vector3d fr_acceleration_body
Definition: controller.h:161
JointState joint_torques
Definition: controller.h:179
Eigen::Vector3d fr_position_body
Definition: controller.h:145
std::string robot_name
Definition: controller.h:108
bool MoveFootToPosition(const Kinematics::LegType &leg_type, const Eigen::Matrix< double, 3, 1 > &goal_foot_position)
Definition: controller.cpp:720
void WriteToLog()
Definition: controller.cpp:287
bool Shutdown(std_srvs::Empty::Request &_req, std_srvs::Empty::Response &_res)
The Shutdown function handles an incoming shutdown service request.
Definition: controller.cpp:850
std::string node_name
Definition: controller.h:106
bool MoveFeetToPositions(Eigen::Matrix< double, 3, 1 > fl_goal_foot_pos, Eigen::Matrix< double, 3, 1 > fr_goal_foot_pos, Eigen::Matrix< double, 3, 1 > rl_goal_foot_pos, Eigen::Matrix< double, 3, 1 > rr_goal_foot_pos)
Definition: controller.cpp:513
ros::Publisher base_twist_command_logger
Definition: controller.h:220
double lin_vel_x
The desired linear robot velocity in the body frame's x direction.
Definition: controller.h:186
ros::Publisher base_pose_state_logger
Definition: controller.h:218
Eigen::Vector3d rr_velocity_body
Definition: controller.h:157
Eigen::Vector3d rl_offset
Definition: controller.h:139
void waitForReadyToProceed()
Definition: controller.cpp:14
Eigen::Vector3d rr_acceleration_body
Definition: controller.h:165
ros::Subscriber joint_state_subscriber
Definition: controller.h:124
ros::ServiceServer shutdownService
ROS Shutdown Service.
Definition: controller.h:117
ros::ServiceServer readyToProceedService
ROS Ready to Proceed Service.
Definition: controller.h:120
JointState joint_torque_commands
Definition: controller.h:173
JointState joint_angle_commands
Definition: controller.h:167
void initROS()
Definition: controller.cpp:139
Eigen::Vector3d rl_velocity_body
Definition: controller.h:155
void SetTwistCommand(double lin_vel_cmd_x, double lin_vel_cmd_y, double ang_vel_cmd_z)
Definition: controller.cpp:280
Eigen::Matrix< double, 12, 1 > JointState
Definition: controller.h:30
double _lin_vel_z_measured
Definition: controller.h:200
double nominal_base_height
Definition: controller.h:206
bool RunStandUpSequence()
Definition: controller.cpp:386
bool sendJointPositionCommands()
Definition: controller.cpp:85
JointState joint_velocities
Definition: controller.h:177
bool MoveJointsToSetpoints(Eigen::Matrix< double, 12, 1 > goal_joint_angles)
Definition: controller.cpp:668
virtual void setInitialConfiguration()
Definition: controller.cpp:120
ros::Publisher base_pose_command_logger
Definition: controller.h:222
Eigen::Vector3d fr_velocity_body
Definition: controller.h:153
A class for analytical Kinematics Solving.
Definition: kinematics.h:54
LegType
Leg type enumerator.
Definition: kinematics.h:56
#define LEG_OFFSET_LENGTH
Definition: controller.h:25
#define UNINITIALIZED_JOINT_STATE
Definition: controller.h:23
#define ROBOT_NAME
Definition: controller.h:24
Eigen::Vector3d Vector3d
Definition: kinematics.h:49