Skip to content

Commit

Permalink
Add command line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
l-gonz committed Mar 13, 2022
1 parent b493b8a commit 551a3da
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 27 deletions.
28 changes: 20 additions & 8 deletions dronecontrol/command_line.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import click
from dronecontrol.hands import mapper, graphics
from dronecontrol.follow import start

from dronecontrol import mapper
from dronecontrol import graphics

@click.group()
def main():
pass

@click.command()
def hand():
mapper.main()
@main.command()
@click.option("-p", "--port", type=int, help="port for UDP connections")
@click.option("--udp", "serial", flag_value=False, default=True, help="connect to drone system through UDP, default address is localhost")
@click.option("--serial", "serial", flag_value=True, help="connect to drone system through serial, default device is ttyUSB0")
def hand(port, serial):
mapper.main(port, serial)

@click.command()
@main.command()
def follow():
start.main()

@main.group()
def utils():
pass

@utils.command()
def take_image():
graphics.take_images()

main.add_command(hand)
main.add_command(take_image)
@utils.command()
def take_video():
graphics.take_video()
Empty file added dronecontrol/follow/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions dronecontrol/follow/start.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main():
print("Follow!")
Empty file added dronecontrol/hands/__init__.py
Empty file.
File renamed without changes.
3 changes: 2 additions & 1 deletion dronecontrol/graphics.py → dronecontrol/hands/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import mediapipe.python.solutions.drawing_utils as mp_drawing
import mediapipe.python.solutions.hands_connections as mp_connections

from dronecontrol import gestures, utils
from dronecontrol import utils
from dronecontrol.hands import gestures

class Color():
"""Define color constants to use with cv2."""
Expand Down
14 changes: 8 additions & 6 deletions dronecontrol/mapper.py → dronecontrol/hands/mapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio

from dronecontrol import graphics, utils
from dronecontrol import utils
from dronecontrol.hands import graphics
from .gestures import Gesture
from .pilot import System
from ..pilot import System

def map_gesture_to_action(system, gesture):
"""Map a hand gesture to a drone action."""
Expand Down Expand Up @@ -46,11 +47,12 @@ async def cancel_pending(task):
log.info("All tasks finished")


async def run():
async def run(port=None, serial=None):
"""Runs the GUI loop and the drone control thread simultaneously."""
global log
log = utils.make_logger(__name__)
system = System(serial="ttyUSB0")

system = System(port, serial)
gui = graphics.HandGui()

task = asyncio.create_task(system.start())
Expand All @@ -66,7 +68,7 @@ async def run():
log.warning("System stop")
await cancel_pending(task)

def main():
asyncio.run(run())
def main(port=None, serial=None):
asyncio.run(run(port, serial))


17 changes: 9 additions & 8 deletions dronecontrol/pilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ class System():
"""
STOP_VELOCITY = VelocityBodyYawspeed(0.0, 0.0, 0.0, 0.0)
WAIT_TIME = 0.05
DEFAULT_SERIAL_ADDRESS = "ttyUSB0"
DEFAULT_UDP_PORT = 14540
TIMEOUT = 15


def __init__(self, port=14540, serial=None):
def __init__(self, port=None, use_serial=False):
self.is_offboard = False
self.actions = [] # type: typing.List[Action]
self.port = port
self.serial = serial
self.port = port or self.DEFAULT_UDP_PORT
self.serial = self.DEFAULT_SERIAL_ADDRESS if use_serial else None
self.mav = mavsdk.System()
self.log = utils.make_logger(__name__)

Expand Down Expand Up @@ -90,12 +93,10 @@ def clear_queue(self):
async def connect(self):
"""Connect to mavsdk server."""
# Triggers TimeoutError if it can't connect
self.log.info("Waiting for drone to connect...")

if self.serial:
await asyncio.wait_for(self.mav.connect(system_address=f"serial:///dev/{self.serial}"), timeout=30)
else:
await asyncio.wait_for(self.mav.connect(system_address=f"udp://:{self.port}"), timeout=5)
address = f"serial:///dev/{self.serial}" if self.serial else f"udp://:{self.port}"
self.log.info("Waiting for drone to connect on address " + address)
await asyncio.wait_for(self.mav.connect(system_address=address), timeout=self.TIMEOUT)

async for state in self.mav.core.connection_state():
if state.is_connected:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='dronecontrol', # Required
version='0.0.1', # Required
version='0.2', # Required
description='A sample Python project', # Optional
long_description=long_description, # Optional
long_description_content_type='text/markdown', # Optional (see note above)
Expand All @@ -41,7 +41,7 @@
],

keywords='drone computer-vision px4', # Optional
packages=find_packages(where='dronecontrol'), # Required
packages=find_packages(exclude=['test', 'Firmware']), # Required
python_requires='>=3.6, <4',
#install_requires=['peppercorn'], # Optional
entry_points={ # Optional
Expand Down
1 change: 1 addition & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ DONT_RUN=1 make px4_sitl_default none_iris

# WSL
# export PX4_SIM_HOST_ADDR=172.25.48.1
# pip install -e .
3 changes: 2 additions & 1 deletion tests/test_gestures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dronecontrol import graphics, gestures
from dronecontrol.hands import graphics
from dronecontrol.hands import gestures

gui = graphics.HandGui()

Expand Down
3 changes: 2 additions & 1 deletion tests/test_graphics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import numpy
from dronecontrol import graphics, gestures
from dronecontrol.hands import graphics
from dronecontrol.hands import gestures

NO_CAM = 999

Expand Down

0 comments on commit 551a3da

Please sign in to comment.