Mirror of infinity (Input=Output)

In this example, we mirror inputs to outputs in a loop. The DIO module in piCtory must be configured with Inputs: 19 / Outputs: 17. Thus, the inputs and outputs are combined into one word and are accessible as two bytes ().

We have integrated the cProfile here, so you can check the performance on the RevPi. This can be done with any program, but it should be removed from the release: D

We use an endless loop here! At the beginning of the loop, we read all inputs from the process image into our buffer, process the values, and write the set outputs back from the buffer to the process image.

We have included the signals, so that the program can exit cleanly by Ctrl + C, because if end = True, the loop will end!

#!/usr/bin/python3
import revpimodio
import signal
import time


class RevPiModIOTest():

    """Small test class for a self-made whipe-loop.

    When the class is instantiated, the start() function is called
    and goes into our endless loop. This can be left by Ctrl + C cleanly
    werden

    """

    def __init__(self):
        """Is called while instantiation."""
        # Signal events
        signal.signal(signal.SIGINT, self._sigexit)
        signal.signal(signal.SIGTERM, self._sigexit)

        # Variable to exit the Whipe-Loop
        self.beenden = False

        # RevPiModIO Instantieren
        self.revpi = revpimodio.RevPiModIO()

    def _sigexit(self, signum, frame):
        """Signal handler to exit."""
        self.beenden = True

    def start(self):
        """The program is running in an endless Loop here."""

        # LED A1 set to green on Core
        self.revpi.devices.core.A1 = 1

        # Get into the loop
        while not self.beenden:

            # Reading inputs from Processimage in to the buffer
            self.revpi.devices.readprocimg()

            # Mirroring input Bytes on Output Bytes
            # (NOTE: Read the above mentioned piCtory configuration of the DIOs!)
            self.revpi.devices[32]["Output"].value = self.revpi.devices[32]["Input"].value

            # Writeing outputs from Buffer in to Prozessimage
            self.revpi.devices.writeprocimg()

            # Timestamp as Screen output (Otherwise it is Looks so dead, costs
            # time though!)
            print(time.time(), end="\r")

            # Wait 50 Milliseconds
            time.sleep(0.05)

        # Turn off LED A1 after quitting the While-Loop
        self.revpi.devices.core.A1 = 0
        self.revpi.devices.writeprocimg()


if __name__ == "__main__":
    root = RevPiModIOTest()
    root.start()

The matching piCtory configuration looks like this:

Leave a Reply

Your email address will not be published.