pr2_teleop_general_joystick.cpp
Go to the documentation of this file.
1 /*
2  * pr2_teleop_general_joystick
3  * Copyright (c) 2010, Willow Garage, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the <ORGANIZATION> nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 // Author: E. Gil Jones
32 
33 #include <termios.h>
34 #include <signal.h>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <unistd.h>
38 #include <math.h>
39 
40 #include <ros/ros.h>
41 
42 #include <sensor_msgs/Joy.h>
44 
52 };
53 
54 static const unsigned int BODY_LAYOUT_BUTTON = 10;
55 static const unsigned int RIGHT_ARM_LAYOUT_BUTTON = 9;
56 static const unsigned int LEFT_ARM_LAYOUT_BUTTON = 8;
57 static const unsigned int HEAD_LAYOUT_BUTTON = 11;
58 
59 static const unsigned int HEAD_MODE_TOGGLE_BUTTON = 4;
60 static const unsigned int PROJECTOR_TOGGLE_BUTTON = 5;
61 static const unsigned int LASER_TOGGLE_BUTTON = 7;
62 static const unsigned int PROSILICA_POLL_BUTTON = 6;
63 static const unsigned int OPEN_GRIPPER_BUTTON = 15;
64 static const unsigned int CLOSE_GRIPPER_BUTTON = 13;
65 static const unsigned int ARM_MODE_TOGGLE_BUTTON = 4;
66 static const unsigned int ARM_POSE_BUTTON = 6;
67 
68 static const unsigned int LEFT_AXIS_NUMBER = 1;
69 static const unsigned int RIGHT_AXIS_NUMBER = 1;
70 
71 static const unsigned int TORSO_UP_BUTTON = 12;
72 static const unsigned int TORSO_DOWN_BUTTON = 14;
73 static const unsigned int SPEED_UP_BUTTON = 11;
74 
75 static const unsigned int WRIST_CLOCKWISE_BUTTON = 12;
76 static const unsigned int WRIST_COUNTER_BUTTON = 14;
77 
78 static const unsigned int VX_AXIS = 3;
79 static const unsigned int VY_AXIS = 2;
80 static const unsigned int VW_AXIS = 0;
81 
82 static const unsigned int HEAD_PAN_AXIS = 0;
83 static const unsigned int HEAD_TILT_AXIS = 3;
84 
85 static const unsigned int ARM_X_AXIS = 3;
86 static const unsigned int ARM_Y_AXIS = 2;
87 static const unsigned int ARM_Z_AXIS = 1;
88 static const unsigned int ARM_YAW_AXIS = 0;
89 
90 static const unsigned int ARM_UNTUCK_BUTTON = 7;
91 static const unsigned int ARM_TUCK_BUTTON = 5;
92 
93 static const unsigned int MOVE_TO_WALK_ALONG_BUTTON = 0;
94 static const unsigned int SET_WALK_ALONG_BUTTON = 3;
95 
96 static const ros::Duration DOUBLE_TAP_TIMEOUT(.25);
97 
99 {
100 public:
101  Pr2TeleopGeneralJoystick(bool deadman_no_publish = false)
102  {
103  gc = NULL;
104  }
105 
106  void init()
107  {
110 
111  des_torso_pos_ = 0;
112 
113  des_torso_vel_ = 0.0;
114 
115  ros::NodeHandle n_local("~");
116 
117  // Head pan/tilt parameters
118  n_local.param("max_pan", max_pan_, 2.7);
119  n_local.param("max_tilt", max_tilt_, 1.4);
120  n_local.param("min_tilt", min_tilt_, -0.4);
121 
122  n_local.param("tilt_scale", tilt_scale_, .5);
123  n_local.param("pan_scale", pan_scale_, 1.2);
124 
125  n_local.param("torso_step", torso_step_, 0.05);
126  n_local.param("min_torso", min_torso_, 0.0);
127  n_local.param("max_torso", max_torso_, 0.3);
128 
129  n_local.param("vx_scale", vx_scale_, 0.6);
130  n_local.param("vy_scale", vy_scale_, 0.6);
131  n_local.param("vw_scale", vw_scale_, 0.8);
132 
133  n_local.param("arm_x_scale", arm_x_scale_, .15);
134  n_local.param("arm_y_scale", arm_y_scale_, .15);
135  n_local.param("arm_z_scale", arm_z_scale_, .15);
136 
137  n_local.param("arm_roll_scale", arm_roll_scale_, -1.50);
138  n_local.param("arm_pitch_scale", arm_pitch_scale_, 1.50);
139  n_local.param("arm_yaw_scale", arm_yaw_scale_, 1.50);
140 
141  n_local.param("wrist_velocity",wrist_velocity_, 4.5);
142 
143  n_local.param("walk_along_x_speed_scale", walk_along_x_speed_scale_, 9.0);
144  n_local.param("walk_along_y_speed_scale", walk_along_y_speed_scale_, 9.0);
145  n_local.param("walk_along_w_speed_scale", walk_along_w_speed_scale_, 9.0);
146  n_local.param("walk_along_thresh", walk_along_thresh_, .015);
147  n_local.param("walk_along_x_dist_max", walk_along_x_dist_max_, .5);
148  n_local.param("walk_along_y_dist_max", walk_along_y_dist_max_, .5);
149 
150  n_local.param("prosilica_namespace", prosilica_namespace_, std::string("prosilica_polled"));
151 
152  bool control_prosilica;
153  n_local.param("control_prosilica", control_prosilica, true);
154 
155  bool control_body;
156  n_local.param("control_body", control_body, true);
157 
158  bool control_larm;
159  n_local.param("control_larm", control_larm, true);
160 
161  bool control_rarm;
162  n_local.param("control_rarm", control_rarm, true);
163 
164  bool control_head;
165  n_local.param("control_head", control_head, true);
166 
167  std::string arm_controller_name;
168  n_local.param("arm_controller_name", arm_controller_name,std::string(""));
169 
170  ROS_DEBUG("tilt scale: %.3f rad\n", tilt_scale_);
171  ROS_DEBUG("pan scale: %.3f rad\n", pan_scale_);
172 
173  ROS_INFO("Initing general commander");
174 
175  if(arm_controller_name.empty()) {
176  gc = new GeneralCommander(control_body,
177  control_head,
178  control_rarm,
179  control_larm,
180  control_prosilica);
181  } else {
182  gc = new GeneralCommander(control_body,
183  control_head,
184  control_rarm,
185  control_larm,
186  control_prosilica,
187  arm_controller_name);
188  }
189  first_callback_ = true;
190 
191  head_init_ = false;
192  torso_init_ = false;
193 
194  proj_toggle_com_ = false;
195 
196  walk_along_init_waiting_ = false;
197  set_walk_along_mode_ = false;
198 
200  }
201 
202 
204  if(gc != NULL) {
205  delete gc;
206  }
207  }
208 
209  bool buttonOkAndOn(unsigned int buttonNum, const sensor_msgs::Joy::ConstPtr& joy_msg) const {
210  if(buttonNum >= joy_msg->buttons.size()) return false;
211  return(joy_msg->buttons[buttonNum]);
212  }
213 
214  bool axisOk(unsigned int axisNum, const sensor_msgs::Joy::ConstPtr& joy_msg) const {
215  return (axisNum < joy_msg->axes.size());
216  }
217 
218  bool sameValueAsLast(unsigned int button,
219  const sensor_msgs::Joy::ConstPtr& new_msg,
220  const sensor_msgs::Joy::ConstPtr& old_msg) {
221  return (buttonOkAndOn(button, new_msg) == buttonOkAndOn(button, old_msg));
222  }
223 
224 
225  void joy_cb(const sensor_msgs::Joy::ConstPtr& joy_msg)
226  {
227  if(first_callback_) {
228  last_joy_ = joy_msg;
229  first_callback_ = false;
230  }
231 
232  JoystickLayoutMode layout;
233 
234  if(buttonOkAndOn(BODY_LAYOUT_BUTTON, joy_msg)) {
235  layout = LAYOUT_BODY;
237  layout = LAYOUT_BOTH_ARMS;
238  } else if (buttonOkAndOn(RIGHT_ARM_LAYOUT_BUTTON,joy_msg)) {
239  layout = LAYOUT_RIGHT_ARM;
240  } else if (buttonOkAndOn(LEFT_ARM_LAYOUT_BUTTON, joy_msg)) {
241  layout = LAYOUT_LEFT_ARM;
242  } else if (buttonOkAndOn(HEAD_LAYOUT_BUTTON, joy_msg)) {
243  layout = LAYOUT_HEAD;
244  } else {
245  layout = LAYOUT_NONE;
246  }
247 
248  bool setting_walk_along_this_cycle_ = false;
249 
250  if(layout == LAYOUT_HEAD) {
252  ros::Time now = ros::Time::now();
254  //only matters if this is off
255  if(!gc->isWalkAlongOk()) {
256  set_walk_along_mode_ = true;
257  setting_walk_along_this_cycle_ = true;
258  }
259  }
260  if(gc->isWalkAlongOk()) {
261  gc->turnOffWalkAlong();
262  ROS_INFO("Turning off walk along");
263  } else {
264  last_walk_along_time_ = now;
265  }
266  }
267  }
268 
269  bool in_walk_along = false;
270  if(gc->isWalkAlongOk()) {
272  gc->turnOffWalkAlong();
273  ROS_INFO("Turning off walk along");
274  } else {
275  vel_val_pan_ = 0.0;
276  vel_val_tilt_ = 0.0;
277  des_torso_vel_ = 0.0;
278  des_vx_ = 0.0;
279  des_vy_ = 0.0;
280  des_vw_ = 0.0;
281  des_right_wrist_vel_ = 0.0;
282  right_arm_vx_ = 0.0;
283  right_arm_vy_ = 0.0;
284  right_arm_vz_ = 0.0;
285  des_left_wrist_vel_ = 0.0;
286  left_arm_vx_ = 0.0;
287  left_arm_vy_ = 0.0;
288  left_arm_vz_ = 0.0;
289  right_arm_vel_roll_ = 0.0;
290  right_arm_vel_pitch_ = 0.0;
291  right_arm_vel_yaw_ = 0.0;
292  left_arm_vel_roll_ = 0.0;
293  left_arm_vel_pitch_ = 0.0;
294  left_arm_vel_yaw_ = 0.0;
295  in_walk_along = true;
296  }
297  }
298 
299  //we must be moving the arms into the mode
300  if(!in_walk_along && layout == LAYOUT_HEAD && set_walk_along_mode_) {
304  }
305  if(!setting_walk_along_this_cycle_ && buttonOkAndOn(MOVE_TO_WALK_ALONG_BUTTON, joy_msg) && !sameValueAsLast(MOVE_TO_WALK_ALONG_BUTTON, joy_msg, last_joy_)) {
306  set_walk_along_mode_ = false;
307  ROS_INFO("No longer waiting for set");
308  }
309  }
310 
311  if(!in_walk_along && layout == LAYOUT_HEAD && walk_along_init_waiting_) {
314  bool ok = gc->initWalkAlong();
315  if(!ok) {
317  } else {
319  ROS_INFO("Should be in walk along");
320  walk_along_init_waiting_ = false;
321  }
322  }
324  walk_along_init_waiting_ = false;
325  ROS_INFO("No longer waiting for init");
326  }
327  }
328  if(layout == LAYOUT_RIGHT_ARM || layout == LAYOUT_LEFT_ARM || layout == LAYOUT_BOTH_ARMS) {
329  if(buttonOkAndOn(CLOSE_GRIPPER_BUTTON, joy_msg)
331  if(layout == LAYOUT_RIGHT_ARM) {
333  } else if(layout == LAYOUT_LEFT_ARM) {
335  } else {
337  }
338  }
339  if(buttonOkAndOn(OPEN_GRIPPER_BUTTON, joy_msg)
341  if(layout == LAYOUT_RIGHT_ARM) {
343  } else if(layout == LAYOUT_LEFT_ARM) {
345  } else {
347  }
348  }
349  }
350 
351  if(!in_walk_along && layout == LAYOUT_HEAD) {
352 
357  }
358 
362  }
363 
366 
368  ROS_DEBUG("Head mode to left");
372  ROS_DEBUG("Head mode to right");
375  ROS_DEBUG("Head mode to mannequin");
376  } else {
377  ROS_DEBUG("Head mode to joystick");
378  head_init_ = false;
380  }
381  }
382 
383  if(buttonOkAndOn(LASER_TOGGLE_BUTTON, joy_msg)
389  } else {
391  }
392  }
393 
394  if(axisOk(HEAD_PAN_AXIS, joy_msg))
395  {
396  vel_val_pan_ = joy_msg->axes[HEAD_PAN_AXIS] * pan_scale_;
397  }
398 
399  if(axisOk(HEAD_TILT_AXIS, joy_msg))
400  {
401  vel_val_tilt_ = joy_msg->axes[HEAD_TILT_AXIS] * tilt_scale_;
402  }
403  } else {
404  vel_val_pan_ = 0.0;
405  vel_val_tilt_ = 0.0;
406  }
407 
408  if(!in_walk_along && layout == LAYOUT_BODY) {
409  bool down = buttonOkAndOn(TORSO_DOWN_BUTTON, joy_msg);
410  bool up = buttonOkAndOn(TORSO_UP_BUTTON, joy_msg);
411  bool speedup = buttonOkAndOn(SPEED_UP_BUTTON, joy_msg);
412  bool unsafe = buttonOkAndOn(RIGHT_ARM_LAYOUT_BUTTON, joy_msg); // for jsk_pr2_startup
413 
414  //ROS_INFO_STREAM("Down is " << down);
415  //ROS_INFO_STREAM("Up is " << up);
416 
417  if(down && !up) {
419  } else if(!down && up) {
421  } else {
422  //ROS_INFO_STREAM("Setting des vel to 0.0");
423  des_torso_vel_ = 0.0;
424  }
425  if(axisOk(VX_AXIS, joy_msg)) {
426  des_vx_ = joy_msg->axes[VX_AXIS]*vx_scale_*(unsafe?0.5:(speedup?2.0:1.0));
427  } else {
428  des_vx_ = 0.0;
429  }
430  if(axisOk(VY_AXIS, joy_msg)) {
431  des_vy_ = joy_msg->axes[VY_AXIS]*vy_scale_*(unsafe?0.5:(speedup?2.0:1.0));
432  } else {
433  des_vy_ = 0.0;
434  }
435  if(axisOk(VW_AXIS, joy_msg)) {
436  des_vw_ = joy_msg->axes[VW_AXIS]*vw_scale_*(unsafe?0.5:(speedup?2.0:1.0));
437  } else {
438  des_vw_ = 0.0;
439  }
440  } else {
441  des_torso_vel_ = 0.0;
442  des_vx_ = 0.0;
443  des_vy_ = 0.0;
444  des_vw_ = 0.0;
445  }
446 
447  if(layout == LAYOUT_RIGHT_ARM) {
449  if(in_walk_along) {
450  gc->turnOffWalkAlong();
451  ROS_INFO("Turning off walk along");
452  }
457  } else {
459  }
460  }
461 
463  if(in_walk_along) {
464  gc->turnOffWalkAlong();
465  ROS_INFO("Turning off walk along");
466  }
468  } else if(buttonOkAndOn(ARM_UNTUCK_BUTTON, joy_msg) && !sameValueAsLast(ARM_UNTUCK_BUTTON, joy_msg, last_joy_)) {
469  if(in_walk_along) {
470  gc->turnOffWalkAlong();
471  ROS_INFO("Turning off walk along");
472  }
474  }
475 
476  if(!in_walk_along) {
477 
478  bool lookAnalog = false;
479  bool rotClock = buttonOkAndOn(WRIST_CLOCKWISE_BUTTON, joy_msg);
480  bool rotCounter = buttonOkAndOn(WRIST_COUNTER_BUTTON, joy_msg);
481  bool rotateArm = buttonOkAndOn(ARM_POSE_BUTTON, joy_msg);
482  if(rotClock && !rotCounter) {
484  } else if(!rotClock && rotCounter) {
486  } else {
487  des_right_wrist_vel_ = 0.0;
488  lookAnalog = true;
489  }
490 
491  right_arm_vx_ = 0.0;
492  right_arm_vy_ = 0.0;
493  right_arm_vz_ = 0.0;
494  right_arm_vel_roll_ = 0.0;
495  right_arm_vel_pitch_ = 0.0;
496  right_arm_vel_yaw_ = 0.0;
497 
498  if(lookAnalog) {
499  //look at analog sticks if we aren't supposed to wrist rotate
500  if(axisOk(ARM_X_AXIS, joy_msg)) {
501  if(!rotateArm)
502  right_arm_vx_ = joy_msg->axes[ARM_X_AXIS]*arm_x_scale_;
503  else
505  }
506  if(axisOk(ARM_Y_AXIS, joy_msg)) {
507  if(!rotateArm)
508  right_arm_vy_ = joy_msg->axes[ARM_Y_AXIS]*arm_y_scale_;
509  else
511  }
512  if(axisOk(ARM_Z_AXIS, joy_msg) && (!rotateArm)) {
513  right_arm_vz_ = joy_msg->axes[ARM_Z_AXIS]*arm_z_scale_;
514  }
515  if(axisOk(ARM_YAW_AXIS, joy_msg) && rotateArm) {
517  }
518  //ROS_INFO_STREAM("Setting vx " << right_arm_vx_ << " " << right_arm_vy_ << " " << right_arm_vz_);
519  }
520  }
521  } else if (layout != LAYOUT_BOTH_ARMS) {
522  des_right_wrist_vel_ = 0.0;
523  right_arm_vx_ = 0.0;
524  right_arm_vy_ = 0.0;
525  right_arm_vz_ = 0.0;
526  right_arm_vel_roll_ = 0.0;
527  right_arm_vel_pitch_ = 0.0;
528  right_arm_vel_yaw_ = 0.0;
529  }
530  if(layout == LAYOUT_LEFT_ARM) {
532  if(in_walk_along) {
533  gc->turnOffWalkAlong();
534  ROS_INFO("Turning off walk along");
535  }
540  } else {
542  }
543  }
544 
546  if(in_walk_along) {
547  gc->turnOffWalkAlong();
548  ROS_INFO("Turning off walk along");
549  }
551  } else if(buttonOkAndOn(ARM_UNTUCK_BUTTON, joy_msg) && !sameValueAsLast(ARM_UNTUCK_BUTTON, joy_msg, last_joy_)) {
552  if(in_walk_along) {
553  gc->turnOffWalkAlong();
554  ROS_INFO("Turning off walk along");
555  }
557  }
558 
559  if(!in_walk_along) {
560  bool lookAnalog = false;
561  bool rotClock = buttonOkAndOn(WRIST_CLOCKWISE_BUTTON, joy_msg);
562  bool rotCounter = buttonOkAndOn(WRIST_COUNTER_BUTTON, joy_msg);
563  bool rotateArm = buttonOkAndOn(ARM_POSE_BUTTON, joy_msg);
564  if(rotClock && !rotCounter) {
566  } else if(!rotClock && rotCounter) {
568  } else {
569  des_left_wrist_vel_ = 0.0;
570  lookAnalog = true;
571  }
572 
573  left_arm_vx_ = 0.0;
574  left_arm_vy_ = 0.0;
575  left_arm_vz_ = 0.0;
576  left_arm_vel_roll_ = 0.0;
577  left_arm_vel_pitch_ = 0.0;
578  left_arm_vel_yaw_ = 0.0;
579 
580  if(lookAnalog) {
581  //look at analog sticks if we aren't supposed to wrist rotate
582  if(axisOk(ARM_X_AXIS, joy_msg)) {
583  if(!rotateArm)
584  left_arm_vx_ = joy_msg->axes[ARM_X_AXIS]*arm_x_scale_;
585  else
587  }
588  if(axisOk(ARM_Y_AXIS, joy_msg)) {
589  if(!rotateArm)
590  left_arm_vy_ = joy_msg->axes[ARM_Y_AXIS]*arm_y_scale_;
591  else
593  }
594  if(axisOk(ARM_Z_AXIS, joy_msg) && (!rotateArm)) {
595  left_arm_vz_ = joy_msg->axes[ARM_Z_AXIS]*arm_z_scale_;
596  }
597  if(axisOk(ARM_YAW_AXIS, joy_msg) && rotateArm) {
599  }
600  //ROS_INFO_STREAM("Setting vx " << left_arm_vx_ << " " << left_arm_vy_ << " " << left_arm_vz_);
601  }
602  }
603  } else if (layout != LAYOUT_BOTH_ARMS) {
604  des_left_wrist_vel_ = 0.0;
605  left_arm_vx_ = 0.0;
606  left_arm_vy_ = 0.0;
607  left_arm_vz_ = 0.0;
608  left_arm_vel_roll_ = 0.0;
609  left_arm_vel_pitch_ = 0.0;
610  left_arm_vel_yaw_ = 0.0;
611  }
612  if(layout == LAYOUT_BOTH_ARMS) {
615  if(in_walk_along) {
616  gc->turnOffWalkAlong();
617  ROS_INFO("Turning off walk along");
618  }
625  } else {
627  }
629  }
630 
632  if(in_walk_along) {
633  gc->turnOffWalkAlong();
634  ROS_INFO("Turning off walk along");
635  }
637  } else if(buttonOkAndOn(ARM_UNTUCK_BUTTON, joy_msg) && !sameValueAsLast(ARM_UNTUCK_BUTTON, joy_msg, last_joy_)) {
638  if(in_walk_along) {
639  gc->turnOffWalkAlong();
640  ROS_INFO("Turning off walk along");
641  }
643  }
644 
645  if(!in_walk_along) {
646  bool lookAnalog = false;
647  bool rotClock = buttonOkAndOn(WRIST_CLOCKWISE_BUTTON, joy_msg);
648  bool rotCounter = buttonOkAndOn(WRIST_COUNTER_BUTTON, joy_msg);
649  bool rotateArm = buttonOkAndOn(ARM_POSE_BUTTON, joy_msg);
650  if(rotClock && !rotCounter) {
653  } else if(!rotClock && rotCounter) {
656  } else {
657  des_left_wrist_vel_ = 0.0;
658  des_right_wrist_vel_ = 0.0;
659  lookAnalog = true;
660  }
661 
662  left_arm_vx_ = 0.0;
663  left_arm_vy_ = 0.0;
664  left_arm_vz_ = 0.0;
665  right_arm_vx_ = 0.0;
666  right_arm_vy_ = 0.0;
667  right_arm_vz_ = 0.0;
668  left_arm_vel_roll_ = 0.0;
669  left_arm_vel_pitch_ = 0.0;
670  left_arm_vel_yaw_ = 0.0;
671  right_arm_vel_roll_ = 0.0;
672  right_arm_vel_pitch_ = 0.0;
673  right_arm_vel_yaw_ = 0.0;
674 
675  if(lookAnalog) {
676  //look at analog sticks if we aren't supposed to wrist rotate
677  if(axisOk(ARM_X_AXIS, joy_msg)) {
678  if(!rotateArm) {
679  left_arm_vx_ = joy_msg->axes[ARM_X_AXIS]*arm_x_scale_;
680  right_arm_vx_ = joy_msg->axes[ARM_X_AXIS]*arm_x_scale_;
681  } else {
684  }
685  }
686  if(axisOk(ARM_Y_AXIS, joy_msg)) {
687  if(!rotateArm) {
688  left_arm_vy_ = joy_msg->axes[ARM_Y_AXIS]*arm_y_scale_;
689  right_arm_vy_ = joy_msg->axes[ARM_Y_AXIS]*arm_y_scale_;
690  } else {
693  }
694  }
695  if(axisOk(ARM_Z_AXIS, joy_msg) && (!rotateArm)) {
696  left_arm_vz_ = joy_msg->axes[ARM_Z_AXIS]*arm_z_scale_;
697  right_arm_vz_ = joy_msg->axes[ARM_Z_AXIS]*arm_z_scale_;
698  }
699  if(axisOk(ARM_YAW_AXIS, joy_msg) && rotateArm) {
702  }
703  //ROS_INFO_STREAM("Setting vx " << left_arm_vx_ << " " << left_arm_vy_ << " " << left_arm_vz_);
704  }
705  }
706  } else if (layout != LAYOUT_RIGHT_ARM && layout != LAYOUT_LEFT_ARM) {
707  des_right_wrist_vel_ = 0.0;
708  des_left_wrist_vel_ = 0.0;
709  left_arm_vx_ = 0.0;
710  left_arm_vy_ = 0.0;
711  left_arm_vz_ = 0.0;
712  right_arm_vx_ = 0.0;
713  right_arm_vy_ = 0.0;
714  right_arm_vz_ = 0.0;
715  left_arm_vel_roll_ = 0.0;
716  left_arm_vel_pitch_ = 0.0;
717  left_arm_vel_yaw_ = 0.0;
718  right_arm_vel_roll_ = 0.0;
719  right_arm_vel_pitch_ = 0.0;
720  right_arm_vel_yaw_ = 0.0;
721  }
722 
724  last_joy_ = joy_msg;
725  }
726 
728  if(!torso_init_) {
729  double cur_torso_pos = 0.0;
730  bool ok = gc->getJointPosition("torso_lift_joint", cur_torso_pos);
731  if(!ok) return false;
732  des_torso_pos_ = cur_torso_pos;
733  torso_init_ = true;
734  }
735  double dt = 1.0/double(hz);
736  double horizon = dt*5.0;
737  double cur_torso_pos = 0.0;
738  gc->getJointPosition("torso_lift_joint", cur_torso_pos);
739  des_torso_pos_ = cur_torso_pos+des_torso_vel_ * horizon;
742  return true;
743  }
744 
746 
747  if(!head_init_) {
748  double cur_pan_pos = 0.0;
749  double cur_tilt_pos = 0.0;
750  bool ok = gc->getJointPosition("head_pan_joint", cur_pan_pos);
751  if(!ok) return false;
752  ok = gc->getJointPosition("head_tilt_joint", cur_tilt_pos);
753  if(!ok) return false;
754  des_pan_pos_ = cur_pan_pos;
755  des_tilt_pos_ = cur_tilt_pos;
756  head_init_ = true;
757  }
758  if(fabs(vel_val_pan_) > .0001) {
760  des_pan_pos_ = std::min(des_pan_pos_, max_pan_);
761  des_pan_pos_ = std::max(des_pan_pos_, -max_pan_);
762  }
763  if(fabs(vel_val_tilt_) > .0001) {
765  des_tilt_pos_ = std::min(des_tilt_pos_, max_tilt_);
766  des_tilt_pos_ = std::max(des_tilt_pos_, min_tilt_);
767  }
768  //ROS_INFO_STREAM("Des pan pos " << des_pan_pos_ << " tilt " << des_tilt_pos_);
769  return true;
770  }
771 
772 public:
776 
777  double des_pan_pos_;
779 
780  double vel_val_pan_;
782 
783  double des_vx_;
784  double des_vy_;
785  double des_vw_;
786 
787  double vx_scale_;
788  double vy_scale_;
789  double vw_scale_;
790 
791  double arm_x_scale_;
792  double arm_y_scale_;
793  double arm_z_scale_;
797 
801 
802  double left_arm_vx_;
803  double left_arm_vy_;
804  double left_arm_vz_;
805 
809 
813 
816 
819 
822  double torso_step_;
823  double min_torso_;
824  double max_torso_;
825 
829 
836 
839 
840  std::string prosilica_namespace_;
841 
843 
847 
849 
851 
852  sensor_msgs::JoyConstPtr last_joy_;
854 
857 
861 
863 
864 };
865 
866 static const double FastHz = 100;
867 static const double SlowHz = 20;
868 
870  ros::spin();
871 }
872 
873 // void quit(int sig)
874 // {
875 // delete generaljoy;
876 // ros::shutdown();
877 // spin_thread.join();
878 // exit(0);
879 // }
880 
881 int main(int argc, char **argv)
882 {
883  ros::init(argc, argv, "pr2_telep_general_joystick", ros::init_options::NoSigintHandler);
884  //signal(SIGINT,quit);
885 
886  boost::thread spin_thread(boost::bind(&spin_function));
887 
888  Pr2TeleopGeneralJoystick generaljoy;
889  generaljoy.init();
890 
891  ros::Rate pub_rate(FastHz);
892 
893  unsigned int counter_limit = (unsigned int)(FastHz/SlowHz);
894 
895  unsigned int counter = 0;
896 
897  ros::Time beforeCall = ros::Time::now();
898  ros::Time afterCall = ros::Time::now();
899  while (ros::ok())
900  {
901  //ROS_INFO_STREAM("Time since last " << (ros::Time::now()-beforeCall).toSec());
902  beforeCall = ros::Time::now();
903 
904  if(!generaljoy.gc->isWalkAlongOk() && !generaljoy.set_walk_along_mode_ && !generaljoy.walk_along_init_waiting_) {
905  if(generaljoy.convertCurrentVelToDesiredHeadPos(FastHz)) {
906  generaljoy.gc->sendHeadCommand(generaljoy.des_pan_pos_, generaljoy.des_tilt_pos_);
907  }
908  generaljoy.gc->sendHeadTrackCommand();
909  generaljoy.gc->sendBaseCommand(generaljoy.des_vx_, generaljoy.des_vy_, generaljoy.des_vw_);
910  }
911 
912  if((counter % counter_limit) == 0) {
913 
914  counter = 0;
915 
916  if(generaljoy.set_walk_along_mode_) {
917  bool ok = generaljoy.gc->moveToWalkAlongArmPose();
918  //if we didn't get a select while moving the arms
919  if(ok && generaljoy.set_walk_along_mode_) {
920  ROS_INFO("Arms in walk position");
921  generaljoy.walk_along_init_waiting_ = true;
922  } else {
923  ROS_INFO("Arms not in walk position");
924  }
925  generaljoy.set_walk_along_mode_ = false;
926  }
927 
928  if(generaljoy.gc->isWalkAlongOk()) {
929 
930  generaljoy.gc->sendWalkAlongCommand(generaljoy.walk_along_thresh_,
931  generaljoy.walk_along_x_dist_max_,
932  generaljoy.walk_along_x_speed_scale_,
933  generaljoy.walk_along_y_dist_max_,
934  generaljoy.walk_along_y_speed_scale_,
935  generaljoy.walk_along_w_speed_scale_);
936  } else {
938  generaljoy.gc->sendTorsoCommand(generaljoy.des_torso_pos_, generaljoy.des_torso_vel_);
939  }
940  //generaljoy.gc->updateCurrentWristPositions();
941  generaljoy.gc->sendWristVelCommands(generaljoy.des_right_wrist_vel_, generaljoy.des_left_wrist_vel_, SlowHz);
942 
943  generaljoy.gc->sendArmVelCommands(generaljoy.right_arm_vx_, generaljoy.right_arm_vy_, generaljoy.right_arm_vz_, generaljoy.right_arm_vel_roll_, generaljoy.right_arm_vel_pitch_, generaljoy.right_arm_vel_yaw_,
944  generaljoy.left_arm_vx_, generaljoy.left_arm_vy_, generaljoy.left_arm_vz_, generaljoy.left_arm_vel_roll_, generaljoy.left_arm_vel_pitch_, generaljoy.left_arm_vel_yaw_,
945  SlowHz);
946  }
947  }
948  //ROS_INFO_STREAM("Everything took " << (afterCall-beforeCall).toSec());
949  counter++;
950  pub_rate.sleep();
951  }
952 
953  ros::shutdown();
954  spin_thread.join();
955  return 0;
956 }
void sendWalkAlongCommand(double thresh, double x_dist_max, double x_speed_scale, double y_dist_max, double y_speed_scale, double rot_scale)
static const unsigned int HEAD_LAYOUT_BUTTON
static const unsigned int RIGHT_AXIS_NUMBER
static const unsigned int ARM_POSE_BUTTON
bool buttonOkAndOn(unsigned int buttonNum, const sensor_msgs::Joy::ConstPtr &joy_msg) const
static const unsigned int SET_WALK_ALONG_BUTTON
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void setHeadMode(HeadControlMode mode)
static const unsigned int CLOSE_GRIPPER_BUTTON
static const unsigned int LASER_TOGGLE_BUTTON
static const unsigned int ARM_MODE_TOGGLE_BUTTON
void sendHeadSequence(HeadSequence seq)
void sendArmVelCommands(double r_x_vel, double r_y_vel, double r_z_vel, double r_roll_vel, double r_pitch_vel, double r_yaw_vel, double l_x_vel, double l_y_vel, double l_z_vel, double l_roll_vel, double l_pitch_vel, double l_yaw_vel, double hz)
void setArmMode(WhichArm which, ArmControlMode mode)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
static const unsigned int HEAD_PAN_AXIS
int main(int argc, char **argv)
static const unsigned int RIGHT_ARM_LAYOUT_BUTTON
static const unsigned int SPEED_UP_BUTTON
void sendWristVelCommands(double right_wrist_vel, double left_wrist_vel, double hz)
ROSCPP_DECL void spin(Spinner &spinner)
static const unsigned int VY_AXIS
static const unsigned int TORSO_DOWN_BUTTON
static const unsigned int ARM_Y_AXIS
static const ros::Duration DOUBLE_TAP_TIMEOUT(.25)
static const double SlowHz
static const unsigned int PROSILICA_POLL_BUTTON
void spin_function()
static const unsigned int OPEN_GRIPPER_BUTTON
bool axisOk(unsigned int axisNum, const sensor_msgs::Joy::ConstPtr &joy_msg) const
void sendBaseCommand(double vx, double vy, double vw)
#define ROS_INFO(...)
static const double FastHz
bool sameValueAsLast(unsigned int button, const sensor_msgs::Joy::ConstPtr &new_msg, const sensor_msgs::Joy::ConstPtr &old_msg)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
static const unsigned int HEAD_TILT_AXIS
void sendProjectorStartStop(bool start)
ROSCPP_DECL bool ok()
static const unsigned int HEAD_MODE_TOGGLE_BUTTON
ArmControlMode getArmMode(WhichArm which)
static const unsigned int ARM_X_AXIS
static const unsigned int ARM_UNTUCK_BUTTON
void sendHeadCommand(double req_pan, double req_tilt)
static const unsigned int BODY_LAYOUT_BUTTON
static const unsigned int PROJECTOR_TOGGLE_BUTTON
bool getJointPosition(const std::string &name, double &pos) const
void requestProsilicaImage(std::string ns)
bool sleep()
static const unsigned int ARM_Z_AXIS
void joy_cb(const sensor_msgs::Joy::ConstPtr &joy_msg)
void setLaserMode(LaserControlMode mode)
static Time now()
void sendGripperCommand(WhichArm which, bool close)
Pr2TeleopGeneralJoystick(bool deadman_no_publish=false)
static const unsigned int ARM_TUCK_BUTTON
ROSCPP_DECL void shutdown()
void sendTorsoCommand(double pos, double vel)
static const unsigned int TORSO_UP_BUTTON
static const unsigned int LEFT_AXIS_NUMBER
static const unsigned int LEFT_ARM_LAYOUT_BUTTON
static const unsigned int ARM_YAW_AXIS
static const unsigned int VW_AXIS
static const unsigned int WRIST_COUNTER_BUTTON
static const unsigned int WRIST_CLOCKWISE_BUTTON
static const unsigned int MOVE_TO_WALK_ALONG_BUTTON
#define ROS_DEBUG(...)
static const unsigned int VX_AXIS


pr2_teleop_general
Author(s): Gil Jones
autogenerated on Sat Feb 27 2021 04:01:05