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)

Saturday, September 13, 2014

Getting started with ardupilot-sdk-python

This ardupilot-sdk-python provided basic function

Sent command to arducopter
arm(),disarm(),takeoff(),land(),flyTo()

Received arducopter status isArmed(),getMode(),getAttitude(),getAltitude(),getHeading(),getSpeed(),getLocation()

First Clone ardupilot-sdk-python from github
git clone https://github.com/drgowen/ardupilot-sdk-python

Adding function for getting Raw IMU in SDK
 def getIMU(self):
   if 'RAW_IMU' not in self.mav.messages:
     raise Error("Haven't received RAW_IMU information yet")
   att = self.mav.messages['RAW_IMU']
   return self.dictCopy(att, ['xacc','yacc','zacc'])


Example for get Attitude,Raw IMU from /dev/ttyUSB0 Baudrate:57600
from ardupilot import ArduPilot
import time as time_ #make sure we don't override time

def millis():
    return int(round(time_.time() * 1000))

ac = ArduPilot("/dev/ttyUSB0",57600)
#ac.arm()
#ac.disarm()
start_time = millis()
while(1):
   if(millis()-start_time > 200):
      print ac.getAttitude()
      print ac.getIMU()
      start_time = millis()     


Reference: https://github.com/drgowen/ardupilot-sdk-python