Wednesday, September 17, 2014

Update PyMAVLink Library

due lag of update in PyMAVLINK inside ardupilot-sdk-python

I decide to using PyMAVLink Library from MAVLink Github instead

git clone https://github.com/mavlink/pymavlink

this repository come with pre-generate MAVLink basic messages definitions from ardupilotmega.xml,common.xml
This was Simple code modified from mavtester.py (example in PyMAVLink Library) to demonstrate MAVLink Functional by received HEARTBEAT message from Arducopter Board(APM2)
#!/usr/bin/env python

'''
PyMAVLink Test

'''
import sys, os, time
from math import radians

import mavlinkv10 as mavlink
import mavutil

# create a mavlink instance
mav1 = mavutil.mavlink_connection("COM12", 57600)

print("Waiting for HEARTBEAT")
mav1.wait_heartbeat()

print("Heartbeat from APM (system %u component %u)" % (mav1.target_system, mav1.target_component ))

while True:
    mav1.recv_msg()
    #Do Somethings
    time.sleep(0.01)


But this code doesn't run correctly with library from MAVLink Github after examination found that mavutil.py from MAVLink Github looklike it make for support both MAVLink Version 0.9 and Version 1.0
Here are Part of code from mavutil.py

if os.getenv('MAVLINK10'):
    import mavlinkv10 as mavlink
else:
    import mavlink

So I have to set value for MAVLINK10 for make mavutil.py recognize to run MAVLink Version 1.0
Code after add command to set value of MAVLINK10
#!/usr/bin/env python

import sys, os, time
from math import radians

os.environ["MAVLINK10"] = "1" #Set Mavlink Version 1.0

import mavlinkv10 as mavlink
import mavutil


# create a mavlink instance
mav1 = mavutil.mavlink_connection("COM12", 57600)

print("Waiting for HEARTBEAT")
mav1.wait_heartbeat()

print("Heartbeat from APM (system %u component %u)" % (mav1.target_system, mav1.target_component ))

while True:
    mav1.recv_msg()
    #Do Somethings
    time.sleep(0.01)

No comments:

Post a Comment