Skip to content

Commit

Permalink
Added more docstrings to ProfiledPIDController. Created and used Type…
Browse files Browse the repository at this point in the history
…Aliases (FloatSupplier & FloatOrFloatSupplier). Updated UseOutputFucntion protocol based on testing.
  • Loading branch information
cwstryker committed Feb 10, 2024
1 parent 9306cf1 commit 07af9dc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions commands2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import button
from . import cmd
from . import typing

from .commandscheduler import CommandScheduler
from .conditionalcommand import ConditionalCommand
Expand Down
13 changes: 9 additions & 4 deletions commands2/profiledpidcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
# the WPILib BSD license file in the root directory of this project.
#

from typing import Any, Callable, Generic, Union
from typing import Any, Generic

from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians

from .command import Command
from .subsystem import Subsystem
from .typing import TProfiledPIDController, UseOutputFunction
from .typing import (
FloatOrFloatSupplier,
FloatSupplier,
TProfiledPIDController,
UseOutputFunction,
)


class ProfiledPIDCommand(Command, Generic[TProfiledPIDController]):
Expand All @@ -26,8 +31,8 @@ class ProfiledPIDCommand(Command, Generic[TProfiledPIDController]):
def __init__(
self,
controller: TProfiledPIDController,
measurementSource: Callable[[], float],
goalSource: Union[float, Callable[[], float]],
measurementSource: FloatSupplier,
goalSource: FloatOrFloatSupplier,
useOutput: UseOutputFunction,
*requirements: Subsystem,
):
Expand Down
20 changes: 10 additions & 10 deletions commands2/profiledpidsubsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ def __init__(
controller: TProfiledPIDController,
initial_position: float = 0,
):
"""Creates a new PIDSubsystem."""
"""
Creates a new Profiled PID Subsystem using the provided PID Controller
:param controller: the controller that controls the output
:param initial_position: the initial value of the process variable
"""
super().__init__()
self._controller: TProfiledPIDController = controller
self._enabled = False
Expand All @@ -46,15 +52,11 @@ def getController(
return self._controller

def setGoal(self, goal):
"""
Sets the goal state for the subsystem.
"""
"""Sets the goal state for the subsystem."""
self._controller.setGoal(goal)

def useOutput(self, output: float, setpoint: TTrapezoidProfileState):
"""
Uses the output from the controller object.
"""
"""Uses the output from the controller object."""
raise NotImplementedError(f"{self.__class__} must implement useOutput")

def getMeasurement(self) -> float:
Expand All @@ -75,7 +77,5 @@ def disable(self):
self.useOutput(0, TrapezoidProfile.State())

def isEnabled(self) -> bool:
"""
Returns whether the controller is enabled.
"""
"""Returns whether the controller is enabled."""
return self._enabled
12 changes: 10 additions & 2 deletions commands2/typing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Protocol, TypeVar
from typing import Callable, Protocol, TypeVar, Union

from typing_extensions import TypeAlias
from wpimath.controller import ProfiledPIDController, ProfiledPIDControllerRadians
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians

# Generic Types
TProfiledPIDController = TypeVar(
"TProfiledPIDController", ProfiledPIDControllerRadians, ProfiledPIDController
)
Expand All @@ -13,10 +15,16 @@
)


# Protocols - Structural Typing
class UseOutputFunction(Protocol):

def __init__(self): ...
def __init__(self, *args, **kwargs) -> None: ...

def __call__(self, t: float, u: TTrapezoidProfileState) -> None: ...

def accept(self, t: float, u: TTrapezoidProfileState) -> None: ...


# Type Aliases
FloatSupplier: TypeAlias = Callable[[], float]
FloatOrFloatSupplier: TypeAlias = Union[float, Callable[[], float]]

0 comments on commit 07af9dc

Please sign in to comment.