gripper_controller.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 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: Taehun Lim (Darby) */
18 
20 
21 using namespace open_manipulator;
22 
24  :priv_nh_("~"),
25  using_gazebo_(false),
26  robot_name_(""),
27  palm_num_(2),
28  is_moving_(false)
29 {
30  // Init parameter
31  nh_.getParam("gazebo", using_gazebo_);
32  nh_.getParam("robot_name", robot_name_);
33 
36 
38 
41 
42  initServer();
43 }
44 
46 {
47  ros::shutdown();
48  return;
49 }
50 
51 void GripperController::initPublisher(bool using_gazebo)
52 {
53  if (using_gazebo)
54  {
55  ROS_INFO("SET Gazebo Simulation Mode(Gripper)");
56 
57  if (robot_name_ == "open_manipulator")
58  {
59  gazebo_gripper_position_pub_[LEFT_PALM] = nh_.advertise<std_msgs::Float64>(robot_name_ + "/grip_joint_position/command", 10);
60  gazebo_gripper_position_pub_[RIGHT_PALM] = nh_.advertise<std_msgs::Float64>(robot_name_ + "/grip_joint_sub_position/command", 10);
61  }
62  else
63  {
64  gazebo_gripper_position_pub_[LEFT_PALM] = nh_.advertise<std_msgs::Float64>("/grip_joint_position/command", 10);
65  gazebo_gripper_position_pub_[RIGHT_PALM] = nh_.advertise<std_msgs::Float64>("/grip_joint_sub_position/command", 10);
66  }
67  }
68  else
69  {
70  gripper_position_pub_ = nh_.advertise<sensor_msgs::JointState>(robot_name_ + "/goal_gripper_position", 10);
71  }
72 
73  gripper_state_pub_ = nh_.advertise<open_manipulator_msgs::State>(robot_name_ + "/gripper_state", 10);
74 }
75 
76 void GripperController::initSubscriber(bool using_gazebo)
77 {
78  gripper_onoff_sub_ = nh_.subscribe(robot_name_ + "/gripper", 10,
80 
81  display_planned_path_sub_ = nh_.subscribe("/move_group/display_planned_path", 10,
83 }
84 
86 {
88 }
89 
90 bool GripperController::setGripperPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req,
91  open_manipulator_msgs::SetJointPosition::Response &res)
92 {
93  open_manipulator_msgs::JointPosition msg = req.joint_position;
94  res.isPlanned = calcPlannedPath(msg);
95 }
96 
97 bool GripperController::calcPlannedPath(open_manipulator_msgs::JointPosition msg)
98 {
100  spinner.start();
101 
102  bool isPlanned = false;
103 
104  const robot_state::JointModelGroup *joint_model_group = move_group->getCurrentState()->getJointModelGroup("gripper");
105 
106  moveit::core::RobotStatePtr current_state = move_group->getCurrentState();
107 
108  std::vector<double> joint_group_positions;
109  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
110 
111  msg.joint_name.push_back("grip_joint");
112  msg.joint_name.push_back("grip_joint_sub");
113 
114  for (uint8_t index = 0; index < palm_num_; index++)
115  {
116  joint_group_positions[index] = msg.position[0];
117  joint_group_positions[index] = msg.position[0];
118  }
119 
120  move_group->setJointValueTarget(joint_group_positions);
121 
122  move_group->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
123  move_group->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
124 
126 
127  if (is_moving_ == false)
128  {
129  bool success = (move_group->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
130 
131  if (success)
132  {
133  isPlanned = true;
134  }
135  else
136  {
137  ROS_WARN("Planning (joint space goal) is FAILED");
138  isPlanned = false;
139  }
140  }
141  else
142  {
143  ROS_WARN("ROBOT IS WORKING");
144  isPlanned = false;
145  }
146 
147  spinner.stop();
148 
149  return isPlanned;
150 }
151 
152 void GripperController::gripperOnOffMsgCallback(const std_msgs::String::ConstPtr &msg)
153 {
154  open_manipulator_msgs::JointPosition joint_msg;
155 
156  joint_msg.max_velocity_scaling_factor = 0.3;
157  joint_msg.max_accelerations_scaling_factor = 0.01;
158 
159  if (msg->data == "grip_on")
160  {
161  joint_msg.position.push_back(GRIP_ON);
162  calcPlannedPath(joint_msg);
163  }
164  else if (msg->data == "grip_off")
165  {
166  joint_msg.position.push_back(GRIP_OFF);
167  calcPlannedPath(joint_msg);
168  }
169  else if (msg->data == "neutral")
170  {
171  joint_msg.position.push_back(NEUTRAL);
172  calcPlannedPath(joint_msg);
173  }
174  else
175  {
176  ROS_ERROR("If you want to grip or release something, publish 'grip_on', 'grip_off' or 'neutral'");
177  }
178 }
179 
180 void GripperController::displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
181 {
182  // Can find 'grip'
183  if (msg->trajectory[0].joint_trajectory.joint_names[0].find("grip") != std::string::npos)
184  {
185  ROS_INFO("Get Gripper Planned Path");
186  uint8_t gripper_num = 2;
187 
188  planned_path_info_.waypoints = msg->trajectory[0].joint_trajectory.points.size();
189 
191 
192  for (uint16_t point_num = 0; point_num < planned_path_info_.waypoints; point_num++)
193  {
194  for (uint8_t num = 0; num < gripper_num; num++)
195  {
196  float joint_position = msg->trajectory[0].joint_trajectory.points[point_num].positions[num];
197 
198  planned_path_info_.planned_path_positions.coeffRef(point_num , num) = joint_position;
199  }
200  }
201 
203 
204  ros::WallDuration sleep_time(0.5);
205  sleep_time.sleep();
206 
207  is_moving_ = true;
208  }
209 }
210 
212 {
213  static uint16_t step_cnt = 0;
214  std_msgs::Float64 gazebo_goal_gripper_position;
215  sensor_msgs::JointState goal_gripper_position;
216  open_manipulator_msgs::State state;
217 
218  if (is_moving_)
219  {
220  if (using_gazebo_)
221  {
222  gazebo_goal_gripper_position.data = planned_path_info_.planned_path_positions(step_cnt, 0);
223  gazebo_gripper_position_pub_[LEFT_PALM].publish(gazebo_goal_gripper_position);
224 
225  gazebo_goal_gripper_position.data = planned_path_info_.planned_path_positions(step_cnt, 1);
226  gazebo_gripper_position_pub_[RIGHT_PALM].publish(gazebo_goal_gripper_position);
227  }
228  else
229  {
230  goal_gripper_position.position.push_back(planned_path_info_.planned_path_positions(step_cnt, 0));
231  goal_gripper_position.position.push_back(planned_path_info_.planned_path_positions(step_cnt, 1));
232 
233  gripper_position_pub_.publish(goal_gripper_position);
234  }
235 
236  if (step_cnt >= all_time_steps_)
237  {
238  is_moving_ = false;
239  step_cnt = 0;
240 
241  ROS_INFO("Complete Execution");
242  }
243  else
244  {
245  step_cnt++;
246  }
247 
248  state.robot = state.IS_MOVING;
250  }
251  else
252  {
253  state.robot = state.STOPPED;
255  }
256 }
257 
258 int main(int argc, char **argv)
259 {
260  ros::init(argc, argv, "gripper_controller_for_OpenManipulator");
261 
262  ros::WallDuration sleep_time(3.0);
263  sleep_time.sleep();
264 
265  GripperController controller;
266 
267  ros::Rate loop_rate(ITERATION_FREQUENCY);
268 
269  while (ros::ok())
270  {
271  controller.process();
272 
273  ros::spinOnce();
274  loop_rate.sleep();
275  }
276 
277  return 0;
278 }
#define RIGHT_PALM
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
#define ROS_WARN(...)
void spinner()
unsigned int index
bool calcPlannedPath(open_manipulator_msgs::JointPosition msg)
bool sleep() const
#define ROS_INFO(...)
#define GRIP_OFF
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define LEFT_PALM
bool sleep()
bool setGripperPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
#define NEUTRAL
#define GRIP_ON
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void shutdown()
void gripperOnOffMsgCallback(const std_msgs::String::ConstPtr &msg)
ros::ServiceServer set_gripper_position_server_
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
void displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
#define ITERATION_FREQUENCY


open_manipulator_position_ctrl
Author(s): Darby Lim
autogenerated on Sat Jun 2 2018 02:43:38