Dronekit代码学习(二)基础状态实时获取

实时获取飞控当前状态

代码如下:

起飞5m后,读取飞机当前状态,返航,关闭连接。

# -*- coding: utf-8 -*-

'''
---------------------------------------------------------------------------
获取无人机基础状态信息

---------------------------------------------------------------------------

'''
from __future__ import print_function
import time
from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink import mavutil


#连接
vehicle = connect('127.0.0.1:14551', wait_ready=True)

#起飞
def arm_and_takeoff(aTargetAltitude):
    
    print("起飞前检查...") 
    while not vehicle.is_armable:
        print (" 等待飞机初始化...") 
        time.sleep(1)
    print ("切换至GUIDED模式...")
    vehicle.mode    = VehicleMode("GUIDED")
    print ("解锁...")
    vehicle.armed   = True
    while not vehicle.armed:
        print ("等待解锁...")
        time.sleep(1)
    print ("起飞!!!...")
    vehicle.simple_takeoff(aTargetAltitude) 
    while True:
        print (" 当前高度: ", vehicle.location.global_relative_frame.alt)
        
        if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95:
            print ("到达目标高度...")
            break
        time.sleep(1)
#起飞5m
arm_and_takeoff(5)

#版本
print ("Autopilot Firmware version: %s" % vehicle.version)
#全球定位信息
#经纬和高度
print ("Global Location: %s" % vehicle.location.global_frame) 
#经纬和相对高度
print ("Global Location (relative altitude): %s" % vehicle.location.global_relative_frame) 
#相对home点的位置信息
print ("Local Location: %s" % vehicle.location.local_frame  )  
#高度
print ("Attitude: %s" % vehicle.attitude) 
#速度
print ("Velocity: %s" % vehicle.velocity) 
#Gps质量
print ("GPS: %s" % vehicle.gps_0) 
#地速
print ("Groundspeed: %s" % vehicle.groundspeed)
#空速 
print ("Airspeed: %s" % vehicle.airspeed) 
#云台
print ("Gimbal status: %s" % vehicle.gimbal) 
#电量
print ("Battery: %s" % vehicle.battery) 
#ekf
print ("EKF OK?: %s" % vehicle.ekf_ok) 
#心跳
print ("Last Heartbeat: %s" % vehicle.last_heartbeat) 
#超声波或激光
print ("Rangefinder: %s" % vehicle.rangefinder) 
#距离
print ("Rangefinder distance: %s" % vehicle.rangefinder.distance) 
#电压
print ("Rangefinder voltage: %s" % vehicle.rangefinder.voltage) 
#朝向
print ("Heading: %s" % vehicle.heading) 
#是否可以解锁
print ("Is Armable?: %s" % vehicle.is_armable) 
#系统状态
print ("System status: %s" % vehicle.system_status.state) 
#飞行模式
print ("Mode: %s" % vehicle.mode.name  )   
#解锁状态
print ("Armed: %s" % vehicle.armed  )  


print("设置模式返航...")
vehicle.mode = VehicleMode("LAND")
print("关闭连接")
vehicle.close()