open_manipulator_p_teleop_keyboard.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2019 ROBOTIS CO., LTD.
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 /* Authors: Darby Lim, Hye-Jong KIM, Ryan Shim, Yong-Ho Na */
18 
20 
22 : node_handle_(""),
23  priv_node_handle_("~")
24 {
25  /************************************************************
26  ** Initialize ROS parameters
27  ************************************************************/
28  with_gripper_ = priv_node_handle_.param<bool>("with_gripper", false);
29 
30  /************************************************************
31  ** Initialize variables
32  ************************************************************/
35 
36  /************************************************************
37  ** Initialize ROS Subscribers and Clients
38  ************************************************************/
40  initClient();
41 
43  ROS_INFO("Start OpenManipulator-P Teleop Keyboard");
44 }
45 
47 {
49  ROS_INFO("Terminate OpenManipulator-P Teleop Keyboard");
50  ros::shutdown();
51 }
52 
54 {
55  goal_joint_space_path_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path");
56  goal_joint_space_path_from_present_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path_from_present");
57  goal_task_space_path_from_present_position_only_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path_from_present_position_only");
58  goal_tool_control_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_tool_control");
59 }
60 
62 {
65 }
66 
67 void OpenManipulatorTeleop::jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
68 {
69  std::vector<double> temp_angle;
70  temp_angle.resize(NUM_OF_JOINT);
71  for (std::vector<int>::size_type i = 0; i < msg->name.size(); i++)
72  {
73  if (!msg->name.at(i).compare("joint1")) temp_angle.at(0) = (msg->position.at(i));
74  else if (!msg->name.at(i).compare("joint2")) temp_angle.at(1) = (msg->position.at(i));
75  else if (!msg->name.at(i).compare("joint3")) temp_angle.at(2) = (msg->position.at(i));
76  else if (!msg->name.at(i).compare("joint4")) temp_angle.at(3) = (msg->position.at(i));
77  else if (!msg->name.at(i).compare("joint5")) temp_angle.at(4) = (msg->position.at(i));
78  else if (!msg->name.at(i).compare("joint6")) temp_angle.at(5) = (msg->position.at(i));
79  }
80  present_joint_angle_ = temp_angle;
81 }
82 
83 void OpenManipulatorTeleop::kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
84 {
85  std::vector<double> temp_position;
86  temp_position.push_back(msg->pose.position.x);
87  temp_position.push_back(msg->pose.position.y);
88  temp_position.push_back(msg->pose.position.z);
89  present_kinematic_position_ = temp_position;
90 }
91 
93 {
94  return present_joint_angle_;
95 }
96 
98 {
100 }
101 
102 bool OpenManipulatorTeleop::setJointSpacePathFromPresent(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
103 {
104  open_manipulator_msgs::SetJointPosition srv;
105  srv.request.joint_position.joint_name = joint_name;
106  srv.request.joint_position.position = joint_angle;
107  srv.request.path_time = path_time;
108 
110  {
111  return srv.response.is_planned;
112  }
113  return false;
114 }
115 
116 bool OpenManipulatorTeleop::setJointSpacePath(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
117 {
118  open_manipulator_msgs::SetJointPosition srv;
119  srv.request.joint_position.joint_name = joint_name;
120  srv.request.joint_position.position = joint_angle;
121  srv.request.path_time = path_time;
122 
124  {
125  return srv.response.is_planned;
126  }
127  return false;
128 }
129 
130 bool OpenManipulatorTeleop::setToolControl(std::vector<double> joint_angle)
131 {
132  open_manipulator_msgs::SetJointPosition srv;
133  srv.request.joint_position.joint_name.push_back(priv_node_handle_.param<std::string>("end_effector_name", "gripper"));
134  srv.request.joint_position.position = joint_angle;
135 
137  {
138  return srv.response.is_planned;
139  }
140  return false;
141 }
142 
143 bool OpenManipulatorTeleop::setTaskSpacePathFromPresentPositionOnly(std::vector<double> kinematics_pose, double path_time)
144 {
145  open_manipulator_msgs::SetKinematicsPose srv;
146  srv.request.planning_group = priv_node_handle_.param<std::string>("end_effector_name", "gripper");
147  srv.request.kinematics_pose.pose.position.x = kinematics_pose.at(0);
148  srv.request.kinematics_pose.pose.position.y = kinematics_pose.at(1);
149  srv.request.kinematics_pose.pose.position.z = kinematics_pose.at(2);
150  srv.request.path_time = path_time;
151 
153  {
154  return srv.response.is_planned;
155  }
156  return false;
157 }
158 
160 {
161  printf("\n");
162  printf("---------------------------------\n");
163  printf("Control Your OpenMANIPULATOR-P!\n");
164  printf("---------------------------------\n");
165  printf("w : increase x axis in task space\n");
166  printf("s : decrease x axis in task space\n");
167  printf("a : increase y axis in task space\n");
168  printf("d : decrease y axis in task space\n");
169  printf("z : increase z axis in task space\n");
170  printf("x : decrease z axis in task space\n");
171  printf("\n");
172  printf("r : increase joint 1 angle\n");
173  printf("f : decrease joint 1 angle\n");
174  printf("t : increase joint 2 angle\n");
175  printf("g : decrease joint 2 angle\n");
176  printf("y : increase joint 3 angle\n");
177  printf("h : decrease joint 3 angle\n");
178  printf("u : increase joint 4 angle\n");
179  printf("j : decrease joint 4 angle\n");
180  printf("i : increase joint 5 angle\n");
181  printf("k : decrease joint 5 angle\n");
182  printf("o : increase joint 6 angle\n");
183  printf("l : decrease joint 6 angle\n");
184  printf(" \n");
185  if (with_gripper_)
186  {
187  printf("\n");
188  printf("v : gripper open\n");
189  printf("b : gripper close\n");
190  }
191  printf("1 : init pose\n");
192  printf("2 : home pose\n");
193  printf(" \n");
194  printf("q to quit\n");
195  printf("-------------------------------------------------------------------------------\n");
196  printf("Present Joint Angle J1: %.3lf J2: %.3lf J3: %.3lf J4: %.3lf J5: %.3lf J6: %.3lf\n",
197  getPresentJointAngle().at(0),
198  getPresentJointAngle().at(1),
199  getPresentJointAngle().at(2),
200  getPresentJointAngle().at(3),
201  getPresentJointAngle().at(4),
202  getPresentJointAngle().at(5));
203  printf("Present Kinematics Position X: %.3lf Y: %.3lf Z: %.3lf\n",
204  getPresentKinematicsPose().at(0),
205  getPresentKinematicsPose().at(1),
206  getPresentKinematicsPose().at(2));
207  printf("-------------------------------------------------------------------------------\n");
208 }
209 
211 {
212  std::vector<double> goalPose; goalPose.resize(3, 0.0);
213  std::vector<double> goalJoint; goalJoint.resize(6, 0.0);
214 
215  if (ch == 'w' || ch == 'W')
216  {
217  printf("input : w \tincrease(++) x axis in task space\n");
218  goalPose.at(0) = DELTA;
220  }
221  else if (ch == 's' || ch == 'S')
222  {
223  printf("input : s \tdecrease(--) x axis in task space\n");
224  goalPose.at(0) = -DELTA;
226  }
227  else if (ch == 'a' || ch == 'A')
228  {
229  printf("input : a \tincrease(++) y axis in task space\n");
230  goalPose.at(1) = DELTA;
232  }
233  else if (ch == 'd' || ch == 'D')
234  {
235  printf("input : d \tdecrease(--) y axis in task space\n");
236  goalPose.at(1) = -DELTA;
238  }
239  else if (ch == 'z' || ch == 'Z')
240  {
241  printf("input : z \tincrease(++) z axis in task space\n");
242  goalPose.at(2) = DELTA;
244  }
245  else if (ch == 'x' || ch == 'X')
246  {
247  printf("input : x \tdecrease(--) z axis in task space\n");
248  goalPose.at(2) = -DELTA;
250  }
251  else if (ch == 'r' || ch == 'R')
252  {
253  printf("input : r \tincrease(++) joint 1 angle\n");
254  std::vector<std::string> joint_name;
255  joint_name.push_back("joint1"); goalJoint.at(0) = JOINT_DELTA;
256  joint_name.push_back("joint2");
257  joint_name.push_back("joint3");
258  joint_name.push_back("joint4");
259  joint_name.push_back("joint5");
260  joint_name.push_back("joint6");
261  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
262  }
263  else if (ch == 'f' || ch == 'F')
264  {
265  printf("input : f \tdecrease(--) joint 1 angle\n");
266  std::vector<std::string> joint_name;
267  joint_name.push_back("joint1"); goalJoint.at(0) = -JOINT_DELTA;
268  joint_name.push_back("joint2");
269  joint_name.push_back("joint3");
270  joint_name.push_back("joint4");
271  joint_name.push_back("joint5");
272  joint_name.push_back("joint6");
273  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
274  }
275  else if (ch == 't' || ch == 'T')
276  {
277  printf("input : t \tincrease(++) joint 2 angle\n");
278  std::vector<std::string> joint_name;
279  joint_name.push_back("joint1");
280  joint_name.push_back("joint2"); goalJoint.at(1) = JOINT_DELTA;
281  joint_name.push_back("joint3");
282  joint_name.push_back("joint4");
283  joint_name.push_back("joint5");
284  joint_name.push_back("joint6");
285  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
286  }
287  else if (ch == 'g' || ch == 'G')
288  {
289  printf("input : g \tdecrease(--) joint 2 angle\n");
290  std::vector<std::string> joint_name;
291  joint_name.push_back("joint1");
292  joint_name.push_back("joint2"); goalJoint.at(1) = -JOINT_DELTA;
293  joint_name.push_back("joint3");
294  joint_name.push_back("joint4");
295  joint_name.push_back("joint5");
296  joint_name.push_back("joint6");
297  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
298  }
299  else if (ch == 'y' || ch == 'Y')
300  {
301  printf("input : y \tincrease(++) joint 3 angle\n");
302  std::vector<std::string> joint_name;
303  joint_name.push_back("joint1");
304  joint_name.push_back("joint2");
305  joint_name.push_back("joint3"); goalJoint.at(2) = JOINT_DELTA;
306  joint_name.push_back("joint4");
307  joint_name.push_back("joint5");
308  joint_name.push_back("joint6");
309  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
310  }
311  else if (ch == 'h' || ch == 'H')
312  {
313  printf("input : h \tdecrease(--) joint 3 angle\n");
314  std::vector<std::string> joint_name;
315  joint_name.push_back("joint1");
316  joint_name.push_back("joint2");
317  joint_name.push_back("joint3"); goalJoint.at(2) = -JOINT_DELTA;
318  joint_name.push_back("joint4");
319  joint_name.push_back("joint5");
320  joint_name.push_back("joint6");
321  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
322  }
323  else if (ch == 'u' || ch == 'U')
324  {
325  printf("input : u \tincrease(++) joint 4 angle\n");
326  std::vector<std::string> joint_name;
327  joint_name.push_back("joint1");
328  joint_name.push_back("joint2");
329  joint_name.push_back("joint3");
330  joint_name.push_back("joint4"); goalJoint.at(3) = JOINT_DELTA;
331  joint_name.push_back("joint5");
332  joint_name.push_back("joint6");
333  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
334  }
335  else if (ch == 'j' || ch == 'J')
336  {
337  printf("input : j \tdecrease(--) joint 4 angle\n");
338  std::vector<std::string> joint_name;
339  joint_name.push_back("joint1");
340  joint_name.push_back("joint2");
341  joint_name.push_back("joint3");
342  joint_name.push_back("joint4"); goalJoint.at(3) = -JOINT_DELTA;
343  joint_name.push_back("joint5");
344  joint_name.push_back("joint6");
345  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
346  }
347  else if (ch == 'i' || ch == 'I')
348  {
349  printf("input : i \tincrease(++) joint 5 angle\n");
350  std::vector<std::string> joint_name;
351  joint_name.push_back("joint1");
352  joint_name.push_back("joint2");
353  joint_name.push_back("joint3");
354  joint_name.push_back("joint4");
355  joint_name.push_back("joint5"); goalJoint.at(4) = JOINT_DELTA;
356  joint_name.push_back("joint6");
357  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
358  }
359  else if (ch == 'k' || ch == 'K')
360  {
361  printf("input : k \tdecrease(--) joint 5 angle\n");
362  std::vector<std::string> joint_name;
363  joint_name.push_back("joint1");
364  joint_name.push_back("joint2");
365  joint_name.push_back("joint3");
366  joint_name.push_back("joint4");
367  joint_name.push_back("joint5"); goalJoint.at(4) = -JOINT_DELTA;
368  joint_name.push_back("joint6");
369  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
370  }
371  else if (ch == 'o' || ch == 'O')
372  {
373  printf("input : o \tincrease(++) joint 6 angle\n");
374  std::vector<std::string> joint_name;
375  joint_name.push_back("joint1");
376  joint_name.push_back("joint2");
377  joint_name.push_back("joint3");
378  joint_name.push_back("joint4");
379  joint_name.push_back("joint5");
380  joint_name.push_back("joint6"); goalJoint.at(5) = JOINT_DELTA;
381  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
382  }
383  else if (ch == 'l' || ch == 'L')
384  {
385  printf("input : l \tdecrease(--) joint 6 angle\n");
386  std::vector<std::string> joint_name;
387  joint_name.push_back("joint1");
388  joint_name.push_back("joint2");
389  joint_name.push_back("joint3");
390  joint_name.push_back("joint4");
391  joint_name.push_back("joint5");
392  joint_name.push_back("joint6"); goalJoint.at(5) = -JOINT_DELTA;
393  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
394  }
395  else if (ch == 'v' || ch == 'V')
396  {
397  if (with_gripper_)
398  {
399  printf("input : v \topen gripper\n");
400  std::vector<double> joint_angle;
401  joint_angle.push_back(0.0);
402  setToolControl(joint_angle);
403  }
404  }
405  else if (ch == 'b' || ch == 'B')
406  {
407  if (with_gripper_)
408  {
409  printf("input : b \tclose gripper\n");
410  std::vector<double> joint_angle;
411  joint_angle.push_back(1.1);
412  setToolControl(joint_angle);
413  }
414  }
415  else if (ch == '2')
416  {
417  printf("input : 2 \thome pose\n");
418  std::vector<std::string> joint_name;
419  std::vector<double> joint_angle;
420  double path_time = 2.0;
421 
422  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
423  joint_name.push_back("joint2"); joint_angle.push_back(-PI/4);
424  joint_name.push_back("joint3"); joint_angle.push_back(PI/8);
425  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
426  joint_name.push_back("joint5"); joint_angle.push_back(PI/8);
427  joint_name.push_back("joint6"); joint_angle.push_back(0.0);
428  setJointSpacePath(joint_name, joint_angle, path_time);
429  }
430  else if (ch == '1')
431  {
432  printf("input : 1 \tinit pose\n");
433 
434  std::vector<std::string> joint_name;
435  std::vector<double> joint_angle;
436  double path_time = 2.0;
437  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
438  joint_name.push_back("joint2"); joint_angle.push_back(0.0);
439  joint_name.push_back("joint3"); joint_angle.push_back(0.0);
440  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
441  joint_name.push_back("joint5"); joint_angle.push_back(0.0);
442  joint_name.push_back("joint6"); joint_angle.push_back(0.0);
443  setJointSpacePath(joint_name, joint_angle, path_time);
444  }
445 }
446 
448 {
449  tcsetattr(0, TCSANOW, &oldt_); /* Apply saved settings */
450 }
451 
453 {
454  struct termios newt;
455 
456  tcgetattr(0, &oldt_); /* Save terminal settings */
457  newt = oldt_; /* Init new settings */
458  newt.c_lflag &= ~(ICANON | ECHO); /* Change settings */
459  tcsetattr(0, TCSANOW, &newt); /* Apply settings */
460 }
461 
462 int main(int argc, char **argv)
463 {
464  // Init ROS node
465  ros::init(argc, argv, "open_manipulator_p_teleop_keyboard");
466  OpenManipulatorTeleop openManipulatorTeleop;
467 
468  char ch;
469  openManipulatorTeleop.printText();
470  while (ros::ok() && (ch = std::getchar()) != 'q')
471  {
472  ros::spinOnce();
473  openManipulatorTeleop.printText();
474  ros::spinOnce();
475  openManipulatorTeleop.setGoal(ch);
476  }
477 
478  return 0;
479 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::vector< double > getPresentKinematicsPose()
void jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
std::vector< double > getPresentJointAngle()
ros::ServiceClient goal_joint_space_path_from_present_client_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool setTaskSpacePathFromPresentPositionOnly(std::vector< double > kinematics_pose, double path_time)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
bool setJointSpacePath(std::vector< std::string > joint_name, std::vector< double > joint_angle, double path_time)
int main(int argc, char **argv)
std::vector< double > present_kinematic_position_
bool setJointSpacePathFromPresent(std::vector< std::string > joint_name, std::vector< double > joint_angle, double path_time)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
ros::ServiceClient goal_task_space_path_from_present_position_only_client_
bool setToolControl(std::vector< double > joint_angle)
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()


open_manipulator_p_teleop
Author(s): Ryan Shim , Yong-Ho Na , Hye-Jong KIM
autogenerated on Thu Oct 22 2020 03:16:41