Hey
@DJKalian could you post your PSM solution here so others can benefit? Indeed the Pi5 has a different GPIO structure. I couldn’t find any C library that works with it
No problem, I am just very inexperienced with forums posting, code and such. I will try to post my modified file.
Below is my PSM code. I use gpiozero to read and write to GPIO pins, and I used the psm.get_var and psm.set_var to update the IO variables. I think that any other working GPIO library can also be used in this way, I only used what came with Bookworm for simplicity.
It should be noted that I have modified my raspberrypi.cpp file to exclude some of the GPIO pins from being used as IO pins in OpenPLC. The reason for this is because eventually I would still like to use the I2C, SPI and UART interfaces to communicate with different external sensors that I am working on. For testing, I am only using 8 input and 8 output pins as physical IO. Some companies sell small 8 channel GPIO expander boards that can be used with I2C and SPI so I would like to make this a modular expansion system. That is why you see in my PSM code that I only use a limited number of GPIO.
I am only learning Python at the moment, so my code is very simple. I have not yet come to the point where I can write clean loops for updating the IO, such as what is in your original hardware drivers. I look forward to any improvements that can be suggested. This is an entirely self learning project for me.
Also, this is the file I copy to all of my different Pi architectures and so far they all run. I am working on adding and testing more architectures as well.
# - OpenPLC Python SubModule (PSM) -
#
# PSM is the bridge connecting OpenPLC core to Python programs. PSM allows
# you to directly interface OpenPLC IO using Python and even write drivers
# for expansion boards using just regular Python.
#
# PSM API is quite simple and just has a few functions. When writing your
# own programs, avoid touching on the "__main__" function as this regulates
# how PSM works on the PLC cycle. You can write your own hardware initialization
# code on hardware_init(), and your IO handling code on update_inputs() and
# update_outputs()
#
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
# psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
# psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
#
# Below you will find a simple example that uses PSM to switch OpenPLC's
# first digital input (%IX0.0) every second. Also, if the first digital
# output (%QX0.0) is true, PSM will display "QX0.0 is true" on OpenPLC's
# dashboard. Feel free to reuse this skeleton to write whatever you want.
#import all your libraries here
import psm
import time
from gpiozero import LED, Button
#global variables
input_IX0_0 = Button("GPIO17",pull_up=False)
input_IX0_1 = Button("GPIO27",pull_up=False)
input_IX0_2 = Button("GPIO22",pull_up=False)
input_IX0_3 = Button("GPIO5",pull_up=False)
input_IX0_4 = Button("GPIO6",pull_up=False)
input_IX0_5 = Button("GPIO13",pull_up=False)
input_IX0_6 = Button("GPIO19",pull_up=False)
input_IX0_7 = Button("GPIO26",pull_up=False)
output_QX0_0 = LED("GPIO23")
output_QX0_1 = LED("GPIO24")
output_QX0_2 = LED("GPIO25")
output_QX0_3 = LED("GPIO12")
output_QX0_4 = LED("GPIO16")
output_QX0_5 = LED("GPIO20")
output_QX0_6 = LED("GPIO21")
def hardware_init():
#Insert your hardware initialization code in here
psm.start()
def update_inputs():
#place here your code to update inputs
if input_IX0_0.is_pressed:
psm.set_var("IX0.0", True)
else:
psm.set_var("IX0.0", False)
if input_IX0_1.is_pressed:
psm.set_var("IX0.1", True)
else:
psm.set_var("IX0.1", False)
if input_IX0_2.is_pressed:
psm.set_var("IX0.2", True)
else:
psm.set_var("IX0.2", False)
if input_IX0_3.is_pressed:
psm.set_var("IX0.3", True)
else:
psm.set_var("IX0.3", False)
if input_IX0_4.is_pressed:
psm.set_var("IX0.4", True)
else:
psm.set_var("IX0.4", False)
if input_IX0_5.is_pressed:
psm.set_var("IX0.5", True)
else:
psm.set_var("IX0.5", False)
if input_IX0_6.is_pressed:
psm.set_var("IX0.6", True)
else:
psm.set_var("IX0.6", False)
if input_IX0_7.is_pressed:
psm.set_var("IX0.7", True)
else:
psm.set_var("IX0.7", False)
def update_outputs():
#place here your code to work on outputs
a = psm.get_var("QX0.0")
if a == True:
output_QX0_0.on()
else:
output_QX0_0.off()
a = psm.get_var("QX0.1")
if a == True:
output_QX0_1.on()
else:
output_QX0_1.off()
a = psm.get_var("QX0.2")
if a == True:
output_QX0_2.on()
else:
output_QX0_2.off()
a = psm.get_var("QX0.3")
if a == True:
output_QX0_3.on()
else:
output_QX0_3.off()
a = psm.get_var("QX0.4")
if a == True:
output_QX0_4.on()
else:
output_QX0_4.off()
a = psm.get_var("QX0.5")
if a == True:
output_QX0_5.on()
else:
output_QX0_5.off()
a = psm.get_var("QX0.6")
if a == True:
output_QX0_6.on()
else:
output_QX0_6.off()
if __name__ == "__main__":
hardware_init()
while (not psm.should_quit()):
update_inputs()
update_outputs()
time.sleep(0.1) #You can adjust the psm cycle time here
psm.stop()