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

Experiment: Using gpiod for driving pins #271

Closed
wants to merge 29 commits into from
Closed

Conversation

RobinTail
Copy link
Collaborator

Probably closes #270 and maybe will help with #255

@RobinTail RobinTail added enhancement New feature or request refactoring Better implementation of the same thing breaking Breaking changes labels May 19, 2024
@RobinTail RobinTail changed the base branch from master to develop May 19, 2024 08:56
@RobinTail RobinTail removed refactoring Better implementation of the same thing breaking Breaking changes labels May 19, 2024
@RobinTail RobinTail changed the title Using gpiod for driving pins Experiment: Using gpiod for driving pins May 19, 2024
@RobinTail RobinTail force-pushed the use-gpiod branch 2 times, most recently from e2fc545 to b5f8192 Compare May 19, 2024 10:34
@RobinTail RobinTail added the breaking Breaking changes label May 19, 2024
setup.py Show resolved Hide resolved
mypy.ini Outdated Show resolved Hide resolved
mypy.ini Outdated Show resolved Hide resolved
@@ -35,7 +35,7 @@ def get_version_and_cmdclass(pkg_path):

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
plugin_description = """A plugin that adds buttons to the navbar for toggling GPIO on the RPi. It can be used for turning relays on and off."""
plugin_description = """A plugin that adds buttons to the navbar for toggling GPIO on Raspberry Pi. It can be used for turning relays on and off."""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not only Raspberry Pi now


def xor(left: bool, right: bool) -> bool:
return left is not right

class Relay():
def __init__(self, pin: int, inverted: bool):
self.request = request_lines(
"/dev/gpiochip0",
Copy link
Collaborator Author

@RobinTail RobinTail May 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ this should be configurable

tests/test_driver.py Outdated Show resolved Hide resolved
@RobinTail
Copy link
Collaborator Author

RobinTail commented May 19, 2024

🚀 5.0.0a1

🆗 It does some relay switching on startup.

❌ Revealed issues:

  • Relays marked as inverted are switched to the wrong position on startup (something is messed up with it)
  • Some relay seems to be exclusively locked and which causes a system error while handling event
2024-05-19 13:52:34,482 - octoprint.plugins.octorelay - DEBUG - Received the ClientOpened event having payload: {'remoteAddress': ''}
2024-05-19 13:52:34,483 - octoprint.plugins.octorelay - DEBUG - Updating the UI
2024-05-19 13:52:34,508 - octoprint.plugins.octorelay - DEBUG - Finding the upcoming tasks
2024-05-19 13:52:34,511 - octoprint.plugin - ERROR - Error while calling plugin octorelay
Traceback (most recent call last):
  File "/home/pi/oprint/lib/python3.9/site-packages/octoprint/plugin/__init__.py", line 275, in call_plugin
    result = getattr(plugin, method)(*args, **kwargs)
  File "/home/pi/oprint/lib/python3.9/site-packages/octoprint/util/__init__.py", line 1686, in wrapper
    return f(*args, **kwargs)
  File "/home/pi/oprint/lib/python3.9/site-packages/octoprint_octorelay/__init__.py", line 181, in on_event
    self.update_ui()
  File "/home/pi/oprint/lib/python3.9/site-packages/octoprint_octorelay/__init__.py", line 316, in update_ui
    relay = Relay(
  File "/home/pi/oprint/lib/python3.9/site-packages/octoprint_octorelay/driver.py", line 11, in __init__
    self.request = request_lines(
  File "/home/pi/oprint/lib/python3.9/site-packages/gpiod/__init__.py", line 54, in request_lines
    return chip.request_lines(*args, **kwargs)
  File "/home/pi/oprint/lib/python3.9/site-packages/gpiod/chip.py", line 315, in request_lines
    req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size)
OSError: [Errno 16] Device or resource busy

@RobinTail RobinTail added the help wanted Extra attention is needed label May 19, 2024
@RobinTail
Copy link
Collaborator Author

RobinTail commented May 19, 2024

🚀 5.0.0a2

❌ not fixed (initial state)
❌ switching happens twice (on/off) immediately

request_lines seems to be always pushing some initial value, it should not be used for reading the state

@RobinTail
Copy link
Collaborator Author

RobinTail commented May 19, 2024

It seems that this library won't work with existing code implementation treating each switch as a standalone operation.
Instead, the driver should either be a singleton, or the code should be refactored to create Relay instances once and then operate them as needed. It could be places in Model entry.
https://www.tomshardware.com/how-to/control-raspberry-pi-5-gpio-with-python-3

@RobinTail
Copy link
Collaborator Author

I also found out, that there is a mode Direction.AS_IS that does not set the initial value.
Moreover, it's possible to set value on demand from that mode.
However, I'm not sure how'd it work if the pin is initially in Input mode.

@RobinTail
Copy link
Collaborator Author

RobinTail commented May 19, 2024

Alternatively, there is a much better documented library
https://gpiozero.readthedocs.io/en/latest/index.html
That is confirmed to work on Raspberry 5 (applicable to #255 )
But not sure if it's applicable for #270
UPD: no, it does not

And more gpiod examples: https://github.com/hhk7734/python3-gpiod/tree/main/py_src/gpiod/test

@RobinTail RobinTail force-pushed the use-gpiod branch 4 times, most recently from 7a555a3 to 5a0ca35 Compare May 20, 2024 06:40
@RobinTail
Copy link
Collaborator Author

🚀 5.0.0a3

🆗 Fixed initial state
🆗 No conflicts this time
🆗 Switching works
❌ Polling thread does not seems to monitor changes correctly

@RobinTail
Copy link
Collaborator Author

Won't work without much greater refactoring

@RobinTail RobinTail closed this May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking changes dependencies enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BIGTREETECH BTT Pi V1.2 The plugin is installed but it is not in the plugin manager.
2 participants