-
Notifications
You must be signed in to change notification settings - Fork 0
finished gpio repositories, PID logic sservice and temp_service #4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,52 +1,71 @@ | ||||||||||||||||
| from abc import ABC, abstractmethod | ||||||||||||||||
| import threading | ||||||||||||||||
| from typing import Any, Optional | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| class GPIORepository(ABC): | ||||||||||||||||
| """Repository for controlling a GPIO port as PWM output | ||||||||||||||||
| """ | ||||||||||||||||
|
|
||||||||||||||||
| @abstractmethod | ||||||||||||||||
| def setup_pwm(self, pin: int, frequency: float) -> None: | ||||||||||||||||
| """Setup a GPIO pin for PWM output | ||||||||||||||||
| def setup_pwm(self) -> None: ... | ||||||||||||||||
| @abstractmethod | ||||||||||||||||
| def set_duty_cycle(self, duty_cycle: float) -> None: ... | ||||||||||||||||
| @abstractmethod | ||||||||||||||||
| def get_duty_cycle(self) -> float: ... | ||||||||||||||||
| @abstractmethod | ||||||||||||||||
| def cleanup(self) -> None: ... | ||||||||||||||||
|
|
||||||||||||||||
| Args: | ||||||||||||||||
| pin (int): GPIO pin number | ||||||||||||||||
| frequency (float): PWM frequency in Hz | ||||||||||||||||
| """ | ||||||||||||||||
| pass | ||||||||||||||||
|
|
||||||||||||||||
| @abstractmethod | ||||||||||||||||
| def cleanup_channel(self, pin: int) -> None: | ||||||||||||||||
| """Cleanup a GPIO pin | ||||||||||||||||
| class RPiGPIORepository(GPIORepository): | ||||||||||||||||
| def __init__(self, gpio: Any, pin: int, frequency: float, duty_cycle: float = 0.0) -> None: | ||||||||||||||||
| self._lock: threading.Lock = threading.Lock() | ||||||||||||||||
| self.gpio = gpio | ||||||||||||||||
| self.pin: int = int(pin) | ||||||||||||||||
| self.frequency: float = float(frequency) | ||||||||||||||||
| self.duty_cycle: float = float(duty_cycle) | ||||||||||||||||
| self.pwm: Optional[Any] = None | ||||||||||||||||
|
|
||||||||||||||||
| Args: | ||||||||||||||||
| pin (int): GPIO pin number | ||||||||||||||||
| """ | ||||||||||||||||
| pass | ||||||||||||||||
| required_attrs = ("setmode", "BCM", "setup", "OUT", "PWM", "cleanup") | ||||||||||||||||
| missing = [ | ||||||||||||||||
| attr for attr in required_attrs if not hasattr(self.gpio, attr)] | ||||||||||||||||
| if missing: | ||||||||||||||||
| raise RuntimeError( | ||||||||||||||||
| f"gpio object missing attributes: {', '.join(missing)}, gpio interface must have these methods and properties to function properly") | ||||||||||||||||
|
||||||||||||||||
| f"gpio object missing attributes: {', '.join(missing)}, gpio interface must have these methods and properties to function properly") | |
| f"gpio object missing attributes: {', '.join(missing)}. GPIO interface must have these methods and properties to function properly") |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message does not include the underlying exception details, making debugging difficult. Include the original exception information using 'from e' or include the exception message in the error text to provide more context about what went wrong.
| except Exception: | |
| raise RuntimeError( | |
| "Failed to stop existing PWM instance, could not reinitialize PWM") | |
| except Exception as e: | |
| raise RuntimeError( | |
| f"Failed to stop existing PWM instance, could not reinitialize PWM: {e}" | |
| ) from e |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message does not include the underlying exception details, making debugging difficult. Include the original exception information using 'from e' or include the exception message in the error text to provide more context about what went wrong.
| except Exception: | |
| raise RuntimeError( | |
| "Failed to stop PWM instance during cleanup") | |
| except Exception as e: | |
| raise RuntimeError( | |
| "Failed to stop PWM instance during cleanup") from e |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message does not include the underlying exception details, making debugging difficult. Include the original exception information using 'from e' or include the exception message in the error text to provide more context about what went wrong.
| except Exception: | |
| raise RuntimeError( | |
| "Failed to cleanup GPIO pin during cleanup") | |
| except Exception as e: | |
| raise RuntimeError( | |
| "Failed to cleanup GPIO pin during cleanup") from e |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||
| import threading | ||||||
| from typing import Optional | ||||||
|
|
||||||
|
|
||||||
| class PIDController: | ||||||
| def __init__(self, kp: float, ki: float, kd: float, setpoint: float = 0.0): | ||||||
| self.kp = float(kp) | ||||||
| self.ki = float(ki) | ||||||
| self.kd = float(kd) | ||||||
| self.setpoint = float(setpoint) | ||||||
|
|
||||||
| self._integral = 0.0 | ||||||
| self._last_error = 0.0 | ||||||
| self._last_measurement: Optional[float] = None | ||||||
| self._lock = threading.Lock() | ||||||
|
|
||||||
| # ---------------------- | ||||||
| # PID Logic | ||||||
| # ---------------------- | ||||||
| def update(self, measurement: float) -> float: | ||||||
| with self._lock: | ||||||
| error = self.setpoint - measurement | ||||||
| new_integral = self._integral + error | ||||||
| derivative = 0.0 if self._last_measurement is None else ( | ||||||
| error - self._last_error) | ||||||
|
||||||
| error - self._last_error) | |
| -(measurement - self._last_measurement)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RPiGPIORepository implementation (lines 59-71 in gpio.py) does not validate that PWM is set up before calling cleanup(), but the mock raises an error if PWM is not set up. This inconsistency in error handling between the real implementation and mock could hide bugs. Either add validation to RPiGPIORepository.cleanup() or remove it from the mock to maintain consistency.