I am interfacing industrial PLC's with the Raspberry Pi 3 in order to read the efficiency of the given line, and display live results. In essence I need to do the following things:
- Get notified when the the line is running. A simple input to the Raspberry Pi will do. However, I need to continuously note the time since the PLC was turned on, presumably using
- While keeping track of how much time has passed, other inputs to the RPi must be detected. These may represent parts that the machine produces. For the sake of simplicity, let us say that this is a button press.
- I then need to perform calculations every 10 seconds based on the number of parts produced and the time it took for these to be produced.
In this example I am using a button press (
GPIO.input(17)) to simulate when the line will start. Next, I am using another button (
GPIO.input(27)) to simulate the number of parts produced by the machine.
import time GPIO.setup(17, GPIO.IN) GPIO.setup(27, GPIO.IN) timer_a = time.time() if GPIO.input(27) == 0: #machine starts working flag_work = 1 while flag_work == 1: if time.time() - timer_a > 10 timer_a = time.time() # perform calculations here every 10 seconds if GPIO.input(17) == 0: parts_produced += 1 sleep(0.2) # debouncing purposes
As can be seen from the above example, the program execution remains inside while loop since the flag remains set to 1. I am looking for a way to keep track of the time that has passed while reading up to a potentially large number of inputs per second. Which is the best way to these both simultaneously?
Should multi threading, interrupts or callbacks be used in such a case?
Any suggestions on how to approach such a problem would be appreciated.