在 Mission Planner 中使用Python 脚本

Mission Planner 的一个很酷的功能是它可以运行 Python 脚本,这是一种扩展程序内建函数之外的功能的简单方法。它还可以很容易地与其它dll和模块集成,远远超出Mission Planner的现有功能限制。


概述

你可以轻松地对无人机进行编程以执行任何操作,从机器人杂技到脚本驱动的任务。前支持 Python 2.x,最高支持 2.7(Mission Planner 在内部使用 IronPython 的实现)。

除了常规的 Python 命令,以下是你可以使用的特殊Mission Planner命令:

cs.VARIABLENAME = currentstate

可以使用Mission Planner中“飞行数据”的状态选项卡上的任何变量。

Script.METHODNAME(...)


选项是:

Script.Sleep(ms)
Script.ChangeParam(name,value)
Script.GetParam(name)
Script.ChangeMode(mode) (与飞行模式设置界面中“AUTO”中显示的相同)
>Script.WaitFor(string,timeout)
Script.SendRC(channel,pwm,sendnow)


这里有一个例子,控制多翼飞机在空中翻滚!

print 'Start Script'
for chan in range(1,9):
    Script.SendRC(chan,1500,False)
    Script.SendRC(3,Script.GetParam('RC3_MIN'),True)

Script.Sleep(5000)

while cs.lat == 0:
    print 'Waiting for GPS'
    Script.Sleep(1000)

print 'Got GPS'
jo = 10 * 13
print jo
Script.SendRC(3,1000,False)
Script.SendRC(4,2000,True)
cs.messages.Clear()
Script.WaitFor('ARMING MOTORS',30000)
Script.SendRC(4,1500,True)
print 'Motors Armed!'
Script.SendRC(3,1700,True)

while cs.alt < 50:
    Script.Sleep(50)

Script.SendRC(5,2000,True) # acro
Script.SendRC(1,2000,False) # roll
Script.SendRC(3,1370,True) # throttle

while cs.roll > -45: # top half 0 - 180
    Script.Sleep(5)

while cs.roll < -45: # -180 - -45
    Script.Sleep(5)

Script.SendRC(5,1500,False) # stabilize
Script.SendRC(1,1500,True) # level roll
Script.Sleep(2000) # 2 sec to stabilize
Script.SendRC(3,1300,True) # throttle back to land
thro = 1350 # will descend

while cs.alt > 0.1:
    Script.Sleep(300)

Script.SendRC(3,1000,False)
Script.SendRC(4,1000,True)
Script.WaitFor('DISARMING MOTORS',30000)
Script.SendRC(4,1500,True)

print 'Roll complete'


由Mission Planner公开给Python的类

Mission Planner使用以下代码公开类:

将类引用到python

scope.SetVariable(Variablename, ClassInstance);


公开了以下类:
scope.SetVariable("MAV", MainV2.comPort);
scope.SetVariable("cs", MainV2.comPort.MAV.cs);
scope.SetVariable("Script", this);
scope.SetVariable("mavutil", this);

你可以在此处添加自己的类。现在让我们探索你可以使用这些类在脚本中使用的重要方法和属性。

Class Name: Script.cs
Python Variable: Script , mavutil

MethodDescription
bool ChangeParam (string param, float value) 
Float GetParam (string param) 
bool ChangeMode (string mode)Changes flying mode
bool SendRC (int channel, ushort pwm, bool sendnow)Send Chxout values.


Class Name: CurrentState.cs
Python Variable: cs

MethodTypeDescription
rollfloatRoll (deg)
pitchfloatPitch (deg)
yawfloatYaw (deg)
latfloatLatitude (deg)
lngfloatLongitude (deg)
groundcoursefloatGround Course (deg)
altfloatAltitude (dist)
altoffsethomefloatAltitude Home Offset (dist)
gpsstatusfloatGPS Status
gpshdopfloatGPS HDOP
satcountfloatSatellite Count
altd100floatAltitude / 100
altd1000floatAltitude / 1000
airspeedfloatAirspeed (speed)
targetairspeedfloatAirspeed Target (speed)
groundspeedfloatGround Speed (speed)
verticalspeedfloatVertical Speed (speed)
wind_dirfloatWind Direction (deg)
wind_velfloatWind Velocity (speed)
ax, ay, azfloatAcceleration Values in x,y,z
gx, gy, gzfloatGyro Values in x,y,z
mx, my, mzfloatMag Values in x,y,z
failsafeboolFail Safe Active or Not
rxrssifloat 
chx1in, chx2in, …. chx8infloatInput Channels from 1 to 8
ch1out, chx2out, …. chx8outfloatOutput Channel form 1 to 8
nav_rollfloatRoll Target (deg)
nav_pitchfloatPitch Target (deg)
nav_bearingfloatBearing target (deg)
target_bearingfloatBearing Target (deg)
wp_distfloatDistance to Next Waypoint (dist)
alt_errorfloatAltitude Error (dist)
ber_errorfloatBearing Error (dist)
aspd_errorfloatAirspeed Error (speed)
wpnofloatFlying Mode
modeStringFlying Mode
dimbratefloatClimb Rate (speed)
totintTime over target (sec)
distTraveledfloatDistance Traveled (dist)
timeInAirfloatTime in Air (sec)
turnratefloatTurn Rate (speed)
radiusfloatTurn Radius (dist)
battery_voltagefloatBattery Voltage (volt)
battery_remainingfloatBattery Remaining (%)
currentfloatbattery Current (Amps)
HomeAltfloat 
DistToHomefloatAbsolute Pressure Value
press_absfloatAbsolute Pressure Value
sonarrangefloatSonar Range (meters)
sonarVoltagefloatSonar Voltage (volt)
armedboolTrue if Armed


请注意,虽然这些属性是读/写,但写入其中一些可能会破坏状态。使用Script类中的方法来控制车辆。例如,使用Script.ChangeMode(xmode)而不是 cs.mode = xmode。

Class Name: MavLink.cs
Python Variable: MAV

MethodDescription
bool setParam (string paramname, float value)Same as Script.ChangeParam()
bool doARM (bool armit)BE CAREFUL when using it.
byte getWPCount ( )Gets Waypoints Count.


使用 CPython 标准库

你可以从常规的Python 2中导入标准库。将以下代码添加到脚本的顶部(将"c:\python27\lib"替换为你自己的文件夹):

import sys
sys.path.append(r"c:\python27\lib")


例如,在这里,我们正在导入串行库、os库和线程库,它们在典型的Python 2.7安装目录里面的两个文件夹中。
添加这三个库的特定文件夹路径后,我们就可以使用下一个“import”命令来导入它们:

import sys
sys.path.append(r"c:\Python27\Lib\site-packages")
sys.path.append(r"c:\Python27\Lib")
import serial, os, threading


教程和示例脚本

编写固定翼特技
添加新变量和类
编写基于时间的脚本



登录后回复