Git Product home page Git Product logo

Comments (6)

yingshaoxo avatar yingshaoxo commented on August 18, 2024 2

You are right.

If we want to get the value once even if it blocks the main process, we could use something like this:

previous_altitude = None
async def print_altitude(drone):
    """ Prints the altitude when it changes """
    global previous_altitude
    async for position in drone.telemetry.position():
        print("reading...")
        altitude = round(position.relative_altitude_m)
        if altitude != previous_altitude:
            previous_altitude = altitude
            print(f"Altitude: {altitude}")
            break # use return if you want to return a value

or if we want to get the value new_altitude constantly, we could do this:

asyncio.ensure_future(print_altitude(drone)) # create a parallel task

new_altitude = None
async def print_altitude(drone):
    """ Prints the altitude when it changes """
    global new_altitude

    previous_altitude = None

    async for position in drone.telemetry.position():
        print("reading...")
        altitude = round(position.relative_altitude_m)
        if altitude != previous_altitude:
            previous_altitude = altitude
            print(f"Altitude: {altitude}")
            new_altitude = altitude

Now everything is clear to me, thank you again for helping me.


For those who don't know asyncio in Python3, I would recommend you take a look at:
https://python.readthedocs.io/en/stable/library/asyncio-task.html#example-chain-coroutines
https://docs.python.org/3/library/asyncio-task.html#coroutines

from mavsdk-python.

julianoes avatar julianoes commented on August 18, 2024

I suppose if you do await before each async call it basically makes it a blocking/sync call, right @JonasVautherin?

from mavsdk-python.

JonasVautherin avatar JonasVautherin commented on August 18, 2024

Since MAVSDK-Python doesn't have any documentation, meanwhile the example of this use case is not covered, I really don't know how to make it happen.

I think that all the functions are documented, e.g. see here.

The use of asyncio is not documented in MAVSDK because that's part of the Python standard library and has numerous tutorials available already, so we probably wouldn't make a better job here 😇.

This said, it seems to me like your issue is that you need to mix non-asyncio modules with MAVSDK, in which case I would advise you to have a look at this stackoverflow question.

To paraphrase the accepted answer there, you'll need an executor, e.g.:

executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)

And you'll need to run your blocking functions inside of it, e.g.:

loop.run_in_executor(executor, <your_blocking_function>, 12)

An alternative would be to wrap asyncio behind blocking functions (but I believe that's less elegant). For instance by having a class that defines drone and the loop, and then wrapping e.g. arm() into something like:

def arm(self):
    async def arm_aio():
        await self.drone.action.arm()

    self.loop.run_until_complete(arm_aio())

from mavsdk-python.

yingshaoxo avatar yingshaoxo commented on August 18, 2024

I see, thanks, I'll try it out.


But there is still have something that I can't fully understand:

In this example: https://github.com/mavlink/MAVSDK-Python/blob/master/examples/telemetry_takeoff_and_land.py

It created a parallel task by asyncio.ensure_future(print_altitude(drone))

Then, it will call the following function whenever the altitude gets change:

async def print_altitude(drone):
    """ Prints the altitude when it changes """

    previous_altitude = None

    async for position in drone.telemetry.position():
        altitude = round(position.relative_altitude_m)
        if altitude != previous_altitude:
            previous_altitude = altitude
            print(f"Altitude: {altitude}")

The problem is how does the program know the position has changed? Since it's only a for item in a_list? The list should be a fixed-length object. And the function should be called once and return immediately.

It seems like the drone.telemetry.position() is infinite in an async architecture. If that was true, how should I get the drone.telemetry.position() just for once in a blocking function?

from mavsdk-python.

JonasVautherin avatar JonasVautherin commented on August 18, 2024

It seems like the drone.telemetry.position() is infinite in an async architecture.

It is an "async generator", and effectively behave like an infinite stream 👍.

How should I get the drone.telemetry.position() just for once in a blocking function?

This example does something similar in the beginning:

print("Waiting for drone...")
async for state in drone.core.connection_state():
    if state.is_connected:
        print(f"Drone discovered with UUID: {state.uuid}")
        break

So you could just break after you get the first value. Or if you want to regularly poll it, you could have a coroutine populating your state somewhere, and poll the value from that, I would say.

from mavsdk-python.

JonasVautherin avatar JonasVautherin commented on August 18, 2024

Awesome! 🎉

from mavsdk-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.