arm_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  init_position_(false),
28  joint_num_(4),
29  is_moving_(false)
30 {
31  // Init parameter
32  nh_.getParam("gazebo", using_gazebo_);
33  nh_.getParam("robot_name", robot_name_);
34  priv_nh_.getParam("init_position", init_position_);
35 
37 
40 
42 
45 
46  initServer();
47 
48  if (init_position_ == true)
50 }
51 
53 {
54  ros::shutdown();
55  return;
56 }
57 
59 {
60  open_manipulator_msgs::JointPosition msg;
61 
62  msg.joint_name.push_back("joint1");
63  msg.joint_name.push_back("joint2");
64  msg.joint_name.push_back("joint3");
65  msg.joint_name.push_back("joint4");
66 
67  msg.position.push_back(0.0);
68  msg.position.push_back(-1.5707);
69  msg.position.push_back(1.37);
70  msg.position.push_back(0.2258);
71 
72  msg.max_velocity_scaling_factor = 0.2;
73  msg.max_accelerations_scaling_factor = 0.5;
74 
75  calcPlannedPath(msg);
76 }
77 
78 void ArmController::initPublisher(bool using_gazebo)
79 {
80  if (using_gazebo)
81  {
82  ROS_INFO("SET Gazebo Simulation Mode(Joint)");
83 
84  std::string joint_name[joint_num_] = {"joint1", "joint2", "joint3", "joint4"};
85 
86  for (uint8_t index = 0; index < joint_num_; index++)
87  {
88  if (robot_name_ == "open_manipulator")
89  {
91  = nh_.advertise<std_msgs::Float64>(robot_name_ + "/" + joint_name[index] + "_position/command", 10);
92  }
93  else
94  {
96  = nh_.advertise<std_msgs::Float64>(joint_name[index] + "_position/command", 10);
97  }
98  }
99  }
100  else
101  {
102  goal_joint_position_pub_ = nh_.advertise<sensor_msgs::JointState>(robot_name_ + "/goal_joint_position", 10);
103  }
104 
105  arm_state_pub_ = nh_.advertise<open_manipulator_msgs::State>(robot_name_ + "/arm_state", 10);
106 }
107 
108 void ArmController::initSubscriber(bool using_gazebo)
109 {
110  display_planned_path_sub_ = nh_.subscribe("/move_group/display_planned_path", 10,
112 }
113 
115 {
120 }
121 
122 bool ArmController::getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req,
123  open_manipulator_msgs::GetJointPosition::Response &res)
124 {
126  spinner.start();
127 
128  const std::vector<std::string> &joint_names = move_group->getJointNames();
129  std::vector<double> joint_values = move_group->getCurrentJointValues();
130 
131  for (std::size_t i = 0; i < joint_names.size(); ++i)
132  {
133  ROS_INFO("%s: %f", joint_names[i].c_str(), joint_values[i]);
134 
135  res.joint_position.joint_name.push_back(joint_names[i]);
136  res.joint_position.position.push_back(joint_values[i]);
137  }
138 
139  spinner.stop();
140 }
141 
142 bool ArmController::getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req,
143  open_manipulator_msgs::GetKinematicsPose::Response &res)
144 {
146  spinner.start();
147 
148  const std::string &pose_reference_frame = move_group->getPoseReferenceFrame();
149  ROS_INFO("Pose Reference Frame = %s", pose_reference_frame.c_str());
150 
151  geometry_msgs::PoseStamped current_pose = move_group->getCurrentPose();
152 
153  res.header = current_pose.header;
154  res.kinematics_pose.group_name = "arm";
155  res.kinematics_pose.pose = current_pose.pose;
156 
157  spinner.stop();
158 }
159 
160 bool ArmController::setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req,
161  open_manipulator_msgs::SetJointPosition::Response &res)
162 {
163  open_manipulator_msgs::JointPosition msg = req.joint_position;
164  res.isPlanned = calcPlannedPath(msg);
165 }
166 
167 bool ArmController::setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
168  open_manipulator_msgs::SetKinematicsPose::Response &res)
169 {
170  open_manipulator_msgs::KinematicsPose msg = req.kinematics_pose;
171  res.isPlanned = calcPlannedPath(msg);
172 }
173 
174 bool ArmController::calcPlannedPath(open_manipulator_msgs::KinematicsPose msg)
175 {
177  spinner.start();
178 
179  bool isPlanned = false;
180  geometry_msgs::Pose target_pose = msg.pose;
181 
182  move_group->setPoseTarget(target_pose);
183 
184  move_group->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
185  move_group->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
186 
187  move_group->setGoalTolerance(msg.tolerance);
188 
190 
191  if (is_moving_ == false)
192  {
193  bool success = (move_group->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
194 
195  if (success)
196  {
197  isPlanned = true;
198  }
199  else
200  {
201  ROS_WARN("Planning (task space goal) is FAILED");
202  isPlanned = false;
203  }
204  }
205  else
206  {
207  ROS_WARN("ROBOT IS WORKING");
208  isPlanned = false;
209  }
210 
211  spinner.stop();
212 
213  return isPlanned;
214 }
215 
216 bool ArmController::calcPlannedPath(open_manipulator_msgs::JointPosition msg)
217 {
219  spinner.start();
220 
221  bool isPlanned = false;
222 
223  const robot_state::JointModelGroup *joint_model_group = move_group->getCurrentState()->getJointModelGroup("arm");
224 
225  moveit::core::RobotStatePtr current_state = move_group->getCurrentState();
226 
227  std::vector<double> joint_group_positions;
228  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
229 
230  for (uint8_t index = 0; index < joint_num_; index++)
231  {
232  if (msg.joint_name[index] == ("joint" + std::to_string((index+1))))
233  {
234  joint_group_positions[index] = msg.position[index];
235  }
236  }
237 
238  move_group->setJointValueTarget(joint_group_positions);
239 
240  move_group->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
241  move_group->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
242 
244 
245  if (is_moving_ == false)
246  {
247  bool success = (move_group->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
248 
249  if (success)
250  {
251  isPlanned = true;
252  }
253  else
254  {
255  ROS_WARN("Planning (joint space goal) is FAILED");
256  isPlanned = false;
257  }
258  }
259  else
260  {
261  ROS_WARN("ROBOT IS WORKING");
262  isPlanned = false;
263  }
264 
265  spinner.stop();
266 
267  return isPlanned;
268 }
269 
270 void ArmController::displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
271 {
272  // Can't find 'grip'
273  if (msg->trajectory[0].joint_trajectory.joint_names[0].find("grip") == std::string::npos)
274  {
275  ROS_INFO("Get ARM Planned Path");
276  uint8_t joint_num = joint_num_;
277 
278  planned_path_info_.waypoints = msg->trajectory[0].joint_trajectory.points.size();
279 
281 
282  for (uint16_t point_num = 0; point_num < planned_path_info_.waypoints; point_num++)
283  {
284  for (uint8_t num = 0; num < joint_num; num++)
285  {
286  float joint_position = msg->trajectory[0].joint_trajectory.points[point_num].positions[num];
287 
288  planned_path_info_.planned_path_positions.coeffRef(point_num , num) = joint_position;
289  }
290  }
291 
293 
294  ros::WallDuration sleep_time(0.5);
295  sleep_time.sleep();
296 
297  is_moving_ = true;
298  }
299 }
300 
302 {
303  static uint16_t step_cnt = 0;
304  std_msgs::Float64 gazebo_goal_joint_position;
305  sensor_msgs::JointState goal_joint_position;
306  open_manipulator_msgs::State state;
307 
308  if (is_moving_)
309  {
310  if (using_gazebo_)
311  {
312  for (uint8_t num = 0; num < joint_num_; num++)
313  {
314  gazebo_goal_joint_position.data = planned_path_info_.planned_path_positions(step_cnt, num);
315  gazebo_goal_joint_position_pub_[num].publish(gazebo_goal_joint_position);
316  }
317  }
318  else
319  {
320  for (uint8_t num = 0; num < joint_num_; num++)
321  {
322  goal_joint_position.position.push_back(planned_path_info_.planned_path_positions(step_cnt, num));
323  }
324 
325  goal_joint_position_pub_.publish(goal_joint_position);
326  }
327 
328  if (step_cnt >= all_time_steps_)
329  {
330  is_moving_ = false;
331  step_cnt = 0;
332 
333  ROS_INFO("Complete Execution");
334  }
335  else
336  {
337  step_cnt++;
338  }
339 
340  state.robot = state.IS_MOVING;
341  arm_state_pub_.publish(state);
342  }
343  else
344  {
345  state.robot = state.STOPPED;
346  arm_state_pub_.publish(state);
347  }
348 }
349 
350 int main(int argc, char **argv)
351 {
352  ros::init(argc, argv, "joint_controller_for_OpenManipulator");
353 
354  ros::WallDuration sleep_time(3.0);
355  sleep_time.sleep();
356 
357  ArmController controller;
358 
359  ros::Rate loop_rate(ITERATION_FREQUENCY);
360 
361  while (ros::ok())
362  {
363  controller.process();
364 
365  ros::spinOnce();
366  loop_rate.sleep();
367  }
368 
369  return 0;
370 }
bool getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req, open_manipulator_msgs::GetJointPosition::Response &res)
bool setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void publish(const boost::shared_ptr< M > &message) const
ros::ServiceServer get_joint_position_server_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Subscriber display_planned_path_sub_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define JOINT_NUM
void displayPlannedPathMsgCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void initPublisher(bool using_gazebo)
#define ROS_WARN(...)
ros::Publisher gazebo_goal_joint_position_pub_[10]
ros::ServiceServer set_kinematics_pose_server_
void spinner()
unsigned int index
bool sleep() const
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
ros::Publisher goal_joint_position_pub_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
bool getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req, open_manipulator_msgs::GetKinematicsPose::Response &res)
ros::ServiceServer set_joint_position_server_
int main(int argc, char **argv)
bool getParam(const std::string &key, std::string &s) const
void initSubscriber(bool using_gazebo)
ROSCPP_DECL void shutdown()
bool calcPlannedPath(open_manipulator_msgs::JointPosition msg)
bool setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
ROSCPP_DECL void spinOnce()
ros::ServiceServer get_kinematics_pose_server_
#define ITERATION_FREQUENCY


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