2

I'm working on a project which controls the speed of a motor and the temperature of a heating element. I have to run the sample time of the motor on 10Hz. The duration of the function that I use to read my temperature sensor is 0.8sec. So i can't use the temperature reading in my main loop. Now I'm trying to use the concurrent.futures module to run the function of the temperature seperate from my main program once for every 10 cycles of my main program (duration main programm = 0.1s).

This is my code:

with concurrent.futures.ProcessPoolExecutor() as executor:
    p1 = executor.submit(GetTemp)  

while(time.time() < t_end):
    #Get Speed of Motor and Temperature
    RealRPMExtruder = round((tach.RPM()/2080),2) 

    if Counter >= 10:
        RealTemp = p1.result()
        p1 = executor.submit(GetTemp)
        counter = 0
    Counter += 1 

    #PID Controllers
    controlExtruder = pidExtruder(RealRPMExtruder)
    controlHeater = pidHeater(RealTemp)

    #Setting Output Values
    PWMExtruder.ChangeDutyCycle(controlExtruder)
    PWMHeater.ChangeDutyCycle(controlHeater)

The first 10 cycles it runs just fine, but the second time i try to start the function my programms stops. I need to run this continuous and for a long time, so using queue's and list (multiprocessing) won't work (I think). What do I need to do to call the function mutilple times with some sort of multiprocessing.

Barry
  • 21
  • 2
  • Once the context manager - `with concurrent.futures….` - exits, you cannot submit anything else to `executor`. You could put everything in/under the context manager but that doesn't seem right. I would probably set up a thread and a queue where the thread has a `while True` loop that gets the temperature data and puts it on the queue then read the queue from the *main* thread. – wwii Jan 03 '20 at 14:06
  • I believe your implementation only works the first time because your global `Counter` is never reset. The case do not match, and thus you assign a different variable named `counter` instead of `Counter`. – Jordan Brière Jan 03 '20 at 14:08
  • If reading the temperature sensor is blocking then maybe use multiprocessing instead of a thread or just dig in and using asyncio. – wwii Jan 03 '20 at 14:10

0 Answers0