|  | 
|  | 1 | +#!/usr/bin/env python3 | 
|  | 2 | + | 
|  | 3 | +import asyncio | 
|  | 4 | +from mavsdk import System, mavlink_direct | 
|  | 5 | +import json | 
|  | 6 | +import random | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | +async def send_airspeed(): | 
|  | 10 | +    drone = System() | 
|  | 11 | +    print("Waiting for drone to connect...") | 
|  | 12 | +    await drone.connect(system_address="udpin://0.0.0.0:14540") | 
|  | 13 | + | 
|  | 14 | +    async for state in drone.core.connection_state(): | 
|  | 15 | +        if state.is_connected: | 
|  | 16 | +            print(f"-- Connected to drone!") | 
|  | 17 | +            break | 
|  | 18 | + | 
|  | 19 | +    await drone.mavlink_direct.load_custom_xml(""" | 
|  | 20 | +    <mavlink> | 
|  | 21 | +      <messages> | 
|  | 22 | +        <message id="295" name="AIRSPEED"> | 
|  | 23 | +          <description>Airspeed information from a sensor.</description> | 
|  | 24 | +          <field type="uint8_t" name="id" instance="true">Sensor ID.</field> | 
|  | 25 | +          <field type="float" name="airspeed" units="m/s">Calibrated airspeed (CAS).</field> | 
|  | 26 | +          <field type="int16_t" name="temperature" units="cdegC">Temperature. INT16_MAX for value unknown/not supplied.</field> | 
|  | 27 | +          <field type="float" name="raw_press" units="hPa">Raw differential pressure. NaN for value unknown/not supplied.</field> | 
|  | 28 | +          <field type="uint8_t" name="flags" enum="AIRSPEED_SENSOR_FLAGS">Airspeed sensor flags.</field> | 
|  | 29 | +        </message> | 
|  | 30 | +      </messages> | 
|  | 31 | +    </mavlink> | 
|  | 32 | +    """) | 
|  | 33 | + | 
|  | 34 | +    for i in range(20): | 
|  | 35 | +        print(f"Sending airspeed {i}...") | 
|  | 36 | +        fields = { | 
|  | 37 | +            "id": 0, | 
|  | 38 | +            "airspeed": random.random() * 10, | 
|  | 39 | +            "temperature": 20 * 100, | 
|  | 40 | +            "raw_press": 1010, | 
|  | 41 | +            "flags": 0, | 
|  | 42 | +        } | 
|  | 43 | + | 
|  | 44 | +        message = mavlink_direct.MavlinkMessage( | 
|  | 45 | +            message_name="AIRSPEED", | 
|  | 46 | +            system_id=245, | 
|  | 47 | +            component_id=25, | 
|  | 48 | +            target_system_id=0, | 
|  | 49 | +            target_component_id=0, | 
|  | 50 | +            fields_json=json.dumps(fields), | 
|  | 51 | +        ) | 
|  | 52 | +        await drone.mavlink_direct.send_message(message) | 
|  | 53 | +        await asyncio.sleep(0.1) | 
|  | 54 | + | 
|  | 55 | + | 
|  | 56 | +if __name__ == "__main__": | 
|  | 57 | +    asyncio.run(send_airspeed()) | 
0 commit comments