-
Notifications
You must be signed in to change notification settings - Fork 1
/
bebop_agent.py
243 lines (208 loc) · 9.76 KB
/
bebop_agent.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import time
import random
import numpy as np
from collections import deque
import torch
import cv2
import rospy
import ros_numpy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image
from sensor_msgs.msg import Joy
from std_msgs.msg import Empty
# from cv_bridge import CvBridge, CvBridgeError
from agent import Agent
import constants
class BebopAgent(Agent):
def __init__(self, placeRecognition=None, navigation=None, teachCommandsFile=None):
super(BebopAgent, self).__init__(placeRecognition, navigation)
rospy.init_node('bebop_agent')
self.joySubscriber = rospy.Subscriber("/joy", Joy, self.joy_callback)
self.imageSubscriber = rospy.Subscriber("/bebop/image_raw", Image, self.image_callback)
self.commandPublisher = rospy.Publisher('/bebop/cmd_vel', Twist, queue_size=10)
self.takeoffPublisher = rospy.Publisher('/bebop/takeoff', Empty, queue_size=1)
self.landPublisher = rospy.Publisher('/bebop/land', Empty, queue_size=1)
self.closestMatchPublisher = rospy.Publisher('/closest_match', Image, queue_size=10)
self.futureMatchPublisher = rospy.Publisher('/future_match', Image, queue_size=10)
# self.bridge = CvBridge()
self.set_state(constants.ROBOT_MODE_IDLE)
self.latest_image = None
self.joy = None
self.goal = None
self.goal_index = 0
self.init = None
self.teachCommandsFile = teachCommandsFile
def image_callback(self, image):
self.latest_image = image
def joy_callback(self, joy):
self.joy = joy
if (joy.buttons[constants.JOY_BUTTONS_TEACH_ID]): # X
if (self.state == constants.ROBOT_MODE_TEACH_MANUALLY):
self.set_state(constants.ROBOT_MODE_IDLE)
self.process()
else:
self.set_state(constants.ROBOT_MODE_TEACH_MANUALLY)
elif (joy.buttons[constants.JOY_BUTTONS_LAND_ID]): # A
self.set_state(constants.ROBOT_MODE_IDLE)
self.landPublisher.publish(Empty())
print ("Landing..")
elif (joy.buttons[constants.JOY_BUTTONS_REPEAT_ID]): # B
self.set_state(constants.ROBOT_MODE_REPEAT)
elif (joy.buttons[constants.JOY_BUTTONS_TAKEOFF_ID]): # Y
self.set_state(constants.ROBOT_MODE_IDLE)
print ("Taking off..")
self.takeoffPublisher.publish(Empty())
elif (joy.buttons[constants.JOY_BUTTONS_IDLE_ID]): # LB
self.set_state(constants.ROBOT_MODE_IDLE)
elif (joy.buttons[constants.JOY_BUTTONS_MANUAL_CONTROL_ID]): # RB
self.set_state(constants.ROBOT_MODE_IDLE)
cmd_vel = Twist()
cmd_vel.linear.x = joy.axes[3]
cmd_vel.linear.y = joy.axes[2]
cmd_vel.linear.z = joy.axes[5]
cmd_vel.angular.z = joy.axes[0]
self.commandPublisher.publish(cmd_vel)
elif (joy.buttons[constants.JOY_BUTTONS_CLEAR_MEMORY_ID]): # LT
self.set_state(constants.ROBOT_MODE_IDLE)
self.sptm.clear()
print ("Memory has been cleared.")
def process(self):
self.sptm.build_graph()
goal, self.goal_index, similarity = self.sptm.find_closest(self.goal)
if (self.goal_index < 0):
print ("Cannot find goal")
def set_state(self, state):
self.state = state
print ("State: {}".format(constants.ROBOT_MODE_STRINGS[self.state]))
def hover(self):
cmd_stop_vel = Twist()
self.commandPublisher.publish(cmd_stop_vel)
time.sleep(constants.BEBOP_ACTION_STOP_DURATION) # let it stop completely
def take_action(self, action):
cmd_vel = Twist()
if action == 0:
cmd_vel.linear.x = constants.BEBOP_STRAIGHT_SPEED
elif action == 1:
cmd_vel.angular.z = -constants.BEBOP_YAW_SPEED
elif action == 2:
cmd_vel.angular.z = constants.BEBOP_YAW_SPEED
start = time.time()
duration = constants.BEBOP_ACTION_DURATION
while duration > time.time() - start:
self.commandPublisher.publish(cmd_vel)
time.sleep(constants.BEBOP_ACTION_FREQ)
def run(self):
if (self.teachCommandsFile != None):
teach_action_file = open(self.teachCommandsFile)
teach_actions = [int(val) for val in teach_action_file.read().split('\n') if val.isdigit()]
teach_index = 0
previous_state = None
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
if (self.state == constants.ROBOT_MODE_TEACH_MANUALLY):
if (self.latest_image == None):
print ("waiting for an image")
rate.sleep()
continue
# if (teach_index > 10):
# self.state = constants.ROBOT_MODE_IDLE
# print ("teaching is done")
# continue
current_state = ros_numpy.numpify(self.latest_image)
# start debug
# cv_image = current_state[...,::-1] # rgb to bgr
# cv2.imshow("image", cv_image)
# cv2.waitKey(10)
# end debug
rep, _ = self.sptm.append_keyframe(current_state, -1, False)
self.goal = current_state
time.sleep(constants.BEBOP_ACTION_STOP_DURATION)
teach_index = teach_index+1
print ("Teaching manually: index %d stored in memory" % (teach_index))
elif (self.state == constants.ROBOT_MODE_TEACH):
if (self.latest_image == None):
print ("waiting for an image")
rate.sleep()
continue
if teach_index >= len(teach_actions):
self.state = constants.ROBOT_MODE_IDLE
continue
"""
try:
cv_image = self.bridge.imgmsg_to_cv2(self.latest_image, "bgr8")
self.latest_image = None
except CvBridgeError as e:
print(e)
rate.sleep()
continue
current_state = np.asarray(cv_image)
"""
current_state = ros_numpy.numpify(self.latest_image)
# start debug
cv_image = current_state[...,::-1] # rgb to bgr
cv2.imshow("image", cv_image)
cv2.waitKey(10)
# end debug
action = teach_actions[teach_index]
rep, _ = self.sptm.append_keyframe(current_state, action, False)
print ("commanded walk: index %d action %d" % (teach_index, action))
self.take_action(action)
self.goal = current_state
teach_index = teach_index+1
elif (self.state == constants.ROBOT_MODE_REPEAT):
if (self.latest_image == None):
print ("waiting for an image")
rate.sleep()
continue
self.hover()
current_state = ros_numpy.numpify(self.latest_image)
if (previous_state is None):
previous_state = current_state
matched_index, similarity_score, best_velocity = self.sptm.relocalize(current_state)
# start debug
closest_match = self.sptm.memory[matched_index].state
closest_match_message = ros_numpy.msgify(Image, closest_match, encoding='rgb8')
self.closestMatchPublisher.publish(closest_match_message)
# end debug
path = self.sptm.find_shortest_path(matched_index, self.goal_index)
print (matched_index, similarity_score, best_velocity, path)
if (len(path) < 2): # achieved the goal
print ("Goal reached")
self.set_state(constants.ROBOT_MODE_IDLE)
continue
action, future_state = self.navigate(current_state, path, previous_action=-1)
# start debug
future_match_message = ros_numpy.msgify(Image, future_state, encoding='rgb8')
self.futureMatchPublisher.publish(future_match_message)
# end debug
# start debug
# from PIL import Image
# current_image = Image.fromarray(current_state)
# future_image = Image.fromarray(future_state)
# current_image.save("current.png", "PNG")
# future_image.save("future.png", "PNG")
# end debug
self.take_action(action)
previous_state = current_state
else: # IDLE
if (self.latest_image == None):
print ("waiting for an image")
rate.sleep()
continue
current_state = ros_numpy.numpify(self.latest_image)
matched_index, similarity_score, best_velocity = self.sptm.relocalize(current_state)
print (matched_index, similarity_score, best_velocity)
# start debug
if (matched_index >= 0):
closest_match = self.sptm.memory[matched_index].state
closest_match_message = ros_numpy.msgify(Image, closest_match, encoding='rgb8')
self.closestMatchPublisher.publish(closest_match_message)
# end debug
# start debug
# cv_image = self.sptm.memory[matched_index].state[...,::-1] # rgb to bgr
# cv2.imshow("image", cv_image)
# cv2.waitKey(10)
# end debug
time.sleep(constants.BEBOP_ACTION_STOP_DURATION)
# self.latest_image = None
rate.sleep()