11 #include "mf_robot_simulator/Command.h" 12 #include "mf_robot_simulator/CartesianCommand.h" 13 #include "mf_common/Float32Array.h" 14 #include <visualization_msgs/Marker.h> 15 #include <nav_msgs/Odometry.h> 16 #include <geometry_msgs/TransformStamped.h> 17 #include <geometry_msgs/Pose.h> 18 #include <tf2/LinearMath/Matrix3x3.h> 19 #include <tf2_geometry_msgs/tf2_geometry_msgs.h> 20 #include <tf2_ros/transform_broadcaster.h> 30 RobotSimulator::RobotSimulator():
46 vector<double> model_csts;
54 nh_.param<vector<double>>(
"model_constants", model_csts, vector<double>(15, 0.0));
55 nh_.param<vector<double>>(
"init_state",
state_, vector<double>(13, 0.0));
57 nh_.param<vector<double>>(
"bnd_input",
bnd_input_, vector<double>(4, 0.0));
60 odom_pub_ =
nh_.advertise<nav_msgs::Odometry>(
"odom", 1);
61 state_pub_ =
nh_.advertise<mf_common::Float32Array>(
"state", 1);
62 rviz_pub_ =
nh_.advertise<visualization_msgs::Marker>(
"rviz_markers", 1);
101 cout <<
"Robot state: " << endl
106 <<
"delta_m=" <<
state_[12] << endl;
117 for (
int i = 0; i <
input_.size(); i++) {
134 state_[0] = msg->position.x;
135 state_[1] = msg->position.y;
136 state_[2] = msg->position.z;
138 tf2::Quaternion quat(msg->orientation.x, msg->orientation.y, msg->orientation.z,
165 mf_common::Float32Array state_msg;
166 vector<float> float_state(
state_.begin(),
state_.end());
167 state_msg.data = float_state;
171 nav_msgs::Odometry odom;
172 odom.header.stamp = ros::Time::now();
175 odom.pose.pose.position.x =
state_[0];
176 odom.pose.pose.position.y =
state_[1];
177 odom.pose.pose.position.z =
state_[2];
179 tf2::Quaternion quat;
181 tf2::convert(quat, odom.pose.pose.orientation);
183 odom.twist.twist.linear.x =
state_[6];
184 odom.twist.twist.linear.y =
state_[7];
185 odom.twist.twist.linear.z =
state_[8];
186 odom.twist.twist.angular.x =
state_[9];
187 odom.twist.twist.angular.y =
state_[10];
188 odom.twist.twist.angular.z =
state_[11];
193 geometry_msgs::TransformStamped transform;
194 transform.header.stamp = ros::Time::now();
197 transform.transform.translation.x = odom.pose.pose.position.x;
198 transform.transform.translation.y = odom.pose.pose.position.y;
199 transform.transform.translation.z = odom.pose.pose.position.z;
200 transform.transform.rotation.x = odom.pose.pose.orientation.x;
201 transform.transform.rotation.y = odom.pose.pose.orientation.y;
202 transform.transform.rotation.z = odom.pose.pose.orientation.z;
203 transform.transform.rotation.w = odom.pose.pose.orientation.w;
204 tf_br_.sendTransform(transform);
207 visualization_msgs::Marker marker;
209 marker.header.stamp = ros::Time::now();
212 marker.color.r = 0.4;
213 marker.color.g = 0.4;
214 marker.color.b = 0.4;
215 marker.color.a = 1.0;
217 marker.type = visualization_msgs::Marker::CYLINDER;
218 marker.action = visualization_msgs::Marker::ADD;
220 marker.pose.orientation.x = 0;
221 marker.pose.orientation.y = 0.7071068;
222 marker.pose.orientation.z = 0;
223 marker.pose.orientation.w = 0.7071068;
240 int main(
int argc,
char **argv)
242 ros::init(argc, argv,
"robot_simulator");
float update_freq_
State update frequency.
Declaration of physical underwater robot model.
ros::Subscriber pose_input_sub_
ROS subscriber for setting the pose.
void init_node()
Initialise the node and its parameters.
ros::Publisher rviz_pub_
ROS publisher for rviz markers.
void control_input_cb(const mf_robot_simulator::Command::ConstPtr &msg)
Callback for the control input.
void pose_input_cb(const geometry_msgs::Pose::ConstPtr &msg)
Callback for setting the pose.
ros::Subscriber cart_input_sub_
ROS subscriber for the debug cartesian control input.
void run_node()
Run the simulator.
tf2_ros::TransformBroadcaster tf_br_
Tf broadcaster.
std::string fixed_frame_
Frame in which the pose is expressed.
int nbr_int_steps_
Initial number of integration steps during the update.
ros::Publisher state_pub_
ROS publisher for the current state.
void integrate(state_type &state, const input_type &input, double t1, double t2, double init_step)
Integrates the model over a period of time.
RobotModel::input_type bnd_input_
void cart_input_cb(const mf_robot_simulator::CartesianCommand::ConstPtr &msg)
Callback for the debug cartesian control input.
Class defining the robot model.
RobotModel::state_type state_
Current state of the robot.
void disp_state()
Display the current state.
void update_state(float dt)
Updates the robot state by integrating the ODE.
int main(int argc, char **argv)
Main function called before node initialisation.
float bnd_delta_m_
Bound on delta_m (ballast control)
Declaration of underwater robot simulator.
ros::Publisher odom_pub_
ROS publisher for the odometry output.
float robot_radius_
Radius of the robot (assuming cylindrical shape)
std::vector< float > cart_input_
Current debug cartesian control input.
void publish_state()
Publishes the robot state.
RobotModel robot_model_
Robot model.
ros::Subscriber control_input_sub_
ROS subscriber for the control input.
ros::NodeHandle nh_
ROS node handler.
RobotModel::input_type input_
Current control input.
float robot_length_
Length of the robot (assuming cylindrical shape)
std::string robot_frame_
Frame of the robot.