Replies: 2 comments 2 replies
-
You are right to use In particular |
Beta Was this translation helpful? Give feedback.
-
You can't. The input function is a synchronous blocking function. It will block the whole event loop in the same thread. import uasyncio as asyncio
async def forward():
print("forward")
await asyncio.sleep_ms(1000)
async def backwards():
print("backwards")
await asyncio.sleep_ms(1000)
async def hcsr():
# if the eventloop is blocked by a
# sychronous function, then this while-true loop blocks
# until the sychronous function is done
while True:
print("Hcsr04")
await asyncio.sleep_ms(100)
async def main(direction):
# not awaiting for hcsr
# because it never finishes
# and it will block if you await for it
asyncio.create_task(hcsr())
# now the task runs concurently in the eventloop
while True:
if direction == "f":
# awaiting forward()
await asyncio.create_task(forward())
await asyncio.sleep(0.1)
elif direction == "b":
# awaiting backwards
await asyncio.create_task(backwards())
await asyncio.sleep(0.1)
# input() here is a no no, because it blocks the whole eventloop
# an async input function is required to do this right
# but for testing it's ok
# you'll see also, that hcsr is blocked, until
# the user enters a direction
direction = input("forward or backwards ? f or b ")
if __name__ == "__main__":
asyncio.run(main("f")) If you find an async input function, you could use this. I don't know if someone made a library for it. There is an If you're running this code on a Microcontroller, then just use two real buttons to test the functions. The access to gpios is very fast, and you won't recognize anything blocking, expect you program it to wait. @peterhinch also has good async functions for buttons and other hardware related stuff, which should run async. BTW: I was watching this video again: Asyncio in (Micro)Python |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to order a robot using asyncio.
For the tests I made a minimal program which does not do what I want!
To summarize, if I move forward, I would like the forward function to execute once and the hscr function to run in a loop, to test for the presence of an obstacle.
In my program, each function executes once.
And secondly when I order backwards, I would like the hscr function to be stopped. If the robot goes backwards and the probe tests forward, it's useless.
Do you have any idea on the feasibility of these two features?
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions