DroneKit教程(七):遙控信道覆蓋

DroneKit教程(七):遙控信道覆蓋

MAVLink支持一項有用卻又非常危險的功能:遙控信道覆蓋(Channel Override)。遙控信道覆蓋可以將任一至全部通道的遙控輸入信號改寫爲任意值。當前,DroneKit的開發者已經建議不再使用此功能

在使用時,需要維持輸出值在上下限之間、飛控與指令發送端的連接穩定可靠、有完備的故障保護手段。遙控信道覆蓋可以用作虛擬搖桿操控、或者以最“直觀”的方式執行一些自動化任務。但是一般情況下,我們還是建議使用MAVLink指令

預先準備

根據“使用從源碼編譯的SITL測試DroneKit代碼”中的要求,運行SITL和MAVProxy:

  1. 打開Cygwin Terminal,依次輸入

    cd ~/ardupilot/ArduCopter/
    ./ArduCopter.elf --home -35,149,584,270 --model quad
  2. 新開一個cmd,運行

    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
  3. (可選)使用MissionPlanner地面站監控無人機的狀態。運行MissionPlanner地面站,右上角選擇UDP,點擊connect連接。端口填寫14550

在測試過程中,請保持SITL和MAVProxy運行。

編程示例

本示例改寫自DroneKit的官方示例

#!/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.overridesvehicle.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

版本信息

1.0 20170916 initial commit

知識共享許可協議
本作品採用知識共享署名-相同方式共享 3.0 未本地化版本許可協議進行許可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章