-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparallax_task.py
432 lines (358 loc) · 13.6 KB
/
parallax_task.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
Parallax Jumping Task
Pilot:
- 2x FLIR cameras at 100fps
- 9DOF sensor
Child:
- 2x opencv cameras at 30fps
"""
import itertools
import tables
import typing
from collections import OrderedDict as odict
import threading
from datetime import datetime
import numpy as np
from autopilot.tasks import Task
from autopilot.networking import Net_Node
from autopilot import prefs
TASK = 'Parallax'
class Parallax(Task):
STAGE_NAMES = ["request", "jump", "reinforcement"]
PARAMS = odict()
PARAMS['reward'] = {
'tag': 'Reward Duration (ms)',
'type': 'int'
}
PARAMS['request_bbox'] = {
'tag': 'Bounding Box of request platform, like [top, left, width, height] in pixels',
'type': 'list'
}
PARAMS['platform_height'] = {
'tag': 'Height to raise platform to on request, in mm',
'type': 'float'
}
PARAMS['platform_widths'] = {
'tag': 'Centered widths of pillars to pick from like [2,4,6]',
'type': 'list'
}
PARAMS['platform_distances'] = {
'tag': 'Distances of single rows to raise to pick from like [0, 1, 5]',
'type': 'list'
}
PARAMS['velocity_multipliers'] = {
'tag': 'Scale factors for converting IMU velocity to platform velocity, as a list like [0, -1, 0.5]',
'type': 'list'
}
PARAMS['timeout'] = {
'tag': 'Time from request to aborting trial if mouse has not jumped, in seconds',
'type': 'float'
},
PARAMS['bail_height'] = {
'tag': 'Height below which to consider the trial bailed',
'type': 'int'
}
PARAMS['dlc_model_name'] = {
'tag': 'name of DLC model to use, model files must be in <autopilot_dir>/dlc on the processing child',
'type': 'str'
},
PARAMS['dlc_use_point'] = {
'tag': 'Name of point from DLC model to use in motion estimation and task control',
'type': 'str'
}
PARAMS['dlc_platform_points'] = {
'tag': 'Names of left and right points used to track the platform lip (as a list)',
'type': 'list'
}
PARAMS['dlc_jumpoff_points'] = {
'tag': 'Names of left and right points used to track the platform the mouse jumps from (as a list)',
'type': 'list'
}
PARAMS['child_dlc_id'] = {
'tag': 'ID of agent that is running the DLC transformation',
'type': 'str'
}
PARAMS['child_motion_id'] = {
'tag': 'ID of agent that is running the motion controller',
'type': 'str'
}
PARAMS['node_port'] = {
'tag': 'Port to receive messages from our children',
'type': 'int'
}
PLOT = {
'data': {
'accel_y': 'shaded'
},
# 'video' : ['SIDE', 'EYE', 'POV'],
'video': ['SIDE'],
'continuous': True
}
class TrialData(tables.IsDescription):
trial_num = tables.Int32Col()
platform_width = tables.Int32Col()
platform_distance = tables.Int32Col()
timestamp_request = tables.StringCol(26)
timestamp_raised = tables.StringCol(26)
timestamp_jumped = tables.StringCol(26)
bailed = tables.BoolCol()
multiplier = tables.Float32Col()
# since continuous data is one folder per session, one table per stream, specify with dict
ContinuousData = {
'accel_x': tables.Float64Col(),
'accel_y': tables.Float64Col(),
'accel_z': tables.Float64Col(),
'SIDE': 'infer',
'velocity': tables.Float64Col(),
'pose': 'infer'
}
HARDWARE = {
'PLAT':{
'FORM': "Parallax_Platform"
}
}
CHILDREN = {
'SENSOR': {
'task_type': "Parallax Child",
'cams': [
{'type': 'Camera_OpenCV',
'name': 'POV',
'camera_idx': 0,
'stream': True
},
# {'type': 'Camera_OpenCV',
# 'name': 'head_2',
# 'camera_idx': 2,
# 'stream': True,
# }
]
}
}
def __init__(self, reward:float=20, request_bbox:tuple=(0,0,128,128),
platform_height:float=50,
platform_widths:typing.Tuple[int, ...]=(2, 4, 6),
platform_distances:typing.Tuple[int, ...]=(0,1,2,3,4,5),
velocity_multipliers:typing.Tuple[float, ...] = (1,),
timeout:float=10,
bail_height:int=None,
dlc_model_name:typing.Optional[str]=None,
dlc_use_point:typing.Optional[str] = None,
dlc_platform_points:typing.Optional[list] = None,
dlc_jumpoff_points:typing.Optional[list] = None,
child_dlc_id: typing.Optional[str] = None,
child_motion_id: typing.Optional[str] = None,
node_port:int=5570,
stage_block = None, *args, **kwargs):
super(Parallax, self).__init__(*args, **kwargs)
# store parameters
self.reward = float(reward)
self.request_bbox = (int(bbox) for bbox in request_bbox)
self.platform_height = float(platform_height)
self.platform_widths = (int(width) for width in platform_widths)
self.platform_distances = (int(distance) for distance in platform_distances)
self.velocity_multipliers = (float(vel) for vel in velocity_multipliers)
self.timeout = float(timeout)
self.bail_height = bail_height
self.dlc_model_name = dlc_model_name
self.dlc_use_point = dlc_use_point
self.dlc_platform_points = dlc_platform_points
self.dlc_jumpoff_points = dlc_jumpoff_points
self.child_dlc_id = child_dlc_id
self.child_motion_id = child_motion_id
self.stage_block = stage_block
self.node_port = node_port
self.subject = kwargs.get('subject', prefs.get('SUBJECT'))
# init attributes
self.velocity_multiplier = 0
"""
Current velocity multiplier to use when setting platform velocity
"""
self.velocity_active = threading.Event()
"""
:class:`threading.Event` that is set when velocity readings from the IMU are
to be fed to the :class:`~.hardware.esoteric.Parallax_Platform` , multiplied
by :attr:`~.velocity_multiplier`
"""
self.platform_width = None
"""
int: current platform width
"""
self.platform_distance = None
"""
int: current platform distance
"""
self.current_stage = None
"""
str: name of current stage
"""
self.init_hardware()
# initialize net node for communicating with child
self.node_id ="{}_TASK".format(prefs.get('NAME'))
self.node = Net_Node(id=self.node_id,
upstream=prefs.get('NAME'),
port=prefs.get('MSGPORT'),
router_port=node_port,
listens = {
'MOTION': self.l_motion
},
instance = True)
# stream to send continuous data back to Terminal
self.stream = self.node.get_stream(
id="{}_TASK_STREAM".format(prefs.get('NAME')),
key='CONTINUOUS'
)
## ------------------
# Children
# start DLC child
transform_descriptor = [
{
'transform': 'image.DLC',
'kwargs': {
'model_dir': self.dlc_model_name
}
}
]
# # this will start the DLC transformer, see tasks/children::Transformer
# self.node.send(
# to=[prefs.get('NAME'), self.child_dlc_id],
# key='START',
# value= {
# 'child': {'parent': prefs.get('NAME'), 'subject': self.subject},
# 'task_type': 'Transformer',
# 'subject': self.subject,
# 'operation': 'stream',
# 'transform': transform_descriptor,
# 'return_id': self.node_id,
# 'return_ip': self.node.ip,
# 'return_port': self.node_port,
# 'return_key': 'DLC'
# }
# )
# start IMU child
self.node.send(
to = [prefs.get('NAME'), self.child_motion_id],
key='START',
value = {
'child': {'parent': prefs.get('NAME'), 'subject': self.subject},
'task_type': 'Parallax_Child',
'subject': self.subject,
'return_id': self.node_id,
'return_ip': self.node.ip,
'return_port': self.node_port,
'return_key': 'MOTION',
'device': ('I2C', 'IMU'),
}
)
## -------------------
self.stages = itertools.cycle([self.request, self.jump, self.reinforcement])
self.n_trials = itertools.count()
# --------------------------------------------------
# level platform
self.hardware['PLAT']['FORM'].level()
# self.hardware['CAMS']['EYE'].capture()
# self.hardware['CAMERAS']['SIDE'].stream(to="T")
# self.hardware['CAMERAS']['SIDE'].capture()
# --------------------------------------------------
# Stage methods
# --------------------------------------------------
def request(self):
# self.stage_block.clear()
# prevent calling any triggers rn
self.trigger_lock.acquire()
self.triggers = {}
self.current_stage = "request"
# reset state flags
self.velocity_active.clear()
# calculate stage params
self.platform_width = np.random.choice(self.platform_widths)
self.platform_distance = np.random.choice(self.platform_distances)
self.velocity_multiplier = np.random.choice(self.velocity_multipliers)
self.current_trial = next(self.trial_counter)
self.trigger_lock.release()
return {
'trial_num': self.current_trial,
'platform_width': self.platform_width,
'platform_distance': self.platform_distance,
'velocity_multiplier': self.velocity_multiplier
}
def jump(self):
"""
Raise the platform to the particular level,
Returns:
"""
self.stage_block.clear()
timestamp_requested = datetime.now().isoformat()
# raise platform to height
mask = self.hardware['PLAT']['FORM'].mask
mask[:,:] = 0
if self.platform_width == 2:
mask[2:4, self.platform_distance:] = 1
elif self.platform_width == 4:
mask[1:5, self.platform_distance:] = 1
elif self.platform_width == 6:
mask[:,self.platform_distance:] = 1
self.hardware['PLAT']['FORM'].mask = mask
self.hardware['PLAT']['FORM'].height = self.platform_height
# wait until the movement is finished
self.hardware['PLAT']['FORM'].join()
timestamp_raise = datetime.now().isoformat()
# set the platform velocity control mode active
self.velocity_active.set()
self.current_stage = 'jump'
# TODO: start timeout timer
return {
'timestamp_request': timestamp_requested,
'timestamp_raise': timestamp_raise
}
def reinforcement(self):
timestamp_jumped = datetime.now().isoformat()
# TODO: clear timeout timer
# TODO: deliver reward or don't
# reset platform (level is blocking)
self.hardware['PLAT']['FORM'].level()
return {
'timestamp_jumped': timestamp_jumped
}
def l_motion(self, value):
if self.velocity_active.is_set():
# TODO: kalman filter and transformation
self.hardware['PLAT']['FORM'].velocity = value['velocity_y']*self.velocity_multiplier
# TODO: send velocity on to T
def l_dlc(self, value):
if self.current_stage == 'request':
# test that the mouse is above and between the jump platform
on_plat = self._point_above(
value[self.dlc_use_point],
value[self.dlc_jumpoff_points[0]],
value[self.dlc_jumpoff_points[1]]
)
# set the stage block to advance the task
if on_plat:
# unset until the stage method re-sets it
self.current_stage = ''
self.stage_block.set()
elif self.current_stage == 'jump':
# test that the mouse is above and between the parallax platform
# test that the mouse is above and between the jump platform
on_plat = self._point_above(
value[self.dlc_use_point],
value[self.dlc_platform_points[0]],
value[self.dlc_platform_points[1]]
)
# set the stage block to advance the task
if on_plat:
# unset until the stage method re-sets it
self.current_stage = ''
self.stage_block.set()
# TODO: send to terminal and add to kalman filter
def _point_above(self, test_point, left_point, right_point):
# check if x coord is within the left and right points
within = left_point[0]<=test_point[0]<=right_point[0]
if not within:
return False
# check if y coord is above the line formed by the two points
b = left_point[1]
m = (right_point[1] - left_point[1]) / (right_point[0] - left_point[0])
# minimum y for the x position of the point
y = m*(test_point[0]-left_point[0]) + b
return test_point[1]>y