open_manipulator_teleop_joystick.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_task_space_path_from_present_position_only_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path_from_present_position_only");
45  goal_joint_space_path_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path");
46  goal_tool_control_client_ = node_handle_.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_tool_control");
47 
48 }
50 {
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 void OpenManipulatorTeleop::joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
80 {
81  if(msg->axes.at(1) >= 0.9) setGoal("x+");
82  else if(msg->axes.at(1) <= -0.9) setGoal("x-");
83  else if(msg->axes.at(0) >= 0.9) setGoal("y+");
84  else if(msg->axes.at(0) <= -0.9) setGoal("y-");
85  else if(msg->buttons.at(3) == 1) setGoal("z+");
86  else if(msg->buttons.at(0) == 1) setGoal("z-");
87  else if(msg->buttons.at(5) == 1) setGoal("home");
88  else if(msg->buttons.at(4) == 1) setGoal("init");
89 
90  if(msg->buttons.at(2) == 1) setGoal("gripper close");
91  else if(msg->buttons.at(1) == 1) setGoal("gripper open");
92 }
93 
95 {
96  return present_joint_angle_;
97 }
99 {
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 
146 void OpenManipulatorTeleop::setGoal(const char* str)
147 {
148  std::vector<double> goalPose; goalPose.resize(3, 0.0);
149  std::vector<double> goalJoint; goalJoint.resize(4, 0.0);
150 
151  if(str == "x+")
152  {
153  printf("increase(++) x axis in cartesian space\n");
154  goalPose.at(0) = DELTA;
156  }
157  else if(str == "x-")
158  {
159  printf("decrease(--) x axis in cartesian space\n");
160  goalPose.at(0) = -DELTA;
162  }
163  else if(str == "y+")
164  {
165  printf("increase(++) y axis in cartesian space\n");
166  goalPose.at(1) = DELTA;
168  }
169  else if(str == "y-")
170  {
171  printf("decrease(--) y axis in cartesian space\n");
172  goalPose.at(1) = -DELTA;
174  }
175  else if(str == "z+")
176  {
177  printf("increase(++) z axis in cartesian space\n");
178  goalPose.at(2) = DELTA;
180  }
181  else if(str == "z-")
182  {
183  printf("decrease(--) z axis in cartesian space\n");
184  goalPose.at(2) = -DELTA;
186  }
187 
188  else if(str == "gripper open")
189  {
190  printf("open gripper\n");
191  std::vector<double> joint_angle;
192 
193  joint_angle.push_back(0.01);
194  setToolControl(joint_angle);
195  }
196  else if(str == "gripper close")
197  {
198  printf("close gripper\n");
199  std::vector<double> joint_angle;
200  joint_angle.push_back(-0.01);
201  setToolControl(joint_angle);
202  }
203 
204  else if(str == "home")
205  {
206  printf("home pose\n");
207  std::vector<std::string> joint_name;
208  std::vector<double> joint_angle;
209  double path_time = 2.0;
210 
211  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
212  joint_name.push_back("joint2"); joint_angle.push_back(-1.05);
213  joint_name.push_back("joint3"); joint_angle.push_back(0.35);
214  joint_name.push_back("joint4"); joint_angle.push_back(0.70);
215  setJointSpacePath(joint_name, joint_angle, path_time);
216  }
217  else if(str == "init")
218  {
219  printf("init pose\n");
220 
221  std::vector<std::string> joint_name;
222  std::vector<double> joint_angle;
223  double path_time = 2.0;
224  joint_name.push_back("joint1"); joint_angle.push_back(0.0);
225  joint_name.push_back("joint2"); joint_angle.push_back(0.0);
226  joint_name.push_back("joint3"); joint_angle.push_back(0.0);
227  joint_name.push_back("joint4"); joint_angle.push_back(0.0);
228  setJointSpacePath(joint_name, joint_angle, path_time);
229  }
230 }
231 
232 int main(int argc, char **argv)
233 {
234  // Init ROS node
235  ros::init(argc, argv, "open_manipulator_TELEOP");
236  OpenManipulatorTeleop openManipulatorTeleop;
237 
238  ROS_INFO("OpenManipulator teleoperation using joystick start");
239 
240  ros::spin();
241 
242  printf("Teleop. is finished\n");
243  return 0;
244 }
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()
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)
int main(int argc, char **argv)
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_
#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()
void joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
bool setToolControl(std::vector< double > joint_angle)
ROSCPP_DECL bool isStarted()
ROSCPP_DECL void shutdown()
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