|
| 1 | +""" |
| 2 | +Author: @woosal1337 |
| 3 | +""" |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import pygame |
| 7 | + |
| 8 | +from mavsdk import System |
| 9 | + |
| 10 | +drone = System() |
| 11 | + |
| 12 | +pygame.init() |
| 13 | +window = pygame.display.set_mode((300, 300)) |
| 14 | + |
| 15 | + |
| 16 | +async def setup(): |
| 17 | + """ |
| 18 | + General configurations, setups, and connections are done here. |
| 19 | + :return: |
| 20 | + """ |
| 21 | + |
| 22 | + await drone.connect(system_address="udp://:14540") |
| 23 | + |
| 24 | + print("Waiting for drone to connect...") |
| 25 | + async for state in drone.core.connection_state(): |
| 26 | + if state.is_connected: |
| 27 | + print(f"Drone discovered!") |
| 28 | + break |
| 29 | + |
| 30 | + print("Waiting for drone to have a global position estimate...") |
| 31 | + async for health in drone.telemetry.health(): |
| 32 | + if health.is_global_position_ok: |
| 33 | + print("Global position estimate ok") |
| 34 | + break |
| 35 | + |
| 36 | + |
| 37 | +async def main(): |
| 38 | + """ |
| 39 | + Launching a specific pygame window retrieves and works according to the received commands. |
| 40 | + :return: |
| 41 | + """ |
| 42 | + |
| 43 | + running = True |
| 44 | + |
| 45 | + while running: |
| 46 | + |
| 47 | + for event in pygame.event.get(): |
| 48 | + if event.type == pygame.QUIT: |
| 49 | + running = False |
| 50 | + |
| 51 | + keys = pygame.key.get_pressed() |
| 52 | + |
| 53 | + # While being in air and landing mode the drone is not likely to takeoff again, so |
| 54 | + # a condition check is required here to avoid such a condition. |
| 55 | + |
| 56 | + if keys[pygame.K_UP] and (await print_in_air(drone) != True): |
| 57 | + await takeoff() |
| 58 | + |
| 59 | + elif keys[pygame.K_DOWN]: |
| 60 | + await land() |
| 61 | + |
| 62 | + elif keys[pygame.K_RIGHT]: |
| 63 | + await print_in_air(drone) |
| 64 | + |
| 65 | + elif keys[pygame.K_i]: |
| 66 | + await info(drone) |
| 67 | + |
| 68 | + |
| 69 | +async def takeoff(): |
| 70 | + """ |
| 71 | + Default takeoff command seperated and taken from takeoff_and_land.py |
| 72 | + :return: |
| 73 | + """ |
| 74 | + |
| 75 | + print("-- Arming") |
| 76 | + await drone.action.arm() |
| 77 | + |
| 78 | + print("-- Taking off") |
| 79 | + await drone.action.takeoff() |
| 80 | + |
| 81 | + |
| 82 | +async def land(): |
| 83 | + """ |
| 84 | + Default land command seperated and taken from takeoff_and_land.py |
| 85 | + :return: |
| 86 | + """ |
| 87 | + |
| 88 | + await drone.action.land() |
| 89 | + |
| 90 | + |
| 91 | +async def print_in_air(drone=drone): |
| 92 | + async for in_air in drone.telemetry.in_air(): |
| 93 | + print(f"In air: {in_air}") |
| 94 | + return in_air |
| 95 | + |
| 96 | + |
| 97 | +async def info(drone=drone): |
| 98 | + """ |
| 99 | + This is the combination of the print_battery, print_in_air, print_gps_info, and print_position functions aimed |
| 100 | + to display all of the counted data/information at the same exact time. |
| 101 | + :param drone: |
| 102 | + :return: |
| 103 | + """ |
| 104 | + |
| 105 | + await print_battery(drone) |
| 106 | + await print_in_air(drone) |
| 107 | + await print_gps_info(drone) |
| 108 | + await print_position(drone) |
| 109 | + |
| 110 | + return True |
| 111 | + |
| 112 | + |
| 113 | +async def print_battery(drone=drone): |
| 114 | + """ |
| 115 | + Default print_battery command seperated and taken from telemetry.py |
| 116 | + :param drone: |
| 117 | + :return: |
| 118 | + """ |
| 119 | + |
| 120 | + async for battery in drone.telemetry.battery(): |
| 121 | + print(f"Battery: {battery.remaining_percent}") |
| 122 | + return battery.remaining_percent |
| 123 | + |
| 124 | + |
| 125 | +async def print_gps_info(drone=drone): |
| 126 | + """ |
| 127 | + Default print_gps_info command seperated and taken from telemetry.py |
| 128 | + :param drone: |
| 129 | + :return: |
| 130 | + """ |
| 131 | + |
| 132 | + async for gps_info in drone.telemetry.gps_info(): |
| 133 | + print(f"GPS info: {gps_info}") |
| 134 | + return gps_info |
| 135 | + |
| 136 | + |
| 137 | +async def print_position(drone=drone): |
| 138 | + """ |
| 139 | + Default print_position command seperated and taken from telemetry.py |
| 140 | + :param drone: |
| 141 | + :return: |
| 142 | + """ |
| 143 | + |
| 144 | + async for position in drone.telemetry.position(): |
| 145 | + print(position) |
| 146 | + return position |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + |
| 151 | + loop = asyncio.get_event_loop() |
| 152 | + loop.run_until_complete(setup()) |
| 153 | + loop.run_until_complete(main()) |
| 154 | + loop.run_until_complete(print_in_air(drone)) |
| 155 | + loop.run_until_complete(info(drone)) |
| 156 | + loop.run_until_complete(print_battery()) |
| 157 | + loop.run_until_complete(print_gps_info()) |
| 158 | + loop.run_until_complete(print_position()) |
0 commit comments