qnode.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2019 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 
19 /*****************************************************************************
20 ** Includes
21 *****************************************************************************/
22 
23 #include <ros/ros.h>
24 #include <ros/network.h>
25 #include <string>
26 #include <std_msgs/String.h>
27 #include <sstream>
28 #include "../include/open_manipulator_p_control_gui/qnode.hpp"
29 
30 /*****************************************************************************
31 ** Namespaces
32 *****************************************************************************/
33 
35 
36 /*****************************************************************************
37 ** Implementation
38 *****************************************************************************/
39 
40 QNode::QNode(int argc, char** argv )
41  :init_argc(argc),
42  init_argv(argv),
43  open_manipulator_actuator_enabled_(false),
44  open_manipulator_is_moving_(false),
45  with_gripper_(false)
46 {}
47 
49  if(ros::isStarted()) {
50  ros::shutdown(); // explicitly needed since we use ros::start();
52  }
53  wait();
54 }
55 
56 bool QNode::init() {
57  ros::init(init_argc,init_argv,"open_manipulator_p_control_gui");
58  if ( ! ros::master::check() ) {
59  return false;
60  }
61  ros::start(); // explicitly needed since our nodehandle is going out of scope.
62  ros::NodeHandle n("");
63  ros::NodeHandle priv_n("~");
64 
65  // msg publisher
66  open_manipulator_option_pub_ = n.advertise<std_msgs::String>("option", 10);
67  // msg subscriber
71  // service client
72  goal_joint_space_path_client_ = n.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_joint_space_path");
73  goal_task_space_path_position_only_client_ = n.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path_position_only");
74  goal_task_space_path_client_= n.serviceClient<open_manipulator_msgs::SetKinematicsPose>("goal_task_space_path");
75  goal_tool_control_client_ = n.serviceClient<open_manipulator_msgs::SetJointPosition>("goal_tool_control");
76  set_actuator_state_client_ = n.serviceClient<open_manipulator_msgs::SetActuatorState>("set_actuator_state");
77  goal_drawing_trajectory_client_ = n.serviceClient<open_manipulator_msgs::SetDrawingTrajectory>("goal_drawing_trajectory");
78 
79  // ROS params
80  with_gripper_ = priv_n.param<bool>("with_gripper", false);
81 
82  start();
83  return true;
84 }
85 
86 void QNode::run() {
87  ros::Rate loop_rate(10);
88  while ( ros::ok() ) {
89  ros::spinOnce();
90  loop_rate.sleep();
91  }
92  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
93  Q_EMIT rosShutdown();
94 }
95 
96 void QNode::manipulatorStatesCallback(const open_manipulator_msgs::OpenManipulatorState::ConstPtr &msg)
97 {
98  if(msg->open_manipulator_moving_state == msg->IS_MOVING)
100  else
102 
103  if(msg->open_manipulator_actuator_state == msg->ACTUATOR_ENABLED)
105  else
107 }
108 void QNode::jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
109 {
110  std::vector<double> temp_angle;
111  temp_angle.resize(NUM_OF_JOINT_AND_TOOL - 1);
112  if (getWithGripperState()) temp_angle.resize(NUM_OF_JOINT_AND_TOOL);
113 
114  for(int i = 0; i < msg->name.size(); i ++)
115  {
116  if (!msg->name.at(i).compare("joint1")) temp_angle.at(0) = (msg->position.at(i));
117  else if(!msg->name.at(i).compare("joint2")) temp_angle.at(1) = (msg->position.at(i));
118  else if(!msg->name.at(i).compare("joint3")) temp_angle.at(2) = (msg->position.at(i));
119  else if(!msg->name.at(i).compare("joint4")) temp_angle.at(3) = (msg->position.at(i));
120  else if(!msg->name.at(i).compare("joint5")) temp_angle.at(4) = (msg->position.at(i));
121  else if(!msg->name.at(i).compare("joint6")) temp_angle.at(5) = (msg->position.at(i));
122 
123  if (getWithGripperState())
124  {
125  if(!msg->name.at(i).compare("gripper")) temp_angle.at(6) = (msg->position.at(i));
126  }
127  }
128  present_joint_angle_ = temp_angle;
129 }
130 
131 void QNode::kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
132 {
133  std::vector<double> temp_position;
134  temp_position.push_back(msg->pose.position.x);
135  temp_position.push_back(msg->pose.position.y);
136  temp_position.push_back(msg->pose.position.z);
137 
138  Eigen::Quaterniond temp_orientation(msg->pose.orientation.w, msg->pose.orientation.x, msg->pose.orientation.y, msg->pose.orientation.z);
139 
140  present_kinematics_position_ = temp_position;
141  present_kinematics_orientation_ = temp_orientation;
142 
143  kinematics_pose_.pose = msg->pose;
144 }
145 
146 std::vector<double> QNode::getPresentJointAngle()
147 {
148  return present_joint_angle_;
149 }
151 {
153 }
155 {
157 }
159 {
161 
163 }
165 {
167 }
169 {
171 }
173 {
174  return with_gripper_;
175 }
176 
177 void QNode::setOption(std::string opt)
178 {
179  std_msgs::String msg;
180  msg.data = opt;
182 }
183 
184 bool QNode::setJointSpacePath(std::vector<std::string> joint_name, std::vector<double> joint_angle, double path_time)
185 {
186  open_manipulator_msgs::SetJointPosition srv;
187  srv.request.joint_position.joint_name = joint_name;
188  srv.request.joint_position.position = joint_angle;
189  srv.request.path_time = path_time;
190 
192  {
193  return srv.response.is_planned;
194  }
195  return false;
196 }
197 
198 bool QNode::setTaskSpacePath(std::vector<double> kinematics_pose, double path_time)
199 {
200  open_manipulator_msgs::SetKinematicsPose srv;
201 
202  srv.request.end_effector_name = "gripper";
203 
204  srv.request.kinematics_pose.pose.position.x = kinematics_pose.at(0);
205  srv.request.kinematics_pose.pose.position.y = kinematics_pose.at(1);
206  srv.request.kinematics_pose.pose.position.z = kinematics_pose.at(2);
207 
208  srv.request.kinematics_pose.pose.orientation.w = kinematics_pose.at(3);
209  srv.request.kinematics_pose.pose.orientation.x = kinematics_pose.at(4);
210  srv.request.kinematics_pose.pose.orientation.y = kinematics_pose.at(5);
211  srv.request.kinematics_pose.pose.orientation.z = kinematics_pose.at(6);
212 
213  srv.request.path_time = path_time;
214 
216  {
217  return srv.response.is_planned;
218  }
219  return false;
220 }
221 
222 bool QNode::setDrawingTrajectory(std::string name, std::vector<double> arg, double path_time)
223 {
224  open_manipulator_msgs::SetDrawingTrajectory srv;
225  srv.request.end_effector_name = "gripper";
226  srv.request.drawing_trajectory_name = name;
227  srv.request.path_time = path_time;
228  for(int i = 0; i < arg.size(); i ++)
229  srv.request.param.push_back(arg.at(i));
230 
232  {
233  return srv.response.is_planned;
234  }
235  return false;
236 }
237 
238 bool QNode::setToolControl(std::vector<double> joint_angle)
239 {
240  open_manipulator_msgs::SetJointPosition srv;
241  srv.request.joint_position.joint_name.push_back("gripper");
242  srv.request.joint_position.position = joint_angle;
243 
245  {
246  return srv.response.is_planned;
247  }
248  return false;
249 }
250 
251 bool QNode::setActuatorState(bool actuator_state)
252 {
253  open_manipulator_msgs::SetActuatorState srv;
254  srv.request.set_actuator_state = actuator_state;
255 
257  {
258  return srv.response.is_planned;
259  }
260  return false;
261 }
262 
263 
264 } // namespace open_manipulator_p_control_gui
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::vector< double > getPresentJointAngle()
Definition: qnode.cpp:146
ROSCPP_DECL bool check()
ros::ServiceClient goal_tool_control_client_
Definition: qnode.hpp:128
ROSCPP_DECL void start()
void publish(const boost::shared_ptr< M > &message) const
bool setActuatorState(bool actuator_state)
Definition: qnode.cpp:251
ros::ServiceClient goal_drawing_trajectory_client_
Definition: qnode.hpp:130
void setOption(std::string opt)
Definition: qnode.cpp:177
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Subscriber open_manipulator_joint_states_sub_
Definition: qnode.hpp:121
Eigen::Quaterniond present_kinematics_orientation_
Definition: qnode.hpp:134
std::vector< double > present_kinematics_position_
Definition: qnode.hpp:133
void manipulatorStatesCallback(const open_manipulator_msgs::OpenManipulatorState::ConstPtr &msg)
Definition: qnode.cpp:96
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
Eigen::Quaterniond getPresentKinematicsOrientation()
Definition: qnode.cpp:154
Eigen::Vector3d convertQuaternionToRPYVector(const Eigen::Quaterniond &quaternion)
void jointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
Definition: qnode.cpp:108
#define NUM_OF_JOINT_AND_TOOL
Definition: qnode.hpp:52
ros::Subscriber open_manipulator_kinematics_pose_sub_
Definition: qnode.hpp:122
Eigen::Vector3d present_kinematics_orientation_rpy_
Definition: qnode.hpp:135
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ROSCPP_DECL bool ok()
std::vector< double > getPresentKinematicsPosition()
Definition: qnode.cpp:150
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
bool setJointSpacePath(std::vector< std::string > joint_name, std::vector< double > joint_angle, double path_time)
Definition: qnode.cpp:184
ROSCPP_DECL bool isStarted()
ros::Publisher open_manipulator_option_pub_
Definition: qnode.hpp:117
std::vector< double > present_joint_angle_
Definition: qnode.hpp:132
QNode(int argc, char **argv)
Definition: qnode.cpp:40
ROSCPP_DECL void shutdown()
open_manipulator_msgs::KinematicsPose kinematics_pose_
Definition: qnode.hpp:136
bool setDrawingTrajectory(std::string name, std::vector< double > arg, double path_time)
Definition: qnode.cpp:222
bool setTaskSpacePath(std::vector< double > kinematics_pose, double path_time)
Definition: qnode.cpp:198
ros::ServiceClient goal_joint_space_path_client_
Definition: qnode.hpp:125
ROSCPP_DECL void spinOnce()
Eigen::Vector3d getPresentKinematicsOrientationRPY()
Definition: qnode.cpp:158
ros::ServiceClient goal_task_space_path_position_only_client_
Definition: qnode.hpp:126
void kinematicsPoseCallback(const open_manipulator_msgs::KinematicsPose::ConstPtr &msg)
Definition: qnode.cpp:131
ros::Subscriber open_manipulator_states_sub_
Definition: qnode.hpp:120
ros::ServiceClient goal_task_space_path_client_
Definition: qnode.hpp:127
ROSCPP_DECL void waitForShutdown()
ros::ServiceClient set_actuator_state_client_
Definition: qnode.hpp:129
bool setToolControl(std::vector< double > joint_angle)
Definition: qnode.cpp:238


open_manipulator_p_control_gui
Author(s): Ryan Shim , Yong-Ho Na , Hye-Jong KIM
autogenerated on Thu Oct 22 2020 03:16:32