qnode.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2020 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: Ryan Shim */
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/turtlebot3_manipulation_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 {}
44 
46 {
47  if(ros::isStarted())
48  {
49  ros::shutdown(); // explicitly needed since we use ros::start();
51  }
52  wait();
53 }
54 
56 {
57  ros::init(init_argc,init_argv,"turtlebot3_manipulation_gui");
58  if ( ! ros::master::check() )
59  {
60  return false;
61  }
62  ros::start(); // explicitly needed since our nodehandle is going out of scope.
63  ros::NodeHandle n("");
64 
65  // Moveit
67  spinner.start();
68 
69  // Move group arm
70  std::string planning_group_name = "arm";
72 
73  // Move group gripper
74  std::string planning_group_name2 = "gripper";
76 
77  start();
78  return true;
79 }
80 
81 void QNode::run()
82 {
83  ros::Rate loop_rate(10);
84  while ( ros::ok() )
85  {
87  ros::spinOnce();
88  loop_rate.sleep();
89  }
90  std::cout << "Ros shutdown, proceeding to close the gui." << std::endl;
91  Q_EMIT rosShutdown();
92 }
93 
95 {
97  spinner.start();
98 
99  std::vector<double> jointValues = move_group_->getCurrentJointValues();
100  std::vector<double> jointValues2 = move_group2_->getCurrentJointValues();
101  std::vector<double> temp_angle;
102  temp_angle.push_back(jointValues.at(0));
103  temp_angle.push_back(jointValues.at(1));
104  temp_angle.push_back(jointValues.at(2));
105  temp_angle.push_back(jointValues.at(3));
106  temp_angle.push_back(jointValues2.at(0));
107  present_joint_angle_ = temp_angle;
108 
109  geometry_msgs::Pose current_pose = move_group_->getCurrentPose().pose;
110  std::vector<double> temp_position;
111  temp_position.push_back(current_pose.position.x);
112  temp_position.push_back(current_pose.position.y);
113  temp_position.push_back(current_pose.position.z);
114  present_kinematics_position_ = temp_position;
115 }
116 
117 std::vector<double> QNode::getPresentJointAngle()
118 {
119  return present_joint_angle_;
120 }
121 
123 {
125 }
126 
127 bool QNode::setJointSpacePath(std::vector<double> joint_angle, double path_time)
128 {
130  spinner.start();
131 
132  // Next get the current set of joint values for the group.
133  const robot_state::JointModelGroup* joint_model_group =
134  move_group_->getCurrentState()->getJointModelGroup("arm");
135 
136  moveit::core::RobotStatePtr current_state = move_group_->getCurrentState();
137 
138  std::vector<double> joint_group_positions;
139  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
140 
141  // Now, let's modify one of the joints, plan to the new joint space goal and visualize the plan.
142  joint_group_positions[0] = joint_angle.at(0); // radians
143  joint_group_positions[1] = joint_angle.at(1); // radians
144  joint_group_positions[2] = joint_angle.at(2); // radians
145  joint_group_positions[3] = joint_angle.at(3); // radians
146  move_group_->setJointValueTarget(joint_group_positions);
147 
149  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
150  if (success == false)
151  return false;
152 
153  move_group_->move();
154 
155  spinner.stop();
156  return true;
157 }
158 
159 bool QNode::setTaskSpacePath(std::vector<double> kinematics_pose, double path_time)
160 {
162  spinner.start();
163 
164  // Uncomment below to keep the end-effector parallel to the ground
165  /*
166  moveit_msgs::OrientationConstraint oc;
167  oc.link_name = "end_effector_link";
168  oc.header.frame_id = "link1";
169  oc.orientation.w = 1.0;
170  oc.absolute_x_axis_tolerance = 0.1;
171  oc.absolute_y_axis_tolerance = 0.1;
172  oc.absolute_z_axis_tolerance = 3.14;
173  oc.weight = 1.0;
174  moveit_msgs::Constraints constraints;
175  constraints.orientation_constraints.push_back(oc);
176  move_group_->setPathConstraints(constraints);
177  */
178 
179  geometry_msgs::Pose target_pose;
180  target_pose.position.x = kinematics_pose.at(0);
181  target_pose.position.y = kinematics_pose.at(1);
182  target_pose.position.z = kinematics_pose.at(2);
183  // move_group_->setPoseTarget(target_pose); // Cannot use setPoseTarget as the robot has only 4DOF.
185  target_pose.position.x,
186  target_pose.position.y,
187  target_pose.position.z);
188 
190  bool success = (move_group_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
191  if (success == false)
192  return false;
193 
194  move_group_->move();
195 
196  spinner.stop();
197  return true;
198 }
199 
200 bool QNode::setToolControl(std::vector<double> joint_angle)
201 {
203  spinner.start();
204 
205  // Next get the current set of joint values for the group.
206  const robot_state::JointModelGroup* joint_model_group =
207  move_group2_->getCurrentState()->getJointModelGroup("gripper");
208 
209  moveit::core::RobotStatePtr current_state = move_group2_->getCurrentState();
210 
211  std::vector<double> joint_group_positions;
212  current_state->copyJointGroupPositions(joint_model_group, joint_group_positions);
213 
214  // Now, let's modify one of the joints, plan to the new joint space goal and visualize the plan.
215  joint_group_positions[0] = joint_angle.at(0); // radians
216  move_group2_->setJointValueTarget(joint_group_positions);
217 
219  bool success = (move_group2_->plan(my_plan) == moveit::planning_interface::MoveItErrorCode::SUCCESS);
220  if (success == false)
221  return false;
222 
223  move_group2_->move();
224 
225  spinner.stop();
226  return true;
227 }
228 } // namespace turtlebot3_manipulation_gui
ROSCPP_DECL bool check()
bool setToolControl(std::vector< double > joint_angle)
Definition: qnode.cpp:200
ROSCPP_DECL void start()
moveit::planning_interface::MoveGroupInterface * move_group2_
Definition: qnode.hpp:111
bool setPositionTarget(double x, double y, double z, const std::string &end_effector_link="")
bool setJointValueTarget(const std::vector< double > &group_variable_values)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void spinner()
std::vector< double > present_kinematics_position_
Definition: qnode.hpp:107
std::vector< double > getPresentKinematicsPosition()
Definition: qnode.cpp:122
std::vector< double > present_joint_angle_
Definition: qnode.hpp:106
moveit::planning_interface::MoveGroupInterface * move_group_
Definition: qnode.hpp:110
robot_state::RobotStatePtr getCurrentState(double wait=1)
ROSCPP_DECL bool ok()
geometry_msgs::PoseStamped getCurrentPose(const std::string &end_effector_link="")
std::vector< double > getPresentJointAngle()
Definition: qnode.cpp:117
bool sleep()
void wait(int seconds)
ROSCPP_DECL bool isStarted()
bool setTaskSpacePath(std::vector< double > kinematics_pose, double path_time)
Definition: qnode.cpp:159
bool setJointSpacePath(std::vector< double > joint_angle, double path_time)
Definition: qnode.cpp:127
ROSCPP_DECL void shutdown()
planning_group_name
QNode(int argc, char **argv)
Definition: qnode.cpp:40
ROSCPP_DECL void spinOnce()
ROSCPP_DECL void waitForShutdown()


turtlebot3_manipulation_gui
Author(s): Darby Lim , Ryan Shim
autogenerated on Sun May 10 2020 03:49:19