open_manipulator_teleop_keyboard.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2018 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 {
27 
28  initClient();
30 
31  ROS_INFO("OpenManipulator initialization");
32 }
33 
35 {
36  if(ros::isStarted()) {
37  ros::shutdown(); // explicitly needed since we use ros::start();
39  }
40 }
41 
43 {
44  goal_joint_space_path_from_present_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path_from_present");
45  goal_task_space_path_from_present_position_only_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path_from_present_position_only");
46 
47  goal_joint_space_path_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path");
48  goal_tool_control_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_tool_control");
49 }
51 {
54 }
55 
56 void OpenManipulatorTeleop::jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
57 {
58  std::vector<double> temp_angle;
59  temp_angle.resize(NUM_OF_JOINT);
60  for(std::vector<int>::size_type i = 0; i < msg->name.size(); i ++)
61  {
62  if(!msg->name.at(i).compare("joint1")) temp_angle.at(0) = (msg->position.at(i));
63  else if(!msg->name.at(i).compare("joint2")) temp_angle.at(1) = (msg->position.at(i));
64  else if(!msg->name.at(i).compare("joint3")) temp_angle.at(2) = (msg->position.at(i));
65  else if(!msg->name.at(i).compare("joint4")) temp_angle.at(3) = (msg->position.at(i));
66  }
67  present_joint_angle_ = temp_angle;
68 
69 }
70 
71 void OpenManipulatorTeleop::kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
72 {
73  std::vector<double> temp_position;
74  temp_position.push_back(msg->pose.position.x);
75  temp_position.push_back(msg->pose.position.y);
76  temp_position.push_back(msg->pose.position.z);
77  present_kinematic_position_ = temp_position;
78 }
79 
81 {
82  return present_joint_angle_;
83 }
85 {
87 }
88 
89 bool OpenManipulatorTeleop::setJointSpacePathFromPresent(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
90 {
91  open_manipulator_msgs::SetJointPosition srv;
92  srv.request.joint_position.joint_name = joint_name;
93  srv.request.joint_position.position = joint_angle;
94  srv.request.path_time = path_time;
95 
97  {
98  return srv.response.is_planned;
99  }
100  return false;
101 }
102 
103 bool OpenManipulatorTeleop::setJointSpacePath(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
104 {
105  open_manipulator_msgs::SetJointPosition srv;
106  srv.request.joint_position.joint_name = joint_name;
107  srv.request.joint_position.position = joint_angle;
108  srv.request.path_time = path_time;
109 
111  {
112  return srv.response.is_planned;
113  }
114  return false;
115 }
116 
117 bool OpenManipulatorTeleop::setToolControl(std::vector<double> joint_angle)
118 {
119  open_manipulator_msgs::SetJointPosition srv;
120  srv.request.joint_position.joint_name.push_back(priv_node_handle_.param<std::string>("end_effector_name", "gripper"));
121  srv.request.joint_position.position = joint_angle;
122 
124  {
125  return srv.response.is_planned;
126  }
127  return false;
128 }
129 
130 bool OpenManipulatorTeleop::setTaskSpacePathFromPresentPositionOnly(std::vector<double> kinematics_pose, double path_time)
131 {
132  open_manipulator_msgs::SetKinematicsPose srv;
133  srv.request.planning_group = priv_node_handle_.param<std::string>("end_effector_name", "gripper");
134  srv.request.kinematics_pose.pose.position.x = kinematics_pose.at(0);
135  srv.request.kinematics_pose.pose.position.y = kinematics_pose.at(1);
136  srv.request.kinematics_pose.pose.position.z = kinematics_pose.at(2);
137  srv.request.path_time = path_time;
138 
140  {
141  return srv.response.is_planned;
142  }
143  return false;
144 }
145 
147 {
148  printf("\n");
149  printf("---------------------------\n");
150  printf("Control Your OpenManipulator!\n");
151  printf("---------------------------\n");
152  printf("w : increase x axis in task space\n");
153  printf("s : decrease x axis in task space\n");
154  printf("a : increase y axis in task space\n");
155  printf("d : decrease y axis in task space\n");
156  printf("z : increase z axis in task space\n");
157  printf("x : decrease z axis in task space\n");
158  printf("\n");
159  printf("y : increase joint 1 angle\n");
160  printf("h : decrease joint 1 angle\n");
161  printf("u : increase joint 2 angle\n");
162  printf("j : decrease joint 2 angle\n");
163  printf("i : increase joint 3 angle\n");
164  printf("k : decrease joint 3 angle\n");
165  printf("o : increase joint 4 angle\n");
166  printf("l : decrease joint 4 angle\n");
167  printf("\n");
168  printf("g : gripper open\n");
169  printf("f : gripper close\n");
170  printf(" \n");
171  printf("1 : init pose\n");
172  printf("2 : home pose\n");
173  printf(" \n");
174  printf("q to quit\n");
175  printf("---------------------------\n");
176 
177  printf("Present Joint Angle J1: %.3lf J2: %.3lf J3: %.3lf J4: %.3lf\n",
178  getPresentJointAngle().at(0),
179  getPresentJointAngle().at(1),
180  getPresentJointAngle().at(2),
181  getPresentJointAngle().at(3));
182  printf("Present Kinematics Position X: %.3lf Y: %.3lf Z: %.3lf\n",
183  getPresentKinematicsPose().at(0),
184  getPresentKinematicsPose().at(1),
185  getPresentKinematicsPose().at(2));
186  printf("---------------------------\n");
187 
188 }
189 
191 {
192  std::vector<double> goalPose; goalPose.resize(3, 0.0);
193  std::vector<double> goalJoint; goalJoint.resize(4, 0.0);
194 
195  if(ch == 'w' || ch == 'W')
196  {
197  printf("input : w \tincrease(++) x axis in task space\n");
198  goalPose.at(0) = DELTA;
200  }
201  else if(ch == 's' || ch == 'S')
202  {
203  printf("input : s \tdecrease(--) x axis in task space\n");
204  goalPose.at(0) = -DELTA;
206  }
207  else if(ch == 'a' || ch == 'A')
208  {
209  printf("input : a \tincrease(++) y axis in task space\n");
210  goalPose.at(1) = DELTA;
212  }
213  else if(ch == 'd' || ch == 'D')
214  {
215  printf("input : d \tdecrease(--) y axis in task space\n");
216  goalPose.at(1) = -DELTA;
218  }
219  else if(ch == 'z' || ch == 'Z')
220  {
221  printf("input : z \tincrease(++) z axis in task space\n");
222  goalPose.at(2) = DELTA;
224  }
225  else if(ch == 'x' || ch == 'X')
226  {
227  printf("input : x \tdecrease(--) z axis in task space\n");
228  goalPose.at(2) = -DELTA;
230  }
231  else if(ch == 'y' || ch == 'Y')
232  {
233  printf("input : y \tincrease(++) joint 1 angle\n");
234  std::vector<std::string> joint_name;
235  joint_name.push_back("joint1"); goalJoint.at(0) = JOINT_DELTA;
236  joint_name.push_back("joint2");
237  joint_name.push_back("joint3");
238  joint_name.push_back("joint4");
239  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
240  }
241  else if(ch == 'h' || ch == 'H')
242  {
243  printf("input : h \tdecrease(--) joint 1 angle\n");
244  std::vector<std::string> joint_name;
245  joint_name.push_back("joint1"); goalJoint.at(0) = -JOINT_DELTA;
246  joint_name.push_back("joint2");
247  joint_name.push_back("joint3");
248  joint_name.push_back("joint4");
249  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
250  }
251 
252  else if(ch == 'u' || ch == 'U')
253  {
254  printf("input : u \tincrease(++) joint 2 angle\n");
255  std::vector<std::string> joint_name;
256  joint_name.push_back("joint1");
257  joint_name.push_back("joint2"); goalJoint.at(1) = JOINT_DELTA;
258  joint_name.push_back("joint3");
259  joint_name.push_back("joint4");
260  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
261  }
262  else if(ch == 'j' || ch == 'J')
263  {
264  printf("input : j \tdecrease(--) joint 2 angle\n");
265  std::vector<std::string> joint_name;
266  joint_name.push_back("joint1");
267  joint_name.push_back("joint2"); goalJoint.at(1) = -JOINT_DELTA;
268  joint_name.push_back("joint3");
269  joint_name.push_back("joint4");
270  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
271  }
272 
273  else if(ch == 'i' || ch == 'I')
274  {
275  printf("input : i \tincrease(++) joint 3 angle\n");
276  std::vector<std::string> joint_name;
277  joint_name.push_back("joint1");
278  joint_name.push_back("joint2");
279  joint_name.push_back("joint3"); goalJoint.at(2) = JOINT_DELTA;
280  joint_name.push_back("joint4");
281  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
282  }
283  else if(ch == 'k' || ch == 'K')
284  {
285  printf("input : k \tdecrease(--) joint 3 angle\n");
286  std::vector<std::string> joint_name;
287  joint_name.push_back("joint1");
288  joint_name.push_back("joint2");
289  joint_name.push_back("joint3"); goalJoint.at(2) = -JOINT_DELTA;
290  joint_name.push_back("joint4");
291  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
292  }
293 
294  else if(ch == 'o' || ch == 'O')
295  {
296  printf("input : o \tincrease(++) joint 4 angle\n");
297  std::vector<std::string> joint_name;
298  joint_name.push_back("joint1");
299  joint_name.push_back("joint2");
300  joint_name.push_back("joint3");
301  joint_name.push_back("joint4"); goalJoint.at(3) = JOINT_DELTA;
302  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
303  }
304  else if(ch == 'l' || ch == 'L')
305  {
306  printf("input : l \tdecrease(--) joint 4 angle\n");
307  std::vector<std::string> joint_name;
308  joint_name.push_back("joint1");
309  joint_name.push_back("joint2");
310  joint_name.push_back("joint3");
311  joint_name.push_back("joint4"); goalJoint.at(3) = -JOINT_DELTA;
312  setJointSpacePathFromPresent(joint_name, goalJoint, PATH_TIME);
313  }
314 
315  else if(ch == 'g' || ch == 'G')
316  {
317  printf("input : g \topen gripper\n");
318  std::vector<double> joint_angle;
319 
320  joint_angle.push_back(0.01);
321  setToolControl(joint_angle);
322  }
323  else if(ch == 'f' || ch == 'F')
324  {
325  printf("input : f \tclose gripper\n");
326  std::vector<double> joint_angle;
327  joint_angle.push_back(-0.01);
328  setToolControl(joint_angle);
329  }
330 
331  else if(ch == '2')
332  {
333  printf("input : 2 \thome pose\n");
334  std::vector<std::string> joint_name;
335  std::vector<double> joint_angle;
336  double path_time = 2.0;
337 
338  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
339  joint_name.push_back("joint2"); joint_angle.push_back(-1.05);
340  joint_name.push_back("joint3"); joint_angle.push_back(0.35);
341  joint_name.push_back("joint4"); joint_angle.push_back(0.70);
342  setJointSpacePath(joint_name, joint_angle, path_time);
343  }
344  else if(ch == '1')
345  {
346  printf("input : 1 \tinit pose\n");
347 
348  std::vector<std::string> joint_name;
349  std::vector<double> joint_angle;
350  double path_time = 2.0;
351  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
352  joint_name.push_back("joint2"); joint_angle.push_back(0.0);
353  joint_name.push_back("joint3"); joint_angle.push_back(0.0);
354  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
355  setJointSpacePath(joint_name, joint_angle, path_time);
356  }
357 }
358 
360 {
361  tcsetattr(0, TCSANOW, &oldt_); /* Apply saved settings */
362 }
363 
365 {
366  struct termios newt;
367 
368  tcgetattr(0, &oldt_); /* Save terminal settings */
369  newt = oldt_; /* Init new settings */
370  newt.c_lflag &= ~(ICANON | ECHO); /* Change settings */
371  tcsetattr(0, TCSANOW, &newt); /* Apply settings */
372 }
373 
374 int main(int argc, char **argv)
375 {
376  // Init ROS node
377  ros::init(argc, argv, "open_manipulator_teleop");
378 
379  OpenManipulatorTeleop openManipulatorTeleop;
380 
381  ROS_INFO("OpenManipulator teleoperation using keyboard start");
382  openManipulatorTeleop.disableWaitingForEnter();
383 
384  ros::spinOnce();
385  openManipulatorTeleop.printText();
386 
387  char ch;
388  while (ros::ok() && (ch = std::getchar()) != 'q')
389  {
390  ros::spinOnce();
391  openManipulatorTeleop.printText();
392  ros::spinOnce();
393  openManipulatorTeleop.setGoal(ch);
394  }
395 
396  printf("input : q \tTeleop. is finished\n");
397  openManipulatorTeleop.restoreTerminalSettings();
398 
399  return 0;
400 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::vector< double > getPresentKinematicsPose()
ros::ServiceClient goal_joint_space_path_client_
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)
std::vector< double > present_joint_angle_
bool setJointSpacePath(std::vector< std::string > joint_name, std::vector< double > joint_angle, double path_time)
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_
int main(int argc, char **argv)
bool setToolControl(std::vector< double > joint_angle)
ROSCPP_DECL bool isStarted()
ROSCPP_DECL void shutdown()
ROSCPP_DECL void spinOnce()
ROSCPP_DECL void waitForShutdown()


open_manipulator_teleop
Author(s): Darby Lim , Hye-Jong KIM , Ryan Shim , Yong-Ho Na
autogenerated on Mon Jun 10 2019 14:12:20