moveit_bridge.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  :nh_(""),
23  priv_nh_("~")
24 {
25  // Init parameter
26  planning_group_ = priv_nh_.param<std::string>("planning_group", "manipulator");
27  use_platform_ = priv_nh_.param<bool>("use_platform", "true");
28 
30 }
31 
33 {
34  ros::shutdown();
35  return;
36 }
37 
39 {
40  joint_trajectory_point_pub_ = priv_nh_.advertise<std_msgs::Float64MultiArray>("joint_trajectory_point", 1000);
41 }
42 
44 {
45  display_planned_path_sub_ = nh_.subscribe("move_group/display_planned_path", 100,
47 }
48 
50 {
55 }
56 
57 bool MoveItBridge::getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req,
58  open_manipulator_msgs::GetJointPosition::Response &res)
59 {
61  spinner.start();
62 
63  const std::vector<std::string> &joint_names = move_group_->getJointNames();
64  std::vector<double> joint_values = move_group_->getCurrentJointValues();
65 
66  for (std::size_t i = 0; i < joint_names.size(); i++)
67  {
68  res.joint_position.joint_name.push_back(joint_names[i]);
69  res.joint_position.position.push_back(joint_values[i]);
70  }
71 
72  spinner.stop();
73  return true;
74 }
75 
76 bool MoveItBridge::getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req,
77  open_manipulator_msgs::GetKinematicsPose::Response &res)
78 {
80  spinner.start();
81 
82  geometry_msgs::PoseStamped current_pose = move_group_->getCurrentPose();
83 
84  res.header = current_pose.header;
85  res.kinematics_pose.pose = current_pose.pose;
86 
87  spinner.stop();
88  return true;
89 }
90 
91 bool MoveItBridge::setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req,
92  open_manipulator_msgs::SetJointPosition::Response &res)
93 {
94  open_manipulator_msgs::JointPosition msg = req.joint_position;
95  res.is_planned = calcPlannedPath(req.planning_group, msg);
96 
97  return true;
98 }
99 
100 bool MoveItBridge::setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
101  open_manipulator_msgs::SetKinematicsPose::Response &res)
102 {
103  open_manipulator_msgs::KinematicsPose msg = req.kinematics_pose;
104  res.is_planned = calcPlannedPath(req.planning_group, msg);
105 
106  return true;
107 }
108 
109 bool MoveItBridge::calcPlannedPath(const std::string planning_group, open_manipulator_msgs::KinematicsPose msg)
110 {
112  spinner.start();
113 
114  bool is_planned = false;
115  geometry_msgs::Pose target_pose = msg.pose;
116 
117  move_group_->setPoseTarget(target_pose);
118 
119  move_group_->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
120  move_group_->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
121 
122  move_group_->setGoalTolerance(msg.tolerance);
123 
125 
126  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
127 
128  if (success)
129  {
130  is_planned = true;
131  }
132  else
133  {
134  ROS_WARN("Planning (task space goal) is FAILED");
135  is_planned = false;
136  }
137 
138  spinner.stop();
139 
140  return is_planned;
141 }
142 
143 bool MoveItBridge::calcPlannedPath(const std::string planning_group, open_manipulator_msgs::JointPosition msg)
144 {
146  spinner.start();
147 
148  bool is_planned = false;
149 
150  const robot_state::JointModelGroup *joint_model_group = move_group_->getCurrentState()->getJointModelGroup(planning_group);
151 
152  moveit::core::RobotStatePtr current_state = move_group_->getCurrentState();
153 
154  std::vector<double> joint_group_positions;
155  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
156 
157  uint8_t joint_num = msg.position.size();
158  for (uint8_t index = 0; index < joint_num; index++)
159  {
160  joint_group_positions[index] = msg.position[index];
161  }
162 
163  move_group_->setJointValueTarget(joint_group_positions);
164 
165  move_group_->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
166  move_group_->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
167 
169 
170  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
171 
172  if (success)
173  {
174  is_planned = true;
175  }
176  else
177  {
178  ROS_WARN("Planning (joint space goal) is FAILED");
179  is_planned = false;
180  }
181 
182  spinner.stop();
183 
184  return is_planned;
185 }
186 
187 void MoveItBridge::displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
188 {
189  ROS_INFO("Get Planned Path");
190 
191  trajectory_msgs::JointTrajectory jnt_tra = msg->trajectory[0].joint_trajectory;
192  std_msgs::Float64MultiArray jnt_tra_pts;
193 
194  if (use_platform_ == false)
195  {
196  for (std::vector<uint32_t>::size_type i = 0; i < jnt_tra.points.size(); i++)
197  {
198  jnt_tra_pts.data.push_back(jnt_tra.points[i].time_from_start.toSec());
199  for (std::vector<uint32_t>::size_type j = 0; j < jnt_tra.points[i].positions.size(); j++)
200  {
201  jnt_tra_pts.data.push_back(jnt_tra.points[i].positions[j]);
202  }
203  }
204 
206  }
207  else
208  {
209  uint32_t all_points_size = jnt_tra.points.size();
210  const uint8_t POINTS_STEP_SIZE = 10;
211  uint32_t steps = floor((double)all_points_size/(double)POINTS_STEP_SIZE);
212 
213  for (uint32_t i = 0; i < all_points_size; i = i + steps)
214  {
215  jnt_tra_pts.data.push_back(jnt_tra.points[i].time_from_start.toSec());
216  for (std::vector<uint32_t>::size_type j = 0; j < jnt_tra.points[i].positions.size(); j++)
217  {
218  jnt_tra_pts.data.push_back(jnt_tra.points[i].positions[j]);
219  }
220  }
221 
223  }
224 }
225 
226 int main(int argc, char **argv)
227 {
228  ros::init(argc, argv, "moveit_bridge");
229 
230  MoveItBridge bridge;
231 
232  bridge.initPublisher();
233  bridge.initSubscriber();
234 
235  bridge.initServer();
236 
237  ros::Rate loop_rate(100);
238 
239  while (ros::ok())
240  {
241  ros::spinOnce();
242  loop_rate.sleep();
243  }
244 
245  return 0;
246 }
virtual ~MoveItBridge()
void publish(const boost::shared_ptr< M > &message) const
moveit::planning_interface::MoveGroupInterface * move_group_
Definition: moveit_bridge.h:78
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool setJointValueTarget(const std::vector< double > &group_variable_values)
std::string planning_group_
Definition: moveit_bridge.h:60
bool getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req, open_manipulator_msgs::GetKinematicsPose::Response &res)
bool setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
ros::ServiceServer get_kinematics_pose_server_
Definition: moveit_bridge.h:71
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool use_platform_
Definition: moveit_bridge.h:61
bool setPoseTarget(const Eigen::Affine3d &end_effector_pose, const std::string &end_effector_link="")
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
int main(int argc, char **argv)
#define ROS_WARN(...)
void initSubscriber()
void initPublisher()
void spinner()
ros::ServiceServer set_joint_position_server_
Definition: moveit_bridge.h:72
unsigned int index
ros::Subscriber display_planned_path_sub_
Definition: moveit_bridge.h:67
ros::NodeHandle priv_nh_
Definition: moveit_bridge.h:57
#define ROS_INFO(...)
ros::ServiceServer set_kinematics_pose_server_
Definition: moveit_bridge.h:73
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::ServiceServer get_joint_position_server_
Definition: moveit_bridge.h:70
robot_state::RobotStatePtr getCurrentState(double wait=1)
void setMaxVelocityScalingFactor(double max_velocity_scaling_factor)
ROSCPP_DECL bool ok()
ros::Publisher joint_trajectory_point_pub_
Definition: moveit_bridge.h:64
void displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
bool setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
geometry_msgs::PoseStamped getCurrentPose(const std::string &end_effector_link="")
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req, open_manipulator_msgs::GetJointPosition::Response &res)
bool sleep()
ros::NodeHandle nh_
Definition: moveit_bridge.h:56
const std::vector< std::string > & getJointNames()
bool calcPlannedPath(const std::string planning_group, open_manipulator_msgs::JointPosition msg)
ROSCPP_DECL void shutdown()
void setMaxAccelerationScalingFactor(double max_acceleration_scaling_factor)
ROSCPP_DECL void spinOnce()


open_manipulator_with_tb3_tools
Author(s): Darby Lim
autogenerated on Thu Sep 10 2020 03:52:23