RevPi ModIO2 and OOP

Topics about the Software of Revolution Pi
Post Reply
wmatte
Posts: 4
Joined: 03 Mar 2023, 16:14

RevPi ModIO2 and OOP

Post by wmatte »

Hello everybody,

I am happy to take part into this forum using this amazing product :)

I am managing an industrial physical simulator with a bunch of sensors and actuators.
I would like to set up my code following OOP principles, with a class structure that reflects the actual real world.

BTW, I am having some issues, since looks like the RevPi I am using does not digest very well my idea.

In the following my setup:
- RevPi Core S
- RevPi DIO module.

In the following I report a test code that I am using:

The first class is called TestCycle. It contains the revpi cycle loop.

Code: Select all

import revpimodio2
from saw import Saw

class TestCycle():
    """Testing over the RevPi OOP functionalities."""
    def __init__(self):
        # Instantiate RevPiModIO controlling library
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
        # Handle SIGINT / SIGTERM to exit program cleanly
        self.rpi.handlesignalend(self.cleanup_revpi)
        
        # Saw process time counter
        self.time_sens_saw_count = 0

        # Instantiating all the needed objects
        self.saw = Saw()
        

    def cleanup_revpi(self):
        """Cleanup function to leave the RevPi in a defined state."""
        # Switch of LED and outputs before exit program
        self.rpi.core.a1green.value = False
        # Support time sensors
        self.time_sens_saw_count = 0
          
    def write(self):
        """Writes the output actuators states"""
        # Assigning to a the relative sensor, the variable value
        # Saw - Motor activation
        #self.rpi.io['O_4'].value = self.act_saw
        

    def start(self):
        """Start event system and own cyclic loop."""
        print('start')
        # Start event system loop without blocking here. Reference at 
        # https://revpimodio.org/en/events-in-the-mainloop/
        self.rpi.mainloop(blocking=False)

        while (self.rpi.exitsignal.wait(0.05) == False):
            # Sets the Rpi a1 light: switch on / off green part of LED A1 | or 
            # do other things
            self.rpi.core.a1green.value = not self.rpi.core.a1green.value

            # 2. Makes something
            if (self.time_sens_saw_count < 30):
                self.saw.turn_on()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 30 and 
                  self.time_sens_saw_count < 60):
                self.saw.turn_off()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 60):
                self.time_sens_saw_count = 0

if __name__ == "__main__":
    # Instantiating the controlling class
    root = TestCycle()
    # Launch the start function of the RevPi event control system
    root.start()
The second class is the one imported in the TestCycle __init__. In the following, the code:

Code: Select all

import revpimodio2

class Saw(object):
    """Saw class for saw objects."""
    def __init__(self):
        # Instantiate RevPiModIO controlling library
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
    
    def turn_on(self):
        self.rpi.io['O_4'].value = True

    def turn_off(self):
        self.rpi.io['O_4'].value = False
Well, I would really like to be able to control actuators and look for sensors from inside objects, using associated classes, like I did in the Saw() class.
Btw, it seems not completely possible: doing so, the actuator voltage does not stay steadily at 24V, but instead floats between 15 and 23.something Volts.

What I am doing wrong?
How can I achieve my target?

Thank you in advance for the support of the community! :)
User avatar
RevPiModIO
KUNBUS
Posts: 335
Joined: 20 Jan 2017, 08:44
Contact:

Re: RevPi ModIO2 and OOP

Post by RevPiModIO »

Hi :D

On the Revolution Pi should be only one instance of RevPiModIO. So you just have to pass your instance to the other classes and your program should work :D

By that method you can write control classes independently of the instantiation of the RevPiModIO module. The control classes will work alway, doesn't matter if you pass a RevPiModIO or RevPiNetIO instance.

Code: Select all

import revpimodio2
from saw import Saw

