모든 MAVLink 구성 요소는 주기적으로 메시지를 브로드캐스트하고 다른 시스템의 하트비트를 수신해야 합니다. 시스템은 다른 시스템에서 정기적으로 HEARTBEAT를 받는 한 자신이 다른 시스템에 연결된 것으로 간주합니다 .
메시지 는 생성된 Python 다이알렉트 파일의 HEARTBEAT메시지를 사용하여 보낼 수 있습니다 . MAVLink.heartbeat_send()메서드 정의는 다음과 같습니다.
def heartbeat_send(self, type, autopilot, base_mode, custom_mode, system_status, mavlink_version=3, force_mavlink1=False):
'''
The heartbeat message shows that a system is present and responding.
The type of the MAV and Autopilot hardware allow the
receiving system to treat further messages from this
system appropriate (e.g. by laying out the user
interface based on the autopilot).
type : Type of the MAV (quadrotor, helicopter, etc.) (type:uint8_t, values:MAV_TYPE)
autopilot : Autopilot type / class. (type:uint8_t, values:MAV_AUTOPILOT)
base_mode : System mode bitmap. (type:uint8_t, values:MAV_MODE_FLAG)
custom_mode : A bitfield for use for autopilot-specific flags (type:uint32_t)
system_status : System status flag. (type:uint8_t, values:MAV_STATE)
mavlink_version : MAVLink version, not writable by user, gets added by protocol because of magic data type: uint8_t_mavlink_version (type:uint8_t)
'''
mavutil.mavlink_connection()에서 반환하는 이라는 mavutil 링크를 사용한다고 가정하면 다음과 같이 하트비트를 보낼 수 있습니다.
# Send heartbeat from a GCS (types are define as enum in the dialect file).
the_connection.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS,
mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
# Send heartbeat from a MAVLink application.
the_connection.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_ONBOARD_CONTROLLER,
mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
하트비트를 전송해야 하는 속도는 채널에 따라 다르지만 일반적으로 1Hz입니다.
일반적으로 다른 모든 메시지와 동일한 스레드에서 보내야 합니다. 이는 스레드가 정상일 때만 하트비트가 게시되도록 하기 위한 것입니다.
메시지 서명
Pymavlink 라이브러리는 이미 서명 메시지에 대해 예상되는 거의 모든 동작을 구현합니다. 비밀 키와 초기 타임스탬프를 제공하고 선택적으로 발신 메시지의 서명 여부, 링크 ID 및 수락할 서명되지 않은 메시지(있는 경우)를 결정하기 위한 콜백을 지정하기만 하면 됩니다.
이를 수행하는 방법은 mavutil을 사용하여 연결을 관리하는지 또는 MAVLink객체를 직접 사용하는지에 따라 다릅니다.
이 항목에서는 다루지 않지만 다음에 대한 코드도 작성해야 합니다.
영구 저장소에서 키와 마지막 타임스탬프 저장 및 로드
MAVLink 클래스를 사용한 서명
MAVLink클래스를 직접 사용하는 경우 MAVLink.signing 속성을사용하여 MAVLinkSigning개체에 액세스 하고 필요한 특성을 설정할 수 있습니다.
# Create a MAVLink instance (in this case on a file object "f")
mav = mavlink.MAVLink(f)
if signing:
mav.signing.secret_key = chr(42)*32
mav.signing.link_id = 0
mav.signing.timestamp = 0
mav.signing.sign_outgoing = True
mavutil을 사용하여 서명 (MAVLINK V2)
mavutil을 사용하여 연결을 관리하는 경우 아래 표시된 방법을 사용하여 서명을 설정/비활성화할 수 있습니다.
#Setup signing
def setup_signing(self, secret_key, sign_outgoing=True, allow_unsigned_callback=None, initial_timestamp=None, link_id=None)
# Disable signing (clear secret key and all the other settings specified with setup_signing)
def disable_signing(self):
이 메서드는 연결이 소유한 개체를 setup_signing()설정 하고 몇 가지 추가 코드를 제공합니다.MAVLink
지정하지 않으면 link_id내부적으로 값이 반복됩니다.
설정되지 않은 경우 initial_timestamp현재 시간에 대한 적절한 값이 기본 OS에서 채워집니다.
allow_unsigned_callback 사용
Pymavlink는 이 목적을 위해 선택적 allow_unsigned_callback()콜백을 제공합니다. 이 함수의 프로토타입은 다음과 같습니다.
bool allow_unsigned_callback(self, msgId)
서명 구성의 일부로 설정되면 이 함수는 서명되지 않은 패킷(모든 MAVLink 1 패킷 포함) 또는 서명이 잘못된 패킷에서 호출됩니다. 함수가 False 반환하면 메시지가 삭제됩니다(그렇지 않으면 서명된 것처럼 처리됨).
서명되지 않은 패킷을 수락해야 하는 규칙은 구현에 따라 다르지만 구현 시 항상 3DR 라디오(서명을 지원하지 않음)의 피드백을 위해 RADIO_STATUS패킷을 수락하는 것이 좋습니다.
예를 들어:
# Assuming you already have a connection set up
the_connection = mavutil.mavlink_connection(...)
# Create a callback to specify the messages to accept
def my_allow_unsigned_callback(self,msgId):
#Allow radio status messages
if msgId==mavutil.mavlink.MAVLINK_MSG_ID_RADIO_STATUS:
return True
return False
# Pass the callback to the connection (here we also pass an arbitrary secret key)
secret_key = chr(42)*32
the_connection.setup_signing(secret_key, sign_outgoing=True, allow_unsigned_callback=my_allow_unsigned_callback)
Pymavlink는 를 사용할 때 (인증)을 지원합니다 .
키를 만들고 공유하는 메커니즘을 구현합니다. 자세한 내용은 참조하십시오 .
example 스크립트는 임의의 비밀 키를 사용하여 이를 수행하는 방법을 보여줍니다.
클래스는 패킷의 link_id 또는 timestamp이니셜 이 적절한 MAVLink지 확인하지 않습니다 . 초기 타임스탬프는 현재 시스템 시간을 기반으로 해야 합니다. 자세한 내용은 참조하십시오 .
및 메시지 서명 구현이 라이브러리 사용자가 서명되지 않았거나 잘못 서명된 패킷을 조건부로 수락하도록 선택할 수 있는 메커니즘을 제공해야 함을 지정합니다.