@@ -40,6 +40,11 @@ using MoveGroupInterface = moveit::planning_interface::MoveGroupInterface;
4040class PickAndPlaceTf : public rclcpp ::Node
4141{
4242public:
43+ // グリッパの開閉角度
44+ inline static const double GRIPPER_OPEN = angles::from_degrees(60.0 );
45+ inline static const double GRIPPER_GRASP = angles::from_degrees(20.0 );
46+ inline static const double GRIPPER_CLOSE = 0.0 ;
47+
4348 PickAndPlaceTf (
4449 rclcpp::Node::SharedPtr move_group_arm_node,
4550 rclcpp::Node::SharedPtr move_group_gripper_node)
@@ -52,29 +57,7 @@ class PickAndPlaceTf : public rclcpp::Node
5257 move_group_gripper_ = std::make_shared<MoveGroupInterface>(move_group_gripper_node, " gripper" );
5358
5459 // SRDFに定義されている"home"の姿勢にする
55- move_group_arm_->setNamedTarget (" home" );
56- move_group_arm_->move ();
57-
58- // 可動範囲を制限する
59- moveit_msgs::msg::Constraints constraints;
60- constraints.name = " arm_constraints" ;
61-
62- moveit_msgs::msg::JointConstraint joint_constraint;
63- joint_constraint.joint_name = " crane_x7_lower_arm_fixed_part_joint" ;
64- joint_constraint.position = 0.0 ;
65- joint_constraint.tolerance_above = angles::from_degrees (30 );
66- joint_constraint.tolerance_below = angles::from_degrees (30 );
67- joint_constraint.weight = 1.0 ;
68- constraints.joint_constraints .push_back (joint_constraint);
69-
70- joint_constraint.joint_name = " crane_x7_upper_arm_revolute_part_twist_joint" ;
71- joint_constraint.position = 0.0 ;
72- joint_constraint.tolerance_above = angles::from_degrees (30 );
73- joint_constraint.tolerance_below = angles::from_degrees (30 );
74- joint_constraint.weight = 0.8 ;
75- constraints.joint_constraints .push_back (joint_constraint);
76-
77- move_group_arm_->setPathConstraints (constraints);
60+ move_arm_to_named_pose (" home" );
7861
7962 // 待機姿勢に移動する
8063 init_pose ();
@@ -94,6 +77,13 @@ class PickAndPlaceTf : public rclcpp::Node
9477 move_group_gripper_->move ();
9578 }
9679
80+ // アームを目標位置・姿勢(Pose)に動かす
81+ void move_arm_to_pose (const geometry_msgs::msg::Pose & pose)
82+ {
83+ move_group_arm_->setPoseTarget (pose);
84+ move_group_arm_->move ();
85+ }
86+
9787 // アームを目標位置(x, y, z [m])・姿勢(roll, pitch, yaw [deg])に動かす
9888 void control_arm (
9989 const double x, const double y, const double z,
@@ -106,10 +96,46 @@ class PickAndPlaceTf : public rclcpp::Node
10696 target_pose.position .z = z;
10797 q.setRPY (angles::from_degrees (roll), angles::from_degrees (pitch), angles::from_degrees (yaw));
10898 target_pose.orientation = tf2::toMsg (q);
109- move_group_arm_->setPoseTarget (target_pose);
99+ move_arm_to_pose (target_pose);
100+ }
101+
102+ // SRDFに定義された姿勢名でアームを動かす
103+ void move_arm_to_named_pose (const std::string & name)
104+ {
105+ move_group_arm_->setNamedTarget (name);
110106 move_group_arm_->move ();
111107 }
112108
109+ // アームの関節の一部に可動制限を設定する
110+ void set_constraints ()
111+ {
112+ moveit_msgs::msg::Constraints constraints;
113+ constraints.name = " arm_constraints" ;
114+
115+ moveit_msgs::msg::JointConstraint joint_constraint;
116+ joint_constraint.joint_name = " crane_x7_lower_arm_fixed_part_joint" ;
117+ joint_constraint.position = 0.0 ;
118+ joint_constraint.tolerance_above = angles::from_degrees (30 );
119+ joint_constraint.tolerance_below = angles::from_degrees (30 );
120+ joint_constraint.weight = 1.0 ;
121+ constraints.joint_constraints .push_back (joint_constraint);
122+
123+ joint_constraint.joint_name = " crane_x7_upper_arm_revolute_part_twist_joint" ;
124+ joint_constraint.position = 0.0 ;
125+ joint_constraint.tolerance_above = angles::from_degrees (30 );
126+ joint_constraint.tolerance_below = angles::from_degrees (30 );
127+ joint_constraint.weight = 0.8 ;
128+ constraints.joint_constraints .push_back (joint_constraint);
129+
130+ move_group_arm_->setPathConstraints (constraints);
131+ }
132+
133+ // 設定された関節可動制限をクリアする
134+ void clear_constraints ()
135+ {
136+ move_group_arm_->clearPathConstraints ();
137+ }
138+
113139private:
114140 void on_timer ()
115141 {
@@ -179,19 +205,15 @@ class PickAndPlaceTf : public rclcpp::Node
179205
180206 void picking (tf2::Vector3 target_position)
181207 {
182- const double GRIPPER_DEFAULT = 0.0 ;
183- const double GRIPPER_OPEN = angles::from_degrees (60.0 );
184- const double GRIPPER_CLOSE = angles::from_degrees (20.0 );
185-
186208 // 何かを掴んでいた時のためにハンドを開閉
187209 set_gripper_angle (GRIPPER_OPEN );
188- set_gripper_angle (GRIPPER_DEFAULT );
210+ set_gripper_angle (GRIPPER_CLOSE );
189211
190212 // ピック動作(掴みに行く)
191213 control_arm (target_position.x (), target_position.y (), target_position.z () + 0.12 , -180 , 0 , 90 );
192214 set_gripper_angle (GRIPPER_OPEN );
193215 control_arm (target_position.x (), target_position.y (), target_position.z () + 0.07 , -180 , 0 , 90 );
194- set_gripper_angle (GRIPPER_CLOSE );
216+ set_gripper_angle (GRIPPER_GRASP );
195217 control_arm (target_position.x (), target_position.y (), target_position.z () + 0.12 , -180 , 0 , 90 );
196218
197219 // プレース動作(移動して置く)
@@ -202,7 +224,7 @@ class PickAndPlaceTf : public rclcpp::Node
202224
203225 // 待機姿勢に戻る
204226 init_pose ();
205- set_gripper_angle (GRIPPER_DEFAULT );
227+ set_gripper_angle (GRIPPER_CLOSE );
206228 }
207229
208230 std::shared_ptr<MoveGroupInterface> move_group_arm_;
@@ -226,10 +248,14 @@ int main(int argc, char ** argv)
226248 auto pick_and_place_tf_node = std::make_shared<PickAndPlaceTf>(
227249 move_group_arm_node,
228250 move_group_gripper_node);
251+ pick_and_place_tf_node->set_constraints ();
252+
229253 exec.add_node (pick_and_place_tf_node);
230254 exec.add_node (move_group_arm_node);
231255 exec.add_node (move_group_gripper_node);
232256 exec.spin ();
257+
258+ pick_and_place_tf_node->clear_constraints ();
233259 rclcpp::shutdown ();
234260 return 0 ;
235261}
0 commit comments