joint_operator.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 
22  :node_handle_(""),
23  priv_node_handle_("~"),
24  is_loop_(false)
25 {
26  std::string yaml_file = node_handle_.param<std::string>("trajectory_info", "");
27  jnt_tra_msg_ = new trajectory_msgs::JointTrajectory;
28 
29  bool result = getTrajectoryInfo(yaml_file, jnt_tra_msg_);
30  if (result == false)
31  {
32  ROS_ERROR("Please check YAML file");
33  exit(0);
34  }
35 
36  joint_trajectory_pub_ = node_handle_.advertise<trajectory_msgs::JointTrajectory>("joint_trajectory", 100);
38 
39  is_loop_ = priv_node_handle_.param<bool>("is_loop", "false");
40 }
41 
43 {
44 }
45 
46 bool JointOperator::getTrajectoryInfo(const std::string yaml_file, trajectory_msgs::JointTrajectory *jnt_tra_msg)
47 {
48  YAML::Node file;
49  file = YAML::LoadFile(yaml_file.c_str());
50 
51  if (file == NULL)
52  return false;
53 
54  YAML::Node joint = file["joint"];
55  uint8_t joint_size = joint["names"].size();
56 
57  for (uint8_t index = 0; index < joint_size; index++)
58  {
59  std::string joint_name = joint["names"][index].as<std::string>();
60  jnt_tra_msg->joint_names.push_back(joint["names"][index].as<std::string>());
61  }
62 
63  YAML::Node motion = file["motion"];
64  uint8_t motion_size = motion["names"].size();
65 
66  for (uint8_t index = 0; index < motion_size; index++)
67  {
68  trajectory_msgs::JointTrajectoryPoint jnt_tra_point;
69 
70  std::string name = motion["names"][index].as<std::string>();
71  YAML::Node motion_name = motion[name];
72  for (uint8_t size = 0; size < joint_size; size++)
73  {
74  if (joint_size != motion_name["step"].size())
75  {
76  ROS_ERROR("Please check motion step size. It must be equal to joint size");
77  return 0;
78  }
79 
80  jnt_tra_point.positions.push_back(motion_name["step"][size].as<double>());
81 
82  ROS_INFO("motion_name : %s, step : %f", name.c_str(), motion_name["step"][size].as<double>());
83  }
84 
85  if (motion_name["time_from_start"] == NULL)
86  {
87  ROS_ERROR("Please check time_from_start. It must be set time_from_start each step");
88  return 0;
89  }
90 
91  jnt_tra_point.time_from_start.fromSec(motion_name["time_from_start"].as<double>());
92 
93  ROS_INFO("time_from_start : %f", motion_name["time_from_start"].as<double>());
94 
95  jnt_tra_msg->points.push_back(jnt_tra_point);
96  }
97 
98  return true;
99 }
100 
101 bool JointOperator::moveCommandMsgCallback(std_srvs::Trigger::Request &req,
102  std_srvs::Trigger::Response &res)
103 {
105 
106  res.success = true;
107  res.message = "Success to publish joint trajectory";
108 
109  return true;
110 }
111 
112 int main(int argc, char **argv)
113 {
114  // Init ROS node
115  ros::init(argc, argv, "joint_operator");
116  JointOperator joint_operator;
117 
118  ROS_INFO("For now, you can use publish joint trajectory msgs by triggering service(/execution)");
119 
120  if (joint_operator.isLoop())
121  {
122  while(1)
123  {
124  std_srvs::Trigger trig;
125  joint_operator.moveCommandMsgCallback(trig.request, trig.response);
126  }
127  }
128 
129  ros::spin();
130 
131  return 0;
132 }
ros::Publisher joint_trajectory_pub_
void publish(const boost::shared_ptr< M > &message) const
trajectory_msgs::JointTrajectory * jnt_tra_msg_
ros::NodeHandle node_handle_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool getTrajectoryInfo(const std::string yaml_file, trajectory_msgs::JointTrajectory *jnt_tra_msg)
int main(int argc, char **argv)
bool isLoop(void)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::ServiceServer move_command_server_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
bool moveCommandMsgCallback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::NodeHandle priv_node_handle_
#define ROS_ERROR(...)


dynamixel_workbench_operators
Author(s): Darby Lim , Ryan Shim
autogenerated on Mon Sep 28 2020 03:37:04