-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick_and_move.cpp
147 lines (125 loc) · 5.47 KB
/
pick_and_move.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2022 RT Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Reference:
// https://github.com/ros-planning/moveit2/blob/main/moveit_demo_nodes
// /run_move_group/src/run_move_group.cpp
#include <cmath>
#include "angles/angles.h"
#include "moveit/move_group_interface/move_group_interface.h"
#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/pose.hpp"
#include "geometry_msgs/msg/quaternion.hpp"
#include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
using MoveGroupInterface = moveit::planning_interface::MoveGroupInterface;
static const rclcpp::Logger LOGGER = rclcpp::get_logger("gripper_control");
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
rclcpp::NodeOptions node_options;
node_options.automatically_declare_parameters_from_overrides(true);
auto move_group_arm_node = rclcpp::Node::make_shared("move_group_arm_node", node_options);
auto move_group_gripper_node = rclcpp::Node::make_shared("move_group_gripper_node", node_options);
// For current state monitor
rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(move_group_arm_node);
executor.add_node(move_group_gripper_node);
std::thread([&executor]() {executor.spin();}).detach();
MoveGroupInterface move_group_arm(move_group_arm_node, "arm");
move_group_arm.setMaxVelocityScalingFactor(1.0); // Set 0.0 ~ 1.0
move_group_arm.setMaxAccelerationScalingFactor(1.0); // Set 0.0 ~ 1.0
MoveGroupInterface move_group_gripper(move_group_gripper_node, "gripper");
move_group_gripper.setMaxVelocityScalingFactor(1.0); // Set 0.0 ~ 1.0
move_group_gripper.setMaxAccelerationScalingFactor(1.0); // Set 0.0 ~ 1.0
auto gripper_joint_values = move_group_gripper.getCurrentJointValues();
// SRDFに定義されている"home"の姿勢にする
move_group_arm.setNamedTarget("home");
move_group_arm.move();
gripper_joint_values[0] = angles::from_degrees(60);
move_group_gripper.setJointValueTarget(gripper_joint_values);
move_group_gripper.move();
// 可動範囲を制限する
moveit_msgs::msg::Constraints constraints;
constraints.name = "arm_constraints";
moveit_msgs::msg::JointConstraint joint_constraint;
joint_constraint.joint_name = "crane_x7_lower_arm_fixed_part_joint";
joint_constraint.position = 0.0;
joint_constraint.tolerance_above = angles::from_degrees(30);
joint_constraint.tolerance_below = angles::from_degrees(30);
joint_constraint.weight = 1.0;
constraints.joint_constraints.push_back(joint_constraint);
joint_constraint.joint_name = "crane_x7_upper_arm_revolute_part_twist_joint";
joint_constraint.position = 0.0;
joint_constraint.tolerance_above = angles::from_degrees(30);
joint_constraint.tolerance_below = angles::from_degrees(30);
joint_constraint.weight = 0.8;
constraints.joint_constraints.push_back(joint_constraint);
move_group_arm.setPathConstraints(constraints);
//アームの手先を物体へ
geometry_msgs::msg::Pose target_pose;
tf2::Quaternion q;
target_pose.position.x = 0.3;
target_pose.position.y = -0.15;
target_pose.position.z = 0.1;
q.setRPY(angles::from_degrees(90), angles::from_degrees(0), angles::from_degrees(90));
target_pose.orientation = tf2::toMsg(q);
move_group_arm.setPoseTarget(target_pose);
move_group_arm.move();
//掴む
gripper_joint_values[0] = angles::from_degrees(20);
move_group_gripper.setJointValueTarget(gripper_joint_values);
move_group_gripper.move();
// 持ち上げる
target_pose.position.x = 0.3;
target_pose.position.y = 0.0;
target_pose.position.z = 0.3;
q.setRPY(angles::from_degrees(90), angles::from_degrees(0), angles::from_degrees(90));
target_pose.orientation = tf2::toMsg(q);
move_group_arm.setPoseTarget(target_pose);
move_group_arm.move();
//物体を掴んだまま移動する
target_pose.position.x = 0.3;
target_pose.position.y = 0.15;
target_pose.position.z = 0.1;
q.setRPY(angles::from_degrees(90), angles::from_degrees(0), angles::from_degrees(90));
target_pose.orientation = tf2::toMsg(q);
move_group_arm.setPoseTarget(target_pose);
move_group_arm.move();
/*
// 移動する
target_pose.position.x = 0.2;
target_pose.position.y = 0.2;
target_pose.position.z = 0.1;
q.setRPY(angles::from_degrees(-90), angles::from_degrees(0), angles::from_degrees(-90));
target_pose.orientation = tf2::toMsg(q);
move_group_arm.setPoseTarget(target_pose);
move_group_arm.move();
// 下ろす
target_pose.position.x = 0.0;
target_pose.position.y = 0.2;
target_pose.position.z = 0.1;
q.setRPY(angles::from_degrees(-90), angles::from_degrees(0), angles::from_degrees(-90));
target_pose.orientation = tf2::toMsg(q);
move_group_arm.setPoseTarget(target_pose);
move_group_arm.move();
*/
gripper_joint_values[0] = angles::from_degrees(60);
move_group_gripper.setJointValueTarget(gripper_joint_values);
move_group_gripper.move();
// 可動範囲の制限を解除
move_group_arm.clearPathConstraints();
move_group_arm.setNamedTarget("home");
move_group_arm.move();
rclcpp::shutdown();
return 0;
}