Quad-SDK
robot_driver.h
1#ifndef ROBOT_DRIVER_H
2#define ROBOT_DRIVER_H
3
4#include <eigen_conversions/eigen_msg.h>
5#include <quad_msgs/GRFArray.h>
6#include <quad_msgs/LegCommand.h>
7#include <quad_msgs/LegCommandArray.h>
8#include <quad_msgs/MotorCommand.h>
9#include <quad_msgs/MultiFootPlanContinuous.h>
10#include <quad_msgs/RobotPlan.h>
11#include <quad_msgs/RobotState.h>
12#include <quad_utils/function_timer.h>
13#include <quad_utils/math_utils.h>
14#include <quad_utils/ros_utils.h>
15#include <ros/ros.h>
16#include <std_msgs/Bool.h>
17#include <std_msgs/UInt8.h>
18
19#include <cmath>
20#include <eigen3/Eigen/Eigen>
21
22#include "robot_driver/controllers/grf_pid_controller.h"
23#include "robot_driver/controllers/inverse_dynamics_controller.h"
24#include "robot_driver/controllers/joint_controller.h"
25#include "robot_driver/controllers/leg_controller.h"
26#include "robot_driver/hardware_interfaces/hardware_interface.h"
27#include "robot_driver/hardware_interfaces/spirit_interface.h"
28#include "robot_driver/robot_driver_utils.h"
29#define MATH_PI 3.141592
30
33
40 public:
46 RobotDriver(ros::NodeHandle nh, int argc, char** argv);
47
51 void spin();
52
53 private:
58 void controlModeCallback(const std_msgs::UInt8::ConstPtr& msg);
59
64 void localPlanCallback(const quad_msgs::RobotPlan::ConstPtr& msg);
65
70 void robotStateCallback(const quad_msgs::RobotState::ConstPtr& msg);
71
76 void mocapCallback(const geometry_msgs::PoseStamped::ConstPtr& msg);
77
82 void trajectoryStateCallback(const quad_msgs::RobotState::ConstPtr& msg);
83
88 void singleJointCommandCallback(const geometry_msgs::Vector3::ConstPtr& msg);
89
93 void controlRestartFlagCallback(const std_msgs::Bool::ConstPtr& msg);
94
99 void remoteHeartbeatCallback(const std_msgs::Header::ConstPtr& msg);
100
105
109 bool updateState();
110
114 bool updateControl();
115
119 void publishState();
120
126 void publishControl(bool is_valid);
127
131 void publishHeartbeat();
132
134 ros::Subscriber control_mode_sub_;
135
137 ros::Subscriber body_plan_sub_;
138
140 ros::Subscriber local_plan_sub_;
141
143 ros::Subscriber mocap_sub_;
144
146 ros::Subscriber robot_state_sub_;
147
150
152 ros::Publisher robot_state_pub_;
153
154 // ROS publisher for state estimate
155 ros::Publisher trajectry_robot_state_pub_;
156
158 ros::Subscriber remote_heartbeat_sub_;
159
161 ros::Subscriber single_joint_cmd_sub_;
162
164 ros::Publisher robot_heartbeat_pub_;
165
168
170 ros::Publisher grf_pub_;
171
173 ros::Publisher imu_pub_;
174
176 ros::Publisher joint_state_pub_;
177
179 ros::NodeHandle nh_;
180
183
185 std::string controller_id_;
186
189
192
194 const int num_feet_ = 4;
195
198
200 std::vector<double> torque_limits_;
201
203 const int SIT = 0;
204
206 const int READY = 1;
207
209 const int SIT_TO_READY = 2;
210
212 const int READY_TO_SIT = 3;
213
215 const int SAFETY = 4;
216
218 const int NONE = 0;
219
221 const int LOCAL_PLAN = 1;
222
224 const int GRFS = 2;
225
227 quad_msgs::RobotPlan::ConstPtr last_local_plan_msg_;
228
230 quad_msgs::RobotState last_robot_state_msg_;
231
233 quad_msgs::GRFArray::ConstPtr last_grf_array_msg_;
234
236 std_msgs::Header::ConstPtr last_remote_heartbeat_msg_;
237
240
241 // State timeout threshold in seconds
242 double last_state_time_;
243
244 // Remote heartbeat timeout threshold in seconds
245 double remote_heartbeat_received_time_;
246
248 const double transition_duration_ = 1.0;
249
252
255
258
261
264
266 quad_msgs::LegCommandArray leg_command_array_msg_;
267
269 quad_msgs::GRFArray grf_array_msg_;
270
272 Eigen::VectorXd user_tx_data_;
273
275 Eigen::VectorXd user_rx_data_;
276
279
281 std::vector<double> safety_kp_;
282 std::vector<double> safety_kd_;
283
285 std::vector<double> sit_kp_;
286 std::vector<double> sit_kd_;
287
289 std::vector<double> stand_kp_;
290 std::vector<double> stand_kd_;
291
293 std::vector<double> stance_kp_;
294 std::vector<double> stance_kd_;
295
297 std::vector<double> swing_kp_;
298 std::vector<double> swing_kd_;
299
301 std::vector<double> swing_kp_cart_;
302 std::vector<double> swing_kd_cart_;
303
305 std::vector<double> stand_joint_angles_;
306
308 std::vector<double> sit_joint_angles_;
309
311 std::shared_ptr<quad_utils::QuadKD> quadKD_;
312
314 std::shared_ptr<LegController> leg_controller_;
315
317 std::shared_ptr<HardwareInterface> hardware_interface_;
318
320 geometry_msgs::PoseStamped::ConstPtr last_mocap_msg_;
321
323 sensor_msgs::Imu last_imu_msg_;
324
326 sensor_msgs::JointState last_joint_state_msg_;
327
329 Eigen::Vector3d vel_estimate_;
330
332 Eigen::Vector3d mocap_vel_estimate_;
333
335 Eigen::Vector3d imu_vel_estimate_;
336
339
342
346
349
352
355
357 ros::Time t_pub_;
358
360 int argc_;
361
363 char** argv_;
364
366 struct Filter {
367 // State-space model
368 Eigen::Matrix<double, 2, 2> A;
369 Eigen::Matrix<double, 2, 1> B;
370 Eigen::Matrix<double, 1, 2> C;
371 Eigen::Matrix<double, 1, 1> D;
372
373 // Filter states
374 std::vector<Eigen::Matrix<double, 2, 1>> x;
375
376 // Filter initialization indicator
377 bool init;
378 };
379
382 Filter low_pass_filter;
383 Filter high_pass_filter;
384 };
385
388};
389
390#endif // ROBOT_DRIVER_H
Definition: robot_driver.h:39
std::vector< double > stand_kp_
PD gain when in standing mode.
Definition: robot_driver.h:289
ros::Time transition_timestamp_
Time at which to start transition.
Definition: robot_driver.h:278
double heartbeat_timeout_
Timeout (in s) for receiving new heartbeat messages.
Definition: robot_driver.h:257
bool updateState()
Update the most recent state message with the given data.
Definition: robot_driver.cpp:340
ros::Time last_mocap_time_
Last mocap time.
Definition: robot_driver.h:354
void checkMessagesForSafety()
Check to make sure required messages are fresh.
Definition: robot_driver.cpp:314
const int READY
Define ids for control modes: Stand.
Definition: robot_driver.h:206
void publishHeartbeat()
Function to publish heartbeat message.
Definition: robot_driver.cpp:644
ros::Publisher joint_state_pub_
ROS publisher for joint data.
Definition: robot_driver.h:176
ros::Subscriber single_joint_cmd_sub_
ROS subscriber for single joint command.
Definition: robot_driver.h:161
const int READY_TO_SIT
Define ids for control modes: Stand to sit.
Definition: robot_driver.h:212
std_msgs::Header last_robot_heartbeat_msg_
Most recent robot heartbeat.
Definition: robot_driver.h:239
std::vector< double > sit_joint_angles_
Define sitting joint angles.
Definition: robot_driver.h:308
Eigen::Vector3d vel_estimate_
Best estimate of velocity.
Definition: robot_driver.h:329
void remoteHeartbeatCallback(const std_msgs::Header::ConstPtr &msg)
Callback to handle new remote heartbeat messages.
Definition: robot_driver.cpp:303
void controlModeCallback(const std_msgs::UInt8::ConstPtr &msg)
Verifies and updates new control mode.
Definition: robot_driver.cpp:198
void localPlanCallback(const quad_msgs::RobotPlan::ConstPtr &msg)
Callback function to handle new local plan (states and GRFs)
Definition: robot_driver.cpp:229
quad_msgs::RobotState last_robot_state_msg_
Most recent state estimate.
Definition: robot_driver.h:230
double mocap_rate_
Update rate of the motion capture system.
Definition: robot_driver.h:348
double update_rate_
Update rate for computing new controls;.
Definition: robot_driver.h:188
std::vector< double > swing_kp_
PD gain when foot is in swing.
Definition: robot_driver.h:297
std::vector< double > stand_joint_angles_
Define standing joint angles.
Definition: robot_driver.h:305
bool updateControl()
Function to compute leg command array message.
Definition: robot_driver.cpp:442
const int GRFS
Define ids for input types: grf array.
Definition: robot_driver.h:224
const int LOCAL_PLAN
Define ids for input types: local plan.
Definition: robot_driver.h:221
ros::Subscriber remote_heartbeat_sub_
ROS subscriber for remote heartbeat.
Definition: robot_driver.h:158
void robotStateCallback(const quad_msgs::RobotState::ConstPtr &msg)
Callback function to handle current robot state.
Definition: robot_driver.cpp:298
std::vector< double > sit_kp_
PD gain when in sit mode.
Definition: robot_driver.h:285
double state_timeout_
Timeout (in s) for receiving new state messages.
Definition: robot_driver.h:254
ComplementaryFilter complementary_filter_
Complementray filter.
Definition: robot_driver.h:387
double remote_latency_threshold_error_
Latency threshold on robot messages for error (s)
Definition: robot_driver.h:263
quad_msgs::LegCommandArray leg_command_array_msg_
Message for leg command array.
Definition: robot_driver.h:266
const double transition_duration_
Duration for sit to stand behavior.
Definition: robot_driver.h:248
const int NONE
Define ids for input types: none.
Definition: robot_driver.h:218
Eigen::Vector3d mocap_vel_estimate_
Best estimate of velocity from mocap diff.
Definition: robot_driver.h:332
std::vector< double > safety_kp_
PD gain when in safety mode.
Definition: robot_driver.h:281
void publishState()
Publish the most recent state message with the given data.
Definition: robot_driver.cpp:434
Eigen::VectorXd user_tx_data_
User transmission data.
Definition: robot_driver.h:272
ros::Publisher imu_pub_
ROS publisher for imu data.
Definition: robot_driver.h:173
sensor_msgs::JointState last_joint_state_msg_
Most recent joint data.
Definition: robot_driver.h:326
std::string controller_id_
Controller type.
Definition: robot_driver.h:185
double mocap_dropout_threshold_
Definition: robot_driver.h:345
const int num_feet_
Number of feet.
Definition: robot_driver.h:194
const int SAFETY
Define ids for control modes: Safety.
Definition: robot_driver.h:215
std_msgs::Header::ConstPtr last_remote_heartbeat_msg_
Most recent remote heartbeat.
Definition: robot_driver.h:236
ros::Subscriber control_mode_sub_
Subscriber for control mode.
Definition: robot_driver.h:134
std::vector< double > stance_kp_
PD gain when foot is in stance.
Definition: robot_driver.h:293
double remote_latency_threshold_warn_
Latency threshold on robot messages for warnings (s)
Definition: robot_driver.h:260
void controlRestartFlagCallback(const std_msgs::Bool::ConstPtr &msg)
Callback to handle control restart flag messages.
Definition: robot_driver.cpp:224
ros::Subscriber robot_state_sub_
ROS subscriber for state estimate.
Definition: robot_driver.h:146
void spin()
Calls ros spinOnce and pubs data at set frequency.
Definition: robot_driver.cpp:653
std::shared_ptr< quad_utils::QuadKD > quadKD_
QuadKD class.
Definition: robot_driver.h:311
void publishControl(bool is_valid)
Function to publish leg command array message.
Definition: robot_driver.cpp:624
sensor_msgs::Imu last_imu_msg_
Most recent IMU data.
Definition: robot_driver.h:323
std::shared_ptr< HardwareInterface > hardware_interface_
Mblink converter object.
Definition: robot_driver.h:317
std::vector< double > torque_limits_
Torque limits.
Definition: robot_driver.h:200
const int SIT_TO_READY
Define ids for control modes: Sit to stand.
Definition: robot_driver.h:209
std::shared_ptr< LegController > leg_controller_
Leg Controller template class.
Definition: robot_driver.h:314
bool is_hardware_
Boolean for whether robot layer is hardware (else sim)
Definition: robot_driver.h:182
double input_timeout_
Timeout (in s) for receiving new input reference messages.
Definition: robot_driver.h:251
std::vector< double > swing_kp_cart_
PD gain when foot is in swing (Cartesian)
Definition: robot_driver.h:301
void trajectoryStateCallback(const quad_msgs::RobotState::ConstPtr &msg)
Callback function to handle reference trajectory state.
ros::Time t_pub_
Time of last publishing.
Definition: robot_driver.h:357
ros::Subscriber local_plan_sub_
ROS subscriber for local plan.
Definition: robot_driver.h:140
ros::Publisher robot_heartbeat_pub_
ROS publisher for robot heartbeat.
Definition: robot_driver.h:164
void singleJointCommandCallback(const geometry_msgs::Vector3::ConstPtr &msg)
Callback to handle new leg override commands.
Definition: robot_driver.cpp:216
Eigen::VectorXd user_rx_data_
User recieved data.
Definition: robot_driver.h:275
ros::Subscriber control_restart_flag_sub_
ROS subscriber for control restart flag.
Definition: robot_driver.h:149
double publish_rate_
Update rate for publishing data to ROS;.
Definition: robot_driver.h:191
quad_msgs::GRFArray grf_array_msg_
Message for leg command array.
Definition: robot_driver.h:269
ros::Publisher grf_pub_
ROS publisher for desired GRF.
Definition: robot_driver.h:170
Eigen::Vector3d imu_vel_estimate_
Best estimate of imu velocity.
Definition: robot_driver.h:335
const int SIT
Define ids for control modes: Sit.
Definition: robot_driver.h:203
double filter_weight_
Velocity filter weight.
Definition: robot_driver.h:341
ros::NodeHandle nh_
Nodehandle to pub to and sub from.
Definition: robot_driver.h:179
void mocapCallback(const geometry_msgs::PoseStamped::ConstPtr &msg)
Callback function to handle current robot pose.
Definition: robot_driver.cpp:239
int control_mode_
Robot mode.
Definition: robot_driver.h:197
ros::Publisher robot_state_pub_
ROS publisher for ground truth state.
Definition: robot_driver.h:152
double filter_time_constant_
Velocity filter time constant.
Definition: robot_driver.h:338
geometry_msgs::PoseStamped::ConstPtr last_mocap_msg_
Last mocap data.
Definition: robot_driver.h:320
RobotDriver(ros::NodeHandle nh, int argc, char **argv)
Constructor for RobotDriver.
Definition: robot_driver.cpp:3
char ** argv_
Required for some hardware interfaces.
Definition: robot_driver.h:363
ros::Subscriber body_plan_sub_
ROS subscriber for body plan.
Definition: robot_driver.h:137
int argc_
Required for some hardware interfaces.
Definition: robot_driver.h:360
ros::Subscriber mocap_sub_
ROS subscriber for local plan.
Definition: robot_driver.h:143
quad_msgs::RobotPlan::ConstPtr last_local_plan_msg_
Most recent local plan.
Definition: robot_driver.h:227
double last_mainboard_time_
Last mainboard time.
Definition: robot_driver.h:351
quad_msgs::GRFArray::ConstPtr last_grf_array_msg_
Most recent local plan.
Definition: robot_driver.h:233
ros::Publisher leg_command_array_pub_
ROS publisher for inverse dynamics.
Definition: robot_driver.h:167
Struct of complementray filter with low and high pass filters.
Definition: robot_driver.h:381
Struct of second-order low/high pass filter with derivative/intergral.
Definition: robot_driver.h:366