sr_tactile_rviz_marker.cpp
Go to the documentation of this file.
1 
26 #include <ros/ros.h>
27 #include <string>
28 #include <boost/thread/mutex.hpp>
29 
30 // messages
31 #include <std_msgs/Float64.h>
32 #include <visualization_msgs/Marker.h>
33 
34 // a ros subscriber (will be instantiated later on)
37 std_msgs::Float64::_data_type data[5];
38 boost::mutex update_mutex;
39 float reduction_factor = 10;
40 // Set our initial shape type to be a cube
41 uint32_t shape = visualization_msgs::Marker::ARROW;
42 
43 
44 void publish_marker(unsigned int id, std::string framesuffix, float force)
45 {
46  if (force > 0.01)
47  {
48  visualization_msgs::Marker marker;
49  // Set the frame ID and timestamp. See the TF tutorials for information on these.
50  marker.header.frame_id = "/srh/position/" + framesuffix;
51  marker.header.stamp = ros::Time::now();
52 
53  // Set the namespace and id for this marker. This serves to create a unique ID
54  // Any marker sent with the same namespace and id will overwrite the old one
55  marker.ns = "touch";
56  marker.id = id;
57 
58  // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER
59  marker.type = shape;
60 
61  // Set the marker action. Options are ADD and DELETE
62  marker.action = visualization_msgs::Marker::ADD;
63  geometry_msgs::Point mypoint;
64  // Set the pose of the marker.
65  // Set start point
66  float phalanx_thickness = 0.007;
67  float phalanx_length = 0.01;
68  mypoint.x = 0;
69  mypoint.y = phalanx_thickness;
70  mypoint.z = phalanx_length;
71  marker.points.push_back(mypoint);
72 
73  // Set end point
74  mypoint.x = 0;
75  mypoint.y = (phalanx_thickness + force / 20.0);
76  mypoint.z = phalanx_length;
77  marker.points.push_back(mypoint);
78 
79  // Set the scale of the marker -- 1x1x1 here means 1m on a side
80  marker.scale.x = 0.0025;
81  marker.scale.y = 0.004;
82  marker.scale.z = 0;
83 
84  // Set the color -- be sure to set alpha to something non-zero!
85  marker.color.r = (force) > 0.0 ? (force) : 0.0;
86  marker.color.g = (1.0f - force) > 0.0 ? (1.0f - force) : 0.0;
87  marker.color.b = 0.0f;
88  marker.color.a = 1.0;
89 
90  marker.lifetime = ros::Duration();
91 
92  // Publish the marker
93  marker_pub.publish(marker);
94  }
95 }
96 
97 
98 void callback_ff(const std_msgs::Float64ConstPtr &msg)
99 {
100  update_mutex.lock();
101  data[0] = msg->data;
102  publish_marker(0, "ffdistal", msg->data / reduction_factor);
103  update_mutex.unlock();
104 }
105 
106 void callback_mf(const std_msgs::Float64ConstPtr &msg)
107 {
108  update_mutex.lock();
109  data[1] = msg->data;
110  publish_marker(1, "mfdistal", msg->data / reduction_factor);
111  update_mutex.unlock();
112 }
113 
114 void callback_rf(const std_msgs::Float64ConstPtr &msg)
115 {
116  update_mutex.lock();
117  data[2] = msg->data;
118  publish_marker(2, "rfdistal", msg->data / reduction_factor);
119  update_mutex.unlock();
120 }
121 
122 void callback_lf(const std_msgs::Float64ConstPtr &msg)
123 {
124  update_mutex.lock();
125  data[3] = msg->data;
126  publish_marker(3, "lfdistal", msg->data / reduction_factor);
127  update_mutex.unlock();
128 }
129 
130 void callback_th(const std_msgs::Float64ConstPtr &msg)
131 {
132  update_mutex.lock();
133  data[4] = msg->data;
134  publish_marker(4, "thdistal", msg->data / reduction_factor);
135  update_mutex.unlock();
136 }
137 
138 int main(int argc, char **argv)
139 {
140  ros::init(argc, argv, "sr_tactile_rviz_marker");
141  ros::NodeHandle n;
142  ros::Rate r(10);
143  marker_pub = n.advertise<visualization_msgs::Marker>("sr_tactile_markers", 1);
144 
145  sub[0] = n.subscribe("/sr_tactile/touch/ff", 2, callback_ff);
146  sub[1] = n.subscribe("/sr_tactile/touch/mf", 2, callback_mf);
147  sub[2] = n.subscribe("/sr_tactile/touch/rf", 2, callback_rf);
148  sub[3] = n.subscribe("/sr_tactile/touch/lf", 2, callback_lf);
149  sub[4] = n.subscribe("/sr_tactile/touch/th", 2, callback_th);
150 
151  std_msgs::Float64::_data_type cur_data[5] = {0};
152 
153  while (ros::ok())
154  {
155  update_mutex.lock();
156  for (int i = 0; i < 5; i++)
157  {
158  cur_data[i] = data[i];
159  }
160  update_mutex.unlock();
161 
162 
163  /*ROS_ERROR("TACTILE SENSOR READING: %f %f %f %f %f",
164  cur_data[0],
165  cur_data[1],
166  cur_data[2],
167  cur_data[3],
168  cur_data[4]);*/
169  r.sleep();
170  ros::spinOnce();
171  }
172 }
173 
174 
175 /* For the emacs weenies in the crowd.
176 Local Variables:
177  c-basic-offset: 2
178 End:
179 */
void publish(const boost::shared_ptr< M > &message) const
f
void callback_mf(const std_msgs::Float64ConstPtr &msg)
std_msgs::Float64::_data_type data[5]
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Subscriber sub[5]
ros::Publisher marker_pub
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::mutex update_mutex
uint32_t shape
void callback_lf(const std_msgs::Float64ConstPtr &msg)
void callback_rf(const std_msgs::Float64ConstPtr &msg)
ROSCPP_DECL bool ok()
void publish_marker(unsigned int id, std::string framesuffix, float force)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
void callback_th(const std_msgs::Float64ConstPtr &msg)
static Time now()
int main(int argc, char **argv)
ROSCPP_DECL void spinOnce()
void callback_ff(const std_msgs::Float64ConstPtr &msg)
float reduction_factor


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