< >
Home » DroneKit-Python教程 » DroneKit-python入门教程教程-SITL仿真-继承和自定义Vehicle类

DroneKit-python入门教程教程-SITL仿真-继承和自定义Vehicle类

DroneKit-python入门教程教程-SITL仿真-继承和自定义Vehicle类

说明:

  • 介绍如何继承和自定义Vehicle类
  • MAVLink支持一项有用却又非常危险的功能:遥控信道覆盖(Channel Override)。
  • 遥控信道覆盖可以将任一至全部通道的遥控输入信号改写为任意值。
  • 当前,DroneKit的开发者已经建议不再使用此功能
  • 在使用时,需要维持输出值在上下限之间、飞控与指令发送端的连接稳定可靠、有完备的故障保护手段。
  • 遥控信道覆盖可以用作虚拟摇杆操控、或者以最“直观”的方式执行一些自动化任务。
  • 但是一般情况下,我们还是建议使用MAVLink指令

步骤:

  • 运行SITL
cd ~/ardupilot/ArduCopter/
./ArduCopter.elf --home -35,149,584,270 --model quad
  • 运行MAVProxy
mavproxy.py --master tcp:127.0.0.1:5760 --sitl 127.0.0.1:5501 --out 127.0.0.1:14550 --out 127.0.0.1:14551
  • (可选)使用MissionPlanner地面站监控无人机的状态。
  • 运行MissionPlanner地面站,右上角选择UDP,点击connect连接。端口填写14550
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
© Copyright 2015-2016, 3D Robotics.
channel_overrides.py: 
Demonstrates how set and clear channel-override information.
# NOTE: 
Channel overrides (a.k.a "RC overrides") are highly discommended (they are primarily implemented 
for simulating user input and when implementing certain types of joystick control).
"""
from dronekit import connect

# 连接到SITL
connection_string = '127.0.0.1:14551'
print 'Connecting to vehicle on: %s' % connection_string
vehicle = connect(connection_string, wait_ready=True)


# 显示全部通道的读数
print "Read channels individually:"
print " Ch1: %s" % vehicle.channels['1']
print " Ch2: %s" % vehicle.channels['2']
print " Ch3: %s" % vehicle.channels['3']
print " Ch4: %s" % vehicle.channels['4']
print " Ch5: %s" % vehicle.channels['5']
print " Ch6: %s" % vehicle.channels['6']
print " Ch7: %s" % vehicle.channels['7']
print " Ch8: %s" % vehicle.channels['8']
print "Number of channels: %s" % len(vehicle.channels)


# 开始遥控信道覆盖
print "\nChannel overrides: %s" % vehicle.channels.overrides

# 将通道2的值设置为200(键值访问)
print "Set Ch2 override to 200 (indexing syntax)"
vehicle.channels.overrides['2'] = 200
print " Channel overrides: %s" % vehicle.channels.overrides
print " Ch2 override: %s" % vehicle.channels.overrides['2']

# 将通道3的值设置为300(字典访问,同时会清除通道2的设置)
print "Set Ch3 override to 300 (dictionary syntax)"
vehicle.channels.overrides = {'3':300}
print " Channel overrides: %s" % vehicle.channels.overrides

# 分别设置通道1-8为110-810(字典访问)
print "Set Ch1-Ch8 overrides to 110-810 respectively"
vehicle.channels.overrides = {'1': 110, '2': 210,'3': 310,'4':4100, '5':510,'6':610,'7':710,'8':810}
print " Channel overrides: %s" % vehicle.channels.overrides 


# 清除2通道的信道覆盖(通过键值将相应元素赋值None)
print "\nCancel Ch2 override (indexing syntax)"
vehicle.channels.overrides['2'] = None
print " Channel overrides: %s" % vehicle.channels.overrides 

# 清除3通道的信道覆盖(使用del语句)
print "Clear Ch3 override (del syntax)"
del vehicle.channels.overrides['3']
print " Channel overrides: %s" % vehicle.channels.overrides 

# 清除全部的信道覆盖(设置空字典)
print "Clear all overrides"
vehicle.channels.overrides = {}
print " Channel overrides: %s" % vehicle.channels.overrides 


# 关闭vehicle对象
print "\nClose vehicle object"
vehicle.close()

print("Completed")

读取和设置信道

  • 使用vehicle.channels读取信道
  • vehicle.channels将返回一个字典元素,键值为通道的序数
# 获取当前通道2的输入值
vehicle.channels['2']

# 当前所有通道的输入值
vehicle.channels
  • 使用vehicle.channels.overrides覆盖信道
  • vehicle.channels.overrides与vehicle.channels类似,也是一个字典。
  • 它将对其中存在的元素进行信道覆盖,不存在的元素保留现有输入
# 覆盖通道2的输入值,设置为500
vehicle.channels.overrides['2'] = 500

# 清除通道2的信道覆盖
vehicle.channels.overrides['2'] = None
# 或
del vehicle.channels.overrides['2']

# 一次设置多个通道
vehicle.channels.overrides = {'1': 500, '2': 500, '3': 1000, '4': 500}

# 清除全部通道
vehicle.channels.overrides = {}
# 或
vehicle.channels.overrides = None

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: dronekit-python入门教程教程