sr_virtual_tactile_sensor.cpp
Go to the documentation of this file.
1 
29 #include <boost/algorithm/string.hpp>
30 #include <string>
31 #include <vector>
32 
33 namespace shadowrobot
34 {
35 /**********************************
36  * TACTILE SENSOR *
37  **********************************/
39  std::string touch_name) :
40  SrGenericTactileSensor(name, touch_name),
41  touch_value(0.0)
42  {
43  if (name.find("th") != std::string::npos)
44  {
45  // fills the vector of joint names: we're taking J2 and J1 for TH
46  std::string tmp = boost::to_upper_copy(name);
47  tmp += "J2";
48  names_joints_linked.push_back(tmp);
49  tmp = boost::to_upper_copy(name);
50  tmp += "J1";
51  names_joints_linked.push_back(tmp);
52  }
53  else
54  {
55  // fills the vector of joint names: we're taking J3 and J0
56  std::string tmp = boost::to_upper_copy(name);
57  tmp += "J3";
58  names_joints_linked.push_back(tmp);
59  tmp = boost::to_upper_copy(name);
60  tmp += "J0";
61  names_joints_linked.push_back(tmp);
62  }
63 
64  sub = nh.subscribe("/srh/shadowhand_data", 2, &SrVirtualTactileSensor::callback, this);
65  }
66 
68  {
69  }
70 
71  void SrVirtualTactileSensor::callback(const sr_robot_msgs::joints_dataConstPtr &msg)
72  {
73  double tmp_value = 0.0;
74  int msg_length = msg->joints_list_length;
75  for (size_t index_msg = 0; index_msg < msg_length; ++index_msg)
76  {
77  // get the sensor name
78  std::string sensor_name = msg->joints_list[index_msg].joint_name;
79 
80  for (size_t index_name = 0; index_name < names_joints_linked.size(); ++index_name)
81  {
82  if (sensor_name.compare(names_joints_linked[index_name]) == 0)
83  {
84  // ressure value = sum(positions)
85  tmp_value += msg->joints_list[index_msg].joint_position;
86  break;
87  }
88  }
89  }
90 
91  touch_mutex.lock();
92  touch_value = tmp_value;
93  touch_mutex.unlock();
94  }
95 
97  {
98  double return_value;
99  touch_mutex.lock();
100  return_value = touch_value;
101  touch_mutex.unlock();
102 
103  return return_value;
104  }
105 
106 /**********************************
107  * TACTILE SENSOR MANAGER *
108  **********************************/
111  {
112  std::vector<std::vector<std::string> > all_names = get_all_names();
113 
114  for (unsigned int i = 0; i < all_names[0].size(); ++i)
115  {
116  tactile_sensors.push_back(
118  new SrVirtualTactileSensor(all_names[0][i],
119  all_names[1][i])));
120  }
121  }
122 
124  {
125  }
126 } // namespace shadowrobot
127 
136 int main(int argc, char **argv)
137 {
138  ros::init(argc, argv, "sr_tactile_sensor");
139  ros::NodeHandle n;
140 
142 
143  while (ros::ok())
144  {
145  tact_sens_mgr.publish_all();
146  }
147 
148  return 0;
149 }
150 
151 /* For the emacs weenies in the crowd.
152  Local Variables:
153  c-basic-offset: 2
154  End:
155 */
void callback(const sr_robot_msgs::joints_dataConstPtr &msg)
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)
std::vector< boost::shared_ptr< SrGenericTactileSensor > > tactile_sensors
ROSCPP_DECL bool ok()
SrVirtualTactileSensor(std::string name, std::string touch_name)
int main(int argc, char **argv)
std::vector< std::vector< std::string > > get_all_names()


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