Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Multitasking #613

Closed
MartinGarciaMatienzo opened this issue Jan 20, 2022 · 14 comments
Closed

[Question] Multitasking #613

MartinGarciaMatienzo opened this issue Jan 20, 2022 · 14 comments
Labels
support Request for technical support for a problem that is not a bug or feature request topic: MicroPython Issues related to the MicroPython programming language topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc.

Comments

@MartinGarciaMatienzo
Copy link

MartinGarciaMatienzo commented Jan 20, 2022

Question
Is there a way of using "wait=False" an amount infinite of times or another funtion for parallel tasks?

Context
I was coding a robot using a technic hub. The robot consists in 4 motors, which 3 of them have to move at the same time in different directions and speeds continuously. So, if I try to use wait=False for the different stages of moving, it won't work. I was wondering if there was a way or trick to use this many times or another function (I work directly in the pybricks web by the way). Maybe it´s something basic, I know

Here is part of the code:

from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Color, Button, Stop
from pybricks.tools import wait
from pybricksdev import 
#DECLARACION DE COMPONENTES
cerebro1=TechnicHub()
cerebro1.system.set_stop_button(Button.CENTER)
rojo=Motor(Port.A)
azul=Motor(Port.B)
amarillo=Motor(Port.C)
verde=Motor(Port.D)
#DEMO
rojo.run_time(500, 350, wait=False)
azul.run_time(500, 350, wait=False)
amarillo.run_time(500, 350, wait=False)
wait(1000)
verde.run_time(450, 2000)
if verde.control.done():
    rojo.run_time(-500, 350, wait=False)
azul.run_time(-500, 350, wait=False)
amarillo.run_time(-500, 350, wait=False)
@MartinGarciaMatienzo MartinGarciaMatienzo added support Request for technical support for a problem that is not a bug or feature request triage Issues that have not been triaged yet labels Jan 20, 2022
@dlech dlech added topic: MicroPython Issues related to the MicroPython programming language topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc. and removed triage Issues that have not been triaged yet labels Jan 20, 2022
@dlech
Copy link
Member

dlech commented Jan 20, 2022

@laurensvalk
Copy link
Member

Depending on what you need, the following solution may be a bit simpler:

https://docs.pybricks.com/en/stable/pupdevices/motor.html#waiting-for-two-parallel-actions-to-complete

@MartinGarciaMatienzo
Copy link
Author

MartinGarciaMatienzo commented Jan 20, 2022

I found this example avaible for EV3 does it work for the technic hub Motors?

        from pybricks.experimental import run_parallel
        from pybricks.tools import wait

        def task1():
            wait(1000)
            return 'OK1'

        def task2():
            wait(500)
            return 'OK2'

        result = run_parallel(task1, task2)
        print('task1:', repr(result[task1]))
        print('task2:', repr(result[task2]))
        # prints:
        # task1: 'OK1'
        # task2: 'OK2'

@dlech
Copy link
Member

dlech commented Jan 20, 2022

No, the EV3 has experimental support for threads, but the newer hubs don't support threads.

@MartinGarciaMatienzo
Copy link
Author

So is there a way to re-use wait=False in some kind of closed stages? The parallel task for running multiple Motors didn´t work well :c

@dlech
Copy link
Member

dlech commented Jan 20, 2022

What is the expected behavior and what is the actual behavior of the code that you shared?

@MartinGarciaMatienzo
Copy link
Author

Well, it controls a delta robot so it uses 3 motors to incline the arms that hold the tools. The Demo raises and lowers the tool, therefore all the motors have to run at the same time in one direction, then, all of them 3 run in the opposite direction to go down. That is the Demo, seeing that it didn't work, I didn't continue with the rest. I first planned to control multiple motors in divided sections to get different angles and distances of the tool

@MartinGarciaMatienzo
Copy link
Author

video-1642723407.mp4

It's supposed to do something like this for the Demo

@SpudGunMan
Copy link

the co-routine might be applicable? https://gist.github.com/dlech/fa48f9b2a3a661c79c2c5880684b63ae

@dlech
Copy link
Member

dlech commented Jan 21, 2022

yes, that is what #356 is

@laurensvalk
Copy link
Member

You can extend this for any number of motors.

from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait

# Initialize motors on port A and B.
track_motor = Motor(Port.A)
gripper_motor = Motor(Port.B)

# Make both motors perform an action with wait=False
track_motor.run_angle(500, 360, wait=False)
gripper_motor.run_angle(200, 720, wait=False)

# While one or both of the motors are not done yet,
# do something else. In this example, just wait.
while not track_motor.control.done() or not gripper_motor.control.done():
    wait(10)

print("Both motors are done!")

But in your example, you use run_time, so you could also just do this:

DURATION = 350
rojo.run_time(500, DURATION, wait=False)
azul.run_time(500, DURATION, wait=False)
amarillo.run_time(500, DURATION, wait=False)
wait(DURATION)

@MartinGarciaMatienzo
Copy link
Author

Yes, I did that, but i couldn't run these 3 motors at the same time in reverse after this instruction to raise the tool in the center of the robot

@MartinGarciaMatienzo
Copy link
Author

I could, thanks, i used def to separate every action as dlech said. Thanks for your support :)

@dlech
Copy link
Member

dlech commented Mar 21, 2023

Future visitors will be interested in the latest developments #917.

@dlech dlech closed this as completed Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Request for technical support for a problem that is not a bug or feature request topic: MicroPython Issues related to the MicroPython programming language topic: multitasking Issues releated to parallel threads, tasks, coroutines, etc.
Projects
None yet
Development

No branches or pull requests

4 participants