-
Notifications
You must be signed in to change notification settings - Fork 15
/
property_tour.py
329 lines (275 loc) · 10.8 KB
/
property_tour.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import enum
import math
import numpy as np
from shared.util.error_reporter import error_reporter as er
from shared.util.time_manager import time_manager as tm
from vehicle.skills.skills import Skill
from vehicle.skills.util import core
from vehicle.skills.util.motions import CableMotion
from vehicle.skills.util.motions import LookatMotion
from vehicle.skills.util.motions import OrbitMotion
from vehicle.skills.util.transform import Rot3
from vehicle.skills.util.transform import Transform
from vehicle.skills.util.ui import UiButton
from vehicle.skills.util.ui import UiSlider
class TourState(enum.Enum):
# Wait for the user to start the tour
SETUP = 0
# Execute the tour
GOTO = 1
# Tapped STOP, quit the tour
STOP = 5
class PropertyTour(Skill):
"""
Create a simple tour based on the front door of a house
and some basic size settings.
"""
USER_SETTINGS = (
UiSlider(
identifier='speed',
label='Speed',
detail="Maximum R1 speed during motion.",
units='m/s',
min_value=0.1,
max_value=8.0,
value=6.0,
),
UiSlider(
identifier='width',
label='Width',
detail="The width of the house, assuming R1 is centered.",
units='m',
min_value=5,
max_value=25,
value=10,
),
UiSlider(
identifier='depth',
label='Depth',
detail="The depth of the house, assuming R1 is at one end.",
units='m',
min_value=0,
max_value=50,
value=15,
),
UiSlider(
identifier='height',
label='Height',
detail="Max altitude to fly.",
units='m',
min_value=5,
max_value=30,
value=15,
),
UiSlider(
identifier='radius',
label='Radius',
detail="Orbit radius",
units='m',
min_value=5,
max_value=40,
value=15,
),
)
def __init__(self):
super(PropertyTour, self).__init__()
self.state = TourState.SETUP
self.has_subject = False
self.publish_downsampler = tm.DownSampler(0.5)
self.params = core.AttrDict()
self.params.speed = 4.0
self.params.min_turn_rate = 1 # [rad/update]
self.params.max_turn_rate = 20 # [rad/update]
self.params.distance_margin = 0.5 # [m]
self.params.angle_margin = math.radians(0.75) # [rad]
self.params.giveup_utime = tm.seconds_to_utime(3) # [s]
self.params.giveup_speed = 0.1 # [m/s]
self.motions = []
self.motion_index = None
# AR cache
self.ar_frames = [] # list of Transforms for each cube in the frame(s)
def allow_manual_control(self):
return self.state in (TourState.STOP, TourState.SETUP)
def button_pressed(self, api, button_id_pressed):
if button_id_pressed == 'go':
self.state = TourState.GOTO
# Remember the current location.
nav_T_cam = api.vehicle.get_camera_trans()
yaw = nav_T_cam.get_euler_angles()[2]
nav_T_cam_flat = Transform(
rotation=Rot3.Ypr(yaw, 0, 0),
translation=nav_T_cam.translation(),
)
width = self.get_value_for_user_setting('width')
depth = self.get_value_for_user_setting('depth')
height = self.get_value_for_user_setting('height')
radius = self.get_value_for_user_setting('radius')
# The first point is the home point.
nav_T_start = nav_T_cam.copy()
# Fly backward
nav_T_back = Transform(
rotation=nav_T_cam_flat.rotation(),
translation=nav_T_cam_flat * np.array([-radius / 2, 0, 0]),
)
# Fly Up
nav_T_top = Transform(
rotation=Rot3.Ypr(yaw, math.radians(30), 0),
translation=nav_T_cam_flat * np.array([-radius, 0, height])
)
# Fly over the house
nav_T_over = Transform(
rotation=nav_T_cam_flat.rotation(),
translation=nav_T_cam_flat * np.array([depth, 0, height / 2]),
)
# Crane down
nav_T_down = Transform(
rotation=Rot3.Ypr(yaw, math.radians(88), 0),
translation=nav_T_cam_flat * np.array([-radius / 4, 0, height]),
)
# Go left
nav_T_left = Transform(
rotation=nav_T_cam_flat.rotation(),
translation=nav_T_cam_flat * np.array([-5, width / 2, 0]),
)
# Go Right
nav_T_right = Transform(
rotation=nav_T_cam_flat.rotation(),
translation=nav_T_cam_flat * np.array([-5, -width / 2, 0]),
)
# The center of the house
nav_T_lookat = Transform(
rotation=nav_T_cam_flat.rotation(),
translation=nav_T_cam_flat * np.array([depth / 2, 0, 0]),
)
self.motions = [
# Cable backward as far as possible
CableMotion(nav_T_start, nav_T_top, self.params),
# Move while looking at the house
LookatMotion(
start_point=nav_T_top * np.array([0, width, 0]),
end_point=nav_T_top * np.array([0, -width, 0]),
lookat_point=nav_T_lookat.translation(),
params=self.params,
),
# Orbit around the property
OrbitMotion(nav_T_lookat.translation(), radius, height, 1, self.params),
# Fly down to the ground
CableMotion(nav_T_top, nav_T_back, self.params),
# Fly over the roof
CableMotion(nav_T_back, nav_T_over, self.params),
# Fly back and look down
CableMotion(nav_T_over, nav_T_down, self.params),
# Crane down to front door
CableMotion(nav_T_down, nav_T_start, self.params),
# Fly left
CableMotion(nav_T_start, nav_T_left, self.params),
# Fly right
CableMotion(nav_T_left, nav_T_right, self.params),
# Back to start
CableMotion(nav_T_right, nav_T_start, self.params),
# TODO:
# Various panoramas in different spots
]
self.motion_index = 0
er.REPORT_STATUS("go pressed")
elif button_id_pressed == 'skip':
er.REPORT_STATUS("skip")
self.motion_index += 1
if self.motion_index >= len(self.motions):
self.motion_index = 0
if button_id_pressed == 'goto_cable':
self.state = TourState.GOTO
if button_id_pressed == 'stop':
er.REPORT_STATUS('stopped')
api.subject.cancel_subject_tracking(api.utime)
self.state = TourState.STOP
def get_motion(self):
if self.motion_index is None or self.motion_index >= len(self.motions):
return None
return self.motions[self.motion_index]
def update(self, api):
# Stop tracking
api.subject.cancel_subject_tracking(api.utime)
# Update the speed dynamically, in case the user changes it
self.params.speed = self.get_value_for_user_setting('speed')
if self.allow_manual_control():
api.phone.enable_movement_commands()
else:
api.phone.disable_movement_commands()
if api.planner.is_landing():
er.REPORT_STATUS("Exiting due to planner landing")
api.skills.request_skill(api.utime, 'Basic')
self.has_subject = api.subject.is_following_subject(api.utime)
# Execute the current motion.
if api.vehicle.get_pose():
if self.state == TourState.GOTO:
motion = self.get_motion()
if motion:
motion.update(api)
if motion.done:
motion.reset(api)
er.REPORT_STATUS("motion done, index ++ {}", self.motion_index)
self.motion_index += 1
else:
er.REPORT_STATUS("motion in progress")
else:
er.REPORT_STATUS("No more motions")
self.state = TourState.STOP
else:
er.REPORT_QUIET("Not active")
else:
er.REPORT_QUIET("Vehicle not ready")
# Update the ui periodically
if self.publish_downsampler.ready(api.utime):
self.set_needs_layout()
def get_onscreen_controls(self, api):
title = ''
detail_text = ''
buttons = []
show_stop = True
controls_enabled = False
targets_enabled = False
show_slider = False
promoted_control_id = ''
battery_low_land = api.health_monitor.is_battery_critically_low()
if battery_low_land:
title = 'Low Battery'
detail_text = 'Tour disabled'
controls_enabled = True
elif self.state == TourState.SETUP:
title = 'Press Go to Begin Tour'
detail_text = 'R1 will automatically film the area.'
controls_enabled = True
show_stop = False
targets_enabled = False
buttons.append(UiButton('go', 'Go', style='PRIMARY'))
promoted_control_id = 'go'
elif self.state == TourState.GOTO:
title = 'Tour in Progress'
detail_text = "Step {}".format(self.motion_index)
show_slider = True
targets_enabled = False
buttons.append(UiButton('skip', label='Skip'))
elif self.state == TourState.STOP:
title = 'Press Go to Start New Tour'
controls_enabled = True
show_stop = False
targets_enabled = False
buttons.append(UiButton('go', 'Go', style='PRIMARY'))
promoted_control_id = 'go'
else:
er.REPORT_WARNING('Unknown state "{}"'.format(self.state))
return dict(
title=title,
detail=detail_text,
arrows_enabled=controls_enabled,
height_slider_enabled=controls_enabled,
zoom_slider_enabled=controls_enabled and self.has_subject,
steering_enabled=controls_enabled and not self.has_subject,
tap_targets_enabled=targets_enabled,
double_tap_enabled=controls_enabled,
drag_enabled=controls_enabled,
show_stop=show_stop,
promoted_control='speed' if show_slider else promoted_control_id,
buttons=buttons,
)