picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

need to force IO

Fri Dec 07, 2018 6:36 am

Using the rpi & RPi.GPIO in python. In program "A" I use GPIO.PWM to set up a PWM output pin, works great, the signal comes out fine

A somewhat different & completely independent program "B" uses the same pin the same way (but different PWM settings). However, if program "A" is still running, the signal that comes out is some sort of "mixture" of the 2...essentially both PWMs running. How can "B" force the other PWM to stop, if program "A" is still open? The last program to run needs to completely take over the pin.

It is not desired to close program "A" from "B" (desired to keep "A" open, since it has other, non-interfering, functions), but might be acceptable as a last resort.
The same pin must be used for both programs.

danjperron
Posts: 3370
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: need to force IO

Fri Dec 07, 2018 1:05 pm

A - The best way will be to create a pipe and tell Program A that the program B want to take control of the PWM. Once the program B is done with it it should send a Pipe command again to release the control.


B - Method A could be done using a simple file. Create a ramdisk folder, (tmpfs type), to save your SD card.

C- Use Multiprocess and share memory.

D- Two python applications could also use the processing library. It support shared memory from two applications.

E- Open a TCP IP socket and do like A by sending command.

F- Use One GPIO pin to Tell A not to send the PWM command.

....


This is some ideas how to deal with it. The simplest is F.

picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Re: need to force IO

Fri Dec 07, 2018 4:30 pm

Do you have an example for suggestion "A" ...not sure about pipes
Could program "B" simply "reset" all of the io settings (thereby stopping the pwm from "A")?
How do you explain more about suggestion D (Two python applications could also use the processing library. It support shared memory from two applications.) I am using the Rpi.GPIO library.

danjperron
Posts: 3370
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: need to force IO

Sat Dec 08, 2018 1:40 am

I made the code for python3

Run pipeA.py first and pipeB.py on two different console.

P.S. I assume the pipeA read more often than pipeB. If it is not the case you should include a while loop until nothing is received into the isPWMLock() function.

pipeA.py

Code: Select all

import os,time,sys

pipe_name = '/run/lock/pwm_access'

lockPWM = False;

#create fifo pipe if it didn't exist

try:
    os.mkfifo(pipe_name)
except OSError:
#    print("unable to create fifo")
     pass

try:
  pipe_in = os.open(pipe_name,os.O_RDONLY | os.O_NONBLOCK)
except OSError:
  print("Unable to open Fifo")
  quit()


#return True if it's lock by the other process
#if nothing in buffer keep the latest info
def isPWMLock():
     global lockPWM
     try:
        readBuffer= os.read(pipe_in,1)
     except BlockingIOError as e:
        return 0
     if len(readBuffer)==1:
#        print("got data => {}".format(readBuffer[0]),end="")
        if readBuffer[0] == 1:
#           print("is lock")
           lockPWM= True
        else:
#           print("is not lock")
           lockPWM= False
     return lockPWM

while True:

  if isPWMLock():
     #don't do your PWM
     print("PWM is reserved by the other application")
  else:
     #Do your PWM
     print("I do have the right to PWM")
  time.sleep(1.0)
pipeB.py

Code: Select all

import os,time,sys

pipe_name = '/run/lock/pwm_access'



def lockPWM(lockFlag):
   try:
     file = os.open(pipe_name,os.O_WRONLY)
     if lockFlag:
        os.write(file,bytes([1]))
     else:
        os.write(file,bytes([0]))
     os.close(file)
   except OSError:
       print("Unable to open fifo")
       quit()


while True:

  #lock the PWM
  print("lock")
  lockPWM(1)
  #wait a little to be sure that the other application read the fifo
  time.sleep(5.0)
  #do your stuff

  #unlock the PWM
  print("unlock")
  lockPWM(0)
  time.sleep(5.0)
Last edited by danjperron on Sat Dec 08, 2018 8:21 pm, edited 1 time in total.

picandies
Posts: 186
Joined: Wed Nov 26, 2014 5:13 pm

Re: need to force IO

Sat Dec 08, 2018 5:18 am

danjperron--
Thank you so much...I'm going to give it a try...Need to get familiar with these 'pipes'

Return to “General discussion”