sr_gazebo_virtual_tactile_sensor.cpp
Go to the documentation of this file.
1 
28 #include <boost/algorithm/string.hpp>
29 #include <string>
30 #include <vector>
31 #include <math.h>
32 #include <ros/ros.h>
33 
34 namespace shadowrobot
35 {
36 /**********************************
37  * TACTILE SENSOR *
38  **********************************/
40  std::string gazebo_bumper_topic) :
41  SrGenericTactileSensor(name, ""),
42  touch_value(0.0),
43  touch_freshdata(false)
44  {
45  sub = nh.subscribe(gazebo_bumper_topic, 2, &SrGazeboVirtualTactileSensor::callback, this);
46  }
47 
49  {
50  }
51 
52  void SrGazeboVirtualTactileSensor::callback(const gazebo_msgs::ContactsState &msg)
53  {
54  double tmp_value;
55  ::geometry_msgs::Vector3 v;
56  v.x = 0;
57  v.y = 0;
58  v.z = 0;
59 
60  ROS_DEBUG("Touch by %s", msg.header.frame_id.c_str());
61  // Parse the message to retrieve the relevant touch pressure information
62  // Currently the sum of all contacts.
63  // More sophisticated analysis can be done to take the contact that is the most aligned with the distal link normal
64 
65  if (msg.states.size() > 0)
66  {
67  size_t nb_wrench = msg.states[0].wrenches.size();
68  for (unsigned int i = 0; i < nb_wrench; i++)
69  {
70  v.x = v.x + msg.states[0].wrenches[i].force.x;
71  v.y = v.y + msg.states[0].wrenches[i].force.y;
72  v.z = v.z + msg.states[0].wrenches[i].force.z;
73  }
74  v.x = v.x / nb_wrench;
75  v.y = v.y / nb_wrench;
76  v.z = v.z / nb_wrench;
77  tmp_value = sqrt(pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2));
78  touch_mutex.lock();
79  touch_value = tmp_value;
80  touch_freshdata = true;
81  touch_mutex.unlock();
82  }
83  }
84 
86  {
87  double return_value;
88  touch_mutex.lock();
89  if (touch_freshdata)
90  {
91  return_value = touch_value;
92  touch_freshdata = false;
93  }
94  else
95  {
96  return_value = 0.0;
97  }
98  touch_mutex.unlock();
99 
100  return return_value;
101  }
102 
103 
104 /**********************************
105  * TACTILE SENSOR MANAGER *
106  **********************************/
109  {
110  std::vector<std::vector<std::string> > all_names = get_all_names();
111 
112  for (unsigned int i = 0; i < all_names[0].size(); ++i)
113  {
114  tactile_sensors.push_back(
116  new SrGazeboVirtualTactileSensor(all_names[0][i],
117  "contacts/" + all_names[0][i] + "/distal")));
118  }
119  }
120 
122  {
123  }
124 } // namespace shadowrobot
125 
126 
135 int main(int argc, char **argv)
136 {
137  ros::init(argc, argv, "sr_tactile_sensor");
138  ros::NodeHandle n;
139 
141 
142  while (ros::ok())
143  {
144  tact_sens_mgr.publish_all();
145  }
146 
147  return 0;
148 }
149 
150 /* For the emacs weenies in the crowd.
151 Local Variables:
152  c-basic-offset: 2
153 End:
154 */
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)
SrGazeboVirtualTactileSensor(std::string name, std::string gazebo_topic)
int main(int argc, char **argv)
std::vector< boost::shared_ptr< SrGenericTactileSensor > > tactile_sensors
ROSCPP_DECL bool ok()
void callback(const gazebo_msgs::ContactsState &msg)
std::vector< std::vector< std::string > > get_all_names()
#define ROS_DEBUG(...)


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