0

I am helping coach First Lego League (FLL) and this year with the SPIKE robot they allow us to use Python to command the robot.

We used to be able (using Scratch style coding) to have the robot do two things at once, like drive forward and raise attachement.

But with Python everything is sequential. How could we have it do both of these at once, or send one function and not have it wait for a response before processing the next function?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
J7seaz
  • 1

1 Answers1

0

To run two processes at once in python, you can use the multiprocessing module to create separate processes for each command and run them simultaneously.

import multiprocessing

def run_command1():
    # code for command1

def run_command2():
    # code for command2

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=run_command1)
    p2 = multiprocessing.Process(target=run_command2)

    p1.start()
    p2.start()

This will create two separate processes for the run_command1() and run_command2() functions, and run them simultaneously. Consult the python documentation for more information on the multiprocessing module and managing processes in python.

  • You should make these processes be able to spawn children using `p1.daemon = True` and `p2.daemon = True`, knowing that these are running a robot, which might use Processes of their own. – Cheesebellies Dec 04 '22 at 16:46