88
99from projectairsim import ProjectAirSimClient , World , PayloadActor , Drone
1010from projectairsim .utils import projectairsim_log
11- from typing import List
11+ from projectairsim . image_utils import ImageDisplay
1212
1313
1414# Async main function to wrap async drone commands
1515async def main ():
1616 # Create a Project AirSim client
1717 client = ProjectAirSimClient ()
1818
19+ # Initialize an ImageDisplay object to display camera sub-windows
20+ image_display = ImageDisplay ()
21+
1922 try :
2023 # Connect to simulation environment
2124 client .connect ()
@@ -27,15 +30,126 @@ async def main():
2730 drone = Drone (client , world , "Drone1" )
2831
2932 # Create PayloadActor object
30- payload = PayloadActor (client , world , "Payload" ) # spawn like drone in config and watch it fall
33+ payload = PayloadActor (
34+ client , world , "Payload"
35+ ) # spawn like drone in config and watch it fall
36+
37+ # ------------------------------------------------------------------------------
38+
39+ # Subscribe to chase camera sensor as a client-side pop-up window
40+ chase_cam_window = "ChaseCam"
41+ image_display .add_chase_cam (chase_cam_window )
42+ client .subscribe (
43+ drone .sensors ["Chase" ]["scene_camera" ],
44+ lambda _ , chase : image_display .receive (chase , chase_cam_window ),
45+ )
46+
47+ # Subscribe to the downward-facing camera sensor's RGB and Depth images
48+ rgb_name = "RGB-Image"
49+ image_display .add_image (rgb_name , subwin_idx = 0 )
50+ client .subscribe (
51+ drone .sensors ["DownCamera" ]["scene_camera" ],
52+ lambda _ , rgb : image_display .receive (rgb , rgb_name ),
53+ )
54+
55+ image_display .start ()
56+
57+ # ------------------------------------------------------------------------------
58+
59+ # Set the drone to be ready to fly
60+ drone .enable_api_control ()
61+ drone .arm ()
62+
63+ # ------------------------------------------------------------------------------
64+
65+ projectairsim_log ().info ("takeoff_async: starting" )
66+ takeoff_task = await drone .takeoff_async ()
67+
68+ await takeoff_task
69+ projectairsim_log ().info ("takeoff_async: completed" )
70+
71+ # Command the drone to move up in NED coordinate system at 1 m/s for 3 seconds
72+ move_up_task = await drone .move_by_velocity_async (
73+ v_north = 0.0 , v_east = 0.0 , v_down = - 1.0 , duration = 3.0
74+ )
75+ projectairsim_log ().info ("Move-Up invoked" )
76+
77+ await move_up_task
78+ projectairsim_log ().info ("Move-Up completed" )
79+
80+ # Command the drone to move to the payload position
81+ payload_pos = payload .get_kinematics ()["pose" ]["position" ]
82+
83+ projectairsim_log ().info ("Moving to payload position" )
84+ move_to_payload_task = await drone .move_to_position_async (
85+ north = payload_pos ["x" ], east = payload_pos ["y" ], down = - 3 , velocity = 1.0
86+ )
87+ await move_to_payload_task
88+ projectairsim_log ().info ("Moved to payload" )
89+
90+ # Command the drone to attach payload
91+ projectairsim_log ().info ("Attaching payload" )
92+ success = drone .attach_payload (payload .name )
93+ projectairsim_log ().info (f"Payload was successfully attached: { success } " )
94+
95+ # Command the drone to move up in NED coordinate system at 1 m/s for 2 seconds
96+ move_up_task = await drone .move_by_velocity_async (
97+ v_north = 0.0 , v_east = 0.0 , v_down = - 1.0 , duration = 2.0
98+ )
99+ projectairsim_log ().info ("Move-Up invoked" )
100+ await move_up_task
101+ projectairsim_log ().info ("Move-Up completed" )
102+
103+ # Command the drone to move north in NED coordinate system at 1 m/s for 2 seconds
104+ move_north_task = await drone .move_by_velocity_async (
105+ v_north = 1.0 , v_east = 1.0 , v_down = 0.0 , duration = 4.0
106+ )
107+ projectairsim_log ().info ("Move-North invoked" )
108+ await move_north_task
109+ projectairsim_log ().info ("Move-North completed" )
110+
111+ # Command the drone to move down in NED coordinate system at 1 m/s for 3.5 seconds
112+ move_down_task = await drone .move_by_velocity_async (
113+ v_north = 0.0 , v_east = 0.0 , v_down = 1.0 , duration = 3.0
114+ )
115+ projectairsim_log ().info ("Move-Down invoked" )
116+ await move_down_task
117+ projectairsim_log ().info ("Move-Down completed" )
118+
119+ # Command the drone to detach payload
120+ projectairsim_log ().info ("Detaching payload" )
121+ success = drone .detach_payload ()
122+ projectairsim_log ().info (f"Payload was successfully detached: { success } " )
123+
124+ # Command the drone to move north in NED coordinate system at 1 m/s for 1 second
125+ move_north_task = await drone .move_by_velocity_async (
126+ v_north = 1.0 , v_east = 1.0 , v_down = 0.0 , duration = 1.0
127+ )
128+ projectairsim_log ().info ("Move-North invoked" )
129+ await move_north_task
130+ projectairsim_log ().info ("Move-North completed" )
131+
132+ projectairsim_log ().info ("land_async: starting" )
133+ land_task = await drone .land_async ()
134+ await land_task
135+ projectairsim_log ().info ("land_async: completed" )
136+
137+ # ------------------------------------------------------------------------------
138+
139+ # Shut down the drone
140+ drone .disarm ()
141+ drone .disable_api_control ()
31142
143+ # ------------------------------------------------------------------------------
32144
145+ # logs exception on the console
33146 except Exception as err :
34147 projectairsim_log ().error (f"Exception occurred: { err } " , exc_info = True )
35148
36149 finally :
37150 # Always disconnect from the simulation environment to allow next connection
38151 client .disconnect ()
152+ image_display .stop ()
39153
40154
41155if __name__ == "__main__" :
0 commit comments