Skip to content

Commit

Permalink
Formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Xierumeng committed Feb 21, 2024
1 parent a886a38 commit a94eabe
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 133 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ jobs:
- name: Install zbar library
run: sudo apt-get install libzbar0

# Run linters and formatters
- name: Linters and formatters
run: |
black --check .
flake8 .
pylint .
# Install dependencies and run tests with PyTest
- name: Run PyTest
run: pytest -vv
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

# Python
__pycache__/
.pytest_cache/
venv/

# Logging
*log*
logs/
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,3 @@
Common code across WARG repositories.

Follow the [instructions](https://uwarg-docs.atlassian.net/l/cp/2a6u0duY).

## comms

Python serialization and deserialization for Autonomy.

## camera

Python video ingest from camera device.

## mavlink

Connection to drone using the MAVLink protocol.

## qr

QR scanning and text output.
20 changes: 12 additions & 8 deletions camera/modules/camera_device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Camera device using OpenCV
Camera device using OpenCV.
"""

import sys
import time

Expand All @@ -10,15 +11,15 @@

class CameraDevice:
"""
Wrapper for camera
Wrapper for camera.
"""

def __init__(self, name: "int | str", save_nth_image: int = 0, save_name: str = ""):
def __init__(self, name: "int | str", save_nth_image: int = 0, save_name: str = "") -> None:
"""
name: Device name or index (e.g. /dev/video0 )
name: Device name or index (e.g. /dev/video0 ).
(optional) save_nth_image: For debugging, saves every nth image.
A value of 0 indicates no images should be saved
(optional) save_name: For debugging, file name for saved images
(optional) save_name: For debugging, file name for saved images.
"""
self.__camera = cv2.VideoCapture(name)
assert self.__camera.isOpened()
Expand All @@ -32,7 +33,10 @@ def __init__(self, name: "int | str", save_nth_image: int = 0, save_name: str =
if save_name != "":
self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_"

def __del__(self):
def __del__(self) -> None:
"""
Destructor.
"""
self.__camera.release()

def get_image(self) -> "tuple[bool, np.ndarray]":
Expand All @@ -41,9 +45,9 @@ def get_image(self) -> "tuple[bool, np.ndarray]":
"""
result, image = self.__camera.read()
if not result:
return result, image
return False, None

if self.__divisor != 0:
if self.__filename_prefix != "" and self.__divisor != 0:
if self.__counter % self.__divisor == 0:
filename = self.__filename_prefix + str(self.__counter) + ".png"
cv2.imwrite(filename, image)
Expand Down
19 changes: 16 additions & 3 deletions camera/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
Test camera physically.
"""

import pathlib

import cv2

from camera.modules.camera_device import CameraDevice


IMAGE_LOG_PREFIX = "log_image"
IMAGE_LOG_PREFIX = pathlib.Path("logs", "log_image")


if __name__ == "__main__":
def main() -> int:
"""
Main function.
"""
device = CameraDevice(0, 100, IMAGE_LOG_PREFIX)

while True:
Expand All @@ -24,7 +29,15 @@
cv2.imshow("Camera", image)

# Delay for 1 ms
if cv2.waitKey(1) & 0xFF == ord('q'):
if cv2.waitKey(1) & 0xFF == ord("q"):
break

return 0


if __name__ == "__main__":
result_main = main()
if result_main < 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
14 changes: 12 additions & 2 deletions camera_qr_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from qr.modules import qr_scanner


if __name__ == "__main__":

def main() -> int:
"""
Main function.
"""
camera = camera_device.CameraDevice(0, 0, "")

text = ""
Expand All @@ -30,4 +32,12 @@

print(text)

return 0


if __name__ == "__main__":
result_main = main()
if result_main < 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
9 changes: 6 additions & 3 deletions kml/modules/ground_locations_to_kml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module to convert ground locations list to kml document.
"""

import pathlib
import time

Expand All @@ -9,9 +10,11 @@
from . import location_ground


def ground_locations_to_kml(ground_locations: "list[location_ground.LocationGround]",
document_name_prefix: str,
save_directory: pathlib.Path) -> "tuple[bool, pathlib.Path | None]":
def ground_locations_to_kml(
ground_locations: "list[location_ground.LocationGround]",
document_name_prefix: str,
save_directory: pathlib.Path,
) -> "tuple[bool, pathlib.Path | None]":
"""
Generates KML file from a list of ground locations.
Expand Down
9 changes: 6 additions & 3 deletions kml/modules/location_ground.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Class to use instead of tuple for coordinates.
"""


class LocationGround:
"""
LocationGround class represents a geographical ground location with
Expand All @@ -17,7 +18,8 @@ class LocationGround:
__eq__(other): Checks if two LocationGround objects are equal.
__repr__(): Returns a string representation of the LocationGround object.
"""
def __init__(self, name: str, latitude: float, longitude: float):

def __init__(self, name: str, latitude: float, longitude: float) -> None:
"""
Constructor for the LocationGround object.
Expand Down Expand Up @@ -48,7 +50,8 @@ def __eq__(self, other: "LocationGround") -> bool:

def __repr__(self) -> str:
"""
String representation
String representation.
"""
return \
return (
f"LocationGround: {self.name}, latitude: {self.latitude}, longitude: {self.longitude}"
)
18 changes: 13 additions & 5 deletions kml/test_ground_locations_to_kml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test Process.
"""

import pathlib

import pytest
Expand All @@ -13,8 +14,13 @@
EXPECTED_KML_DOCUMENT_PATH = pathlib.Path(PARENT_DIRECTORY, "expected_document.kml")


# Test functions use test fixture signature names and access class privates
# No enable
# pylint: disable=protected-access,redefined-outer-name


@pytest.fixture
def locations():
def locations() -> "list[location_ground.LocationGround]": # type: ignore
"""
List of LocationGround.
"""
Expand All @@ -25,8 +31,9 @@ def locations():
]


