cob_teleop.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <ros/ros.h>
18 
20 #include <cob_light/LightModes.h>
21 #include <cob_light/SetLightModeAction.h>
22 #include <cob_script_server/ScriptAction.h>
23 #include <cob_sound/SayAction.h>
24 #include <cob_actions/SetStringAction.h>
25 #include <cob_actions/SetStringGoal.h>
26 #include <geometry_msgs/Twist.h>
27 #include <sensor_msgs/JointState.h>
28 #include <sensor_msgs/Joy.h>
29 #include <sensor_msgs/JoyFeedbackArray.h>
30 #include <std_msgs/Float64MultiArray.h>
31 #include <std_msgs/ColorRGBA.h>
32 #include <std_srvs/Trigger.h>
33 #include <XmlRpcValue.h>
34 
35 class CobTeleop
36 {
37 public:
38 
40  {
41  std::string key;
42  std::string twist_topic_name;
44  std::string vel_group_topic_name;
45  std::string sss_default_target;
46  std::vector<double> joint_velocity;
47  std::vector<double> twist_max_vel; //max_vx_, max_vy_, max_vz_, max_rotx_, max_roty_, max_rotz_
48  std::vector<double> twist_max_acc; //max_ax_, max_ay_, max_arotz_
52  };
53 
54  std::map<std::string,component_config> component_config_;
55 
56  //axis
58 
59  //buttons
60  //mode 1: Base
64  //mode 2: Trajectory controller (to default target position using sss.move)
65  //mode 3: Velocity group controller
70  //mode 4: Twist controller
71 
72  //common
77  bool safe_mode_;
78  double publish_freq_;
82  int mode_;
85 
88  ros::Subscriber joy_sub_; //subscribe topic joy
89  ros::Subscriber joint_states_sub_; //subscribe topic joint_states
90 
92  ScriptClient_ * sss_client_;
93  cob_script_server::ScriptGoal script_goal_;
94 
96  SayClient_ * say_client_;
98  std::string sound_action_name_;
99 
101  SetLightClient_ * setlight_client_;
103  std::string light_action_name_;
104 
106  DockClient_ * dock_client_;
107  std::string dock_action_name_;
108 
110  UndockClient_ * undock_client_;
111  std::string undock_action_name_;
112 
113  cob_light::SetLightModeGoal confirm_light_goal_;
114  cob_light::SetLightModeGoal error_light_goal_;
115 
116  std::vector<double> vel_old_;
117  std::vector<double> vel_req_;
118  std::vector<double> vel_base_;
120 
122  void init();
123  void updateBase();
124  void say(std::string text, bool blocking);
125  void setLight(int mode);
126  void joy_cb(const sensor_msgs::Joy::ConstPtr &joy_msg);
127  sensor_msgs::JoyFeedbackArray switch_mode();
128 };
129 
131 {
132  if(n_.hasParam("components"))
133  {
134  ROS_DEBUG("components found ");
135  n_.getParam("components", components_);
137  {
138  ROS_DEBUG("components are of type struct with size %d",(int)components_.size());
139  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator p=components_.begin();p!=components_.end();++p)
140  {
141  std::string comp_name = p->first;
142  ROS_DEBUG("component name: %s",comp_name.c_str());
143  XmlRpc::XmlRpcValue comp_struc = p->second;
144  if(comp_struc.getType() != XmlRpc::XmlRpcValue::TypeStruct)
145  {
146  ROS_WARN("invalid component, name: %s",comp_name.c_str());
147  }
148  component_config tempComponent;
149  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator ps=comp_struc.begin();ps!=comp_struc.end();++ps)
150  {
151  std::string par_name = ps->first;
152  ROS_DEBUG("par name: %s",par_name.c_str());
153  if(par_name.compare("twist_topic_name")==0)
154  {
155  ROS_DEBUG("twist topic name found");
157  ROS_ASSERT(twist_topic_name.getType() == XmlRpc::XmlRpcValue::TypeString);
158  std::string s((std::string)twist_topic_name);
159  ROS_DEBUG("twist_topic_name found = %s",s.c_str());
160  tempComponent.twist_topic_name = s;
161  tempComponent.twist_controller_publisher_ = n_.advertise<geometry_msgs::Twist>((s),1);
162  }
163  if(par_name.compare("twist_safety_topic_name")==0)
164  {
165  ROS_DEBUG("twist saftey topic name found");
167  ROS_ASSERT(twist_safety_topic_name.getType() == XmlRpc::XmlRpcValue::TypeString);
168  std::string s((std::string)twist_safety_topic_name);
169  ROS_DEBUG("twist_safety_topic_name found = %s",s.c_str());
170  tempComponent.twist_safety_topic_name = s;
171  tempComponent.twist_safety_controller_publisher_ = n_.advertise<geometry_msgs::Twist>((s),1);
172  }
173  else if(par_name.compare("velocity_topic_name")==0)
174  {
175  ROS_DEBUG("topic name found");
177  ROS_ASSERT(vel_group_topic_name.getType() == XmlRpc::XmlRpcValue::TypeString);
178  std::string s((std::string)vel_group_topic_name);
179  ROS_DEBUG("topic_name found = %s",s.c_str());
180  tempComponent.vel_group_topic_name = s;
181  tempComponent.vel_group_controller_publisher_ = n_.advertise<std_msgs::Float64MultiArray>((s),1);
182  }
183  else if(par_name.compare("sss_default_target")==0)
184  {
185  ROS_DEBUG("default target position found");
187  ROS_ASSERT(sss_default_target.getType() == XmlRpc::XmlRpcValue::TypeString);
188  std::string s((std::string)sss_default_target);
189  ROS_DEBUG("sss_default_target found = %s",s.c_str());
190  tempComponent.sss_default_target = s;
191  }
192  else if(par_name.compare("joint_velocity")==0)
193  {
194  ROS_DEBUG("joint vels found");
195  XmlRpc::XmlRpcValue joint_velocity = ps->second;
197  ROS_DEBUG("joint_velocity.size: %d \n", joint_velocity.size());
198  for(int i=0;i<joint_velocity.size();i++)
199  {
200  ROS_ASSERT(joint_velocity[i].getType() == XmlRpc::XmlRpcValue::TypeDouble);
201  double vel((double)joint_velocity[i]);
202  ROS_DEBUG("joint_velocity found = %f",vel);
203  tempComponent.joint_velocity.push_back(vel);
204  }
205  }
206  else if(par_name.compare("twist_max_velocity")==0)
207  {
208  ROS_DEBUG("max Velocity found");
209  XmlRpc::XmlRpcValue twist_max_velocity = ps->second;
210  ROS_ASSERT(twist_max_velocity.getType() == XmlRpc::XmlRpcValue::TypeArray);
211  ROS_DEBUG("twist_max_velocity.size: %d \n", twist_max_velocity.size());
212  for(int i=0;i<twist_max_velocity.size();i++)
213  {
214  ROS_ASSERT(twist_max_velocity[i].getType() == XmlRpc::XmlRpcValue::TypeDouble);
215  double vel((double)twist_max_velocity[i]);
216  ROS_DEBUG("twist_max_velocity found = %f",vel);
217  tempComponent.twist_max_vel.push_back(vel);
218  }
219  }
220  else if(par_name.compare("twist_max_acc")==0)
221  {
222  ROS_DEBUG("max Velocity found");
223  XmlRpc::XmlRpcValue twist_max_acc = ps->second;
225  ROS_DEBUG("twist_max_acc.size: %d \n", twist_max_acc.size());
226  for(int i=0;i<twist_max_acc.size();i++)
227  {
228  ROS_ASSERT(twist_max_acc[i].getType() == XmlRpc::XmlRpcValue::TypeDouble);
229  double vel((double)twist_max_acc[i]);
230  ROS_DEBUG("twist_max_acc found = %f",vel);
231  tempComponent.twist_max_acc.push_back(vel);
232  }
233  }
234  }
235  ROS_DEBUG("%s module stored",comp_name.c_str());
236  component_config_.insert(std::pair<std::string,component_config>(comp_name,tempComponent));
237  }
238  }
239  }
240 
241  sss_client_ = new ScriptClient_("/script_server", true);
242 
243  n_.param<bool>("enable_sound", enable_sound_, false);
244  n_.param<std::string>("sound_action_name", sound_action_name_, "/sound/say");
245  say_client_ = new SayClient_(sound_action_name_, true);
246 
247  n_.param<bool>("enable_light", enable_light_, false);
248  n_.param<std::string>("light_action_name", light_action_name_, "set_light");
249  setlight_client_ = new SetLightClient_(light_action_name_, true);
250 
251  n_.param<std::string>("dock_action_name", dock_action_name_, "/docker_control/dock");
252  dock_client_ = new DockClient_(dock_action_name_, true);
253  n_.param<std::string>("undock_action_name", undock_action_name_, "/docker_control/undock");
254  undock_client_ = new UndockClient_(undock_action_name_, true);
255 
256  vel_req_.resize(component_config_["base"].twist_max_acc.size());
257  vel_old_.resize(component_config_["base"].twist_max_acc.size());
258  vel_base_.resize(component_config_["base"].twist_max_acc.size());
259  for(unsigned int i=0; i<component_config_["base"].twist_max_acc.size(); i++)
260  {
261  vel_old_[i]=0;
262  vel_req_[i]=0;
263  }
264 }
265 
266 sensor_msgs::JoyFeedbackArray CobTeleop::switch_mode()
267 {
268  ++mode_;
269 
270  if (mode_ > joy_num_modes_)
271  {
272  mode_ = 1;
273  }
274 
275  std::string saytext;
276  if (mode_ == 1)
277  {
278  ROS_INFO("Switched to mode 1: move the base using twist controller");
279  saytext = "Base mode";
280  }
281  if (mode_ == 2)
282  {
283  ROS_INFO("Switched to mode 2: move the actuators to a default position (Trajectory controller)");
284  saytext = "Default position mode";
285  }
286  if(mode_ == 3)
287  {
288  ROS_INFO("Switched to mode 3: move the actuators using joint group velocity controller");
289  saytext = "Velocity mode";
290  }
291  if(mode_ == 4)
292  {
293  ROS_INFO("Switched to mode 4: move the actuators in cartesian mode using twist controller");
294  saytext = "Cartesian mode";
295  }
296 
297  setLight(mode_);
298  say(saytext, false);
299 
301 
302  sensor_msgs::JoyFeedbackArray joy_fb;
303  joy_fb.array.resize(4);
304  for (unsigned int i=0; i<4; i++)
305  {
306  joy_fb.array[i].type=0;
307  joy_fb.array[i].id=i;
308  joy_fb.array[i].intensity=static_cast<int>(LEDS_[i]);
309  }
310  return joy_fb;
311 }
312 
314 {
315  if (joy_active_)
316  {
317  if(mode_==1)
318  {
319  double dt = 1.0/double(publish_freq_);
320  geometry_msgs::Twist base_cmd;
321  if(!joy_active_)
322  {
323  for(unsigned int i=0; i<3; i++)
324  {
325  vel_old_[i]=0;
326  vel_req_[i]=0;
327  }
328  }
329 
330  if(apply_ramp_)
331  {
332  for( int i =0; i<3; i++)
333  {
334  // filter v with ramp
335  if ((vel_req_[i]-vel_old_[i])/dt > component_config_["base"].twist_max_acc[i])
336  {
337  vel_base_[i] = vel_old_[i] + component_config_["base"].twist_max_acc[i]*dt;
338  }
339  else if((vel_req_[i]-vel_old_[i])/dt < -component_config_["base"].twist_max_acc[i])
340  {
341  vel_base_[i] = vel_old_[i] - component_config_["base"].twist_max_acc[i]*dt;
342  }
343  else
344  {
345  vel_base_[i] = vel_req_[i];
346  }
347  vel_old_[i] = vel_base_[i];
348  }
349  }
350  else
351  {
352  vel_base_[0] = vel_req_[0];
353  vel_base_[1] = vel_req_[1];
354  vel_base_[2] = vel_req_[2];
355  }
356  base_cmd.linear.x = vel_base_[0];
357  base_cmd.linear.y = vel_base_[1];
358  base_cmd.angular.z = vel_base_[2];
359  if (safe_mode_)
360  {
361  component_config_["base"].twist_safety_controller_publisher_.publish(base_cmd);
362  }
363  else
364  {
365  component_config_["base"].twist_controller_publisher_.publish(base_cmd);
366  }
367  }
368  }
369 }
370 
371 void CobTeleop::say(std::string text, bool blocking)
372 {
373  if(enable_sound_)
374  {
375  cob_sound::SayGoal say_goal;
376  say_goal.text = text;
377  say_client_->sendGoal(say_goal);
378  if (blocking)
379  {
381  }
382  }
383 }
384 
386 {
387  if(enable_light_)
388  {
389  cob_light::LightMode light;
390  light.mode = cob_light::LightModes::FLASH;
391  light.frequency = 5;
392  light.priority = 2;
393  std_msgs::ColorRGBA color;
394  color.r = 0;
395  color.g = 1;
396  color.b = 0;
397  color.a = 1;
398  light.pulses = mode;
399  light.colors.push_back(color);
400  cob_light::SetLightModeGoal light_goal;
401  light_goal.mode = light;
402  setlight_client_->sendGoal(light_goal);
403  }
404 }
405 
406 void CobTeleop::joy_cb(const sensor_msgs::Joy::ConstPtr &joy_msg)
407 {
408  if(deadman_button_>=0 && deadman_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[deadman_button_]==1)
409  {
410  ROS_DEBUG("joystick is active");
411  }
412  else
413  {
414  for(unsigned int i=0; i<component_config_["base"].twist_max_acc.size(); i++)
415  {
416  vel_req_[i]=0;
417  vel_old_[i]=0;
418  }
419  ROS_DEBUG("joystick is not active");
420  joy_active_ = false;
421  return;
422  }
423 
424  if(mode_switch_button_>=0 && mode_switch_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[mode_switch_button_]==1)
425  {
426  ROS_INFO("Switch mode button pressed");
427  switch_mode();
428  }
429 
430  if(safety_button_>=0 && safety_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[safety_button_]==1)
431  {
432  safe_mode_ = false;
433  }else
434  {
435  safe_mode_ = true;
436  }
437 
438  if(run_button_>=0 && run_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[run_button_]==1)
439  {
441  }
442  else //button release
443  {
444  run_factor_ = 1.0;
445  }
446 
448  {
449  ROS_INFO("Waiting for script server");
450  return;
451  }
452 
453  if(init_button_>=0 && init_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[init_button_]==1)
454  {
455  ROS_INFO("Init and recover");
456  say("init and recover", true);
457 
458  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator p=components_.begin();p!=components_.end();++p)
459  {
460  std::string comp_name = p->first;
461 
462  ROS_INFO("Init %s",comp_name.c_str());
463  std::string saytext = comp_name;
464  std::replace(saytext.begin(), saytext.end(), '_', ' ');
465  say(saytext, true);
466  script_goal_.component_name = comp_name.c_str();
467  script_goal_.function_name="init";
470  if(sss_client_->getResult()->error_code != 0)
471  {
472  std::string saytext = "Init " + comp_name + " failed";
473  std::replace(saytext.begin(), saytext.end(), '_', ' ');
474  say(saytext, true);
475  }
476 
477  ROS_INFO("Recover %s",comp_name.c_str());
478  script_goal_.function_name="recover";
481  if(sss_client_->getResult()->error_code != 0)
482  {
483  std::string saytext = "Recover " + comp_name + " failed";
484  std::replace(saytext.begin(), saytext.end(), '_', ' ');
485  say(saytext, true);
486 
487  }
488  }
489 
490  say("go", true);
491  }
492 
493  //-------MODE 1
494  if (mode_==1)
495  {
496  ROS_DEBUG("Mode 1: Move the base using twist controller");
497  if(dock_button_>=0 && dock_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[dock_button_]==1)
498  {
502  {
503  ROS_DEBUG_STREAM("docking already in progress: "<<state.getText());
505  return;
506  }
507  dock_client_->sendGoal(cob_actions::SetStringGoal());
509  }
510 
511  if(undock_button_>=0 && undock_button_<(int)joy_msg->buttons.size() && joy_msg->buttons[undock_button_]==1)
512  {
516  {
517  ROS_DEBUG_STREAM("undocking already in progress: "<<state.getText());
519  return;
520  }
521  undock_client_->sendGoal(cob_actions::SetStringGoal());
523  }
524 
525  if(axis_vx_>=0 && axis_vx_<(int)joy_msg->axes.size())
526  {
527  joy_active_ = true;
528  vel_req_[0] = joy_msg->axes[axis_vx_]*component_config_["base"].twist_max_vel[0]*run_factor_;
529  }
530  else
531  {
532  vel_req_[0] =0.0;
533  }
534  if(axis_vy_>=0 && axis_vy_<(int)joy_msg->axes.size())
535  {
536  joy_active_ = true;
537  vel_req_[1] = joy_msg->axes[axis_vy_]*component_config_["base"].twist_max_vel[1]*run_factor_;
538  }
539  else
540  {
541  vel_req_[1] = 0.0;
542  }
543  if(axis_yaw_>=0 && axis_yaw_<(int)joy_msg->axes.size())
544  {
545  joy_active_ = true;
546  vel_req_[2] = joy_msg->axes[axis_yaw_]*component_config_["base"].twist_max_vel[2]*run_factor_;
547  }
548  else
549  {
550  vel_req_[2] = 0.0;
551  }
552  }
553 
554  //-------MODE 2
555  if (mode_==2)
556  {
557  ROS_DEBUG("Mode 2: Move the actuators to a default position (Trajectory controller)");
558 
559  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator p=components_.begin();p!=components_.end();++p)
560  {
561  int component_sss_default_target_button_temp = -1;
562  std::string comp_name = p->first;
563  std::string component_sss_default_target_button = comp_name + "_sss_default_target_button";
564  n_.getParam(component_sss_default_target_button,component_sss_default_target_button_temp);
565  if (component_sss_default_target_button_temp == -1)
566  {
567  ROS_DEBUG("%s_sss_default_target_button parameter not defined",comp_name.c_str());
568  }
569  if(component_sss_default_target_button_temp>=0 && component_sss_default_target_button_temp<(int)joy_msg->buttons.size() && joy_msg->buttons[component_sss_default_target_button_temp]==1)
570  {
571  script_goal_.component_name = comp_name;
572  script_goal_.function_name = "move";
573  script_goal_.parameter_name = component_config_[comp_name].sss_default_target.c_str();
574  ROS_INFO("Move %s to %s",comp_name.c_str(), component_config_[comp_name].sss_default_target.c_str());
575  std::string saytext = "Move " + comp_name + " to " + script_goal_.parameter_name;
576  std::replace(saytext.begin(), saytext.end(), '_', ' ');
577  say(saytext, true);
579  }
580  }
581  }
582 
583  //-------MODE 3
584  if (mode_==3)
585  {
586  ROS_DEBUG("Mode 3: Move the actuators using the group velocity controller");
587  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator p=components_.begin();p!=components_.end();++p)
588  {
589  int count = 0;
590  int component_joint_button_temp = -1;
591  std::string comp_name = p->first;
592  int size = ceil((component_config_[comp_name].joint_velocity.size()+1)/2);
593  for(int i=0; i < size; ++i)
594  {
595  std::ostringstream component_joint_button_stream;
596  component_joint_button_stream << comp_name << "_joint" << i+1 << "_button";
597  std::string component_joint_button = component_joint_button_stream.str();
598  n_.getParam(component_joint_button,component_joint_button_temp);
599  std_msgs::Float64MultiArray vel_cmd;
600  count++;
601  if(!(comp_name.find("left") != std::string::npos) && !(comp_name.find("right") != std::string::npos))
602  {
603  if(component_joint_button_temp>=0 && component_joint_button_temp<(int)joy_msg->buttons.size() && joy_msg->buttons[component_joint_button_temp]==1 && joy_msg->buttons[right_indicator_button_]==0 && joy_msg->buttons[left_indicator_button_]==0)
604  {
605  joy_active_ = true;
606  ROS_INFO("%s velocity mode",comp_name.c_str());
607  vel_cmd.data.resize(component_config_[comp_name].joint_velocity.size());
608  if(up_down_button_>=0 && up_down_button_<(int)joy_msg->axes.size())
609  {
610  vel_cmd.data[count+i-1]=joy_msg->axes[up_down_button_]*component_config_[comp_name].joint_velocity[count+i-1];
611  }
612  if((i+1)*2 <= component_config_[comp_name].joint_velocity.size())
613  {
614  if(right_left_button_>=0 && right_left_button_<(int)joy_msg->axes.size())
615  {
616  vel_cmd.data[count+i]=joy_msg->axes[right_left_button_]*component_config_[comp_name].joint_velocity[count+i];
617  }
618  }
619  component_config_[comp_name].vel_group_controller_publisher_.publish(vel_cmd);
620  }
621  }
622  else if(!(comp_name.find("left") != std::string::npos) && (comp_name.find("right") != std::string::npos))
623  {
624  if(component_joint_button_temp>=0 && component_joint_button_temp<(int)joy_msg->buttons.size() && joy_msg->buttons[component_joint_button_temp]==1 && joy_msg->buttons[right_indicator_button_]==1)
625  {
626  joy_active_ = true;
627  ROS_INFO("%s velocity mode",comp_name.c_str());
628  vel_cmd.data.resize(component_config_[comp_name].joint_velocity.size());
629  if(up_down_button_>=0 && up_down_button_<(int)joy_msg->axes.size())
630  {
631  vel_cmd.data[count+i-1]=joy_msg->axes[up_down_button_]*component_config_[comp_name].joint_velocity[count+i-1];
632  }
633  if((i+1)*2 <= component_config_[comp_name].joint_velocity.size())
634  {
635  if(right_left_button_>=0 && right_left_button_<(int)joy_msg->axes.size())
636  {
637  vel_cmd.data[count+i]=joy_msg->axes[right_left_button_]*component_config_[comp_name].joint_velocity[count+i];
638  }
639  }
640  component_config_[comp_name].vel_group_controller_publisher_.publish(vel_cmd);
641  }
642  }
643  else if((comp_name.find("left") != std::string::npos) && !(comp_name.find("right") != std::string::npos))
644  {
645  if(component_joint_button_temp>=0 && component_joint_button_temp<(int)joy_msg->buttons.size() && joy_msg->buttons[component_joint_button_temp]==1 && joy_msg->buttons[left_indicator_button_]==1)
646  {
647  joy_active_ = true;
648  ROS_INFO("%s velocity mode",comp_name.c_str());
649  vel_cmd.data.resize(component_config_[comp_name].joint_velocity.size());
650  if(up_down_button_>=0 && up_down_button_<(int)joy_msg->axes.size())
651  {
652  vel_cmd.data[count+i-1]=joy_msg->axes[up_down_button_]*component_config_[comp_name].joint_velocity[count+i-1];
653  }
654  if((i+1)*2 <= component_config_[comp_name].joint_velocity.size())
655  {
656  if(right_left_button_>=0 && right_left_button_<(int)joy_msg->axes.size())
657  {
658  vel_cmd.data[count+i]=joy_msg->axes[right_left_button_]*component_config_[comp_name].joint_velocity[count+i];
659  }
660  }
661  component_config_[comp_name].vel_group_controller_publisher_.publish(vel_cmd);
662  }
663  }
664  }
665  }
666  }
667 
668  //-------MODE 4
669  if (mode_==4)
670  {
671  ROS_DEBUG("Mode 4: Move the actuators using the twist controller");
672  for(std::map<std::string,XmlRpc::XmlRpcValue>::iterator p=components_.begin();p!=components_.end();++p)
673  {
674  int component_twist_button_temp = -1;
675  std::string comp_name = p->first;
676  std::string component_twist_button = comp_name + "_twist_button";
677  n_.getParam(component_twist_button,component_twist_button_temp);
678  geometry_msgs::Twist twist_cmd;
679  if (component_twist_button_temp>=0 && component_twist_button_temp<(int)joy_msg->buttons.size() && joy_msg->buttons[component_twist_button_temp]==1)
680  {
681  joy_active_ = true;
682  ROS_INFO("%s twist mode",comp_name.c_str());
683  if(axis_vx_>=0 && axis_vx_<(int)joy_msg->axes.size())
684  twist_cmd.linear.x = joy_msg->axes[axis_vx_]*component_config_[comp_name].twist_max_vel[0]; //*run_factor_;
685  else
686  twist_cmd.linear.x =0.0;
687  if(axis_vy_>=0 && axis_vy_<(int)joy_msg->axes.size())
688  twist_cmd.linear.y = joy_msg->axes[axis_vy_]*component_config_[comp_name].twist_max_vel[1]; //*run_factor_;
689  else
690  twist_cmd.linear.y =0.0;
691  if(axis_vz_>=0 && axis_vz_<(int)joy_msg->axes.size())
692  twist_cmd.linear.z = joy_msg->axes[axis_vz_]*component_config_[comp_name].twist_max_vel[2]; //*run_factor_;
693  else
694  twist_cmd.linear.z =0.0;
695  if(axis_roll_>=0 && axis_roll_<(int)joy_msg->axes.size())
696  twist_cmd.angular.x = joy_msg->axes[axis_roll_]*component_config_[comp_name].twist_max_vel[3]; //*run_factor_;
697  else
698  twist_cmd.angular.x =0.0;
699  if(axis_pitch_>=0 && axis_pitch_<(int)joy_msg->axes.size())
700  twist_cmd.angular.y = joy_msg->axes[axis_pitch_]*component_config_[comp_name].twist_max_vel[4]; //*run_factor_;
701  else
702  twist_cmd.angular.y =0.0;
703  if(axis_yaw_>=0 && axis_yaw_<(int)joy_msg->axes.size())
704  twist_cmd.angular.z = joy_msg->axes[axis_yaw_]*component_config_[comp_name].twist_max_vel[5]; //*run_factor_;
705  else
706  twist_cmd.angular.z =0.0;
707  component_config_[comp_name].twist_controller_publisher_.publish(twist_cmd);
708  }
709  }
710  }
711 }
712 
717 {
718  n_ = ros::NodeHandle("~");
719  if(!n_.hasParam("components"))
720  {
721  ROS_ERROR("parameter components does not exist on ROS Parameter Server, aborting...");
722  exit(0);
723  }
724  // common
725  n_.param("publish_freq",publish_freq_, 30.0);
726  n_.param("run_factor",run_factor_param_,1.5);
727  n_.param("apply_ramp",apply_ramp_,true);
728 
729  // joy config
730  n_.param("joy_num_modes",joy_num_modes_,2);
731  n_.param("mode_switch_button",mode_switch_button_,0);
732 
733  // assign axis
734  n_.param("axis_vx",axis_vx_,17);
735  n_.param("axis_vy",axis_vy_,16);
736  n_.param("axis_vz",axis_vz_,17);
737  n_.param("axis_roll",axis_roll_,16);
738  n_.param("axis_pitch",axis_pitch_,19);
739  n_.param("axis_yaw",axis_yaw_,19);
740 
741  // assign buttons
742  n_.param("deadman_button",deadman_button_,11);
743  n_.param("safety_button",safety_button_,10);
744  n_.param("init_button",init_button_,3);
745 
746  n_.param("run_button",run_button_,9);
747  n_.param("dock_button",dock_button_,3);
748  n_.param("undock_button",undock_button_,1);
749 
750  n_.param("right_indicator_button",right_indicator_button_,9);
751  n_.param("left_indicator_button",left_indicator_button_,8);
752  n_.param("up_down_button",up_down_button_,4);
753  n_.param("right_left_button",right_left_button_,5);
754 
755  // output for debugging
756  ROS_DEBUG("init::axis_vx: %d",axis_vx_);
757  ROS_DEBUG("init::axis_vy: %d",axis_vy_);
758  ROS_DEBUG("init::axis_vz: %d",axis_vz_);
759  ROS_DEBUG("init::axis_roll: %d",axis_roll_);
760  ROS_DEBUG("init::axis_pitch: %d",axis_pitch_);
761  ROS_DEBUG("init::axis_yaw: %d",axis_yaw_);
762 
763  ROS_DEBUG("init::deadman_button: %d",deadman_button_);
764  ROS_DEBUG("init::safety_button: %d",safety_button_);
765  ROS_DEBUG("init::init_button: %d",init_button_);
766  ROS_DEBUG("init::run_button: %d",run_button_);
767 
768  ROS_DEBUG("init::right_indicator_button: %d",right_indicator_button_);
769  ROS_DEBUG("init::left_indicator_button: %d",left_indicator_button_);
770  ROS_DEBUG("init::up_down_button: %d",up_down_button_);
771  ROS_DEBUG("init::right_left_button: %d",right_left_button_);
772 
773  joy_sub_ = n_.subscribe("/joy",1,&CobTeleop::joy_cb,this);
774  mode_ = 1;
776  joy_active_ = false;
777  safe_mode_ = true;
778 
779  cob_light::LightMode light;
780  light.mode = cob_light::LightModes::FLASH;
781  light.frequency = 5;
782  light.priority = 12;
783  std_msgs::ColorRGBA color;
784  color.r = 0;
785  color.g = 1;
786  color.b = 0;
787  color.a = 1;
788  light.pulses = 2;
789  light.colors.push_back(color);
790  confirm_light_goal_.mode = light;
791 
792  color.r = 1;
793  color.g = 0;
794  light.pulses = 3;
795  light.colors.clear();
796  light.colors.push_back(color);
797  error_light_goal_.mode = light;
798 }
799 
800 
801 int main(int argc, char** argv)
802 {
803  ros::init(argc, argv, "cob_teleop");
804  CobTeleop cob_teleop;
805  cob_teleop.init();
806  cob_teleop.getConfigurationFromParameters();
807  ros::Rate loop_rate(cob_teleop.publish_freq_); //Hz
808  while(cob_teleop.n_.ok())
809  {
810  cob_teleop.updateBase();
811  ros::spinOnce();
812  loop_rate.sleep();
813  }
814 
815  exit(0);
816  return(0);
817 }
std::string twist_safety_topic_name
Definition: cob_teleop.cpp:43
double publish_freq_
Definition: cob_teleop.cpp:78
double run_factor_param_
Definition: cob_teleop.cpp:79
std::vector< double > joint_velocity
Definition: cob_teleop.cpp:46
std::vector< double > vel_base_
Definition: cob_teleop.cpp:118
ros::NodeHandle n_
Definition: cob_teleop.cpp:87
std::vector< double > vel_req_
Definition: cob_teleop.cpp:117
ros::Publisher twist_controller_publisher_
Definition: cob_teleop.cpp:50
std::string vel_group_topic_name
Definition: cob_teleop.cpp:44
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
bool safe_mode_
Definition: cob_teleop.cpp:77
ros::Publisher twist_safety_controller_publisher_
Definition: cob_teleop.cpp:51
SetLightClient_ * setlight_client_
Definition: cob_teleop.cpp:101
ros::Subscriber joint_states_sub_
Definition: cob_teleop.cpp:89
SayClient_ * say_client_
Definition: cob_teleop.cpp:96
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
int up_down_button_
Definition: cob_teleop.cpp:68
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
int size() const
bool enable_sound_
Definition: cob_teleop.cpp:97
XmlRpcServer s
std::string sound_action_name_
Definition: cob_teleop.cpp:98
bool mode
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int axis_vx_
Definition: cob_teleop.cpp:57
UndockClient_ * undock_client_
Definition: cob_teleop.cpp:110
actionlib::SimpleActionClient< cob_actions::SetStringAction > UndockClient_
Definition: cob_teleop.cpp:109
int run_button_
Definition: cob_teleop.cpp:61
double run_factor_
Definition: cob_teleop.cpp:79
Type const & getType() const
void joy_cb(const sensor_msgs::Joy::ConstPtr &joy_msg)
Definition: cob_teleop.cpp:406
#define ROS_WARN(...)
std::vector< double > twist_max_vel
Definition: cob_teleop.cpp:47
void init()
Initializes node to get parameters, subscribe to topics.
Definition: cob_teleop.cpp:716
int axis_roll_
Definition: cob_teleop.cpp:57
int right_indicator_button_
Definition: cob_teleop.cpp:66
int axis_pitch_
Definition: cob_teleop.cpp:57
void say(std::string text, bool blocking)
Definition: cob_teleop.cpp:371
XmlRpc::XmlRpcValue LEDS_
Definition: cob_teleop.cpp:83
int deadman_button_
Definition: cob_teleop.cpp:73
actionlib::SimpleActionClient< cob_sound::SayAction > SayClient_
Definition: cob_teleop.cpp:95
int undock_button_
Definition: cob_teleop.cpp:63
void setLight(int mode)
Definition: cob_teleop.cpp:385
actionlib::SimpleActionClient< cob_actions::SetStringAction > DockClient_
Definition: cob_teleop.cpp:105
actionlib::SimpleActionClient< cob_light::SetLightModeAction > SetLightClient_
Definition: cob_teleop.cpp:100
ros::Publisher vel_group_controller_publisher_
Definition: cob_teleop.cpp:49
ScriptClient_ * sss_client_
Definition: cob_teleop.cpp:92
#define ROS_INFO(...)
void updateBase()
Definition: cob_teleop.cpp:313
bool param(const std::string &param_name, T &param_val, const T &default_val) const
cob_light::SetLightModeGoal error_light_goal_
Definition: cob_teleop.cpp:114
std::string undock_action_name_
Definition: cob_teleop.cpp:111
std::vector< double > vel_old_
Definition: cob_teleop.cpp:116
std::string getText() const
std::vector< double > twist_max_acc
Definition: cob_teleop.cpp:48
std::string dock_action_name_
Definition: cob_teleop.cpp:107
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int axis_vz_
Definition: cob_teleop.cpp:57
int main(int argc, char **argv)
Definition: cob_teleop.cpp:801
#define ROS_DEBUG_STREAM(args)
cob_script_server::ScriptGoal script_goal_
Definition: cob_teleop.cpp:93
int axis_vy_
Definition: cob_teleop.cpp:57
bool apply_ramp_
Definition: cob_teleop.cpp:119
int joy_num_modes_
Definition: cob_teleop.cpp:80
bool sleep()
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
actionlib::SimpleActionClient< cob_script_server::ScriptAction > ScriptClient_
Definition: cob_teleop.cpp:91
sensor_msgs::JoyFeedbackArray switch_mode()
Definition: cob_teleop.cpp:266
DockClient_ * dock_client_
Definition: cob_teleop.cpp:106
int init_button_
Definition: cob_teleop.cpp:75
bool getParam(const std::string &key, std::string &s) const
std::map< std::string, component_config > component_config_
Definition: cob_teleop.cpp:54
std::string light_action_name_
Definition: cob_teleop.cpp:103
cob_light::SetLightModeGoal confirm_light_goal_
Definition: cob_teleop.cpp:113
int dock_button_
Definition: cob_teleop.cpp:62
ros::Subscriber joy_sub_
Definition: cob_teleop.cpp:88
int right_left_button_
Definition: cob_teleop.cpp:69
SimpleClientGoalState getState() const
bool ok() const
bool hasParam(const std::string &key) const
ResultConstPtr getResult() const
#define ROS_ASSERT(cond)
int left_indicator_button_
Definition: cob_teleop.cpp:67
bool enable_light_
Definition: cob_teleop.cpp:102
int axis_yaw_
Definition: cob_teleop.cpp:57
ROSCPP_DECL void spinOnce()
XmlRpc::XmlRpcValue components_
Definition: cob_teleop.cpp:86
int mode_switch_button_
Definition: cob_teleop.cpp:81
#define ROS_ERROR(...)
void getConfigurationFromParameters()
Definition: cob_teleop.cpp:130
int safety_button_
Definition: cob_teleop.cpp:74
XmlRpc::XmlRpcValue led_mode_
Definition: cob_teleop.cpp:84
bool joy_active_
Definition: cob_teleop.cpp:76
#define ROS_DEBUG(...)


cob_teleop
Author(s): Florian Weisshardt, Maximilian Sieber
autogenerated on Wed Apr 7 2021 03:03:13