class TestCycle():
    """Testing over the RevPi OOP functionalities."""
    def __init__(self):
        # Instantiate RevPiModIO controlling library
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
        # Handle SIGINT / SIGTERM to exit program cleanly
        self.rpi.handlesignalend(self.cleanup_revpi)
        
        # Saw process time counter
        self.time_sens_saw_count = 0

        # Instantiating all the needed objects
        self.saw = Saw(self.rpi)
        

    def cleanup_revpi(self):
        """Cleanup function to leave the RevPi in a defined state."""
        # Switch of LED and outputs before exit program
        self.rpi.core.a1green.value = False
        # Support time sensors
        self.time_sens_saw_count = 0
          
    def write(self):
        """Writes the output actuators states"""
        # Assigning to a the relative sensor, the variable value
        # Saw - Motor activation
        #self.rpi.io['O_4'].value = self.act_saw
        

    def start(self):
        """Start event system and own cyclic loop."""
        print('start')
        # Start event system loop without blocking here. Reference at 
        # https://revpimodio.org/en/events-in-the-mainloop/
        self.rpi.mainloop(blocking=False)

        while (self.rpi.exitsignal.wait(0.05) == False):
            # Sets the Rpi a1 light: switch on / off green part of LED A1 | or 
            # do other things
            self.rpi.core.a1green.value = not self.rpi.core.a1green.value

            # 2. Makes something
            if (self.time_sens_saw_count < 30):
                self.saw.turn_on()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 30 and 
                  self.time_sens_saw_count < 60):
                self.saw.turn_off()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 60):
                self.time_sens_saw_count = 0

if __name__ == "__main__":
    # Instantiating the controlling class
    root = TestCycle()
    # Launch the start function of the RevPi event control system
    root.start()
----

Code: Select all

class Saw(object):
    """Saw class for saw objects."""
    def __init__(self, rpi):
        # take already existing instance of RevPiModIO controlling library
        self.rpi = rpi
    
    def turn_on(self):
        self.rpi.io['O_4'].value = True

    def turn_off(self):
        self.rpi.io['O_4'].value = False
Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
wmatte
Posts: 4
Joined: 03 Mar 2023, 16:14

Re: RevPi ModIO2 and OOP

Post by wmatte »

RevPiModIO wrote: 10 Mar 2023, 19:27 Hi :D

On the Revolution Pi should be only one instance of RevPiModIO. So you just have to pass your instance to the other classes and your program should work :D

By that method you can write control classes independently of the instantiation of the RevPiModIO module. The control classes will work alway, doesn't matter if you pass a RevPiModIO or RevPiNetIO instance.

Code: Select all

import revpimodio2
from saw import Saw

class TestCycle():
    """Testing over the RevPi OOP functionalities."""
    def __init__(self):
        # Instantiate RevPiModIO controlling library
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
        # Handle SIGINT / SIGTERM to exit program cleanly
        self.rpi.handlesignalend(self.cleanup_revpi)
        
        # Saw process time counter
        self.time_sens_saw_count = 0

        # Instantiating all the needed objects
        self.saw = Saw(self.rpi)
        

    def cleanup_revpi(self):
        """Cleanup function to leave the RevPi in a defined state."""
        # Switch of LED and outputs before exit program
        self.rpi.core.a1green.value = False
        # Support time sensors
        self.time_sens_saw_count = 0
          
    def write(self):
        """Writes the output actuators states"""
        # Assigning to a the relative sensor, the variable value
        # Saw - Motor activation
        #self.rpi.io['O_4'].value = self.act_saw
        

    def start(self):
        """Start event system and own cyclic loop."""
        print('start')
        # Start event system loop without blocking here. Reference at 
        # https://revpimodio.org/en/events-in-the-mainloop/
        self.rpi.mainloop(blocking=False)

        while (self.rpi.exitsignal.wait(0.05) == False):
            # Sets the Rpi a1 light: switch on / off green part of LED A1 | or 
            # do other things
            self.rpi.core.a1green.value = not self.rpi.core.a1green.value

            # 2. Makes something
            if (self.time_sens_saw_count < 30):
                self.saw.turn_on()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 30 and 
                  self.time_sens_saw_count < 60):
                self.saw.turn_off()
                self.time_sens_saw_count += 1
            elif (self.time_sens_saw_count >= 60):
                self.time_sens_saw_count = 0

if __name__ == "__main__":
    # Instantiating the controlling class
    root = TestCycle()
    # Launch the start function of the RevPi event control system
    root.start()
----

Code: Select all

class Saw(object):
    """Saw class for saw objects."""
    def __init__(self, rpi):
        # take already existing instance of RevPiModIO controlling library
        self.rpi = rpi
    
    def turn_on(self):
        self.rpi.io['O_4'].value = True

    def turn_off(self):
        self.rpi.io['O_4'].value = False
Sven
Thank you vey much for your help!
I tried the suggested solution: of course it work, and (more important) I fully understood the meaning of it!

I will report if I will have further issues in the next days or if I accomplished the project as expected!
Post Reply