1+ """
2+ Copyright (C) Microsoft Corporation. All rights reserved.
3+
4+ Demonstrates flying a quadrotor drone with camera sensors.
5+ """
6+
7+ import asyncio
8+ import math
9+
10+ from projectairsim import ProjectAirSimClient , Drone , World
11+ from projectairsim .utils import projectairsim_log
12+ from projectairsim .image_utils import ImageDisplay
13+
14+
15+ # Async main function to wrap async drone commands
16+ async def main ():
17+ # Create a Project AirSim client
18+ client = ProjectAirSimClient ()
19+
20+ # Initialize an ImageDisplay object to display camera sub-windows
21+ image_display = ImageDisplay ()
22+
23+ try :
24+ # Connect to simulation environment
25+ client .connect ()
26+
27+ # Create a World object to interact with the sim world and load a scene
28+ world = World (client , "scene_cessna310.jsonc" , delay_after_load_sec = 2 )
29+
30+ # Create a Drone object to interact with a drone in the loaded sim world
31+ drone = Drone (client , world , "c310" )
32+ #
33+ ## ------------------------------------------------------------------------------
34+ #
35+ ## Subscribe to chase camera sensor as a client-side pop-up window
36+ #chase_cam_window = "ChaseCam"
37+ #image_display.add_chase_cam(chase_cam_window)
38+ #client.subscribe(
39+ # drone.sensors["DownCamera"]["scene_camera"],
40+ # lambda _, chase: image_display.receive(chase, chase_cam_window),
41+ #)
42+ #
43+ ## Subscribe to the downward-facing camera sensor's RGB and Depth images
44+ #rgb_name = "RGB-Image"
45+ #image_display.add_image(rgb_name, subwin_idx=0)
46+ #client.subscribe(
47+ # drone.sensors["DownCamera"]["scene_camera"],
48+ # lambda _, rgb: image_display.receive(rgb, rgb_name),
49+ #)
50+ #
51+ #depth_name = "Depth-Image"
52+ #image_display.add_image(depth_name, subwin_idx=2)
53+ #client.subscribe(
54+ # drone.sensors["DownCamera"]["depth_camera"],
55+ # lambda _, depth: image_display.receive(depth, depth_name),
56+ #)
57+ #
58+ #image_display.start()
59+ #
60+ ## ------------------------------------------------------------------------------
61+ #
62+ ## Set the drone to be ready to fly
63+ ## JSBSim robot currently does not support control the drone at runtime
64+ #drone.enable_api_control()
65+ ##set brakes to 1
66+ #drone.set_brakes(1)
67+ #drone.arm()
68+ #
69+ ## ------------------------------------------------------------------------------
70+ #
71+ ## set takeoff z to 120 meters
72+ #drone.set_take_off_z(-120)
73+ #
74+ ## ------------------------------------------------------------------------------
75+ #
76+ ## Sleep for two seconds to
77+ #await asyncio.sleep(2)
78+ #
79+ ## release brakes
80+ #drone.set_brakes(0)
81+ #
82+ #projectairsim_log().info("takeoff_async: starting")
83+ #takeoff_task = (
84+ # await drone.takeoff_async(timeout_sec=1200)
85+ #) # schedule an async task to start the command
86+ #
87+ #await takeoff_task
88+ #projectairsim_log().info("takeoff_async: completed")
89+ #
90+ #projectairsim_log().info("Waiting to stabilize altitude... (10 seconds)")
91+ #await asyncio.sleep(10)
92+ #
93+ ## ------------------------------------------------------------------------------
94+ #
95+ ## Command the drone to move to position 1000,1000,-200
96+ #move_up_task = await drone.move_to_position_async(
97+ # north=1000, east=1000, down=-200, velocity=33.0, lookahead=100, timeout_sec=60
98+ #)
99+ #projectairsim_log().info("Move to position 1000,1000,-200 invoked")
100+ #
101+ #await move_up_task
102+ #projectairsim_log().info("Move to position completed")
103+ #
104+ ## ------------------------------------------------------------------------------
105+ #
106+ ## Command vehicle to fly at a specific heading and speed
107+ #projectairsim_log().info("Heading 90 invoked")
108+ #heading_45_task = await drone.move_by_heading_async(
109+ # heading=math.radians(90.0), speed=20.0, duration=10
110+ #)
111+ #await heading_45_task
112+ #projectairsim_log().info("Heading 90 complete.")
113+ #
114+ ## ------------------------------------------------------------------------------
115+ #
116+ ## Command the drone to move to position 0,0,-100
117+ #move_up_task = await drone.move_to_position_async(
118+ # north=0, east=0, down=-100, velocity=33.0, lookahead=100, timeout_sec=60
119+ #)
120+ #projectairsim_log().info("Move to position 0,0,-100 invoked")
121+ #
122+ #await move_up_task
123+ #projectairsim_log().info("Move to position completed")
124+ ## ------------------------------------------------------------------------------
125+ #
126+ #projectairsim_log().info("land_async: starting")
127+ #land_task = await drone.land_async()
128+ #await land_task
129+ #projectairsim_log().info("land_async: completed")
130+ ## set brakes to 50%
131+ #drone.set_brakes(0.5)
132+
133+ # ------------------------------------------------------------------------------
134+
135+ # Shut down the drone
136+ drone .disarm ()
137+ drone .disable_api_control ()
138+
139+ # logs exception on the console
140+ except Exception as err :
141+ projectairsim_log ().error (f"Exception occurred: { err } " , exc_info = True )
142+
143+ finally :
144+ # Always disconnect from the simulation environment to allow next connection
145+ client .disconnect ()
146+
147+ image_display .stop ()
148+
149+
150+ if __name__ == "__main__" :
151+ asyncio .run (main ()) # Runner for async main function
0 commit comments