open_manipulator_p_teleop_joystick.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 
42  ROS_INFO("OpenManipulator-P teleoperation using joystick start");
43 }
44 
46 {
47  ROS_INFO("Terminate OpenManipulator Joystick");
48  ros::shutdown();
49 }
50 
52 {
53  goal_joint_space_path_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path");
54  goal_task_space_path_from_present_position_only_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path_from_present_position_only");
55  goal_tool_control_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_tool_control");
56 }
57 
59 {
63 }
64 
65 void OpenManipulatorTeleop::jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
66 {
67  std::vector<double> temp_angle;
68  temp_angle.resize(NUM_OF_JOINT);
69  for (std::vector<int>::size_type i = 0; i < msg->name.size(); i ++)
70  {
71  if (!msg->name.at(i).compare("joint1")) temp_angle.at(0) = (msg->position.at(i));
72  else if (!msg->name.at(i).compare("joint2")) temp_angle.at(1) = (msg->position.at(i));
73  else if (!msg->name.at(i).compare("joint3")) temp_angle.at(2) = (msg->position.at(i));
74  else if (!msg->name.at(i).compare("joint4")) temp_angle.at(3) = (msg->position.at(i));
75  else if (!msg->name.at(i).compare("joint5")) temp_angle.at(4) = (msg->position.at(i));
76  else if (!msg->name.at(i).compare("joint6")) temp_angle.at(5) = (msg->position.at(i));
77  }
78  present_joint_angle_ = temp_angle;
79 }
80 
81 void OpenManipulatorTeleop::kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
82 {
83  std::vector<double> temp_position;
84  temp_position.push_back(msg->pose.position.x);
85  temp_position.push_back(msg->pose.position.y);
86  temp_position.push_back(msg->pose.position.z);
87  present_kinematic_position_ = temp_position;
88 }
89 
90 void OpenManipulatorTeleop::joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
91 {
92  if (msg->axes.at(1) >= 0.9) setGoal("x+");
93  else if (msg->axes.at(1) <= -0.9) setGoal("x-");
94  else if (msg->axes.at(0) >= 0.9) setGoal("y+");
95  else if (msg->axes.at(0) <= -0.9) setGoal("y-");
96  else if (msg->buttons.at(3) == 1) setGoal("z+");
97  else if (msg->buttons.at(0) == 1) setGoal("z-");
98  else if (msg->buttons.at(5) == 1) setGoal("home");
99  else if (msg->buttons.at(4) == 1) setGoal("init");
100 
101  if (with_gripper_)
102  {
103  if (msg->buttons.at(2) == 1) setGoal("gripper close");
104  else if (msg->buttons.at(1) == 1) setGoal("gripper open");
105  }
106 }
107 
108 bool OpenManipulatorTeleop::setJointSpacePath(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
109 {
110  open_manipulator_msgs::SetJointPosition srv;
111  srv.request.joint_position.joint_name = joint_name;
112  srv.request.joint_position.position = joint_angle;
113  srv.request.path_time = path_time;
114 
116  {
117  return srv.response.is_planned;
118  }
119  return false;
120 }
121 
122 bool OpenManipulatorTeleop::setToolControl(std::vector<double> joint_angle)
123 {
124  open_manipulator_msgs::SetJointPosition srv;
125  srv.request.joint_position.joint_name.push_back(priv_node_handle_.param<std::string>("end_effector_name", "gripper"));
126  srv.request.joint_position.position = joint_angle;
127 
129  {
130  return srv.response.is_planned;
131  }
132  return false;
133 }
134 
135 bool OpenManipulatorTeleop::setTaskSpacePathFromPresentPositionOnly(std::vector<double> kinematics_pose, double path_time)
136 {
137  open_manipulator_msgs::SetKinematicsPose srv;
138  srv.request.planning_group = priv_node_handle_.param<std::string>("end_effector_name", "gripper");
139  srv.request.kinematics_pose.pose.position.x = kinematics_pose.at(0);
140  srv.request.kinematics_pose.pose.position.y = kinematics_pose.at(1);
141  srv.request.kinematics_pose.pose.position.z = kinematics_pose.at(2);
142  srv.request.path_time = path_time;
143 
145  {
146  return srv.response.is_planned;
147  }
148  return false;
149 }
150 
151 void OpenManipulatorTeleop::setGoal(const char* str)
152 {
153  std::vector<double> goalPose; goalPose.resize(3, 0.0);
154  std::vector<double> goalJoint; goalJoint.resize(6, 0.0);
155 
156  if (str == "x+")
157  {
158  printf("increase(++) x axis in cartesian space\n");
159  goalPose.at(0) = DELTA;
161  }
162  else if (str == "x-")
163  {
164  printf("decrease(--) x axis in cartesian space\n");
165  goalPose.at(0) = -DELTA;
167  }
168  else if (str == "y+")
169  {
170  printf("increase(++) y axis in cartesian space\n");
171  goalPose.at(1) = DELTA;
173  }
174  else if (str == "y-")
175  {
176  printf("decrease(--) y axis in cartesian space\n");
177  goalPose.at(1) = -DELTA;
179  }
180  else if (str == "z+")
181  {
182  printf("increase(++) z axis in cartesian space\n");
183  goalPose.at(2) = DELTA;
185  }
186  else if (str == "z-")
187  {
188  printf("decrease(--) z axis in cartesian space\n");
189  goalPose.at(2) = -DELTA;
191  }
192  else if (str == "gripper open")
193  {
194  printf("open gripper\n");
195  std::vector<double> joint_angle;
196  joint_angle.push_back(0.0);
197  setToolControl(joint_angle);
198  }
199  else if (str == "gripper close")
200  {
201  printf("close gripper\n");
202  std::vector<double> joint_angle;
203  joint_angle.push_back(1.1);
204  setToolControl(joint_angle);
205  }
206  else if (str == "home")
207  {
208  printf("home pose\n");
209  std::vector<std::string> joint_name;
210  std::vector<double> joint_angle;
211  double path_time = 2.0;
212 
213  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
214  joint_name.push_back("joint2"); joint_angle.push_back(-PI/4);
215  joint_name.push_back("joint3"); joint_angle.push_back(PI/8);
216  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
217  joint_name.push_back("joint5"); joint_angle.push_back(PI/8);
218  joint_name.push_back("joint6"); joint_angle.push_back(0.0);
219  setJointSpacePath(joint_name, joint_angle, path_time);
220  }
221  else if (str == "init")
222  {
223  printf("init pose\n");
224 
225  std::vector<std::string> joint_name;
226  std::vector<double> joint_angle;
227  double path_time = 2.0;
228  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
229  joint_name.push_back("joint2"); joint_angle.push_back(0.0);
230  joint_name.push_back("joint3"); joint_angle.push_back(0.0);
231  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
232  joint_name.push_back("joint5"); joint_angle.push_back(0.0);
233  joint_name.push_back("joint6"); joint_angle.push_back(0.0);
234  setJointSpacePath(joint_name, joint_angle, path_time);
235  }
236 }
237 
238 int main(int argc, char **argv)
239 {
240  // Init ROS node
241  ros::init(argc, argv, "open_manipulator_TELEOP");
242  OpenManipulatorTeleop openManipulatorTeleop;
243  ros::spin();
244  return 0;
245 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
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)
std::vector< double > present_kinematic_position_
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::ServiceClient goal_task_space_path_from_present_position_only_client_
ROSCPP_DECL void spin()
int main(int argc, char **argv)
void joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
bool setToolControl(std::vector< double > joint_angle)
ROSCPP_DECL void shutdown()


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