Corentin Chauvin-Hameau – 2019-2020
Coverage Path Planning for an underwater robot surveying a marine farm
mpc_node.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  *
4  * \brief Definition of a node for Model Predictive Control of an underwater
5  * robot
6  * \author Corentin Chauvin-Hameau
7  * \date 2020
8  */
9 
10 #include "mpc_node.hpp"
11 #include "mf_common/common.hpp"
13 #include "mf_common/Float32Array.h"
14 #include "mf_robot_simulator/Command.h"
15 #include <nav_msgs/Path.h>
16 #include <geometry_msgs/TransformStamped.h>
17 #include <geometry_msgs/PoseArray.h>
18 #include <ros/ros.h>
19 #include <eigen3/Eigen/Dense>
20 #include <iostream>
21 #include <string>
22 
23 using namespace std;
24 using Eigen::MatrixXd;
25 using Eigen::DiagonalMatrix;
26 
27 
28 namespace mfcpp {
29 
30 
31 MPCNode::MPCNode():
32  nh_("~"),
33  tf_listener_(tf_buffer_)
34 {
35 
36 }
37 
38 
40 
41 
43 {
44  // ROS parameters
45  vector<double> model_csts; // model constants
46  vector<double> P, Q_x, R_u, R_delta; // MPC tuning parameters
47 
48  nh_.param<float>("main_freq", main_freq_, 10.0);
49  nh_.param<string>("fixed_frame", fixed_frame_, "ocean");
50  nh_.param<float>("desired_speed", desired_speed_, 1.0);
51  nh_.param<float>("time_horizon", time_horizon_, 1.0);
52  nh_.param<int>("nbr_steps", nbr_steps_, 10);
53  nh_.param<bool>("disable_vbs", disable_vbs_, false);
54  nh_.param<bool>("ltv_mpc", ltv_mpc_, true);
55  nh_.param<vector<double>>("P", P, vector<double>(13, 1.0));
56  nh_.param<vector<double>>("Q_x", Q_x, vector<double>(13, 1.0));
57  nh_.param<vector<double>>("R_u", R_u, vector<double>(4, 1.0));
58  nh_.param<vector<double>>("R_delta", R_delta, vector<double>(4, 1.0));
59 
60  nh_.param<vector<double>>("model_constants", model_csts, vector<double>(11, 0.0));
61  nh_.param<double>("bnd_delta_m", bounds_.delta_m, 1.0);
62  nh_.param<vector<double>>("bnd_input", bounds_.input, vector<double>(4, 0.0));
63 
64 
65  // Other variables
66  robot_model_ = RobotModel(model_csts);
67  path_received_ = false;
68  state_received_ = false;
69  qp_warm_start_ = false;
70 
72  last_control_ = vector<float>(4, 0);
73 
74  if (disable_vbs_)
75  bounds_.delta_m = 0;
76 
81 
82  // ROS subscribers
83  path_sub_ = nh_.subscribe<nav_msgs::Path>("path", 1, &MPCNode::path_cb, this);
84  state_sub_ = nh_.subscribe<mf_common::Float32Array>("state", 1, &MPCNode::state_cb, this);
85 
86  // ROS publishers
87  command_pub_ = nh_.advertise<mf_robot_simulator::Command>("command", 0);
88  expected_traj_pub_ = nh_.advertise<geometry_msgs::PoseArray>("expected_traj", 0);
89 }
90 
91 
93 {
94  init_node();
95 
96  ros::Rate loop_rate(main_freq_);
97 
98  while (ros::ok()) {
99  ros::spinOnce();
100 
102  vector<float> control;
103  bool control_computed;
104  float desired_speed = desired_speed_;
105  geometry_msgs::PoseArray expected_traj;
106 
107  control_computed = compute_control(desired_speed, last_desired_speed_,
108  control, expected_traj
109  );
110 
111  if (control_computed) {
112  last_desired_speed_ = desired_speed;
113 
114  if (disable_vbs_)
115  control[3] = 0;
116 
117  mf_robot_simulator::Command msg;
118  msg.n = control[0];
119  msg.delta_r = control[1];
120  msg.delta_e = control[2];
121  msg.P = control[3];
122 
123  command_pub_.publish(msg);
124 
125  last_control_ = control;
126 
127 
128  expected_traj.header.frame_id = fixed_frame_;
129  expected_traj_pub_.publish(expected_traj);
130  }
131  }
132 
133  loop_rate.sleep();
134  }
135 }
136 
137 
138 void MPCNode::path_cb(const nav_msgs::Path msg)
139 {
140  path_ = msg;
141  path_received_ = true;
142 }
143 
144 
145 void MPCNode::state_cb(const mf_common::Float32Array msg)
146 {
147  state_ = msg.data;
148  state_received_ = true;
149 }
150 
151 
152 } // namespace mfcpp
153 
154 
155 /**
156  * \brief Main function called before node initialisation
157  */
158 int main(int argc, char **argv)
159 {
160  ros::init(argc, argv, "mpc");
161  mfcpp::MPCNode mpc_node;
162  mpc_node.run_node();
163 
164  return 0;
165 }
Declaration of a node for Model Predictive Control of an underwater robot.
Definition: common.hpp:23
Declaration of physical underwater robot model.
bool disable_vbs_
Whether to disable Variable Buoyancy System (VBS)
Definition: mpc_node.hpp:95
ros::Publisher expected_traj_pub_
Publisher for the expected controlled trajectory.
Definition: mpc_node.hpp:69
float time_horizon_
Time horizon (s) for the MPC prediction.
Definition: mpc_node.hpp:93
MPCBounds bounds_
Bounds for the MPC.
Definition: mpc_node.hpp:80
Node for Model Predictive Control of a robot.
Definition: mpc_node.hpp:33
Eigen::MatrixXf Q_x
Penalty on the intermediary states errors.
Definition: mpc_node.hpp:50
ros::Subscriber path_sub_
Subscriber for the desired path.
Definition: mpc_node.hpp:66
nav_msgs::Path path_
Path to follow.
Definition: mpc_node.hpp:73
std::vector< float > last_control_
Last control applied to the robot.
Definition: mpc_node.hpp:78
Eigen::MatrixXf R_delta
Penalty on the control change rate.
Definition: mpc_node.hpp:52
Eigen::MatrixXf R_u
Penalty on the control input error.
Definition: mpc_node.hpp:51
float main_freq_
Frequency of the main loop.
Definition: mpc_node.hpp:88
bool compute_control(float &desired_speed, float last_desired_speed, std::vector< float > &command, geometry_msgs::PoseArray &expected_traj)
Computes the control signal to send to the robot.
Definition: mpc.cpp:531
ros::NodeHandle nh_
Node handler (for topics and services)
Definition: mpc_node.hpp:65
Declaration of common functions.
std::vector< float > state_
Current robot state.
Definition: mpc_node.hpp:77
Eigen::MatrixXf P
Penalty on the last state error.
Definition: mpc_node.hpp:49
bool qp_warm_start_
Whether the QP can start warm.
Definition: mpc_node.hpp:84
std::vector< double > input
Definition: mpc_node.hpp:61
void state_cb(const mf_common::Float32Array msg)
Callback for the current robot state.
Definition: mpc_node.cpp:145
RobotModel robot_model_
Robot model.
Definition: mpc_node.hpp:76
void path_cb(const nav_msgs::Path msg)
Callback for the desired path.
Definition: mpc_node.cpp:138
void fill_diag_mat(const std::vector< T > &v, Eigen::DiagonalMatrix< T, Eigen::Dynamic > &D)
Fills a Eigen DiagonalMatrix from a std::Vector.
Definition: common.hpp:322
Class defining the robot model.
Definition: robot_model.hpp:47
int main(int argc, char **argv)
Main function called before node initialisation.
Definition: mpc_node.cpp:158
float desired_speed_
Desired speed (m/s) of the robot.
Definition: mpc_node.hpp:91
std::string fixed_frame_
Fixed frame.
Definition: mpc_node.hpp:89
int nbr_steps_
Number of steps for the MPC prediction.
Definition: mpc_node.hpp:94
ros::Publisher command_pub_
Publisher for the computed command.
Definition: mpc_node.hpp:68
MPCTuningParameters tuning_params_
MPC tuning parameters.
Definition: mpc_node.hpp:79
void init_node()
Initialises the node and its parameters.
Definition: mpc_node.cpp:42
ros::Subscriber state_sub_
Subscriber for the current robot state.
Definition: mpc_node.hpp:67
void run_node()
Runs the node.
Definition: mpc_node.cpp:92
bool state_received_
Whether the robot state has ever been received.
Definition: mpc_node.hpp:75
bool path_received_
Whether a new path has been received.
Definition: mpc_node.hpp:74
float last_desired_speed_
Last desired speed (m/s) of the robot.
Definition: mpc_node.hpp:92