Comments (6)
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.
I suppose if you do await
before each async call it basically makes it a blocking/sync call, right @JonasVautherin?
from mavsdk-python.
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.
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.
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.
Awesome! 🎉
from mavsdk-python.
Related Issues (20)
- getting mavsdk version HOT 5
- RuntimeError: aclose(): asynchronous generator is already running
- System.connect with time out HOT 3
- Running mavsdk server on Windows HOT 4
- How to manual control
- Unable to start a mission a second time HOT 3
- How to detect offboard command completion HOT 3
- One `mavsdk_server`, multiple drones HOT 2
- install mavsdk on py zero HOT 6
- "import_qgroundcontrol_mission_from_string" function not parsing the altitude correctly HOT 1
- drone.info.get_version() INFORMATION_NOT_RECEIVED_YET HOT 8
- FILE_DOES_NOT_EXIST on calling the drone.ftp.list_directory() to fetch internal directories file HOT 13
- The use of drone.offboard.set_position_ned HOT 9
- Unable to upload rally_items through MAVSDK HOT 10
- Error when uploading mission: INVALID_PARAM1 HOT 6
- `aiogrpc` RuntimeError HOT 35
- Running missions in ArduPilot HOT 12
- How to use mavsdk-python to takeoff without GPS HOT 4
- How do I use `telemetry_server` correctly? HOT 4
- Analyzing log files or Distance travelled data from mavsdk python HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mavsdk-python.