open_manipulator_controller.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 
21 using namespace open_manipulator_controller;
22 
23 OpenManipulatorController::OpenManipulatorController(std::string usb_port, std::string baud_rate)
24  :node_handle_(""),
25  priv_node_handle_("~"),
26  tool_ctrl_state_(false),
27  timer_thread_state_(false),
28  moveit_plan_state_(false),
29  using_platform_(false),
30  using_moveit_(false),
31  moveit_plan_only_(true),
32  control_period_(0.010f),
33  moveit_sampling_time_(0.050f)
34 {
35  control_period_ = priv_node_handle_.param<double>("control_period", 0.010f);
36  moveit_sampling_time_ = priv_node_handle_.param<double>("moveit_sample_duration", 0.050f);
37  using_platform_ = priv_node_handle_.param<bool>("using_platform", false);
38  using_moveit_ = priv_node_handle_.param<bool>("using_moveit", false);
39  std::string planning_group_name = priv_node_handle_.param<std::string>("planning_group_name", "arm");
40 
41  open_manipulator_.initOpenManipulator(using_platform_, usb_port, baud_rate, control_period_);
42 
43  if (using_platform_ == true) log::info("Succeeded to init " + priv_node_handle_.getNamespace());
44  else if (using_platform_ == false) log::info("Ready to simulate " + priv_node_handle_.getNamespace() + " on Gazebo");
45 
46  if (using_moveit_ == true)
47  {
49  log::info("Ready to control " + planning_group_name + " group");
50  }
51 }
52 
54 {
55  timer_thread_state_ = false;
56  pthread_join(timer_thread_, NULL); // Wait for the thread associated with thread_p to complete
57  log::info("Shutdown the OpenManipulator");
59  ros::shutdown();
60 }
61 
63 {
67  // pthread_attr_t attr_;
68  // int error;
69  // struct sched_param param;
70  // pthread_attr_init(&attr_);
71 
72  // error = pthread_attr_setschedpolicy(&attr_, SCHED_RR);
73  // if (error != 0) log::error("pthread_attr_setschedpolicy error = ", (double)error);
74  // error = pthread_attr_setinheritsched(&attr_, PTHREAD_EXPLICIT_SCHED);
75  // if (error != 0) log::error("pthread_attr_setinheritsched error = ", (double)error);
76 
77  // memset(&param, 0, sizeof(param));
78  // param.sched_priority = 31; // RT
79  // error = pthread_attr_setschedparam(&attr_, &param);
80  // if (error != 0) log::error("pthread_attr_setschedparam error = ", (double)error);
81 
82  // if ((error = pthread_create(&this->timer_thread_, &attr_, this->timerThread, this)) != 0)
83  // {
84  // log::error("Creating timer thread failed!!", (double)error);
85  // exit(-1);
86  // }
87  // timer_thread_state_ = true;
89 
90  int error;
91  if ((error = pthread_create(&this->timer_thread_, NULL, this->timerThread, this)) != 0)
92  {
93  log::error("Creating timer thread failed!!", (double)error);
94  exit(-1);
95  }
96  timer_thread_state_ = true;
97 }
98 
100 {
102  static struct timespec next_time;
103  static struct timespec curr_time;
104 
105  clock_gettime(CLOCK_MONOTONIC, &next_time);
106 
107  while(controller->timer_thread_state_)
108  {
109  next_time.tv_sec += (next_time.tv_nsec + ((int)(controller->getControlPeriod() * 1000)) * 1000000) / 1000000000;
110  next_time.tv_nsec = (next_time.tv_nsec + ((int)(controller->getControlPeriod() * 1000)) * 1000000) % 1000000000;
111 
112  double time = next_time.tv_sec + (next_time.tv_nsec*0.000000001);
113  controller->process(time);
114 
115  clock_gettime(CLOCK_MONOTONIC, &curr_time);
117  double delta_nsec = controller->getControlPeriod() - ((next_time.tv_sec - curr_time.tv_sec) + ((double)(next_time.tv_nsec - curr_time.tv_nsec)*0.000000001));
118  //log::info("control time : ", controller->getControlPeriod() - delta_nsec);
119  if(delta_nsec > controller->getControlPeriod())
120  {
121  //log::warn("Over the control time : ", delta_nsec);
122  next_time = curr_time;
123  }
124  else
125  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_time, NULL);
127  }
128  return 0;
129 }
130 
132 {
133  // ros message publisher
134  auto opm_tools_name = open_manipulator_.getManipulator()->getAllToolComponentName();
135 
136  for (auto const& name:opm_tools_name)
137  {
138  ros::Publisher pb;
139  pb = priv_node_handle_.advertise<open_manipulator_msgs::KinematicsPose>(name + "/kinematics_pose", 10);
141  }
142  open_manipulator_states_pub_ = priv_node_handle_.advertise<open_manipulator_msgs::OpenManipulatorState>("states", 10);
143 
144  if(using_platform_ == true)
145  {
146  open_manipulator_joint_states_pub_ = priv_node_handle_.advertise<sensor_msgs::JointState>("joint_states", 10);
147  }
148  else
149  {
150  auto gazebo_joints_name = open_manipulator_.getManipulator()->getAllActiveJointComponentName();
151  gazebo_joints_name.reserve(gazebo_joints_name.size() + opm_tools_name.size());
152  gazebo_joints_name.insert(gazebo_joints_name.end(), opm_tools_name.begin(), opm_tools_name.end());
153 
154  for (auto const& name:gazebo_joints_name)
155  {
156  ros::Publisher pb;
157  pb = priv_node_handle_.advertise<std_msgs::Float64>(name + "_position/command", 10);
158  gazebo_goal_joint_position_pub_.push_back(pb);
159  }
160  }
161  if (using_moveit_ == true)
162  {
163  moveit_update_start_state_pub_ = node_handle_.advertise<std_msgs::Empty>("rviz/moveit/update_start_state", 10);
164  }
165 }
167 {
168  // ros message subscriber
170  if (using_moveit_ == true)
171  {
172  display_planned_path_sub_ = node_handle_.subscribe("/move_group/display_planned_path", 100,
174  move_group_goal_sub_ = node_handle_.subscribe("/move_group/goal", 100,
176  execute_traj_goal_sub_ = node_handle_.subscribe("/execute_trajectory/goal", 100,
178  }
179 }
180 
182 {
187 
191 
193 
197 
201 
202  if (using_moveit_ == true)
203  {
208  }
209 }
210 
211 void OpenManipulatorController::openManipulatorOptionCallback(const std_msgs::String::ConstPtr &msg)
212 {
213  if(msg->data == "print_open_manipulator_setting")
215 }
216 
217 void OpenManipulatorController::displayPlannedPathCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
218 {
219  trajectory_msgs::JointTrajectory joint_trajectory_planned = msg->trajectory[0].joint_trajectory;
220  joint_trajectory_ = joint_trajectory_planned;
221 
222  if(moveit_plan_only_ == false)
223  {
224  log::println("[INFO] [OpenManipulator Controller] Execute Moveit planned path", "GREEN");
225  moveit_plan_state_ = true;
226  }
227  else
228  log::println("[INFO] [OpenManipulator Controller] Get Moveit planned path", "GREEN");
229 }
230 
231 void OpenManipulatorController::moveGroupGoalCallback(const moveit_msgs::MoveGroupActionGoal::ConstPtr &msg)
232 {
233  log::println("[INFO] [OpenManipulator Controller] Get Moveit plnning option", "GREEN");
234  moveit_plan_only_ = msg->goal.planning_options.plan_only; // click "plan & execute" or "plan" button
235 
236 }
237 void OpenManipulatorController::executeTrajGoalCallback(const moveit_msgs::ExecuteTrajectoryActionGoal::ConstPtr &msg)
238 {
239  log::println("[INFO] [OpenManipulator Controller] Execute Moveit planned path", "GREEN");
240  moveit_plan_state_ = true;
241 }
242 
243 bool OpenManipulatorController::goalJointSpacePathCallback(open_manipulator_msgs::SetJointPosition::Request &req,
244  open_manipulator_msgs::SetJointPosition::Response &res)
245 {
246  std::vector <double> target_angle;
247 
248  for(int i = 0; i < req.joint_position.joint_name.size(); i ++)
249  target_angle.push_back(req.joint_position.position.at(i));
250 
251  open_manipulator_.makeJointTrajectory(target_angle, req.path_time);
252 
253  res.is_planned = true;
254  return true;
255 }
256 
257 bool OpenManipulatorController::goalJointSpacePathToKinematicsPoseCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
258  open_manipulator_msgs::SetKinematicsPose::Response &res)
259 {
260  KinematicPose target_pose;
261  target_pose.position[0] = req.kinematics_pose.pose.position.x;
262  target_pose.position[1] = req.kinematics_pose.pose.position.y;
263  target_pose.position[2] = req.kinematics_pose.pose.position.z;
264 
265  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
266  req.kinematics_pose.pose.orientation.x,
267  req.kinematics_pose.pose.orientation.y,
268  req.kinematics_pose.pose.orientation.z);
269 
270  target_pose.orientation = math::convertQuaternionToRotationMatrix(q);
271 
272  open_manipulator_.makeJointTrajectory(req.end_effector_name, target_pose, req.path_time);
273 
274  res.is_planned = true;
275  return true;
276 }
277 
278 bool OpenManipulatorController::goalJointSpacePathToKinematicsPositionCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
279  open_manipulator_msgs::SetKinematicsPose::Response &res)
280 {
281  KinematicPose target_pose;
282  target_pose.position[0] = req.kinematics_pose.pose.position.x;
283  target_pose.position[1] = req.kinematics_pose.pose.position.y;
284  target_pose.position[2] = req.kinematics_pose.pose.position.z;
285 
286  open_manipulator_.makeJointTrajectory(req.end_effector_name, target_pose.position, req.path_time);
287 
288  res.is_planned = true;
289  return true;
290 }
291 
292 bool OpenManipulatorController::goalJointSpacePathToKinematicsOrientationCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
293  open_manipulator_msgs::SetKinematicsPose::Response &res)
294 {
295  KinematicPose target_pose;
296 
297  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
298  req.kinematics_pose.pose.orientation.x,
299  req.kinematics_pose.pose.orientation.y,
300  req.kinematics_pose.pose.orientation.z);
301 
302  target_pose.orientation = math::convertQuaternionToRotationMatrix(q);
303 
304  open_manipulator_.makeJointTrajectory(req.end_effector_name, target_pose.orientation, req.path_time);
305 
306  res.is_planned = true;
307  return true;
308 }
309 
310 bool OpenManipulatorController::goalTaskSpacePathCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
311  open_manipulator_msgs::SetKinematicsPose::Response &res)
312 {
313  KinematicPose target_pose;
314  target_pose.position[0] = req.kinematics_pose.pose.position.x;
315  target_pose.position[1] = req.kinematics_pose.pose.position.y;
316  target_pose.position[2] = req.kinematics_pose.pose.position.z;
317 
318  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
319  req.kinematics_pose.pose.orientation.x,
320  req.kinematics_pose.pose.orientation.y,
321  req.kinematics_pose.pose.orientation.z);
322 
323  target_pose.orientation = math::convertQuaternionToRotationMatrix(q);
324  open_manipulator_.makeTaskTrajectory(req.end_effector_name, target_pose, req.path_time);
325 
326  res.is_planned = true;
327  return true;
328 }
329 
330 bool OpenManipulatorController::goalTaskSpacePathPositionOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
331  open_manipulator_msgs::SetKinematicsPose::Response &res)
332 {
333  Eigen::Vector3d position;
334  position[0] = req.kinematics_pose.pose.position.x;
335  position[1] = req.kinematics_pose.pose.position.y;
336  position[2] = req.kinematics_pose.pose.position.z;
337 
338  open_manipulator_.makeTaskTrajectory(req.end_effector_name, position, req.path_time);
339 
340  res.is_planned = true;
341  return true;
342 }
343 
344 bool OpenManipulatorController::goalTaskSpacePathOrientationOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
345  open_manipulator_msgs::SetKinematicsPose::Response &res)
346 {
347  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
348  req.kinematics_pose.pose.orientation.x,
349  req.kinematics_pose.pose.orientation.y,
350  req.kinematics_pose.pose.orientation.z);
351 
352  Eigen::Matrix3d orientation = math::convertQuaternionToRotationMatrix(q);
353 
354  open_manipulator_.makeTaskTrajectory(req.end_effector_name, orientation, req.path_time);
355 
356  res.is_planned = true;
357  return true;
358 }
359 
360 bool OpenManipulatorController::goalJointSpacePathFromPresentCallback(open_manipulator_msgs::SetJointPosition::Request &req,
361  open_manipulator_msgs::SetJointPosition::Response &res)
362 {
363  std::vector <double> target_angle;
364 
365  for(int i = 0; i < req.joint_position.joint_name.size(); i ++)
366  target_angle.push_back(req.joint_position.position.at(i));
367 
368  open_manipulator_.makeJointTrajectoryFromPresentPosition(target_angle, req.path_time);
369 
370  res.is_planned = true;
371  return true;
372 }
373 
374 bool OpenManipulatorController::goalTaskSpacePathFromPresentCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
375  open_manipulator_msgs::SetKinematicsPose::Response &res)
376 {
377  KinematicPose target_pose;
378  target_pose.position[0] = req.kinematics_pose.pose.position.x;
379  target_pose.position[1] = req.kinematics_pose.pose.position.y;
380  target_pose.position[2] = req.kinematics_pose.pose.position.z;
381 
382  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
383  req.kinematics_pose.pose.orientation.x,
384  req.kinematics_pose.pose.orientation.y,
385  req.kinematics_pose.pose.orientation.z);
386 
387  target_pose.orientation = math::convertQuaternionToRotationMatrix(q);
388 
389  open_manipulator_.makeTaskTrajectoryFromPresentPose(req.planning_group, target_pose, req.path_time);
390 
391  res.is_planned = true;
392  return true;
393 }
394 
395 bool OpenManipulatorController::goalTaskSpacePathFromPresentPositionOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
396  open_manipulator_msgs::SetKinematicsPose::Response &res)
397 {
398  Eigen::Vector3d position;
399  position[0] = req.kinematics_pose.pose.position.x;
400  position[1] = req.kinematics_pose.pose.position.y;
401  position[2] = req.kinematics_pose.pose.position.z;
402 
403  open_manipulator_.makeTaskTrajectoryFromPresentPose(req.planning_group, position, req.path_time);
404 
405  res.is_planned = true;
406  return true;
407 }
408 
409 bool OpenManipulatorController::goalTaskSpacePathFromPresentOrientationOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
410  open_manipulator_msgs::SetKinematicsPose::Response &res)
411 {
412  Eigen::Quaterniond q(req.kinematics_pose.pose.orientation.w,
413  req.kinematics_pose.pose.orientation.x,
414  req.kinematics_pose.pose.orientation.y,
415  req.kinematics_pose.pose.orientation.z);
416 
417  Eigen::Matrix3d orientation = math::convertQuaternionToRotationMatrix(q);
418 
419  open_manipulator_.makeTaskTrajectoryFromPresentPose(req.planning_group, orientation, req.path_time);
420 
421  res.is_planned = true;
422  return true;
423 }
424 
425 bool OpenManipulatorController::goalToolControlCallback(open_manipulator_msgs::SetJointPosition::Request &req,
426  open_manipulator_msgs::SetJointPosition::Response &res)
427 {
428  for(int i = 0; i < req.joint_position.joint_name.size(); i ++)
429  open_manipulator_.makeToolTrajectory(req.joint_position.joint_name.at(i), req.joint_position.position.at(i));
430 
431  res.is_planned = true;
432  return true;
433 }
434 
435 bool OpenManipulatorController::setActuatorStateCallback(open_manipulator_msgs::SetActuatorState::Request &req,
436  open_manipulator_msgs::SetActuatorState::Response &res)
437 {
438  if(req.set_actuator_state == true) // enable actuators
439  {
440  log::println("Wait a second for actuator enable", "GREEN");
441  timer_thread_state_ = false;
442  pthread_join(timer_thread_, NULL); // Wait for the thread associated with thread_p to complete
445  }
446  else // disable actuators
447  {
448  log::println("Wait a second for actuator disable", "GREEN");
449  timer_thread_state_ = false;
450  pthread_join(timer_thread_, NULL); // Wait for the thread associated with thread_p to complete
453  }
454 
455  res.is_planned = true;
456  return true;
457 }
458 
459 bool OpenManipulatorController::goalDrawingTrajectoryCallback(open_manipulator_msgs::SetDrawingTrajectory::Request &req,
460  open_manipulator_msgs::SetDrawingTrajectory::Response &res)
461 {
462  try
463  {
464  if(req.drawing_trajectory_name == "circle")
465  {
466  double draw_circle_arg[3];
467  draw_circle_arg[0] = req.param[0]; // radius (m)
468  draw_circle_arg[1] = req.param[1]; // revolution (rev)
469  draw_circle_arg[2] = req.param[2]; // start angle position (rad)
470  void* p_draw_circle_arg = &draw_circle_arg;
471 
472  open_manipulator_.makeCustomTrajectory(CUSTOM_TRAJECTORY_CIRCLE, req.end_effector_name, p_draw_circle_arg, req.path_time);
473  }
474  else if(req.drawing_trajectory_name == "line")
475  {
476  TaskWaypoint draw_line_arg;
477  draw_line_arg.kinematic.position(0) = req.param[0]; // x axis (m)
478  draw_line_arg.kinematic.position(1) = req.param[1]; // y axis (m)
479  draw_line_arg.kinematic.position(2) = req.param[2]; // z axis (m)
480  void *p_draw_line_arg = &draw_line_arg;
481 
482  open_manipulator_.makeCustomTrajectory(CUSTOM_TRAJECTORY_LINE, req.end_effector_name, p_draw_line_arg, req.path_time);
483  }
484  else if(req.drawing_trajectory_name == "rhombus")
485  {
486  double draw_rhombus_arg[3];
487  draw_rhombus_arg[0] = req.param[0]; // radius (m)
488  draw_rhombus_arg[1] = req.param[1]; // revolution (rev)
489  draw_rhombus_arg[2] = req.param[2]; // start angle position (rad)
490  void* p_draw_rhombus_arg = &draw_rhombus_arg;
491 
492  open_manipulator_.makeCustomTrajectory(CUSTOM_TRAJECTORY_RHOMBUS, req.end_effector_name, p_draw_rhombus_arg, req.path_time);
493  }
494  else if(req.drawing_trajectory_name == "heart")
495  {
496  double draw_heart_arg[3];
497  draw_heart_arg[0] = req.param[0]; // radius (m)
498  draw_heart_arg[1] = req.param[1]; // revolution (rev)
499  draw_heart_arg[2] = req.param[2]; // start angle position (rad)
500  void* p_draw_heart_arg = &draw_heart_arg;
501 
502  open_manipulator_.makeCustomTrajectory(CUSTOM_TRAJECTORY_HEART, req.end_effector_name, p_draw_heart_arg, req.path_time);
503  }
504  res.is_planned = true;
505  return true;
506  }
507  catch ( ros::Exception &e )
508  {
509  log::error("Creation the custom trajectory is failed!");
510  }
511  return true;
512 }
513 
514 bool OpenManipulatorController::getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req,
515  open_manipulator_msgs::GetJointPosition::Response &res)
516 {
518  spinner.start();
519 
520  const std::vector<std::string> &joint_names = move_group_->getJointNames();
521  std::vector<double> joint_values = move_group_->getCurrentJointValues();
522 
523  for (std::size_t i = 0; i < joint_names.size(); i++)
524  {
525  res.joint_position.joint_name.push_back(joint_names[i]);
526  res.joint_position.position.push_back(joint_values[i]);
527  }
528 
529  spinner.stop();
530  return true;
531 }
532 
533 bool OpenManipulatorController::getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req,
534  open_manipulator_msgs::GetKinematicsPose::Response &res)
535 {
537  spinner.start();
538 
539  geometry_msgs::PoseStamped current_pose = move_group_->getCurrentPose();
540 
541  res.header = current_pose.header;
542  res.kinematics_pose.pose = current_pose.pose;
543 
544  spinner.stop();
545  return true;
546 }
547 
548 bool OpenManipulatorController::setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req,
549  open_manipulator_msgs::SetJointPosition::Response &res)
550 {
551  open_manipulator_msgs::JointPosition msg = req.joint_position;
552  res.is_planned = calcPlannedPath(req.planning_group, msg);
553 
554  return true;
555 }
556 
557 bool OpenManipulatorController::setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req,
558  open_manipulator_msgs::SetKinematicsPose::Response &res)
559 {
560  open_manipulator_msgs::KinematicsPose msg = req.kinematics_pose;
561  res.is_planned = calcPlannedPath(req.planning_group, msg);
562 
563  return true;
564 }
565 
566 bool OpenManipulatorController::calcPlannedPath(const std::string planning_group, open_manipulator_msgs::KinematicsPose msg)
567 {
569  spinner.start();
570 
571  bool is_planned = false;
572  geometry_msgs::Pose target_pose = msg.pose;
573 
574  move_group_->setPoseTarget(target_pose);
575 
576  move_group_->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
577  move_group_->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
578 
579  move_group_->setGoalTolerance(msg.tolerance);
580 
582 
583  if (open_manipulator_.getMovingState() == false)
584  {
585  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
586 
587  if (success)
588  {
589  is_planned = true;
590  }
591  else
592  {
593  log::warn("Failed to Plan (task space goal)");
594  is_planned = false;
595  }
596  }
597  else
598  {
599  log::warn("Robot is Moving");
600  is_planned = false;
601  }
602 
603  spinner.stop();
604  return is_planned;
605 }
606 
607 bool OpenManipulatorController::calcPlannedPath(const std::string planning_group, open_manipulator_msgs::JointPosition msg)
608 {
610  spinner.start();
611 
612  bool is_planned = false;
613 
614  const robot_state::JointModelGroup *joint_model_group = move_group_->getCurrentState()->getJointModelGroup(planning_group);
615 
616  moveit::core::RobotStatePtr current_state = move_group_->getCurrentState();
617 
618  std::vector<double> joint_group_positions;
619  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
620 
621  for (uint8_t index = 0; index < msg.position.size(); index++)
622  {
623  joint_group_positions[index] = msg.position[index];
624  }
625 
626  move_group_->setJointValueTarget(joint_group_positions);
627 
628  move_group_->setMaxVelocityScalingFactor(msg.max_velocity_scaling_factor);
629  move_group_->setMaxAccelerationScalingFactor(msg.max_accelerations_scaling_factor);
630 
632 
633  if (open_manipulator_.getMovingState() == false)
634  {
635  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
636 
637  if (success)
638  {
639  is_planned = true;
640  }
641  else
642  {
643  log::warn("Failed to Plan (joint space goal)");
644  is_planned = false;
645  }
646  }
647  else
648  {
649  log::warn("Robot is moving");
650  is_planned = false;
651  }
652 
653  spinner.stop();
654  return is_planned;
655 }
656 
658 {
659  open_manipulator_msgs::OpenManipulatorState msg;
661  msg.open_manipulator_moving_state = msg.IS_MOVING;
662  else
663  msg.open_manipulator_moving_state = msg.STOPPED;
664 
666  msg.open_manipulator_actuator_state = msg.ACTUATOR_ENABLED;
667  else
668  msg.open_manipulator_actuator_state = msg.ACTUATOR_DISABLED;
669 
671 }
672 
674 {
675  open_manipulator_msgs::KinematicsPose msg;
676  auto opm_tools_name = open_manipulator_.getManipulator()->getAllToolComponentName();
677 
678  uint8_t index = 0;
679  for (auto const& tools:opm_tools_name)
680  {
682  msg.pose.position.x = pose.position[0];
683  msg.pose.position.y = pose.position[1];
684  msg.pose.position.z = pose.position[2];
685  Eigen::Quaterniond orientation = math::convertRotationMatrixToQuaternion(pose.orientation);
686  msg.pose.orientation.w = orientation.w();
687  msg.pose.orientation.x = orientation.x();
688  msg.pose.orientation.y = orientation.y();
689  msg.pose.orientation.z = orientation.z();
690 
691  open_manipulator_kinematics_pose_pub_.at(index).publish(msg);
692  index++;
693  }
694 }
695 
697 {
698  sensor_msgs::JointState msg;
699  msg.header.stamp = ros::Time::now();
700 
703 
704  auto joint_value = open_manipulator_.getAllActiveJointValue();
705  auto tool_value = open_manipulator_.getAllToolValue();
706 
707  for(uint8_t i = 0; i < joints_name.size(); i ++)
708  {
709  msg.name.push_back(joints_name.at(i));
710 
711  msg.position.push_back(joint_value.at(i).position);
712  msg.velocity.push_back(joint_value.at(i).velocity);
713  msg.effort.push_back(joint_value.at(i).effort);
714  }
715 
716  for(uint8_t i = 0; i < tool_name.size(); i ++)
717  {
718  msg.name.push_back(tool_name.at(i));
719 
720  msg.position.push_back(tool_value.at(i).position);
721  msg.velocity.push_back(0.0f);
722  msg.effort.push_back(0.0f);
723  }
725 }
726 
728 {
731 
732  for(uint8_t i = 0; i < joint_value.size(); i ++)
733  {
734  std_msgs::Float64 msg;
735  msg.data = joint_value.at(i).position;
736 
737  gazebo_goal_joint_position_pub_.at(i).publish(msg);
738  }
739 
740  for(uint8_t i = 0; i < tool_value.size(); i ++)
741  {
742  std_msgs::Float64 msg;
743  msg.data = tool_value.at(i).position;
744 
745  gazebo_goal_joint_position_pub_.at(joint_value.size() + i).publish(msg);
746  }
747 }
748 
750 {
751  if (using_platform_ == true) publishJointStates();
752  else publishGazeboCommand();
753 
756 }
757 
758 void OpenManipulatorController::moveitTimer(double present_time)
759 {
760  static double priv_time = 0.0f;
761  static uint32_t step_cnt = 0;
762 
763  if (moveit_plan_state_ == true)
764  {
765  double path_time = present_time - priv_time;
766  if (path_time > moveit_sampling_time_)
767  {
768  JointWaypoint target;
769  uint32_t all_time_steps = joint_trajectory_.points.size();
770 
771  for(uint8_t i = 0; i < joint_trajectory_.points[step_cnt].positions.size(); i++)
772  {
773  JointValue temp;
774  temp.position = joint_trajectory_.points[step_cnt].positions.at(i);
775  temp.velocity = joint_trajectory_.points[step_cnt].velocities.at(i);
776  temp.acceleration = joint_trajectory_.points[step_cnt].accelerations.at(i);
777  target.push_back(temp);
778  }
779  open_manipulator_.makeJointTrajectory(target, path_time);
780 
781  step_cnt++;
782  priv_time = present_time;
783 
784  if (step_cnt >= all_time_steps)
785  {
786  step_cnt = 0;
787  moveit_plan_state_ = false;
789  {
790  log::warn("Could not update the start state! Enable External Communications at the Moveit Plugin");
791  }
792  std_msgs::Empty msg;
794  }
795  }
796  }
797  else
798  {
799  priv_time = present_time;
800  }
801 }
802 
804 {
805  moveitTimer(time);
807 }
808 
809 int main(int argc, char **argv)
810 {
811  ros::init(argc, argv, "open_manipulator_controller");
812  ros::NodeHandle node_handle("");
813 
814  std::string usb_port = "/dev/ttyUSB0";
815  std::string baud_rate = "1000000";
816 
817  if (argc < 3)
818  {
819  log::error("Please set '-port_name' and '-baud_rate' arguments for connected Dynamixels");
820  return 0;
821  }
822  else
823  {
824  usb_port = argv[1];
825  baud_rate = argv[2];
826  }
827 
828  OpenManipulatorController om_controller(usb_port, baud_rate);
829 
830  om_controller.initPublisher();
831  om_controller.initSubscriber();
832  om_controller.initServer();
833 
834  om_controller.startTimerThread();
835 
836  ros::Timer publish_timer = node_handle.createTimer(ros::Duration(om_controller.getControlPeriod()), &OpenManipulatorController::publishCallback, &om_controller);
837 
838  ros::Rate loop_rate(100);
839 
840  while (ros::ok())
841  {
842  ros::spinOnce();
843  loop_rate.sleep();
844  }
845 
846  return 0;
847 }
std::vector< Name > getAllToolComponentName()
void publish(const boost::shared_ptr< M > &message) const
void executeTrajGoalCallback(const moveit_msgs::ExecuteTrajectoryActionGoal::ConstPtr &msg)
bool goalToolControlCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool goalTaskSpacePathOrientationOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
bool setJointValueTarget(const std::vector< double > &group_variable_values)
void displayPlannedPathCallback(const moveit_msgs::DisplayTrajectory::ConstPtr &msg)
#define CUSTOM_TRAJECTORY_CIRCLE
f
std::vector< JointValue > getAllActiveJointValue()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool goalTaskSpacePathCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void makeCustomTrajectory(Name trajectory_name, Name tool_name, const void *arg, double move_time, std::vector< JointValue > present_joint_value={})
bool getActuatorEnabledState(Name actuator_name)
void processOpenManipulator(double present_time)
void moveGroupGoalCallback(const moveit_msgs::MoveGroupActionGoal::ConstPtr &msg)
KinematicPose getKinematicPose(Name component_name)
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)
bool goalTaskSpacePathFromPresentOrientationOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
bool goalTaskSpacePathPositionOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void spinner()
std::vector< JointValue > JointWaypoint
void makeTaskTrajectoryFromPresentPose(Name tool_name, Eigen::Vector3d position_meter, double move_time, std::vector< JointValue > present_joint_value={})
std::vector< Name > getAllActiveJointComponentName()
unsigned int index
bool setActuatorStateCallback(open_manipulator_msgs::SetActuatorState::Request &req, open_manipulator_msgs::SetActuatorState::Response &res)
#define CUSTOM_TRAJECTORY_HEART
void openManipulatorOptionCallback(const std_msgs::String::ConstPtr &msg)
bool goalTaskSpacePathFromPresentCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool goalJointSpacePathFromPresentCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
robot_state::RobotStatePtr getCurrentState(double wait=1)
OpenManipulatorController(std::string usb_port, std::string baud_rate)
void setMaxVelocityScalingFactor(double max_velocity_scaling_factor)
ROSCPP_DECL bool ok()
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
bool goalJointSpacePathCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
bool goalJointSpacePathToKinematicsPoseCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void makeJointTrajectory(std::vector< double > goal_joint_position, double move_time, std::vector< JointValue > present_joint_value={})
const std::string & getNamespace() const
#define JOINT_DYNAMIXEL
geometry_msgs::PoseStamped getCurrentPose(const std::string &end_effector_link="")
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool setKinematicsPoseMsgCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
bool goalTaskSpacePathFromPresentPositionOnlyCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void makeTaskTrajectory(Name tool_name, Eigen::Vector3d goal_position, double move_time, std::vector< JointValue > present_joint_value={})
bool sleep()
bool goalDrawingTrajectoryCallback(open_manipulator_msgs::SetDrawingTrajectory::Request &req, open_manipulator_msgs::SetDrawingTrajectory::Response &res)
bool calcPlannedPath(const std::string planning_group, open_manipulator_msgs::JointPosition msg)
uint32_t getNumSubscribers() const
void initOpenManipulator(bool using_actual_robot_state, STRING usb_port="/dev/ttyUSB0", STRING baud_rate="1000000", float control_loop_time=0.010)
void error(STRING str)
std::vector< JointValue > getAllToolValue()
bool goalJointSpacePathToKinematicsPositionCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
#define CUSTOM_TRAJECTORY_LINE
const std::vector< std::string > & getJointNames()
void makeJointTrajectoryFromPresentPosition(std::vector< double > delta_goal_joint_position, double move_time, std::vector< JointValue > present_joint_value={})
static Time now()
ROSCPP_DECL void shutdown()
#define CUSTOM_TRAJECTORY_RHOMBUS
planning_group_name
moveit::planning_interface::MoveGroupInterface * move_group_
bool goalJointSpacePathToKinematicsOrientationCallback(open_manipulator_msgs::SetKinematicsPose::Request &req, open_manipulator_msgs::SetKinematicsPose::Response &res)
void setMaxAccelerationScalingFactor(double max_acceleration_scaling_factor)
ROSCPP_DECL void spinOnce()
bool setJointPositionMsgCallback(open_manipulator_msgs::SetJointPosition::Request &req, open_manipulator_msgs::SetJointPosition::Response &res)
bool getJointPositionMsgCallback(open_manipulator_msgs::GetJointPosition::Request &req, open_manipulator_msgs::GetJointPosition::Response &res)
int main(int argc, char **argv)
bool getKinematicsPoseMsgCallback(open_manipulator_msgs::GetKinematicsPose::Request &req, open_manipulator_msgs::GetKinematicsPose::Response &res)
void makeToolTrajectory(Name tool_name, double tool_goal_position)


open_manipulator_controller
Author(s): Darby Lim , Hye-Jong KIM , Ryan Shim , Yong-Ho Na
autogenerated on Mon Jun 10 2019 14:12:06