首先在树莓派根目录下新建一个名为test的文件夹用于存放测试代码文件。和使用windows系统存放文件一样,不过这是用命令操作,新手可能不太习惯,命令:mkdir ,输入以下命令:
mkdir -p ~/test
可以输入 ls 命令,查看是否新建好这个文件夹,如下所示:
Pixhawk无人机扩展教程(3)---树莓派安装Dronekit及读取飞控数据-4.jpg (32.2 KB, 下载次数: 0)
2020-7-4 19:04 上传
通过cd命令切换到test文件夹下,创建名为connect.py的文件,测试树莓派能否连接飞控:
cd test
touch connect.py
可以输入 ls 命令,查看是否新建好这个文件,如下所示:
Pixhawk无人机扩展教程(3)---树莓派安装Dronekit及读取飞控数据-5.jpg (41.89 KB, 下载次数: 0)
2020-7-4 19:04 上传
用 vim编辑文件:
sudo vim connect.py
将以下代码复制到文件中:
from dronekit import connect
# Connect to the Vehicle (in this case a UDP endpoint)
vehicle = connect('/dev/ttyUSB0', wait_ready=True, baud=921600)
# vehicle is an instance of the Vehicle class
print "Autopilot Firmware version: %s" % vehicle.version
print "Autopilot capabilities (supports ftp): %s" % vehicle.capabilities.ftp
print "Global Location: %s" % vehicle.location.global_frame
print "Global Location (relative altitude): %s" % vehicle.location.global_relative_frame
print "Local Location: %s" % vehicle.location.local_frame #NED
print "Attitude: %s" % vehicle.attitude
print "Velocity: %s" % vehicle.velocity
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
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 # settable
print "Armed: %s" % vehicle.armed # settable
Pixhawk无人机扩展教程(3)---树莓派安装Dronekit及读取飞控数据-6.jpg (129.06 KB, 下载次数: 0)
2020-7-4 19:04 上传
保存代码并退出(如何保存编辑操作请参考《Pixhawk无人机扩展教程(2)》)
以上代码涵盖了大部分的无人机状态,通过执行以上代码就能读取当前飞控状态参数。
注意:不要急着执行以上脚本。可能提示权限不够出错。需要赋予USB0端口权限,以下逐步介绍。