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()
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
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!