1

Currently, I am working with a EV3 lego robot that is controlled by several neurons. Now I want to modify the code (running on python3) in such a way that one can change certain parameter values on the run via the shell (Ubuntu) in order to manipulate the robot's dynamics at any time (and for multiple times). Here is a schema of what I have achieved so far based on a short example code:

from multiprocessing import Process
from multiprocessing import SimpleQueue
import ev3dev.ev3 as ev3

class Neuron:
    (definitions of class variables and update functions)

def check_input(queue):
    while (True):
        try:
            new_para = str(input("Type 'parameter=value': "))
            float(new_para[2:0])  # checking for float in input
            var = new_para[0:2]

            if (var == "k="):  # change parameter k
                queue.put(new_para)
            elif (var == "g="):  # change parameter g
                queue.put(new_para)
            else:
                print("Error". Type 'k=...' or 'g=...')
                queue.put(0)  # put anything in queue
        except (ValueError, EOFError):
            print("New value is not a number. Try again!")

(some neuron-specific initializations)

queue = SimpleQueue()
check = Process(target=check_input, args=(queue,))
check.start()

while (True):
    if (not queue.empty()):
        cmd = queue.get()
        var = cmd[0]
        val = float(cmd[2:])

        if (var == "k"):
            Neuron.K = val
        elif (var == "g"):
            Neuron.g = val

    (updating procedure for neurons, writing data to file)

Since I am new to multiprocessing there are certainly some mistakes concerning taking care of locking, efficiency and so on but the robot moves and input fields occur in the shell. However, the current problem is that it's actually impossible to make an input:

> python3 controller_multiprocess.py
> Type 'parameter=value': New value is not a number. Try again!
> Type 'parameter=value': New value is not a number. Try again!
> Type 'parameter=value': New value is not a number. Try again!
> ... (and so on)

I know that this behaviour is caused by putting the exception of EOFError due to the fact that this error occurs when the exception is removed (and the process crashes). Hence, the program just rushes through the try-loop here and assumes that no input (-> empty string) was made over and over again. Why does this happen? - when not called as a threaded procedure the program patiently waits for an input as expected. And how can one fix or bypass this issue so that changing parameters gets possible as wanted?

Thanks in advance!

catalyst
  • 11
  • 2
  • I hope the answers here will help: https://stackoverflow.com/questions/30134297/python-multiprocessing-stdin-input – ensonic Aug 31 '17 at 12:25
  • Yeah, thank you very much for pointing me to an appropriate issue I needed to solve my problem! @ensonic – catalyst Sep 01 '17 at 17:59

0 Answers0