Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple formatting improvements. #83

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions simple_pid/pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _clamp(value, limits):
return value


class PID(object):
class PID:
"""A simple PID controller."""

def __init__(
Expand Down Expand Up @@ -114,9 +114,10 @@ def __call__(self, input_, dt=None):

now = self.time_fn()
if dt is None:
dt = now - self._last_time if (now - self._last_time) else 1e-16
dt = (now - self._last_time) or 1e-16
elif dt <= 0:
raise ValueError('dt has negative value {}, must be positive'.format(dt))
msg = f'dt has negative value {dt}, must be positive'
raise ValueError(msg)

if self.sample_time is not None and dt < self.sample_time and self._last_output is not None:
# Only update every sample_time seconds
Expand Down Expand Up @@ -162,15 +163,15 @@ def __call__(self, input_, dt=None):

def __repr__(self):
return (
'{self.__class__.__name__}('
'Kp={self.Kp!r}, Ki={self.Ki!r}, Kd={self.Kd!r}, '
'setpoint={self.setpoint!r}, sample_time={self.sample_time!r}, '
'output_limits={self.output_limits!r}, auto_mode={self.auto_mode!r}, '
'proportional_on_measurement={self.proportional_on_measurement!r}, '
'differential_on_measurement={self.differential_on_measurement!r}, '
'error_map={self.error_map!r}'
f'{self.__class__.__name__}('
f'Kp={self.Kp!r}, Ki={self.Ki!r}, Kd={self.Kd!r}, '
f'setpoint={self.setpoint!r}, sample_time={self.sample_time!r}, '
f'output_limits={self.output_limits!r}, auto_mode={self.auto_mode!r}, '
f'proportional_on_measurement={self.proportional_on_measurement!r}, '
f'differential_on_measurement={self.differential_on_measurement!r}, '
f'error_map={self.error_map!r}'
')'
).format(self=self)
)

@property
def components(self):
Expand Down Expand Up @@ -242,7 +243,8 @@ def output_limits(self, limits):
min_output, max_output = limits

if (None not in limits) and (max_output < min_output):
raise ValueError('lower limit must be less than upper limit')
msg = 'lower limit must be less than upper limit'
raise ValueError(msg)

self._min_output = min_output
self._max_output = max_output
Expand Down
28 changes: 15 additions & 13 deletions simple_pid/pid.pyi
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
from typing import Callable, Optional, Tuple
from typing import Callable, Optional

_Limits = Tuple[Optional[float], Optional[float]]
_Components = Tuple[float, float, float]
_Tunings = Tuple[float, float, float]
import typing_extensions

def _clamp(value: Optional[float], limits: _Limits) -> Optional[float]: ...
_Limits: typing_extensions.TypeAlias = tuple[float | None, float | None]
_Components: typing_extensions.TypeAlias = tuple[float, float, float]
_Tunings: typing_extensions.TypeAlias = tuple[float, float, float]

class PID(object):
def _clamp(value: float | None, limits: _Limits) -> float | None: ...

class PID:
Kp: float
Ki: float
Kd: float
setpoint: float
sample_time: Optional[float]
sample_time: float | None
proportional_on_measurement: bool
differential_on_measurement: bool
error_map: Optional[Callable[[float], float]]
error_map: Callable[[float], float] | None
time_fn: Callable[[], float]
def __init__(
self,
Kp: float = ...,
Ki: float = ...,
Kd: float = ...,
setpoint: float = ...,
sample_time: Optional[float] = ...,
sample_time: float | None = ...,
output_limits: _Limits = ...,
auto_mode: bool = ...,
proportional_on_measurement: bool = ...,
differential_on_measurement: bool = ...,
error_map: Optional[Callable[[float], float]] = ...,
time_fn: Optional[Callable[[], float]] = ...,
error_map: Callable[[float], float] | None = ...,
time_fn: Callable[[], float] | None = ...,
starting_output: float = ...,
) -> None: ...
def __call__(self, input_: float, dt: Optional[float] = ...) -> Optional[float]: ...
def __call__(self, input_: float, dt: float | None = ...) -> float | None: ...
def __repr__(self) -> str: ...
@property
def components(self) -> _Components: ...
Expand All @@ -43,7 +45,7 @@ class PID(object):
def auto_mode(self) -> bool: ...
@auto_mode.setter
def auto_mode(self, enabled: bool) -> None: ...
def set_auto_mode(self, enabled: bool, last_output: Optional[float] = ...) -> None: ...
def set_auto_mode(self, enabled: bool, last_output: float | None = ...) -> None: ...
@property
def output_limits(self) -> _Limits: ...
@output_limits.setter
Expand Down