Dronekit代码学习(一)连接及基础状态设置


目前Dronekit可自行设置的内容较少,但能满足基本使用要求。后面还是需要对Mavlink相关指令进行学习。

连接
地址根据实际情况修改,官网如下:
https://dronekit-python.readthedocs.io/en/latest/guide/connecting_vehicle.html

Linux USB 	/dev/ttyUSB0
Linux Serial port (RaspberryPi example) 	/dev/ttyAMA0 (also set baud=57600)
SITL UDP 	127.0.0.1:14550
SITL TCP 	tcp:127.0.0.1:5760
OSX USB 	dev/cu.usbmodem1
Windows USB (in this case on COM14) 	com14
Windows COM14 	com14 (also set baud=57600)

仿真用:

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

飞行模式

#设置mode
print("设置模式Guided...")
vehicle.mode = VehicleMode("GUIDED")

解锁

#解锁
print("解锁...")
vehicle.armed = True

地速,即移速

#设置移动速度,地速
print("设置移动速度...")
vehicle.groundspeed = 3.2

设置目标位置

#设置目标位置a
print("设置目标位置...")
a_location = LocationGlobalRelative(-34.364114, 149.166022, 30)

关闭连接

print("关闭连接")
vehicle.close()

起飞

#起飞
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)