-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.py
121 lines (92 loc) · 3.4 KB
/
back.py
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
#!/usr/bin/env python
import rospy
import roslib
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
from geometry_msgs.msg import Pose, Point, Quaternion
from std_msgs.msg import String
class GoToPose():
def __init__(self):
self.goal_sent = False
# What to do if shut down (e.g. Ctrl-C or failure)
##rospy.on_shutdown(self.shutdown)
# Tell the action client that we want to spin a thread by default
self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
rospy.loginfo("Wait for the action server to come up")
# Allow up to 5 seconds for the action server to come up
self.move_base.wait_for_server(rospy.Duration(5))
def goto(self, pos, quat):
# Send a goal
self.goal_sent = True
goal = MoveBaseGoal()
goal.target_pose.header.frame_id = 'map'
goal.target_pose.header.stamp = rospy.Time.now()
goal.target_pose.pose = Pose(Point(pos['x'], pos['y'], 0.000),
Quaternion(quat['r1'], quat['r2'], quat['r3'], quat['r4']))
# Start moving
self.move_base.send_goal(goal)
# Allow TurtleBot up to 60 seconds to complete task
success = self.move_base.wait_for_result(rospy.Duration(180))
state = self.move_base.get_state()
result = False
if success and state == GoalStatus.SUCCEEDED:
# We made it!
result = True
else:
self.move_base.cancel_goal()
self.goal_sent = False
return result
# def shutdown(self):
# if self.goal_sent:
# self.move_base.cancel_goal()
# rospy.loginfo("Stop")
# rospy.sleep(1)
global s # departure state variable
s = None
def callback(data):
global s
s = data.data
def listener2():
#rospy.init_node('departure_listener',anonymous=True)
rospy.Subscriber("MSB__departure",String,callback)
def arriv_pub(data):
#rospy.init_node('arriv_pub',anonymous=True)
pub = rospy.Publisher('2arrival__MSB',String,queue_size=10)
arr_st = "%s"%data
pub.publish(arr_st)
if __name__ == '__main__':
global s
try:
listener2()
while True:
if s == '4':
navigator = GoToPose()
break
#if s == '1':
position1 = {'x': -12.9 ,'y' : -2.6 }
quaternion1 = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}
rospy.loginfo(s)
rospy.loginfo("Go to (%s, %s) pose", position1['x'], position1['y'])
success = navigator.goto(position1, quaternion1)
position2 = {'x': -3.54 ,'y' : 5.63 }
quaternion2 = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}
rospy.loginfo("Go to (%s, %s) pose", position2['x'], position2['y'])
success = navigator.goto(position2, quaternion2)
position3 = {'x': 0.0847 ,'y' : 0.049 }
quaternion3 = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}
rospy.loginfo("Go to (%s, %s) pose", position3['x'], position3['y'])
success = navigator.goto(position3, quaternion3)
if success:
rospy.loginfo("Hooray, reached the desired pose")
while True:
arriv_pub(5)
listener2()
if s == '6': # From MSB ok sign.
break
else:
rospy.loginfo("The base failed to reach the desired pose")
# Sleep to give the last log messages time to be sent
rospy.sleep(1)
except rospy.ROSInterruptException:
rospy.loginfo("Ctrl-C caught. Quitting")