obstacle_avoidance_example.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import rospy
5 import moveit_commander
6 from geometry_msgs.msg import Pose, PoseStamped
7 from tf.transformations import quaternion_from_euler
8 from crane_x7_examples.srv import ObstacleAvoidance, ObstacleAvoidanceResponse
9 
10 import copy
11 import math
12 
13 server = None
14 
16  global server
17  server.shutdown('rospy shutdown')
18 
19  # shutdown時にvertical姿勢へ移行する
20  print('shutdown')
21  arm = moveit_commander.MoveGroupCommander("arm")
22  arm.set_max_velocity_scaling_factor(0.1)
23  arm.set_max_acceleration_scaling_factor(1.0)
24  arm.set_named_target("vertical")
25  arm.go()
26 
27 
28 def callback(req):
29  SLEEP_TIME = 1.0 # sceneを更新するための待ち時間
30 
31  arm = moveit_commander.MoveGroupCommander("arm")
32  arm.set_max_velocity_scaling_factor(0.1)
33  arm.set_max_acceleration_scaling_factor(1.0)
34  scene = moveit_commander.PlanningSceneInterface()
35  rospy.sleep(SLEEP_TIME)
36 
37  # 障害物を取り除く
38  scene.remove_world_object()
39  rospy.sleep(SLEEP_TIME)
40 
41  # 安全のため床を障害物として生成する
42  floor_name = "floor"
43  floor_size = (2.0, 2.0, 0.01)
44  floor_pose = PoseStamped()
45  floor_pose.header.frame_id = "/base_link"
46  floor_pose.pose.position.z = -floor_size[2]/2.0
47  scene.add_box(floor_name, floor_pose, floor_size)
48  rospy.sleep(SLEEP_TIME)
49 
50  # 障害物を設置する
51  if req.obstacle_enable:
52  scene.add_box(req.obstacle_name, req.obstacle_pose_stamped,
53  (req.obstacle_size.x, req.obstacle_size.y, req.obstacle_size.z))
54  rospy.sleep(SLEEP_TIME)
55 
56  result = True
57  # home姿勢に移行
58  arm.set_named_target('home')
59  if arm.go() is False:
60  result = False
61 
62  # スタート姿勢に移行
63  arm.set_pose_target(req.start_pose)
64  if arm.go() is False:
65  result = False
66 
67  # ゴール姿勢に移行
68  arm.set_pose_target(req.goal_pose)
69  if arm.go() is False:
70  result = False
71 
72  # home姿勢に移行
73  arm.set_named_target('home')
74  if arm.go() is False:
75  result = False
76 
77  # 障害物を取り除く
78  scene.remove_world_object()
79  rospy.sleep(SLEEP_TIME)
80 
81  return ObstacleAvoidanceResponse(result)
82 
83 
84 def main():
85  global server
86 
87  rospy.init_node("obstacle_avoidance_example")
88  rospy.on_shutdown(hook_shutdown)
89 
90  server = rospy.Service('~obstacle_avoidance', ObstacleAvoidance, callback)
91  print('Ready to avoid obstacles')
92 
93  rospy.spin()
94 
95 
96 if __name__ == '__main__':
97  try:
98  if not rospy.is_shutdown():
99  main()
100  except rospy.ROSInterruptException:
101  pass


crane_x7_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Mon Mar 1 2021 03:18:34