Skip to content

Commit

Permalink
refactor: use custom error
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Oct 30, 2024
1 parent 582771c commit 552c738
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 39 deletions.
2 changes: 2 additions & 0 deletions kbm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from kbm.exceptions import NegativeValueError as NegativeValueError
from kbm.model import KinematicBicycleModel as KinematicBicycleModel
23 changes: 0 additions & 23 deletions kbm/__init__.pyi

This file was deleted.

3 changes: 3 additions & 0 deletions kbm/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class NegativeValueError(Exception):
def __init__(self, argument: str) -> None:
super().__init__(f"{argument} must be positive.")
55 changes: 55 additions & 0 deletions kbm/model/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from typing import ReadOnly, TypedDict

class VehicleState(TypedDict):
x: ReadOnly[float]
y: ReadOnly[float]
yaw: ReadOnly[float]
steer: ReadOnly[float]
velocity: ReadOnly[float]
angular_velocity: ReadOnly[float]

class KinematicBicycleModel:
def __init__(self, wheelbase: float, max_steer: float, delta_time: float = 0.05) -> None:
"""
Summary
-------
This class implements the 2D Kinematic Bicycle Model for vehicle dynamics
Attributes
----------
wheelbase (float) : vehicle's wheelbase [m]
max_steer (float) : vehicle's steering limits [rad]
delta_time (float) : discrete time period [s]
Methods
-------
update(x, y, yaw, steer, velocity, acceleration) -> VehicleState
updates the vehicle's state using the kinematic bicycle model
"""
def update(
self,
x: float,
y: float,
yaw: float,
steer: float,
velocity: float,
acceleration: float,
) -> VehicleState:
"""
Summary
-------
updates the vehicle's state using the kinematic bicycle model
Parameters
----------
x (int) : vehicle's x-coordinate [m]
y (int) : vehicle's y-coordinate [m]
yaw (int) : vehicle's heading [rad]
steer (int) : vehicle's steering angle [rad]
velocity (int) : vehicle's velocity in the x-axis [m/s]
acceleration (int) : vehicle's accleration [m/s^2]
Returns
-------
vehicle_state (VehicleState) : updated vehicle state dictionary
"""
6 changes: 3 additions & 3 deletions kbm/kbm.pyx → kbm/model/model.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cython: language_level=3, cdivision=True

from libc.math cimport atan2, cos, sin, tan

from kbm.exceptions import NoNegativeValueError

cdef packed struct VehicleState:
long double x
Expand All @@ -19,10 +19,10 @@ cdef class KinematicBicycleModel:

def __init__(self, const long double wheelbase, const long double max_steer, const long double delta_time=0.05):
if delta_time <= 0:
raise ValueError("`delta_time` must be positive")
raise NoNegativeValueError("delta_time")

if wheelbase <= 0:
raise ValueError("`wheelbase` must be positive")
raise NoNegativeValueError("wheelbase")

self.delta_time = delta_time
self.wheelbase = wheelbase
Expand Down
9 changes: 2 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ reinstall-package = ["scipath"]
scipath = { git = "https://github.com/winstxnhdw/scipath" }

[dependency-groups]
dev = [
"cython>=3.0.11",
"matplotlib>=3.9.2",
"scipath",
]
dev = ["cython>=3.0.11", "matplotlib>=3.9.2", "scipath", "setuptools>=75.3.0"]

[tool.ruff]
select = ["ALL"]
ignore = ["D", "TCH", "PLR0913", "PLC0414"]
src = ["scipy"]
ignore = ["D", "TCH", "PLR0913", "PLC0414", "PYI021"]
line-length = 120

[tool.pyright]
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from Cython.Build import cythonize
from setuptools import find_packages, setup
from setuptools import Extension, find_packages, setup

extensions = [
Extension("kbm.model", ["kbm/model/model.pyx"]),
]

setup(
name="kbm",
packages=find_packages(include=["kbm", "kbm.*"]),
ext_modules=cythonize("kbm/*.pyx"), # pyright: ignore [reportUnknownArgumentType]
ext_modules=cythonize(extensions), # pyright: ignore [reportUnknownArgumentType]
)
69 changes: 65 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 552c738

Please sign in to comment.