Package: libmav
Libmav PythonNative python bindings for thelibmavlibrary.
libmav-python is a runtime defined low-level MAVLink library.In comparison to other python MAVLink libaries, it has certain advantages:Runtime defined message set.No need to recompile on message set change or custom messagesImplemented in C++.Much faster than pure pythonSimple, pythonic API.No need to learn a new API or asyncio, just use pythonInstallationpip install libmavExample usageimportlibmav# Create a message set from a mavlink xml filemessage_set=libmav.MessageSet('<PATH TO common.xml>')# You can also add additional messages here with inline xmlmessage_set.add_from_xml_string('''<mavlink><messages><message>...</message></messages></mavlink>''')# Create a message from the message setmessage=message_set.create('MY_PROTOCOL_MESSAGE')# Set fields of the message individuallymessage['my_numeric_field']=1message['my_char_array_field']='Hello world'message['my_float_array_field']=[1.0,2.0,3.0]message['my_int_array_field']=[4,5,6]# Get fields individuallymy_num=message['my_numeric_field']my_string=message['my_char_array_field']my_float_array_field=message['my_float_array_field']# You can also use pythonic things like list comprehensions# This creates a list of all the values in my_float_array_field multiplied by 2my_float_array_field=[x*2forxinmessage['my_float_array_field']]# Set fields from a dictmessage.set_from_dict({'my_numeric_field':1,'my_char_array_field':'Hello world','my_float_array_field':[1.0,2.0,3.0],'my_int_array_field':[4,5,6]})# Get fields as python dictmessage_dict=message.to_dict()# Connect to a TCP serverconn_physical=libmav.TCPClient('192.168.1.12',14550)conn_runtime=libmav.NetworkRuntime(self.message_set,heartbeat,conn_physical)connection=conn_runtime.await_connection(2000)# Check if connection is still aliveifnotconnection.alive():print('Connection lost, waiting for reconnect...')connection=conn_runtime.await_connection(2000)# Send a messageconnection.send(message)# Receive a message, timeout 1sreceived_message=connection.receive("HEARTBEAT",1000)# Receive a message, no timeoutreceived_message=connection.receive("HEARTBEAT")# Receive a message as the result of a message we send# This avoids the race condition between us sending and then waiting for receptionexpectation=connection.expect("PARAM_VALUE")connection.send(our_param_request_message)received_message=connection.receive(expectation,1000)# Connect to a Serial portconn_physical=libmav.Serial('/dev/ttyUSB0')conn_runtime=libmav.NetworkRuntime(self.message_set,heartbeat,conn_physical)⚠️Note:The network runtime object and the physical interface object must be kept
scope for the duration of the connection. Otherwise, the connection will not have
a valid underlying network and will crash.Install from sourcePrerequisitesA compiler with C++11 supportPip 10+ or CMake >= 3.4 (or 3.14+ on Windows, which was the first version to support VS 2019)Ninja or Pip 10+Just clone this repository and pip install. Note the--recursiveoption which is
needed for the pybind11 submodule:gitclone--recursivehttps://github.com/Auterion/libmav-python.git
pipinstall./libmav-pythonWith thesetup.pyfile included in this project, thepip installcommand will
invoke CMake and build the pybind11 module as specified inCMakeLists.txt.