Tetrapod Project
footstep_planner_plugin.cpp
Go to the documentation of this file.
2 #include<iostream>
3 namespace gazebo{
4 
5 // Constructor
6 FootstepPlannerPlugin::FootstepPlannerPlugin() {std::cout << "Footstep_planner_plugin is constructed" << std::endl;}
7 
8 // Destructor
10 {
11  this->rosNode->shutdown();
12 
13  this->rosProcessQueue.clear();
14  this->rosProcessQueue.disable();
15  this->rosProcessQueueThread.join();
16 }
17 
18 // Initialize ROS Publish and Process Queue Threads
20 {
21  this->rosProcessQueueThread = std::thread(
23  );
24 }
25 
26 // Setup thread to process messages
28 {
29  static const double timeout = 0.000001;
30  while (this->rosNode->ok())
31  {
32  this->rosProcessQueue.callAvailable(ros::WallDuration(timeout));
33  }
34 }
35 
36 
37 void FootstepPlannerPlugin::Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
38 {
39  this->world = _world;
40 
41  InitRos();
42 
44 
45  InitGazebo();
46 }
47 
49 {
50  if (!ros::isInitialized())
51  {
52  int argc = 0;
53  char **argv = NULL;
54  ros::init(
55  argc,
56  argv,
57  "gazebo_footstep_planner",
58  ros::init_options::NoSigintHandler
59  );
60  }
61  this->rosNode.reset(new ros::NodeHandle("gazebo_footstep_planner"));
62 
63  this->footstepPlannerSub = this->rosNode->subscribe("/footstep_planner/footstep_plan", 1000, &FootstepPlannerPlugin::UpdateFootstepPlan, this);
64 }
65 
67 {
68  //gazebo::client::setup();
69  this->gazeboNode.reset(new gazebo::transport::Node());
70 
71  this->gazeboNode->Init();
72 
73  this->indicatorPub = this->gazeboNode->Advertise("~/visual", "gazebo.msgs.Visual");
74 }
75 
76 void FootstepPlannerPlugin::SetVisuals(Eigen::MatrixX3d &poses)
77 {
78  indicators = Eigen::Matrix<msgs::Visual, Eigen::Dynamic, 1>();
79  ROS_INFO("created new indicator matrix.");
80  indicators.conservativeResize(poses.rows(), Eigen::NoChange);
81  ROS_INFO("resized new indicator matrix");
82 
83  int n_steps = indicators.rows();
84 
85  std::string parent_name = "default";
86 
87  std::string name;
88 
89  msgs::Visual *indicator;
90 
91  msgs::Geometry *geometry;
92 
93  Eigen::Vector3d pose;
94 
95  Eigen::Vector3d yellow(1, 1, 0);
96 
97  Eigen::Vector3d green(0, 1, 0);
98 
99  Eigen::Matrix3Xd colors = interpolateColors(yellow, green, n_steps);
100 
101  ignition::math::Color c;
102 
103  ignition::math::Color spec(0.1, 0.1, 0.1);
104 
105  for (int i = 0; i < n_steps; ++i)
106  {
107  ROS_INFO("start of loop");
108  c.Set(colors(0, i), colors(1, i), colors(2, i));
109 
110  pose = poses.row(i).transpose();
111 
112  indicator = &(indicators(i));
113 
114  name = "footstep " + std::to_string(i);
115  ROS_INFO("about to set name");
116  indicator->set_name(name);
117  ROS_INFO("set name completed");
118  indicator->set_visible(true);
119 
120  indicator->set_parent_name(parent_name);
121 
122  indicator->set_cast_shadows(false);
123 
124  geometry = indicator->mutable_geometry();
125 
126  geometry->set_type(msgs::Geometry::CYLINDER);
127 
128  geometry->mutable_cylinder()->set_radius(0.02);
129 
130  geometry->mutable_cylinder()->set_length(0.01);
131 
132  msgs::Set(indicator->mutable_pose(), ignition::math::Pose3d(pose(0), pose(1), pose(2), 0, 0, 0));
133  std::cout << pose(0) << " " << pose(1) << " " << pose(2) << std::endl;
134  msgs::Set(indicator->mutable_material()->mutable_ambient(), c);
135 
136  msgs::Set(indicator->mutable_material()->mutable_diffuse(), c);
137 
138  msgs::Set(indicator->mutable_material()->mutable_ambient(), spec);
139 
140  msgs::Set(indicator->mutable_material()->mutable_emissive(), c);
141 
142  indicator->set_is_static(true);
143  ROS_INFO("end of loop");
144  }
145 }
146 
148 {
149  int n_steps = indicators.rows();
150 
151  msgs::Visual *indicator;
152 
153  for (int i = 0; i < n_steps; ++i)
154  {
155  indicator = &(indicators(i));
156 
157  indicatorPub->Publish(*indicator);
158  }
159 }
160 
162 {
163  int n_steps = indicators.rows();
164 
165  msgs::Visual *indicator;
166 
167  for(int i = 0; i < n_steps; ++i)
168  {
169  indicator = &(indicators(i));
170 
171  indicator->set_delete_me(true);
172 
173  indicatorPub->Publish(*indicator);
174  }
175 }
176 
177 void FootstepPlannerPlugin::UpdateFootstepPlan(const geometry_msgs::PoseArray::ConstPtr& msg)
178 {
179  ROS_INFO("Footstep_planner_plugin: Received updated footstep plan");
180  Eigen::MatrixX3d poses = eigen_matr_from_pose_array(*msg);
181 
182  //DeleteVisuals();
183 
184  SetVisuals(poses);
185 
186  PublishVisuals();
187 }
188 }
physics::WorldPtr world
Pointer to the world.
transport::PublisherPtr indicatorPub
void UpdateFootstepPlan(const geometry_msgs::PoseArray::ConstPtr &msg)
std::unique_ptr< ros::NodeHandle > rosNode
Node used for ROS transport.
std::thread rosProcessQueueThread
Thread running the rosProcessQueue.
void SetVisuals(Eigen::MatrixX3d &poses)
Eigen::Matrix< msgs::Visual, Eigen::Dynamic, 1 > indicators
ros::CallbackQueue rosProcessQueue
ROS callbackque that helps process messages.
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
The Load function is called by Gazebo when the plugin is inserted into simulation.
Eigen::Matrix3Xd interpolateColors(Eigen::Vector3d begin_rgb, Eigen::Vector3d end_rgb, int nsteps)
Definition: color_interp.cpp:3
Eigen::MatrixX3d eigen_matr_from_pose_array(geometry_msgs::PoseArray pose_array)
Eigen::Vector3d Vector3d
Definition: kinematics.h:49