sr_bumper_rviz_marker.cpp
Go to the documentation of this file.
1 
26 #include <ros/ros.h>
27 #include <visualization_msgs/Marker.h>
28 #include <gazebo_msgs/ContactsState.h>
29 
30 #include <string>
31 #include <vector>
32 
33 #include <boost/thread/mutex.hpp>
34 
35 #include <std_msgs/Float64.h>
36 
37 #define COUNTDOWN_MAX 5
38 #define FF 0
39 #define MF 1
40 #define RF 2
41 #define LF 3
42 #define TH 4
43 
44 // a ros subscriber (will be instantiated later on)
47 std_msgs::Float64::_data_type data[5];
48 boost::mutex update_mutex;
49 int colors[8][3] = {{0, 0, 0},
50  {1, 0, 0},
51  {0, 1, 0},
52  {0, 0, 1},
53  {1, 1, 0},
54  {1, 0, 1},
55  {0, 1, 1},
56  {1, 1, 1}};
57 // Set our initial shape type to be a cube
58 uint32_t shape = visualization_msgs::Marker::ARROW;
60 
61 void publish_marker_special(unsigned int id, std::string framesuffix, const ::geometry_msgs::Vector3 &force, int col)
62 {
63  visualization_msgs::Marker marker;
64  // Set the frame ID and timestamp. See the TF tutorials for information on these.
65  marker.header.frame_id = "/" + framesuffix;
66  marker.header.stamp = ros::Time::now();
67 
68  // Set the namespace and id for this marker. This serves to create a unique ID
69  // Any marker sent with the same namespace and id will overwrite the old one
70  marker.ns = "touch";
71  marker.id = id;
72 
73  // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
74  marker.type = shape;
75 
76  // Set the marker action. Options are ADD and DELETE
77  marker.action = visualization_msgs::Marker::ADD;
78  geometry_msgs::Point mypoint;
79 
80  // Set the pose of the marker.
81  // Set start point
82  float phalanx_thickness = 0.007;
83  float phalanx_length = 0.01;
84  mypoint.x = 0;
85  mypoint.y = phalanx_thickness;
86  mypoint.z = phalanx_length;
87  marker.points.push_back(mypoint);
88 
89  // Set end point
90  mypoint.x = (force.x / 20.0);
91  mypoint.y = (phalanx_thickness + force.y / 20.0);
92  mypoint.z = phalanx_length + force.z / 20.0;
93  marker.points.push_back(mypoint);
94 
95  // Set the scale of the marker -- 1x1x1 here means 1m on a side
96  marker.scale.x = 0.0025;
97  marker.scale.y = 0.004;
98  marker.scale.z = 0;
99 
100  // Set the color -- be sure to set alpha to something non-zero!
101  marker.color.r = colors[col][0];
102  marker.color.g = colors[col][1];
103  marker.color.b = colors[col][2];
104  marker.color.a = 1.0;
105 
106  marker.lifetime = ros::Duration(.5);
107 
108  // Publish the marker
109  marker_pub.publish(marker);
110 }
111 
112 void publish_marker(unsigned int id, std::string framesuffix, float force)
113 {
114  if (force > 0.01)
115  {
116  visualization_msgs::Marker marker;
117  // Set the frame ID and timestamp. See the TF tutorials for information on these.
118  marker.header.frame_id = "/srh/position/" + framesuffix;
119  marker.header.stamp = ros::Time::now();
120 
121  // Set the namespace and id for this marker. This serves to create a unique ID
122  // Any marker sent with the same namespace and id will overwrite the old one
123  marker.ns = "touch";
124  marker.id = id;
125 
126  // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
127  marker.type = shape;
128 
129  // Set the marker action. Options are ADD and DELETE
130  marker.action = visualization_msgs::Marker::ADD;
131  geometry_msgs::Point mypoint;
132  // Set the pose of the marker.
133  // Set start point
134  float phalanx_thickness = 0.007;
135  float phalanx_length = 0.01;
136  mypoint.x = 0;
137  mypoint.y = phalanx_thickness;
138  mypoint.z = phalanx_length;
139  marker.points.push_back(mypoint);
140 
141  // Set end point
142  mypoint.x = 0;
143  mypoint.y = (phalanx_thickness + force / 20.0);
144  mypoint.z = phalanx_length;
145  marker.points.push_back(mypoint);
146 
147  // Set the scale of the marker -- 1x1x1 here means 1m on a side
148  marker.scale.x = 0.0025;
149  marker.scale.y = 0.004;
150  marker.scale.z = 0;
151 
152  // Set the color -- be sure to set alpha to something non-zero!
153  marker.color.r = (force) > 0.0f ? (force) : 0.0f;
154  marker.color.g = (1.0f - force) > 0.0f ? (1.0f - force) : 0.0f;
155  marker.color.b = 0.0f;
156  marker.color.a = 1.0;
157 
158  marker.lifetime = ros::Duration();
159 
160  // Publish the marker
161  marker_pub.publish(marker);
162  }
163 }
164 
165 void callback_ff(const gazebo_msgs::ContactsState &msg)
166 {
167  // Publish the marker
168  // once every countdown
169  update_mutex.lock();
170  countdown[FF]--;
171  if (countdown[FF] <= 0)
172  {
174  for (unsigned int i = 0; i < msg.states[0].wrenches.size(); ++i)
175  {
176  const ::geometry_msgs::Vector3 &v = msg.states[0].wrenches[i].force;
177  publish_marker_special(FF, "ffdistal", v, i);
178  }
179  }
180  update_mutex.unlock();
181 }
182 
183 void callback_mf(const gazebo_msgs::ContactsState &msg)
184 {
185  // Publish the marker
186  // once every countdown
187  update_mutex.lock();
188  countdown[MF]--;
189  if (countdown[MF] <= 0)
190  {
192  for (unsigned int i = 0; i < msg.states[0].wrenches.size(); i++)
193  {
194  const ::geometry_msgs::Vector3 &v = msg.states[0].wrenches[i].force;
195  publish_marker_special(MF, "mfdistal", v, i);
196  }
197  }
198  update_mutex.unlock();
199 }
200 
201 void callback_rf(const gazebo_msgs::ContactsState &msg)
202 {
203  // Publish the marker
204  // once every countdown
205  update_mutex.lock();
206  countdown[RF]--;
207  if (countdown[RF] <= 0)
208  {
210  for (unsigned int i = 0; i < msg.states[0].wrenches.size(); i++)
211  {
212  const ::geometry_msgs::Vector3 &v = msg.states[0].wrenches[i].force;
213  publish_marker_special(RF, "rfdistal", v, i);
214  }
215  }
216  update_mutex.unlock();
217 }
218 
219 void callback_lf(const gazebo_msgs::ContactsState &msg)
220 {
221  // Publish the marker
222  // once every countdown
223  update_mutex.lock();
224  countdown[LF]--;
225  if (countdown[LF] <= 0)
226  {
228  for (unsigned int i = 0; i < msg.states[0].wrenches.size(); i++)
229  {
230  const ::geometry_msgs::Vector3 &v = msg.states[0].wrenches[i].force;
231  publish_marker_special(LF, "lfdistal", v, i);
232  }
233  }
234  update_mutex.unlock();
235 }
236 
237 void callback_th(const gazebo_msgs::ContactsState &msg)
238 {
239  // Publish the marker
240  // once every countdown
241  update_mutex.lock();
242  countdown[TH]--;
243  if (countdown[TH] <= 0)
244  {
246  for (unsigned int i = 0; i < msg.states[0].wrenches.size(); i++)
247  {
248  const ::geometry_msgs::Vector3 &v = msg.states[0].wrenches[i].force;
249  publish_marker_special(TH, "thdistal", v, i);
250  }
251  }
252  update_mutex.unlock();
253 }
254 
255 
256 int main(int argc, char **argv)
257 {
258  ros::init(argc, argv, "bumper_markers");
259  ros::NodeHandle n;
260  ros::Rate r(10);
261  marker_pub = n.advertise<visualization_msgs::Marker>("bumper_markers", 1);
262 
263  sub[0] = n.subscribe("/ffdistal_bumper/state", 2, callback_ff);
264  sub[1] = n.subscribe("/mfdistal_bumper/state", 2, callback_mf);
265  sub[2] = n.subscribe("/rfdistal_bumper/state", 2, callback_rf);
266  sub[3] = n.subscribe("/lfdistal_bumper/state", 2, callback_lf);
267  sub[4] = n.subscribe("/thdistal_bumper/state", 2, callback_th);
268 
269  std_msgs::Float64::_data_type cur_data[5] = {0};
270 
271  while (ros::ok())
272  {
273  update_mutex.lock();
274  for (int i = 0; i < 5; i++)
275  {
276  cur_data[i] = data[i];
277  }
278  update_mutex.unlock();
279 
280 
281  /*ROS_ERROR("TACTILE SENSOR READING: %f %f %f %f %f",
282  cur_data[0],
283  cur_data[1],
284  cur_data[2],
285  cur_data[3],
286  cur_data[4]);*/
287  r.sleep();
288  ros::spinOnce();
289  }
290 
291  return 0;
292 }
293 
void callback_mf(const gazebo_msgs::ContactsState &msg)
ros::Publisher marker_pub
std_msgs::Float64::_data_type data[5]
void publish(const boost::shared_ptr< M > &message) const
void callback_ff(const gazebo_msgs::ContactsState &msg)
f
#define MF
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int countdown[5]
uint32_t shape
void callback_lf(const gazebo_msgs::ContactsState &msg)
void publish_marker_special(unsigned int id, std::string framesuffix, const ::geometry_msgs::Vector3 &force, int col)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Subscriber sub[5]
bool sleep()
#define LF
#define COUNTDOWN_MAX
int colors[8][3]
static Time now()
boost::mutex update_mutex
int main(int argc, char **argv)
#define RF
ROSCPP_DECL void spinOnce()
#define TH
void callback_rf(const gazebo_msgs::ContactsState &msg)
#define FF
void callback_th(const gazebo_msgs::ContactsState &msg)
void publish_marker(unsigned int id, std::string framesuffix, float force)


sr_tactile_sensors
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:46