Repository navigation
Expand file tree
/
Copy pathsimulation.py
More file actions
1396 lines (1246 loc) · 58.2 KB
/
Copy pathsimulation.py
File metadata and controls
1396 lines (1246 loc) · 58.2 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cv2
import math
import ai2thor.controller
from collections import deque
controller = None
third_party_frames = []
cv2_frames = []
def setup_scene(scene_name, camera_position, camera_rotation, width=512, height=512, grid_size=0.25, agent_count=2):
global controller, third_party_frames, cv2_frames
controller = ai2thor.controller.Controller(width=width, height=height)
controller.reset(scene_name)
controller.step(dict(action="Initialize", gridSize=grid_size, agentCount=agent_count))
controller.step(
action="AddThirdPartyCamera",
position=camera_position,
rotation=camera_rotation,
fieldOfView=90,
)
third_party_frames = []
cv2_frames = []
return controller
def execute_and_record(action_dict):
event = controller.step(action_dict)
cv2_frames.append(event.cv2img)
agent_id = action_dict.get("agentId", 0)
third_party_frame = event.events[agent_id].third_party_camera_frames[0]
third_party_frames.append(third_party_frame)
return event
def save_video(frames, output_path, convert_rgb_to_bgr=False, fps=10.0):
if not frames:
return
frame_height, frame_width = frames[0].shape[:2]
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
for frame in frames:
if convert_rgb_to_bgr:
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
out.write(frame)
out.release()
def get_surrounding_positions(center_position, grid_size=0.25):
surrounding_positions = []
offsets = [-grid_size, 0, grid_size]
for dx in offsets:
for dz in offsets:
surrounding_positions.append(
{
"x": round(center_position["x"] + dx, 2),
"y": center_position["y"],
"z": round(center_position["z"] + dz, 2),
}
)
return surrounding_positions
def get_reachable_positions(occupied_position, agent_id=0, grid_size=0.25):
event = controller.step(dict(action="GetReachablePositions", agentId=agent_id))
reachable_positions = event.metadata["actionReturn"]
occupied_positions = get_surrounding_positions(occupied_position, grid_size)
return [pos for pos in reachable_positions if pos not in occupied_positions]
def round_to_grid(position, grid_size=0.25):
return {
"x": round(position["x"] / grid_size) * grid_size,
"y": position["y"],
"z": round(position["z"] / grid_size) * grid_size,
}
def round_to_nearest_reachable(position, reachable_positions, grid_size=0.25):
min_distance = float("inf")
nearest_position = None
for pos in reachable_positions:
distance = ((position["x"] - pos["x"]) ** 2 + (position["z"] - pos["z"]) ** 2) ** 0.5
if distance < min_distance:
min_distance = distance
nearest_position = pos
return nearest_position
def find_path(start_pos, target_pos, occupied_pos, agent_id=0):
reachable_positions = get_reachable_positions(occupied_pos, agent_id)
reachable_set = {(round(pos["x"], 2), round(pos["z"], 2)) for pos in reachable_positions}
start_pos = round_to_grid(start_pos)
target_pos = round_to_nearest_reachable(target_pos, reachable_positions)
if target_pos is None:
return None
queue = deque([[start_pos]])
visited = {(start_pos["x"], start_pos["z"])}
while queue:
path = queue.popleft()
current_pos = path[-1]
if current_pos["x"] == target_pos["x"] and current_pos["z"] == target_pos["z"]:
return path
neighbors = [
{"x": current_pos["x"] + 0.25, "y": current_pos["y"], "z": current_pos["z"]},
{"x": current_pos["x"] - 0.25, "y": current_pos["y"], "z": current_pos["z"]},
{"x": current_pos["x"], "y": current_pos["y"], "z": current_pos["z"] + 0.25},
{"x": current_pos["x"], "y": current_pos["y"], "z": current_pos["z"] - 0.25},
]
for neighbor in neighbors:
neighbor_tuple = (round(neighbor["x"], 2), round(neighbor["z"], 2))
if neighbor_tuple not in visited and neighbor_tuple in reachable_set:
visited.add(neighbor_tuple)
queue.append(path + [neighbor])
return None
def get_direction_to_target(agent_pos, target_pos):
dx = target_pos["x"] - agent_pos["x"]
dz = target_pos["z"] - agent_pos["z"]
if abs(dx) > abs(dz):
return 90 if dx > 0 else 270
return 0 if dz > 0 else 180
def rotate_to_direction(target_yaw, agent_id=0):
event = execute_and_record(dict(action="Pass", agentId=agent_id))
agent_yaw = event.metadata["agent"]["rotation"]["y"]
yaw_diff = (target_yaw - agent_yaw + 360) % 360
if yaw_diff == 90:
execute_and_record(dict(action="RotateRight", agentId=agent_id))
elif yaw_diff == 270:
execute_and_record(dict(action="RotateLeft", agentId=agent_id))
elif yaw_diff == 180:
execute_and_record(dict(action="RotateRight", agentId=agent_id))
execute_and_record(dict(action="RotateRight", agentId=agent_id))
def move_along_path(path, agent_id=0):
for target_pos in path[1:]:
while True:
event = execute_and_record(dict(action="Pass", agentId=agent_id))
agent_pos = event.metadata["agent"]["position"]
target_yaw = get_direction_to_target(agent_pos, target_pos)
rotate_to_direction(target_yaw, agent_id)
event = execute_and_record(dict(action="MoveAhead", agentId=agent_id))
agent_pos = event.metadata["agent"]["position"]
if agent_pos == target_pos:
break
execute_and_record(dict(action="MoveHeldObjectBack", moveMagnitude=0.25, agentId=agent_id))
def move(start_position, target_position, occupied_position, agent_id=0):
path = find_path(start_position, target_position, occupied_position, agent_id)
if path:
move_along_path(path, agent_id)
event = controller.step(dict(action="Pass", agentId=agent_id))
agent_pos = event.metadata["agent"]["position"]
target_yaw = get_direction_to_target(agent_pos, target_position)
rotate_to_direction(target_yaw, agent_id)
else:
print("No available path.")
def pickup_object(obj_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
pickflag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["pickupable"] and obj["objectId"] == obj_id:
pickflag = True
event = execute_and_record(dict(action="PickupObject", objectId=obj_id, agentId=agent_id))
print(f'Pick up a {obj["objectType"]}.')
if not pickflag:
print("Failed to pick up.")
return event
def cut_object(target_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
knife_id = None
for obj in event.metadata["objects"]:
if obj["visible"] and obj["pickupable"] and obj["objectType"] == "Knife":
event = execute_and_record(dict(action="PickupObject", objectId=obj["objectId"], agentId=agent_id))
knife_id = obj["objectId"]
break
if knife_id:
if target_id:
event = execute_and_record(dict(action="SliceObject", objectId=target_id, agentId=agent_id))
else:
print("No object to slice.")
else:
print("No available knife.")
return event
def place_object(pickup_id, place_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
pickup_flag = False
pickup_name = None
for obj in event.metadata["objects"]:
if obj["isPickedUp"] and obj["objectId"] == pickup_id:
pickup_name = obj["objectType"]
pickup_flag = True
break
if not pickup_flag:
print("Agent holds no objects.")
return event
place_flag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["objectId"] == place_id:
place_name = obj["objectType"]
event = execute_and_record(dict(action="MoveHeldObjectAhead", moveMagnitude=0.3, agentId=agent_id))
event = execute_and_record(dict(action="DropHandObject", agentId=agent_id))
print(f"{pickup_name} placed on the {place_name}.")
place_flag = True
break
if not place_flag:
print("Failed to place.")
return event
def put_object(pickup_id, place_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
pickup_flag = False
pickup_name = None
for obj in event.metadata["objects"]:
if obj["isPickedUp"] and obj["objectId"] == pickup_id:
pickup_name = obj["objectType"]
pickup_flag = True
break
if not pickup_flag:
print("Agent holds no objects.")
return event
place_flag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["objectId"] == place_id:
place_name = obj["objectType"]
event = execute_and_record(dict(action="PutObject", objectId=place_id, agentId=agent_id))
print(f"{pickup_name} placed on the {place_name}.")
place_flag = True
break
if not place_flag:
print("Failed to place.")
return event
def open_object(target_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
openflag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["openable"] and obj["objectId"] == target_id:
openflag = True
if obj["isOpen"]:
print(f"{obj['objectType']} already open.")
break
event = execute_and_record(dict(action="OpenObject", objectId=target_id, agentId=agent_id))
print(f"Open the {obj['objectType']}.")
if not openflag:
print("Failed to open.")
return event
def close_object(target_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
closeflag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["openable"] and obj["objectId"] == target_id:
closeflag = True
if not obj["isOpen"]:
print(f"{obj['objectType']} already closed.")
break
event = execute_and_record(dict(action="CloseObject", objectId=target_id, agentId=agent_id))
print(f"Close the {obj['objectType']}.")
if not closeflag:
print("Failed to close.")
return event
def pour_water(target_id, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
filledflag = False
for obj in event.metadata["objects"]:
if obj["visible"] and obj["canFillWithLiquid"] and obj["objectId"] == target_id:
filledflag = True
if obj["isFilledWithLiquid"]:
print(f"{obj['objectType']} already filled.")
break
event = execute_and_record(dict(action="FillObjectWithLiquid", objectId=target_id, fillLiquid="water", agentId=agent_id))
print(f"Fill the {obj['objectType']} with water.")
if not filledflag:
print("Cannot be filled.")
return event
def wash_object_start(occupied_position, agent_id=0):
event = controller.step(dict(action="Pass", agentId=agent_id))
start_position = event.metadata["agent"]["position"]
wash_id = None
target_position = None
for obj in event.metadata["objects"]:
if obj["objectType"] in ["Faucet", "Sink"]:
wash_id = obj["objectId"]
target_position = obj["position"]
break
if wash_id:
path = find_path(start_position, target_position, occupied_position, agent_id)
if path:
move_along_path(path, agent_id)
execute_and_record(dict(action="ToggleObjectOn", objectId=wash_id, agentId=agent_id))
for _ in range(2):
execute_and_record(dict(action="Stand", agentId=agent_id))
execute_and_record(dict(action="ToggleObjectOff", objectId=wash_id, agentId=agent_id))
else:
print("Failed to move to the faucet.")
else:
print("No faucet or sink in the current scene.")
return event
def find_object(event, object_type, predicate=None):
for obj in event.metadata["objects"]:
if obj["objectType"] == object_type and (predicate is None or predicate(obj)):
return obj
return None
def count_controller_step_calls(task_fn, *args, **kwargs):
original_step = ai2thor.controller.Controller.step
counter = {"count": 0}
def wrapped_step(self, *step_args, **step_kwargs):
counter["count"] += 1
return original_step(self, *step_args, **step_kwargs)
ai2thor.controller.Controller.step = wrapped_step
try:
result = task_fn(*args, **kwargs)
finally:
ai2thor.controller.Controller.step = original_step
return counter["count"], result
def bathroom_human_task():
setup_scene(
"FloorPlan407",
{"x": -1.8, "y": 2.3, "z": 0.5},
{"x": 50, "y": 140, "z": 0},
)
occupied_event = controller.step(dict(action="Teleport", x=-1.25, y=0.9004555940628052, z=-0.75, agentId=1))
occupied_position = occupied_event.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=0))
start_position = event.metadata["agent"]["position"]
look_down = False
shower_id = None
shower_position = None
shower = find_object(event, "ShowerDoor")
if shower:
shower_id = shower["objectId"]
shower_position = shower["axisAlignedBoundingBox"]["center"]
if shower_id:
print("Move to the shower room.")
move(start_position, shower_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for obj in event.metadata["objects"]:
if obj["objectType"] == "ShowerDoor" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = open_object(shower_id)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
for _ in range(4):
event = execute_and_record(dict(action="MoveAhead", agentId=0))
for _ in range(2):
event = execute_and_record(dict(action="RotateLeft", agentId=0))
event = close_object(shower_id)
occupied_event2 = controller.step(dict(action="Pass", agentId=0))
occupied_position2 = occupied_event2.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=1))
start_position = event.metadata["agent"]["position"]
look_down = False
hand_towel_id = None
hand_towel_position = None
hand_towel = find_object(event, "HandTowel")
if hand_towel:
hand_towel_id = hand_towel["objectId"]
hand_towel_position = hand_towel["position"]
if hand_towel_id:
print("Move to the HandTowel.")
move(start_position, hand_towel_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "HandTowel" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(hand_towel_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
event = controller.step(dict(action="Pass", agentId=1))
print("Move to the shower room.")
event = execute_and_record(dict(action="RotateLeft", agentId=1))
for _ in range(7):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
for _ in range(1):
event = execute_and_record(dict(action="RotateLeft", agentId=1))
for _ in range(5):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
event = execute_and_record(dict(action="RotateRight", agentId=1))
event = execute_and_record(dict(action="DropHandObject", agentId=1))
event = controller.step(dict(action="Pass", agentId=1))
start_position = event.metadata["agent"]["position"]
soap_bar_id = None
soap_bar_position = None
soap_bar = find_object(event, "SoapBar")
if soap_bar:
soap_bar_id = soap_bar["objectId"]
soap_bar_position = soap_bar["position"]
if soap_bar_id:
print("Move to the SoapBar.")
for _ in range(1):
event = execute_and_record(dict(action="RotateRight", agentId=1))
for _ in range(5):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
for _ in range(1):
event = execute_and_record(dict(action="RotateRight", agentId=1))
for _ in range(4):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
event = execute_and_record(dict(action="RotateLeft", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "SoapBar" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(soap_bar_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
event = controller.step(dict(action="Pass", agentId=1))
print("Move to the shower room.")
event = execute_and_record(dict(action="RotateLeft", agentId=1))
for _ in range(4):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
for _ in range(1):
event = execute_and_record(dict(action="RotateLeft", agentId=1))
for _ in range(5):
event = execute_and_record(dict(action="MoveAhead", agentId=1))
event = execute_and_record(dict(action="RotateRight", agentId=1))
event = execute_and_record(dict(action="DropHandObject", agentId=1))
print("Task finished successfully.")
save_video(cv2_frames, "task_mission_bathroom.mp4")
save_video(third_party_frames, "task_mission_bathroom_global.mp4", convert_rgb_to_bgr=True)
return {"scene": "bathroom", "steps": None}
def bathroom_robot_task():
return bathroom_human_task()
def bedroom_human_task():
setup_scene(
"FloorPlan323",
{"x": -2.7, "y": 2.4, "z": -1.8},
{"x": 50, "y": 90, "z": 0},
)
occupied_event = controller.step(dict(action="Pass", agentId=1))
occupied_position = occupied_event.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=0))
start_position = event.metadata["agent"]["position"]
look_down = False
look_up = False
bed_id = None
desk_id = None
bed_receptacles = []
bed = find_object(event, "Bed")
if bed:
bed_id = bed["objectId"]
bed_receptacles = bed.get("receptacleObjectIds", [])
sofa = find_object(event, "Sofa")
if sofa:
desk_id = sofa["objectId"]
desk_position = sofa["position"]
else:
desk_position = None
if bed_id and bed_receptacles:
print("There is clutter on the bed.")
print("Moving clutter away.")
for obj in event.metadata["objects"]:
start_position = event.metadata["agent"]["position"]
if obj["objectId"] in bed_receptacles and obj["objectType"] != "Pillow":
target_position = obj["position"]
print(f"Move to the {obj['objectType']}.")
move(start_position, target_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == obj["objectId"] and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = pickup_object(obj["objectId"])
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
start_position = event.metadata["agent"]["position"]
print("Move to the sofa.")
move(start_position, desk_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == desk_id and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = place_object(obj["objectId"], desk_id)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
pillow = None
for obj in event.metadata["objects"]:
if obj["objectId"] in bed_receptacles and obj["objectType"] == "Pillow":
pillow = obj
break
if pillow:
start_position = event.metadata["agent"]["position"]
print("Ready for sleep.")
move(start_position, pillow["position"], occupied_position, agent_id=0)
occupied_event2 = controller.step(dict(action="Pass", agentId=0))
occupied_position2 = occupied_event2.metadata["agent"]["position"]
look_down = False
look_up = False
event = controller.step(dict(action="Pass", agentId=1))
start_position = event.metadata["agent"]["position"]
target_id = None
target_position = None
blinds = find_object(event, "Blinds", lambda obj: obj["position"]["x"] < 0)
if blinds:
target_id = blinds["objectId"]
target_position = blinds["position"]
if target_id:
print("Move to the window.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Blinds" and not obj["visible"]:
look_up = True
event = execute_and_record(dict(action="LookUp", agentId=1))
event = open_object(target_id, agent_id=1)
if look_up:
look_up = False
event = execute_and_record(dict(action="LookDown", agentId=1))
start_position = event.metadata["agent"]["position"]
target_id = None
target_position = None
mug = find_object(event, "Mug")
if mug:
target_id = mug["objectId"]
target_position = mug["position"]
if target_id:
print("Move to the Mug.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Mug" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(target_id, agent_id=1)
event = pour_water(target_id, agent_id=1)
event = execute_and_record(dict(action="DropHandObject", agentId=1))
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
print("Task finished successfully.")
save_video(cv2_frames, "task_mission_bedroom.mp4")
save_video(third_party_frames, "task_mission_bedroom_global.mp4", convert_rgb_to_bgr=True)
return {"scene": "bedroom", "steps": None}
def bedroom_robot_task():
return bedroom_human_task()
def bedroom2_human_task():
setup_scene(
"FloorPlan323",
{"x": -2.7, "y": 2.4, "z": -1.8},
{"x": 50, "y": 90, "z": 0},
)
occupied_event = controller.step(dict(action="Pass", agentId=1))
occupied_position = occupied_event.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=0))
start_position = event.metadata["agent"]["position"]
look_down = False
look_up = False
desk_id = None
sofa_id = None
recept_list = []
desk = find_object(event, "Desk")
if desk:
desk_id = desk["objectId"]
recept_list = desk.get("receptacleObjectIds", [])
desk_position = desk["position"]
else:
desk_position = None
sofa = find_object(event, "Sofa")
if sofa:
sofa_id = sofa["objectId"]
sofa_position = sofa["position"]
else:
sofa_position = None
if desk_id and recept_list:
print("There is clutter on the desk.")
print("Moving clutter away.")
for obj in event.metadata["objects"]:
start_position = event.metadata["agent"]["position"]
if obj["objectId"] in recept_list and obj["objectType"] in ["Book", "Bowl"]:
target_position = obj["position"]
print(f"Move to the {obj['objectType']}.")
move(start_position, target_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == obj["objectId"] and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = pickup_object(obj["objectId"])
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
start_position = event.metadata["agent"]["position"]
print("Move to the sofa.")
move(start_position, sofa_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == sofa_id and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = place_object(obj["objectId"], sofa_id)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
laptop = None
for obj in event.metadata["objects"]:
if obj["objectType"] == "Laptop":
laptop = obj
break
if laptop:
start_position = event.metadata["agent"]["position"]
move(start_position, laptop["position"], occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == laptop["objectId"] and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = pickup_object(laptop["objectId"])
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
start_position = event.metadata["agent"]["position"]
print("Move to the desk.")
move(start_position, desk_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for o in event.metadata["objects"]:
if o["objectId"] == desk_id and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = execute_and_record(dict(action="RotateHeldObject", yaw=180, agentId=0))
event = place_object(laptop["objectId"], desk_id)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
print("Ready for work.")
occupied_event2 = controller.step(dict(action="Pass", agentId=0))
occupied_position2 = occupied_event2.metadata["agent"]["position"]
look_down = False
look_up = False
event = controller.step(dict(action="Pass", agentId=1))
start_position = event.metadata["agent"]["position"]
target_id = None
target_position = None
cellphone = find_object(event, "CellPhone")
if cellphone:
target_id = cellphone["objectId"]
target_position = cellphone["position"]
if target_id:
print("Move to the CellPhone.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "CellPhone" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(target_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
print("Move to the desk.")
move(start_position, desk_position, occupied_position2, agent_id=1)
event = execute_and_record(dict(action="RotateLeft", agentId=1))
event = controller.step(dict(action="Pass", agentId=1))
for o in event.metadata["objects"]:
if o["objectId"] == desk_id and not o["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = place_object(target_id, desk_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
target_id = None
target_position = None
mug = find_object(event, "Mug")
if mug:
target_id = mug["objectId"]
target_position = mug["position"]
if target_id:
print("Move to the Mug.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Mug" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(target_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
print("Move to the desk.")
move(start_position, desk_position, occupied_position2, agent_id=1)
event = execute_and_record(dict(action="MoveHeldObjectBack", moveMagnitude=0.25, agentId=1))
event = execute_and_record(dict(action="RotateLeft", agentId=1))
event = controller.step(dict(action="Pass", agentId=1))
event = pour_water(target_id, agent_id=1)
event = place_object(target_id, desk_id, agent_id=1)
print("Task finished successfully.")
save_video(cv2_frames, "task_mission_bedroom2.mp4")
save_video(third_party_frames, "task_mission_bedroom2_global.mp4", convert_rgb_to_bgr=True)
return {"scene": "bedroom2", "steps": None}
def bedroom2_robot_task():
return bedroom2_human_task()
def kitchen_human_task():
setup_scene(
"FloorPlan28",
{"x": -4, "y": 2.59, "z": -2},
{"x": 50, "y": 90, "z": 0},
)
occupied_event = controller.step(dict(action="Pass", agentId=1))
occupied_position = occupied_event.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=0))
start_position = event.metadata["agent"]["position"]
look_down = False
target_id = None
target_position = None
bread = find_object(event, "Bread")
if bread:
target_id = bread["objectId"]
target_position = bread["position"]
if target_id:
print("Move to the bread.")
move(start_position, target_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Bread" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = pickup_object(target_id)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
start_position = event.metadata["agent"]["position"]
desk_id = None
desk_position = None
for obj in event.metadata["objects"]:
if obj["objectType"] == "CounterTop" and obj["position"]["x"] < -1:
desk_id = obj["objectId"]
desk_position = obj["position"]
desk_position["x"] += 0.25
print("Move to the desk.")
move(start_position, desk_position, occupied_position, agent_id=0)
if not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = execute_and_record(dict(action="RotateHeldObject", yaw=90, agentId=0))
event = place_object(target_id, desk_id, agent_id=0)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
break
knife = find_object(event, "Knife")
knife_id = knife["objectId"] if knife else None
knife_position = knife["position"] if knife else None
if knife_id:
print("Move to the knife.")
move(start_position, knife_position, occupied_position, agent_id=0)
event = controller.step(dict(action="Pass", agentId=0))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Knife" and not obj["visible"]:
event = execute_and_record(dict(action="LookDown", agentId=0))
event = pickup_object(knife_id)
start_position = event.metadata["agent"]["position"]
print("Move to the countertop.")
move(start_position, desk_position, occupied_position, agent_id=0)
if not obj["visible"]:
event = execute_and_record(dict(action="LookDown", agentId=0))
event = cut_object(target_id)
sliced = find_object(event, "BreadSliced")
sliced_id = sliced["objectId"] if sliced else None
if sliced_id:
event = pickup_object(sliced_id)
toaster = find_object(event, "Toaster")
toaster_id = toaster["objectId"] if toaster else None
toaster_position = toaster["position"] if toaster else None
if toaster_id:
start_position = event.metadata["agent"]["position"]
print("Move to the toaster.")
move(start_position, toaster_position, occupied_position, agent_id=0)
event = execute_and_record(dict(action="PutObject", objectId=toaster_id, agentId=0))
event = execute_and_record(dict(action="ToggleObjectOn", objectId=toaster_id, agentId=0))
print("Toast started.")
occupied_event2 = controller.step(dict(action="Pass", agentId=0))
occupied_position2 = occupied_event2.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=1))
start_position = event.metadata["agent"]["position"]
look_down = False
knife_id = None
target_position = None
plate = find_object(event, "Plate")
if plate:
knife_id = plate["objectId"]
target_position = plate["position"]
if knife_id:
print("Move to the Plate.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "Plate" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(knife_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
table_id = None
table_position = None
for obj in event.metadata["objects"]:
if obj["objectType"] == "CounterTop" and obj["position"]["x"] < -1:
table_id = obj["objectId"]
table_position = obj["position"]
table_position["x"] += 0.5
print("Move to the countertop.")
move(start_position, table_position, occupied_position2, agent_id=1)
if not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
break
event = place_object(knife_id, table_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
knife_id = None
target_position = None
butter_knife = find_object(event, "ButterKnife")
if butter_knife:
knife_id = butter_knife["objectId"]
target_position = butter_knife["position"]
if knife_id:
print("Move to the ButterKnife.")
move(start_position, target_position, occupied_position2, agent_id=1)
event = controller.step(dict(action="Pass", agentId=1))
for obj in event.metadata["objects"]:
if obj["objectType"] == "ButterKnife" and not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
event = pickup_object(knife_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
start_position = event.metadata["agent"]["position"]
table_id = None
table_position = None
for obj in event.metadata["objects"]:
if obj["objectType"] == "CounterTop" and obj["position"]["x"] < -1:
table_id = obj["objectId"]
table_position = obj["position"]
table_position["x"] += 0.5
print("Move to the countertop.")
move(start_position, table_position, occupied_position2, agent_id=1)
event = execute_and_record(dict(action="RotateRight", agentId=1))
if not obj["visible"]:
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=1))
break
event = place_object(knife_id, table_id, agent_id=1)
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=1))
event = execute_and_record(dict(action="ToggleObjectOff", objectId=toaster_id, agentId=0)) if "toaster_id" in locals() and toaster_id else event
event = pickup_object(sliced_id) if "sliced_id" in locals() and sliced_id else event
print("Toast completed.")
start_position = event.metadata["agent"]["position"]
table_id = None
table_position = None
for obj in event.metadata["objects"]:
if obj["objectType"] == "CounterTop" and obj["position"]["x"] < -1:
table_id = obj["objectId"]
table_position = obj["position"]
table_position["x"] += 0.25
print("Move to the countertop.")
move(start_position, table_position, occupied_position, agent_id=0)
break
for obj in event.metadata["objects"]:
if obj["objectType"] == "Plate" and not obj["visible"]:
plate_id = obj["objectId"]
look_down = True
event = execute_and_record(dict(action="LookDown", agentId=0))
event = execute_and_record(dict(action="PutObject", objectId=plate_id, agentId=0))
if look_down:
look_down = False
event = execute_and_record(dict(action="LookUp", agentId=0))
print("Task finished successfully.")
break
save_video(cv2_frames, "task_mission_kitchen.mp4")
save_video(third_party_frames, "task_mission_kitchen_global.mp4", convert_rgb_to_bgr=True)
return {"scene": "kitchen", "steps": None}
def kitchen_robot_task():
return kitchen_human_task()
def kitchen2_human_task():
setup_scene(
"FloorPlan28",
{"x": -4, "y": 2.59, "z": -2},
{"x": 50, "y": 90, "z": 0},
)
occupied_event = controller.step(dict(action="Pass", agentId=1))
occupied_position = occupied_event.metadata["agent"]["position"]
event = controller.step(dict(action="Pass", agentId=0))
start_position = event.metadata["agent"]["position"]
look_down = False
target_id = None
target_position = None
knife = find_object(event, "Knife")
if knife:
target_id = knife["objectId"]
target_position = knife["position"]
if target_id: