NWT.Stuff
Josef, what do you mean by the PWM line ?

That’s the advantage of psm. If GPIO12 is not working just try another available GPIO pin in the initial pin array.
Reply 1 0
Josef Bernhardt
Hello Kevin,
my example works with the standard Raspberry PI driver.
Unfortunately I have a problem with the Python driver!
I use this circuit where I have taken over the pins from the graphic on the website!
Greetings to Josef

RaspiTestBoardSchaltplan.jpg 
Reply 0 0
Josef Bernhardt
Hello Kevin
although I have the lines% IX0.0 to% IX0.7 in my program
only IX0.0 to IX 0.6 and QX0.0 to QX0.6 are displayed in the debug output.
Is there something wrong with the counter?
Best wishes
Joseph

How can I use PWM %QW0

(*if xRun = True then*)

wPwm1 := wPwm1 + 1000;
if wPwm1 > 16000 then
wPwm1 := 0;
end_if;


fbTmr(IN:=xRun,PT:=T#200ms,Q=>xTmrDone,ET=>fbTmrEt);
if xTmrDone = True then
xTmrDone := False;
fbTmr(IN:=xTmrDone,PT:=T#200ms,Q=>xTmrDone,ET=>fbTmrEt);

case myInt of
0: LED1 := NOT LED1;
1: LED2 := NOT LED2;
2: LED3 := NOT LED3;
3: LED4 := NOT LED4;
4: LED5 := NOT LED5;
5: LED6 := NOT LED6;
6: LED7 := NOT LED7;
7: LED8 := NOT LED8;
end_case;

myInt := myInt + 1;
if myInt > 7 then
myInt := 0;
end_if;



end_if;
Reply 0 0
Josef Bernhardt
Now it works with the original pin assignment

now only QX0 is missing


# OpenPLC Python SubModule (PSM) -
#
# This Module was developped for Trailer Board Application but will work for any application
# on Raspberry Pi. 8 Digital Inputs and 8 Digital Outputs are configured

#import all your libraries here
import psm
import time
import RPi.GPIO as GPIO

#global variables
# 8 Inputs Pins GPIO
i_Pin=[2,3,4,17,27,22,10,9]

# 8 Ouputs Pins GPIO
q_Pin=[14,15,23,24,25,8,7,12]

def hardware_init():
# Insert your hardware initialization code in here
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Pin Configuration
for i in range(0,8):
GPIO.setup(i_Pin[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(q_Pin[i], GPIO.OUT)

psm.start()

def update_inputs():
#place here your code to update inputs
#global counter
#global var_state

for i in range(0,8):
i_Str = 'IX0.'+str(i)
i_State = int(GPIO.input(i_Pin[i]))
print (i_Str,i_State)
psm.set_var(i_Str, i_State)

def update_outputs():
#place here your code to work on outputs
for j in range(0,8):
q_Str = 'QX0.'+str(j)
a = psm.get_var(q_Str)
print(q_Str,a)
if a == True:
GPIO.output(q_Pin[j], GPIO.HIGH)
else:
GPIO.output(q_Pin[j], GPIO.LOW)

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()
Reply 0 0
NWT.Stuff
Hi Josef,

I have not done this myself however it seems pretty straightforward.

https://circuitdigest.com/microcontroller-projects/raspberry-pi-pwm-tutorial

In the PSM world I think it should go something like this (not tested)

# Pin Configuration
pwm = IO.PWM(19,100)          #GPIO19 as PWM output, with 100Hz frequency


Then when we update the outputs

def update_outputs():
#place here your code to work on outputs
qw0_Str = 'QW0'
pwm_qw0 = psm.get_var(qw0_Str)

#Add some scaling here e.g. 0 to 65536 to Scale 0 to 100

pwm.ChangeDutyCycle(pwm_qw0)


Good Luck

Kevin
Reply 1 0
Josef Bernhardt
Hello Kevin,
Thank you for the info, unfortunately I can't do it!

This example works:

'''
Control the Brightness of LED using PWM on Raspberry Pi
http://www.electronicwings.com
'''

import RPi.GPIO as GPIO
from time import sleep

ledpin = 12 # PWM pin connected to LED
GPIO.setwarnings(False) #disable warnings
GPIO.setmode(GPIO.BOARD) #set pin numbering system
GPIO.setup(ledpin,GPIO.OUT)
pi_pwm = GPIO.PWM(ledpin,1000) #create PWM instance with frequency
pi_pwm.start(0) #start PWM of required Duty Cycle
while True:
for duty in range(0,65535,1000):
pi_pwm.ChangeDutyCycle(int(duty/655.35)) #provide duty cycle in the range 0-100
sleep(0.01)
sleep(0.5)

Here I get an error message in line:

Hello Kevin,
Thank you for the info, unfortunately I can't do it!

This example works:

Here I get an error message in line 59:

Even with the Init function line 33, nothing happens at the output!



# OpenPLC Python SubModule (PSM) -
#
# This Module was developped for Trailer Board Application but will work for any application
# on Raspberry Pi. 8 Digital Inputs and 8 Digital Outputs are configured

#import all your libraries here
import psm
import time
import RPi.GPIO as GPIO

#global variables
# 8 Inputs Pins GPIO
i_Pin=[2,3,4,17,27,22,10,9]

# 8 Ouputs Pins GPIO
q_Pin=[14,15,23,24,25,8,7,12]


pwmpin = 12

def hardware_init():
# Insert your hardware initialization code in here
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Pin Configuration
# GPIO18 as PWM output, with 1000Hz frequency

GPIO.setup(pwmpin,GPIO.OUT)
mypwm = GPIO.PWM(pwmpin,1000)
mypwm.start(0)
# Test output
mypwm.ChangeDutyCycle(50)

# Pin Configuration
for i in range(0,8):
GPIO.setup(i_Pin[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(q_Pin[i], GPIO.OUT)

psm.start()

def update_inputs():
#place here your code to update inputs
#global counter
#global var_state

for i in range(0,8):
i_Str = 'IX0.'+str(i)
i_State = int(GPIO.input(i_Pin[i]))
print (i_Str,i_State)
psm.set_var(i_Str, i_State)

def update_outputs():
#place here your code to work on outputs

qw0_Str = 'QW0'
pwm_qw0 = psm.get_var(qw0_Str)

#mypwm.ChangeDutyCycle(int(pwm_qw0/655.35))
print(qw0_Str,int(pwm_qw0/655.35))

for j in range(0,8):
q_Str = 'QX0.'+str(j)
a = psm.get_var(q_Str)
print(q_Str,a)
if a == True:
GPIO.output(q_Pin[j], GPIO.HIGH)
else:
GPIO.output(q_Pin[j], GPIO.LOW)

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

Reply 0 0
NWT.Stuff
Hi Josef,

It thing is just a problem with indentation. Try this. It worked for me but I had no hardware connected to GPIO.

# OpenPLC Python SubModule (PSM) -
# 
# This Module was developped for Trailer Board Application but will work for any application
# on Raspberry Pi.  8 Digital Inputs and 8 Digital Outputs are configured
#import all your libraries here
import psm
import time
import RPi.GPIO as GPIO
#global variables
# 8 Inputs Pins GPIO 7-14
i_Pin=[7,8,9,10,11,12,13,14]
# 8 Ouputs Pins GPIO 17-24
q_Pin=[17,18,19,20,21,22,23,24]
def hardware_init():
    # Insert your hardware initialization code in here
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
   
    # Pin Configuration 
    for i in range(0,7):
        GPIO.setup(i_Pin[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.setup(q_Pin[i], GPIO.OUT)
    
    psm.start()
def update_inputs():
    #place here your code to update inputs
    #global counter
    #global var_state
    
    for i in range(0,7):
        i_Str = 'IX0.'+str(i)
        i_State = int(GPIO.input(i_Pin[i]))
        # print (i_Str,i_State)
        psm.set_var(i_Str, i_State)
    
def update_outputs():
    #place here your code to work on outputs
    for j in range(0,7):
        q_Str = 'QX0.'+str(j)
        a = psm.get_var(q_Str)
        # print(q_Str,a)
        if a == True:
            GPIO.output(q_Pin[j], GPIO.HIGH)
        else:
            GPIO.output(q_Pin[j], GPIO.LOW)    
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()

Reply 0 0
Josef Bernhardt
Hello Kevin,
the IO pins are ok, it's about the PWM pin
Greetings to Josef
Reply 0 0
thiagoralves
Python is very sensitive about indentation (the spaces or tabs you place at the start of a line). The code you posted here has absolutely no indentation. If that's the code you're trying to use, it is going to fail.
Reply 0 0
Josef Bernhardt
The code is already OK, the thing with the missing indentations is because I took over the program with cut and paste!
The problem is that the PWM pin cannot be addressed with the Python driver! It works with the raspberry PI C driver!
The same ST program is used in both cases.
ListingPMS.jpg 
Reply 0 0
Josef Bernhardt
Here the Python Driver and the Example Software !
Reply 0 0
Josef Bernhardt
This ist the Example Programm, it works TestPWM.jpg 
Reply 0 0
NWT.Stuff
Ok Josef

I'll have a proper look tomorrow. 

Kevin
Reply 1 0
NWT.Stuff
Hi Josef.

I think I have got it working.

The problem was that your "mypwm" object was created in a subroutine "def hardware ()" so it became a local object not available in "update_inputs" subroutine.

I moved the Configuration to top of program and it worked for me.  Python file attached.

Kevin
Reply 1 0
Josef Bernhardt
Hello Kevin,
I just tested it, congratulations, it works!
Thank you very much again for the effort!
So that the Python driver for the Raspberry Pi is compatible with the existing driver in C.
Best wishes
Joseph

P1000349-small.jpg 
Reply 1 0
Reply