show_axes.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
27 #ifndef _atlascar_transforms_CPP_
28 #define _atlascar_transforms_CPP_
29 
38 #include <boost/algorithm/string.hpp>
39 #include <string>
40 #include <ros/ros.h>
41 #include <sensor_msgs/JointState.h>
42 #include <tf/transform_broadcaster.h>
43 #include <visualization_msgs/Marker.h>
44 #include <visualization_msgs/MarkerArray.h>
45 #include <tf/transform_listener.h>
46 #include <pcl_ros/transforms.h>
47 #include <pcl/conversions.h>
48 #include <pcl/point_cloud.h>
49 #include <pcl/point_types.h>
50 #include <interactive_markers/interactive_marker_server.h>
51 #include <interactive_markers/menu_handler.h>
52 #include <ros/package.h>
53 #include <libxml/encoding.h>
54 #include <libxml/xmlwriter.h>
55 #include <libxml/tree.h>
56 #include <libxml/parser.h>
57 #include <libxml/xpath.h>
58 #include <libxml/xpathInternals.h>
59 #include <libxml/xmlreader.h>
60 
61 
62 #define PFLN {printf("FILE %s LINE %d\n",__FILE__, __LINE__);}
63 
64 using namespace ros;
65 using namespace std;
66 using namespace boost;
67 using namespace visualization_msgs;
68 
69 tf::TransformListener *p_listener;
70 boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server;
71 float marker_pos = 0;
72 
73 typedef struct
74 {
75  double x,y,z;
76  std::string name;
78 
79 std::vector<boost::shared_ptr <t_reference_frame> > v_rf;
80 
81 int get_i_from_name(std::vector<boost::shared_ptr <t_reference_frame> >* v_rf, std::string name)
82 {
83  int ret=-1;
84  for (size_t i=0; i<v_rf->size(); ++i)
85  {
86  if (name == (*v_rf)[i]->name)
87  {
88  ret=i;
89  break;
90  }
91  }
92 
93  return ret;
94 }
95 
96 Marker makeBox( InteractiveMarker &msg )
97 {
98  Marker marker;
99 
100  marker.type = Marker::SPHERE;
101  marker.scale.x = msg.scale * 0.05;
102  marker.scale.y = msg.scale * 0.05;
103  marker.scale.z = msg.scale * 0.05;
104  marker.color.r = 0;
105  marker.color.g = 0.7;
106  marker.color.b = 0;
107  marker.color.a = 0.0;
108 
109  return marker;
110 }
111 void processFeedback( const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback )
112 {
113  switch ( feedback->event_type )
114  {
115  case visualization_msgs::InteractiveMarkerFeedback::POSE_UPDATE:
116  printf("you are moving marker %s\n",feedback->marker_name.c_str());
117 
118 
119  int ret = get_i_from_name(&v_rf, feedback->marker_name);
120 
121  printf("ret=%d\n",ret);
122 
123  v_rf[ret]->x = feedback->pose.position.x;
124  v_rf[ret]->y = feedback->pose.position.y;
125  v_rf[ret]->z = feedback->pose.position.z;
126  break;
127  }
128 
129  server->applyChanges();
130 }
131 
132 InteractiveMarkerControl& makeBoxControl( InteractiveMarker &msg )
133 {
134  InteractiveMarkerControl control;
135  control.always_visible = true;
136  control.markers.push_back( makeBox(msg) );
137  msg.controls.push_back( control );
138  return msg.controls.back();
139 }
140 
141 void set_label_pos(double x, double y, double z, visualization_msgs::Marker* msg, std::vector<visualization_msgs::Marker>* marker_vec)
142 {
143  //tf::StampedTransform transform;
144  //try
145  //{
146  //p_listener->lookupTransform("/atc/vehicle/ground", msg->header.frame_id, ros::Time(0), transform);
147  //}
148  //catch (tf::TransformException ex)
149  //{
150  //ROS_ERROR("%s",ex.what());
151  //}
152 
153 
154  //pcl::PointCloud<pcl::PointXYZ> global;
155  //pcl::PointXYZ p;
156  //p.x = x; p.y=y; p.z=z;
157  //global.push_back(p);
158 
159  //pcl::PointCloud<pcl::PointXYZ> local;
160  //pcl_ros::transformPointCloud(global,local,transform.inverse());
161 
162  geometry_msgs::Point pt;
163  visualization_msgs::Marker lmsg; //declare the msg
164  lmsg.pose.orientation.w = 1.0;
165  lmsg.header.stamp = ros::Time::now();
166  lmsg.id = 0;
167  lmsg.scale.x = 0.02; lmsg.scale.y = 1; lmsg.scale.z = 0.095;
168  lmsg.color.r = 0.0; lmsg.color.g = 0; lmsg.color.b = 0.0; lmsg.color.a = 0.4;
169  lmsg.pose.position.x = 0; lmsg.pose.position.y = 0; lmsg.pose.position.z = 0;
170  lmsg.action = visualization_msgs::Marker::ADD;
171  lmsg.type = visualization_msgs::Marker::LINE_STRIP;
172 
173  lmsg.header.frame_id = msg->header.frame_id;
174  lmsg.ns = "CNT " + msg->header.frame_id;
175 
176  pt.x=0; pt.y=0; pt.z=0;
177  lmsg.points.push_back(pt);
178  //pt.x=local.points[0].x; pt.y=local.points[0].y; pt.z=local.points[0].z;
179  pt.x=x; pt.y=y; pt.z=z;
180  lmsg.points.push_back(pt);
181  marker_vec->push_back(lmsg);
182 
183 
184  //msg->pose.position.x = local.points[0].x; msg->pose.position.y = local.points[0].y; msg->pose.position.z = local.points[0].z;
185  msg->pose.position.x = x; msg->pose.position.y = y; msg->pose.position.z = z;
186 }
187 
188 
189 
190 
191 int add_to_viz_markers_vec(std::vector<visualization_msgs::Marker>* marker_vec)
192 {
193  geometry_msgs::Point p;
194  std_msgs::ColorRGBA color;
195 
196  //Draw the projections names
197  visualization_msgs::Marker msg; //declare the msg
198  msg.pose.orientation.w = 1.0;
199  msg.header.stamp = ros::Time::now();
200  msg.id = 0;
201  msg.scale.x = 1; msg.scale.y = 1; msg.scale.z = 0.1;
202  msg.color.r = 0.0; msg.color.g = 0; msg.color.b = 0.3; msg.color.a = 1;
203  msg.pose.position.x = 0; msg.pose.position.y = 0; msg.pose.position.z = -0.1;
204  msg.action = visualization_msgs::Marker::ADD;
205  msg.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
206 
207  for (size_t i=0; i< v_rf.size(); ++i)
208  {
209  msg.header.frame_id = v_rf[i]->name;
210  msg.ns = msg.header.frame_id.c_str();
211  msg.text = msg.header.frame_id.c_str();
212  set_label_pos(v_rf[i]->x,v_rf[i]->y,v_rf[i]->z, &msg, marker_vec);
213  marker_vec->push_back(msg);
214  }
215 
216  return 1;
217 }
218 
219 void make6DofMarker( bool fixed, std::string name , double x, double y, double z)
220 {
221  InteractiveMarker int_marker;
222  int_marker.header.frame_id = name;
223  int_marker.pose.position.x = x;
224  int_marker.pose.position.y = y;
225  int_marker.pose.position.z = z;
226  int_marker.scale = 0.1;
227 
228  int_marker.name = name;
229  int_marker.description = "";
230 
231  // insert a box
232  makeBoxControl(int_marker);
233  InteractiveMarkerControl control;
234 
235  control.orientation.w = 1;
236  control.orientation.x = 1;
237  control.orientation.y = 0;
238  control.orientation.z = 0;
239  control.name = "move_x";
240  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
241  int_marker.controls.push_back(control);
242 
243  control.orientation.w = 1;
244  control.orientation.x = 0;
245  control.orientation.y = 1;
246  control.orientation.z = 0;
247  control.name = "move_z";
248  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
249  int_marker.controls.push_back(control);
250 
251  control.orientation.w = 1;
252  control.orientation.x = 0;
253  control.orientation.y = 0;
254  control.orientation.z = 1;
255  control.name = "move_y";
256  control.interaction_mode = InteractiveMarkerControl::MOVE_AXIS;
257  int_marker.controls.push_back(control);
258 
259  server->insert(int_marker);
260  server->setCallback(int_marker.name, &processFeedback);
261 }
262 
270 void GetName(xmlTextReaderPtr reader,string& str)
271 {
272  xmlChar*name=xmlTextReaderName(reader);
273  if(!name)return;
274  str=(char*)name;
275  xmlFree(name);
276 }
277 
285 void GetValue(xmlTextReaderPtr reader,string& str)
286 {
287  xmlChar*value=xmlTextReaderValue(reader);
288  if(!value)return;
289  str=(char*)value;
290  xmlFree(value);
291 }
292 
301 void GetAttribute(xmlTextReaderPtr reader,const xmlChar*name,string& str)
302 {
303  xmlChar*attribute=xmlTextReaderGetAttribute(reader,name);
304  if(!attribute)return;
305  str=(char*)attribute;
306  xmlFree(attribute);
307 }
308 
309 void NodeHandler(xmlTextReaderPtr reader)
310 {
311  string name,value;
312  string dev_name;
313  GetName(reader,name);
314 
315  to_upper(name);
316 
317  if(name=="LINK" && xmlTextReaderNodeType(reader)==1)
318  {
319  GetAttribute(reader,BAD_CAST "name",dev_name);
320  boost::shared_ptr<t_reference_frame> rf(new t_reference_frame);
321  rf->name = dev_name;
322  rf->x = 0; rf->y =0; rf->z =0;
323  v_rf.push_back(rf);
324  make6DofMarker( true, v_rf[v_rf.size()-1]->name , v_rf[v_rf.size()-1]->x, v_rf[v_rf.size()-1]->y, v_rf[v_rf.size()-1]->z);
325  }
326 }
327 
328 int main(int argc, char **argv)
329 {
330  ros::init(argc, argv, "atlascar_transforms"); // Initialize ROS coms
331  ros::NodeHandle n; //The node handle
332  ros::Rate loop_rate(1);
333  ros::Publisher vis_pub = n.advertise<visualization_msgs::MarkerArray>( "/axis_markers", 0 );
334  tf::TransformListener listener(n,ros::Duration(10));
335  p_listener=&listener;
336  server.reset( new interactive_markers::InteractiveMarkerServer("show_axes","",false) );
337  ros::Duration(0.1).sleep();
338 
339 
340 
341  //for now have a hard coded link to the atlascar urdf file, we could do this by the command line arguments
342  //std::string urdf_file = ros::package::getPath("atlascar_base") + "/urdf/atlascar_urdf_3dsmodels.xml";
343 
344  std::string urdf_file = ros::package::getPath("coordinate_frames") + "/urdf/urdf.xml";
345 
346  printf("reading %s\n",urdf_file.c_str());
347 
348  int ret;
349  xmlTextReaderPtr reader = xmlNewTextReaderFilename(urdf_file.c_str());
350  if(reader!=NULL)
351  {
352  do
353  {
354  ret=xmlTextReaderRead(reader);//this function returns 1 if ok, -1 on error and 0 if there is no more nodes to read
355  NodeHandler(reader);
356  }while(ret==1);
357 
358  xmlFreeTextReader(reader);
359  if (ret != 0)//zero means the end of the file
360  {
361  ROS_ERROR("Failed to parse file");
362  return -1;
363  }
364  }else
365  {
366  ROS_ERROR("Failed to parse file");
367  return -1;
368  }
369 
370  PFLN
371 
372 
373 
374 
375 
376  server->applyChanges();
377 
378 
379  while (ros::ok())
380  {
381  visualization_msgs::MarkerArray marker_msg;
382 
383  add_to_viz_markers_vec(&(marker_msg.markers));
384 
385  vis_pub.publish(marker_msg);
386 
387  // This will adjust as needed per iteration
388  loop_rate.sleep();
389  ros::spinOnce();
390  ros::spinOnce();
391  ros::spinOnce();
392  ros::spinOnce();
393  ros::spinOnce();
394  ros::spinOnce();
395  }
396 
397 
398  server.reset();
399 }
400 #endif
float marker_pos
Definition: show_axes.cpp:71
int main(int argc, char **argv)
Definition: show_axes.cpp:328
std::vector< boost::shared_ptr< t_reference_frame > > v_rf
Definition: show_axes.cpp:79
void GetValue(xmlTextReaderPtr reader, string &str)
Get the value of a xml element.
Definition: show_axes.cpp:285
tf::TransformListener * p_listener
Definition: show_axes.cpp:69
void set_label_pos(double x, double y, double z, visualization_msgs::Marker *msg, std::vector< visualization_msgs::Marker > *marker_vec)
Definition: show_axes.cpp:141
int add_to_viz_markers_vec(std::vector< visualization_msgs::Marker > *marker_vec)
Definition: show_axes.cpp:191
boost::shared_ptr< interactive_markers::InteractiveMarkerServer > server
Definition: show_axes.cpp:70
void GetName(xmlTextReaderPtr reader, string &str)
Get the name of a xml element.
Definition: show_axes.cpp:270
void NodeHandler(xmlTextReaderPtr reader)
Definition: show_axes.cpp:309
int get_i_from_name(std::vector< boost::shared_ptr< t_reference_frame > > *v_rf, std::string name)
Definition: show_axes.cpp:81
InteractiveMarkerControl & makeBoxControl(InteractiveMarker &msg)
Definition: show_axes.cpp:132
void make6DofMarker(bool fixed, std::string name, double x, double y, double z)
Definition: show_axes.cpp:219
void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
Definition: show_axes.cpp:111
#define PFLN
Definition: show_axes.cpp:62
void GetAttribute(xmlTextReaderPtr reader, const xmlChar *name, string &str)
Get the value of an attribute in a xml element.
Definition: show_axes.cpp:301
std::string name
Definition: show_axes.cpp:76
Marker makeBox(InteractiveMarker &msg)
Definition: show_axes.cpp:96


atlascar_base
Author(s): Jorge Almeida, Sérgio Pinho, Miguel Oliveira, Pedro Salvado, Andre Oliveira and Pedro Pinheiro
autogenerated on Mon Mar 2 2015 01:31:23