obstacle_avoidance_example.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright 2019 RT Corporation
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 import rospy
19 import moveit_commander
20 from geometry_msgs.msg import Pose, PoseStamped
21 from tf.transformations import quaternion_from_euler
22 from crane_x7_examples.srv import ObstacleAvoidance, ObstacleAvoidanceResponse
23 
24 import copy
25 import math
26 
27 server = None
28 
30  global server
31  server.shutdown('rospy shutdown')
32 
33  # shutdown時にvertical姿勢へ移行する
34  print('shutdown')
35  arm = moveit_commander.MoveGroupCommander("arm")
36  arm.set_max_velocity_scaling_factor(0.1)
37  arm.set_max_acceleration_scaling_factor(1.0)
38  arm.set_named_target("vertical")
39  arm.go()
40 
41 
42 def callback(req):
43  SLEEP_TIME = 1.0 # sceneを更新するための待ち時間
44 
45  arm = moveit_commander.MoveGroupCommander("arm")
46  arm.set_max_velocity_scaling_factor(0.1)
47  arm.set_max_acceleration_scaling_factor(1.0)
48  scene = moveit_commander.PlanningSceneInterface()
49  rospy.sleep(SLEEP_TIME)
50 
51  # 障害物を取り除く
52  scene.remove_world_object()
53  rospy.sleep(SLEEP_TIME)
54 
55  # 安全のため床を障害物として生成する
56  floor_name = "floor"
57  floor_size = (2.0, 2.0, 0.01)
58  floor_pose = PoseStamped()
59  floor_pose.header.frame_id = "/base_link"
60  floor_pose.pose.position.z = -floor_size[2]/2.0
61  scene.add_box(floor_name, floor_pose, floor_size)
62  rospy.sleep(SLEEP_TIME)
63 
64  # 障害物を設置する
65  if req.obstacle_enable:
66  scene.add_box(req.obstacle_name, req.obstacle_pose_stamped,
67  (req.obstacle_size.x, req.obstacle_size.y, req.obstacle_size.z))
68  rospy.sleep(SLEEP_TIME)
69 
70  result = True
71  # home姿勢に移行
72  arm.set_named_target('home')
73  if arm.go() is False:
74  result = False
75 
76  # スタート姿勢に移行
77  arm.set_pose_target(req.start_pose)
78  if arm.go() is False:
79  result = False
80 
81  # ゴール姿勢に移行
82  arm.set_pose_target(req.goal_pose)
83  if arm.go() is False:
84  result = False
85 
86  # home姿勢に移行
87  arm.set_named_target('home')
88  if arm.go() is False:
89  result = False
90 
91  # 障害物を取り除く
92  scene.remove_world_object()
93  rospy.sleep(SLEEP_TIME)
94 
95  return ObstacleAvoidanceResponse(result)
96 
97 
98 def main():
99  global server
100 
101  rospy.init_node("obstacle_avoidance_example")
102  rospy.on_shutdown(hook_shutdown)
103 
104  server = rospy.Service('~obstacle_avoidance', ObstacleAvoidance, callback)
105  print('Ready to avoid obstacles')
106 
107  rospy.spin()
108 
109 
110 if __name__ == '__main__':
111  try:
112  if not rospy.is_shutdown():
113  main()
114  except rospy.ROSInterruptException:
115  pass
obstacle_avoidance_example.main
def main()
Definition: obstacle_avoidance_example.py:98
obstacle_avoidance_example.callback
def callback(req)
Definition: obstacle_avoidance_example.py:42
obstacle_avoidance_example.hook_shutdown
def hook_shutdown()
Definition: obstacle_avoidance_example.py:29


crane_x7_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Mon Oct 2 2023 02:39:27