上一篇 | 目录 | 下一篇 |
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
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
Method | Description |
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
Method | Type | Description |
roll | float | Roll (deg) |
pitch | float | Pitch (deg) |
yaw | float | Yaw (deg) |
lat | float | Latitude (deg) |
lng | float | Longitude (deg) |
groundcourse | float | Ground Course (deg) |
alt | float | Altitude (dist) |
altoffsethome | float | Altitude Home Offset (dist) |
gpsstatus | float | GPS Status |
gpshdop | float | GPS HDOP |
satcount | float | Satellite Count |
altd100 | float | Altitude / 100 |
altd1000 | float | Altitude / 1000 |
airspeed | float | Airspeed (speed) |
targetairspeed | float | Airspeed Target (speed) |
groundspeed | float | Ground Speed (speed) |
verticalspeed | float | Vertical Speed (speed) |
wind_dir | float | Wind Direction (deg) |
wind_vel | float | Wind Velocity (speed) |
ax, ay, az | float | Acceleration Values in x,y,z |
gx, gy, gz | float | Gyro Values in x,y,z |
mx, my, mz | float | Mag Values in x,y,z |
failsafe | bool | Fail Safe Active or Not |
rxrssi | float | |
chx1in, chx2in, …. chx8in | float | Input Channels from 1 to 8 |
ch1out, chx2out, …. chx8out | float | Output Channel form 1 to 8 |
nav_roll | float | Roll Target (deg) |
nav_pitch | float | Pitch Target (deg) |
nav_bearing | float | Bearing target (deg) |
target_bearing | float | Bearing Target (deg) |
wp_dist | float | Distance to Next Waypoint (dist) |
alt_error | float | Altitude Error (dist) |
ber_error | float | Bearing Error (dist) |
aspd_error | float | Airspeed Error (speed) |
wpno | float | Flying Mode |
mode | String | Flying Mode |
dimbrate | float | Climb Rate (speed) |
tot | int | Time over target (sec) |
distTraveled | float | Distance Traveled (dist) |
timeInAir | float | Time in Air (sec) |
turnrate | float | Turn Rate (speed) |
radius | float | Turn Radius (dist) |
battery_voltage | float | Battery Voltage (volt) |
battery_remaining | float | Battery Remaining (%) |
current | float | battery Current (Amps) |
HomeAlt | float | |
DistToHome | float | Absolute Pressure Value |
press_abs | float | Absolute Pressure Value |
sonarrange | float | Sonar Range (meters) |
sonarVoltage | float | Sonar Voltage (volt) |
armed | bool | True if Armed |
请注意,虽然这些属性是读/写,但写入其中一些可能会破坏状态。使用Script类中的方法来控制车辆。例如,使用Script.ChangeMode(xmode)而不是 cs.mode = xmode。
Class Name: MavLink.cs
Python Variable: MAV
Method | Description |
bool setParam (string paramname, float value) | Same as Script.ChangeParam() |
bool doARM (bool armit) | BE CAREFUL when using it. |
byte getWPCount ( ) | Gets Waypoints Count. |
你可以从常规的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
编写固定翼特技
添加新变量和类
编写基于时间的脚本