sr_generic_tactile_sensor.cpp
Go to the documentation of this file.
1 
29 #include <string>
30 #include <vector>
31 
32 
33 namespace shadowrobot
34 {
35 /**********************************
36  * TACTILE SENSOR *
37  **********************************/
39  std::string touch_name) :
40  n_tilde("~")
41  {
42  std::string full_topic_touch = "touch/" + name;
43 
44  touch_pub = n_tilde.advertise<std_msgs::Float64>(full_topic_touch, 10);
45  }
46 
48  {
49  }
50 
52  {
53  msg_touch.data = get_touch_data();
54 
56  }
57 
58 /**********************************
59  * TACTILE SENSOR MANAGER *
60  **********************************/
62  n_tilde("~"), publish_rate(20.0)
63  {
64  double publish_freq;
65  n_tilde.param("publish_frequency", publish_freq, 20.0);
66  publish_rate = ros::Rate(publish_freq);
67 
68  // initializing the thresholds to test if the hand is holding
69  // something or not (compared agains the pressure value).
70  double tmp[5] = {117, 117, 113, 111, 0};
71  XmlRpc::XmlRpcValue threshold_xmlrpc;
72  if (n_tilde.getParam("/grasp_touch_thresholds", threshold_xmlrpc))
73  {
74  ROS_ASSERT(threshold_xmlrpc.getType() == XmlRpc::XmlRpcValue::TypeArray);
75  if (threshold_xmlrpc.getType() == XmlRpc::XmlRpcValue::TypeArray)
76  {
77  if (threshold_xmlrpc.size() == 5)
78  {
79  for (int i = 0; i < threshold_xmlrpc.size(); ++i)
80  {
81  if (threshold_xmlrpc[i].getType() == XmlRpc::XmlRpcValue::TypeDouble)
82  {
83  tmp[i] = static_cast<double>(threshold_xmlrpc[i]);
84  }
85  else
86  ROS_ERROR("grasp_touch_thresholds value %d is not a double, not loading", i + 1);
87  }
88  }
89  else
90  ROS_ERROR("grasp_touch_thresholds must be of size 5, using default values");
91  }
92  else
93  ROS_ERROR("grasp_touch_thresholds must be an array, using default values");
94  }
95  else
96  ROS_WARN("grasp_touch_thresholds not set, using default values");
97 
98  is_hand_occupied_thresholds = std::vector<double>(tmp, tmp + 5);
99  ROS_DEBUG("is_hand_occupied_thresholds:[%f %f %f %f %f]", is_hand_occupied_thresholds.at(0),
102 
103 
105  this);
106  which_fingers_are_touching_server = n_tilde.advertiseService("which_fingers_are_touching",
108  this);
109  }
110 
112  {
113  }
114 
115  bool SrTactileSensorManager::is_hand_occupied_cb(sr_robot_msgs::is_hand_occupied::Request &req,
116  sr_robot_msgs::is_hand_occupied::Response &res)
117  {
118  bool is_occupied = true;
119 
120  for (unsigned int i = 0; i < tactile_sensors.size(); ++i)
121  {
122  if (tactile_sensors[i]->get_touch_data() < is_hand_occupied_thresholds[i])
123  {
124  is_occupied = false;
125  ROS_DEBUG("is_hand_occupied_thresholds %d with val %f is smaller than threshold %f", i,
126  tactile_sensors[i]->get_touch_data(), is_hand_occupied_thresholds[i]);
127  break;
128  }
129  }
130 
131  res.hand_occupied = is_occupied;
132 
133  return true;
134  }
135 
136  bool SrTactileSensorManager::which_fingers_are_touching_cb(sr_robot_msgs::which_fingers_are_touching::Request &req,
137  sr_robot_msgs::which_fingers_are_touching::Response &res)
138  {
139  std::vector<double> touch_values(5);
140  ROS_ASSERT(tactile_sensors.size() == 5);
141 
142  double value_tmp = 0.0;
143  for (unsigned int i = 0; i < tactile_sensors.size(); ++i)
144  {
145  value_tmp = tactile_sensors[i]->get_touch_data();
146  if (value_tmp < req.force_thresholds[i])
147  {
148  touch_values[i] = 0.0;
149  }
150  else
151  {
152  touch_values[i] = value_tmp;
153  }
154  }
155  res.touch_forces = touch_values;
156  return true;
157  }
158 
159  std::vector<std::vector<std::string> > SrTactileSensorManager::get_all_names()
160  {
161  std::vector<std::vector<std::string> > all_names_vector;
162  std::vector<std::string> names, sensor_touch_names;
163 
164  std::string tmp;
165  XmlRpc::XmlRpcValue my_list;
166  int list_size;
167  bool bad_params = false;
168 
169  n_tilde.getParam("display_names", my_list);
170  if (my_list.getType() != XmlRpc::XmlRpcValue::TypeArray)
171  {
172  bad_params = true;
173  }
174  list_size = my_list.size();
175  for (int32_t i = 0; i < list_size; ++i)
176  {
177  if (my_list[i].getType() != XmlRpc::XmlRpcValue::TypeString)
178  {
179  bad_params = true;
180  }
181  names.push_back(static_cast<std::string>(my_list[i]));
182  }
183 
184  n_tilde.getParam("sensor_touch_names", my_list);
185  if (my_list.getType() != XmlRpc::XmlRpcValue::TypeArray)
186  {
187  bad_params = true;
188  }
189  if (my_list.size() != list_size)
190  {
191  bad_params = true;
192  }
193  list_size = my_list.size();
194  for (int32_t i = 0; i < list_size; ++i)
195  {
196  if (my_list[i].getType() != XmlRpc::XmlRpcValue::TypeString)
197  {
198  bad_params = true;
199  }
200  sensor_touch_names.push_back(static_cast<std::string>(my_list[i]));
201  }
202 
203  if (bad_params)
204  {
205  ROS_ERROR("Error while reading the parameter for the tactile sensors; using standard parameters");
206  names.clear();
207  sensor_touch_names.clear();
208 
209  names.push_back("ff");
210  names.push_back("mf");
211  names.push_back("rf");
212  names.push_back("lf");
213  names.push_back("th");
214 
215  sensor_touch_names.push_back("FF_Touch");
216  sensor_touch_names.push_back("MF_Touch");
217  sensor_touch_names.push_back("RF_Touch");
218  sensor_touch_names.push_back("LF_Touch");
219  sensor_touch_names.push_back("TH_Touch");
220  }
221 
222  all_names_vector.push_back(names);
223  all_names_vector.push_back(sensor_touch_names);
224 
225  return all_names_vector;
226  }
227 
229  {
230  for (unsigned int i = 0; i < tactile_sensors.size(); ++i)
231  {
232  tactile_sensors[i]->publish_current_values();
233  }
234 
236  ros::spinOnce();
237  }
238 } // namespace shadowrobot
239 
240 
241 /* For the emacs weenies in the crowd.
242 Local Variables:
243  c-basic-offset: 2
244 End:
245 */
This is a generic parent class for the tactile sensors used in the Shadow Robot Dextrous Hand...
void publish(const boost::shared_ptr< M > &message) const
int size() const
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
Type const & getType() const
#define ROS_WARN(...)
std::vector< boost::shared_ptr< SrGenericTactileSensor > > tactile_sensors
bool param(const std::string &param_name, T &param_val, const T &default_val) const
SrGenericTactileSensor(std::string name, std::string touch_name)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
bool which_fingers_are_touching_cb(sr_robot_msgs::which_fingers_are_touching::Request &req, sr_robot_msgs::which_fingers_are_touching::Response &res)
bool getParam(const std::string &key, std::string &s) const
bool is_hand_occupied_cb(sr_robot_msgs::is_hand_occupied::Request &req, sr_robot_msgs::is_hand_occupied::Response &res)
#define ROS_ASSERT(cond)
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
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