def test_locations_to_kml_with_save_path(locations: "list[location_ground.LocationGround]",
tmp_path: pathlib.Path):
def test_locations_to_kml_with_save_path(
locations: "list[location_ground.LocationGround]", tmp_path: pathlib.Path
) -> None:
"""
Basic test case to save KML to the correct path when provided.
"""
Expand All @@ -51,5 +58,6 @@ def test_locations_to_kml_with_save_path(locations: "list[location_ground.Locati
assert actual_kml_file_path.suffix == ".kml"

# Compare the contents of the generated KML file with the static KML file
assert actual_kml_file_path.read_text(encoding="utf-8") \
== EXPECTED_KML_DOCUMENT_PATH.read_text(encoding="utf-8")
assert actual_kml_file_path.read_text(encoding="utf-8") == EXPECTED_KML_DOCUMENT_PATH.read_text(
encoding="utf-8"
)
56 changes: 25 additions & 31 deletions mavlink/modules/drone_odometry.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
"""
Position and orientation of drone.
"""

import math


# Basically a struct
# pylint: disable=too-few-public-methods
class DronePosition:
"""
WGS 84 following ISO 6709 (latitude before longitude).
"""

__create_key = object()

@classmethod
def create(cls,
latitude: "float | None",
longitude: "float | None",
altitude: "float | None") -> "tuple[bool, DronePosition | None]":
def create(
cls, latitude: "float | None", longitude: "float | None", altitude: "float | None"
) -> "tuple[bool, DronePosition | None]":
"""
latitude, longitude in decimal degrees.
altitude in metres.
Expand All @@ -35,11 +34,9 @@ def create(cls,

return True, DronePosition(cls.__create_key, latitude, longitude, altitude)

def __init__(self,
class_private_create_key,
latitude: float,
longitude: float,
altitude: float):
def __init__(
self, class_private_create_key: object, latitude: float, longitude: float, altitude: float
) -> None:
"""
Private constructor, use create() method.
"""
Expand All @@ -49,25 +46,21 @@ def __init__(self,
self.longitude = longitude
self.altitude = altitude

# pylint: enable=too-few-public-methods


# Basically a struct
# pylint: disable=too-few-public-methods
class DroneOrientation:
"""
Yaw, pitch, roll following NED system (x forward, y right, z down).
Specifically, intrinsic (Tait-Bryan) rotations in the zyx/3-2-1 order.
"""

__create_key = object()

@classmethod
# Required for checks
# pylint: disable-next=too-many-return-statements
def create(cls,
yaw: "float | None",
pitch: "float | None",
roll: "float | None") -> "tuple[bool, DroneOrientation | None]":
def create(
cls, yaw: "float | None", pitch: "float | None", roll: "float | None"
) -> "tuple[bool, DroneOrientation | None]":
"""
yaw, pitch, roll in radians.
"""
Expand All @@ -91,7 +84,9 @@ def create(cls,

return True, DroneOrientation(cls.__create_key, yaw, pitch, roll)

def __init__(self, class_private_create_key, yaw: float, pitch: float, roll: float):
def __init__(
self, class_private_create_key: object, yaw: float, pitch: float, roll: float
) -> None:
"""
Private constructor, use create() method.
"""
Expand All @@ -101,21 +96,18 @@ def __init__(self, class_private_create_key, yaw: float, pitch: float, roll: flo
self.pitch = pitch
self.roll = roll

# pylint: enable=too-few-public-methods


# Basically a struct
# pylint: disable=too-few-public-methods
class DroneOdometry:
"""
Wrapper for DronePosition and DroneOrientation.
"""

__create_key = object()

@classmethod
def create(cls,
position: DronePosition,
orientation: DroneOrientation) -> "tuple[bool, DroneOdometry | None]":
def create(
cls, position: DronePosition, orientation: DroneOrientation
) -> "tuple[bool, DroneOdometry | None]":
"""
Position and orientation in one class.
"""
Expand All @@ -127,10 +119,12 @@ def create(cls,

return True, DroneOdometry(cls.__create_key, position, orientation)

def __init__(self,
class_private_create_key,
position: DronePosition,
orientation: DroneOrientation):
def __init__(
self,
class_private_create_key: object,
position: DronePosition,
orientation: DroneOrientation,
) -> None:
"""
Private constructor, use create() method.
"""
Expand Down
Loading

0 comments on commit a94eabe

Please sign in to comment.