Python RevPiModIO library sometimes fails to change output state

Moderator: RevPiModIO

Post Reply
adb
Posts: 4
Joined: 12 Apr 2023, 08:54
Answers: 0

Python RevPiModIO library sometimes fails to change output state

Post by adb »

Hello,

I have a RevPi connect with a RevPI DIO module. I'm controlling the DIO module via a python script running inside a Docker container. The script is switching the output states of the DIO module in the order of 10-100 times a day.

I noticed that sometimes (once every 2-3 days) the script fails to change the load state, i.e., from the script log I see that the script has correctly switched off the output but the output is not switched off (e.g. if I execute piTest -r output)

I'm changing the load state by calling the _issue_deactivation_command of the below class:

Code: Select all

class RevPiAttachedLoad(Load):

    def __init__(self, name: str, consumption: int, revpi_io_obj: IOList, revpi_output_name: str, active_on_high: bool = True):
        super(RevPiAttachedLoad, self).__init__(name, consumption)
        self._revpi_io_obj = revpi_io_obj
        self._revpi_output_name = revpi_output_name

        if active_on_high:
            self._deactivation_value = False
            self._activation_value = True
        else:
            self._deactivation_value = True
            self._activation_value = False

        if revpi_io_obj is None:
            raise ValueError("RevpiConnector MUST be instantiated if one wants to use RevPiAttachedLoad")
        if self._revpi_output_name not in self._revpi_io_obj:
            raise ValueError("Unvalid/Unknown output name %s" % self._revpi_output_name)

    def _issue_deactivation_action(self):
        self._revpi_io_obj[self._revpi_output_name].value = self._deactivation_value

    def _issue_activation_action(self):
        self._revpi_io_obj[self._revpi_output_name].value = self._activation_value
I'm also trying to monitor the ioerror value of the RevPiModIO class but the value remains 0.
Is the above code the right way to change an output?
Is there a way to ensure that the output has correctly changed state?
I noticed that there is also a wait function in the io object but I'm not sure how to use it.

Some additional details on the setup:
Not sure if it's important but the script run several threads. Only one thread is changing the state of a specific output.
I'm running the python script inside a Docker container, with the following docker-compose config:

Code: Select all

version: '2'

services:



  watt-cutter:
    image: watt-cutter:devel
    container_name: watt-cutter
    restart: unless-stopped    
    command: /app/start.sh /app/conf.json
    volumes:
      - ./conf.json:/app/conf.json
      - /etc/revpi/config.rsc:/etc/revpi/config.rsc
    ports:
      - 9105:9105
    devices:
      - /dev/piControl0:/dev/piControl0
Revpi software verson:
2022-05-25-revpi-buster.img

Code: Select all

Found 2 devices:

Address: 0 module type: 105 (0x69) RevPi Connect V1.0
Module is present
     input offset: 113 length: 6
    output offset: 119 length: 5

Address: 31 module type: 96 (0x60) RevPi DIO V1.5
Module is present
     input offset: 0 length: 70
    output offset: 70 length: 18
I really appreciate your help, let me know if you need further details.
Alex
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python RevPiModIO library sometimes fails to change output state

Post by RevPiModIO »

Hi Alex!

The use of RevPiModIO looks just right. You are probably using a single instance for your entire program, which is recommended.

For me, it would be important to know which parameters you use in the instance (autorefresh, shared_procimg, etc.).

Gruß
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
adb
Posts: 4
Joined: 12 Apr 2023, 08:54
Answers: 0

Re: Python RevPiModIO library sometimes fails to change output state

Post by adb »

Hi Sven,
thank you for your super quick response!

Indeed, In the main thread I'm instantiating a single instance of RevPiModIO which is shared with the other threads.
The code that instantiate the RevPiModIO is as follows:

Code: Select all

revpi_connector = revpimodio2.RevPiModIO(autorefresh=True, shared_procimg=True)

... Instantiating other threads ....

if revpi_connector is not None:
        log.info("Registering event handlers")
        add_configured_handlers_to_revpi_event_listeners(revpi_connector.io, conf['revpi_connector']['event_handlers'])
        log.info("Starting main loop for event handling")
        revpi_connector.mainloop(blocking=False)
Now that I'm pasting the code here, I noticed that I still have the shared_procimg set as True. It was configured as True during development and debug, as I was using another python interpreter to experiment with the library. Can this be the root of the issue?

Thank you for your help
Alex
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python RevPiModIO library sometimes fails to change output state

Post by RevPiModIO »

Hi Alex!

It could really be that `shared_procimg=True` cause that problems. I just have a similar problem report with the flag on GitHub.

If you do not need the `shared_procimg=True` because no other Python processes access the process image, I would ask you to remove it / set it to False. If you could then run your system again for a few days with `shared_procimg` deactivated and tell me if the problem is fixed, that would help me a lot!

Greeting
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
adb
Posts: 4
Joined: 12 Apr 2023, 08:54
Answers: 0

Re: Python RevPiModIO library sometimes fails to change output state

Post by adb »

Hi Sven,
I restarted the container with shared_procimg set as false.
I'll write here in a few days with the result.
In the meanwhile, I'll have a look at the library code that manage the internal buffer.
Is this the Github issue you mention? https://github.com/naruxde/revpimodio2/issues/25

Best regards
Alex
adb
Posts: 4
Joined: 12 Apr 2023, 08:54
Answers: 0

Re: Python RevPiModIO library sometimes fails to change output state

Post by adb »

Hello,
a quick update on the issue. The script is running with shared_procimg set as False for one month now and the issue didn't show up again.
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python RevPiModIO library sometimes fails to change output state

Post by RevPiModIO »

Perfect, I think I found the error and can release a new version as RC this week. Could you please test it then?
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

Re: Python RevPiModIO library sometimes fails to change output state

Post by RevPiModIO »

An RC version for fixing the bug is now available on GitHub and as a Debian package.

https://github.com/naruxde/revpimodio2/ ... img-values
http://revpimodio.org/dnl/python3-revpimodio2_2.6.1~1-1_all.deb

It would be nice if it could be tested. The installation of the Debian package can be done as follows:

Code: Select all

wget 'http://revpimodio.org/dnl/python3-revpimodio2_2.6.1~1-1_all.deb'
sudo dpkg -i python3-revpimodio2_2.6.1~1-1_all.deb

When the final version 2.6.1 appears, the pre-release version is automatically replaced and the final version is installed.
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
Post Reply