/****************************************************************************
*
* Copyright (c) 2014 MAVlink Development Team. All rights reserved.
* Author: Trent Lukaczyk, <[email protected]>
* Jaycee Lock, <[email protected]>
* Lorenz Meier, <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file autopilot_interface.cpp
*
* @brief Autopilot interface functions
*
* Functions for sending and recieving commands to an autopilot via MAVlink
*
* @author Trent Lukaczyk, <[email protected]>
* @author Jaycee Lock, <[email protected]>
* @author Lorenz Meier, <[email protected]>
*
*/
// ------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------
#include <iostream>
#include "autopilot_interface.h"
// ----------------------------------------------------------------------------------
// Time
// ------------------- ---------------------------------------------------------------
uint64_t
get_time_usec()
{
static struct timeval _time_stamp;
gettimeofday(&_time_stamp, NULL);
return _time_stamp.tv_sec*1000000 + _time_stamp.tv_usec;
}
// ----------------------------------------------------------------------------------
// Setpoint Helper Functions
// ----------------------------------------------------------------------------------
// choose one of the next three
/*
* Set target local ned position
*
* Modifies a mavlink_set_position_target_local_ned_t struct with target XYZ locations
* in the Local NED frame, in meters.
*/
void
set_position(float x, float y, float z, mavlink_set_position_target_local_ned_t &sp)
{
sp.type_mask =
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_POSITION;
sp.coordinate_frame = MAV_FRAME_LOCAL_NED;
sp.x = x;
sp.y = y;
sp.z = z;
std::cout<< sp.type_mask << std::endl;
printf("POSITION SETPOINT XYZ = [ %.4f , %.4f , %.4f ] \n", sp.x, sp.y, sp.z);
}
/*
* Set target local ned velocity
*
* Modifies a mavlink_set_position_target_local_ned_t struct with target VX VY VZ
* velocities in the Local NED frame, in meters per second.
*/
void
set_velocity(float vx, float vy, float vz, mavlink_set_position_target_local_ned_t &sp)
{
sp.type_mask =
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_VELOCITY ;
sp.coordinate_frame = MAV_FRAME_LOCAL_NED;
sp.vx = vx;
sp.vy = vy;
sp.vz = vz;
std::cout<< sp.type_mask << std::endl;
printf("VELOCITY SETPOINT UVW = [ %.4f , %.4f , %.4f ] \n", sp.vx, sp.vy, sp.vz);
}
/*
* Set target local ned acceleration
*
* Modifies a mavlink_set_position_target_local_ned_t struct with target AX AY AZ
* accelerations in the Local NED frame, in meters per second squared.
*/
void
set_acceleration(float ax, float ay, float az, mavlink_set_position_target_local_ned_t &sp)
{
// NOT IMPLEMENTED
fprintf(stderr,"set_acceleration doesn't work yet \n");
throw 1;
sp.type_mask =
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_ACCELERATION &
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_VELOCITY ;
sp.coordinate_frame = MAV_FRAME_LOCAL_NED;
sp.afx = ax;
sp.afy = ay;
sp.afz = az;
}
// the next two need to be called after one of the above
/*
* Set target local ned yaw
*
* Modifies a mavlink_set_position_target_local_ned_t struct with a target yaw
* in the Local NED frame, in radians.
*/
void
set_yaw(float yaw, mavlink_set_position_target_local_ned_t &sp)
{
sp.type_mask &=
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_YAW_ANGLE ;
sp.yaw = yaw;
printf("POSITION SETPOINT YAW = %.4f \n", sp.yaw);
}
/*
* Set target local ned yaw rate
*
* Modifies a mavlink_set_position_target_local_ned_t struct with a target yaw rate
* in the Local NED frame, in radians per second.
*/
void
set_yaw_rate(float yaw_rate, mavlink_set_position_target_local_ned_t &sp)
{
sp.type_mask &=
MAVLINK_MSG_SET_POSITION_TARGET_LOCAL_NED_YAW_RATE ;
sp.yaw_rate = yaw_rate;
}
// ----------------------------------------------------------------------------------
// Autopilot Interface Class
// ----------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Con/De structors
// ------------------------------------------------------------------------------
Autopilot_Interface::
Autopilot_Interface(Generic_Port *port_)
{
// initialize attributes
write_count = 0;
reading_status = 0; // whether the read thread is running
writing_status = 0; // whether the write thread is running
control_status = 0; // whether the autopilot is in offboard control mode
time_to_exit = false; // flag to signal thread exit
read_tid = 0; // read thread id
write_tid = 0; // write thread id
system_id = 0; // system id
autopilot_id = 0; // autopilot component id
companion_id = 0; // companion computer component id
current_messages.sysid = system_id;
current_messages.compid = autopilot_id;
port = port_; // port management object
}
Autopilot_Interface::
~Autopilot_Interface()
{}
// ------------------------------------------------------------------------------
// Update Setpoint
// ------------------------------------------------------------------------------
void
Autopilot_Interface::
update_setpoint(mavlink_set_position_target_local_ned_t setpoint)
{
std::lock_guard<std::mutex> lock(current_setpoint.mutex);
current_setpoint.data = setpoint;
}
// ------------------------------------------------------------------------------
// Read Messages
// ------------------------------------------------------------------------------
void
Autopilot_Interface::
read_messages()
{
bool success; // receive success flag
bool received_all = false; // receive only one message
Time_Stamps this_timestamps;
// Blocking wait for new data
while ( !received_all and !time_to_exit )
{
// ----------------------------------------------------------------------
// READ MESSAGE
// ----------------------------------------------------------------------
mavlink_message_t message;
success = port->read_message(message);
// ----------------------------------------------------------------------
// HANDLE MESSAGE
// ----------------------------------------------------------------------
if( success )
{
// Store message sysid and compid.
// Note this
mavlink协议,c++语言版本,用于px4飞控通信
需积分: 0 19 浏览量
更新于2025-07-14
收藏 756KB ZIP 举报
MAVLink协议是一种轻量级的消息协议,用于遥控飞行器(UAVs)的通信,广泛应用于无人机飞行控制器与地面站之间的通信。其设计目标是保持消息的简洁性与高效性,同时确保数据交换的完整性和可靠性。该协议支持多种传输方式,包括串行、TCP和UDP等。MAVLink协议通过定义一系列标准化的消息和命令,使得飞行控制器与各种类型的地面控制设备能够无障碍地进行信息交换。
C++语言版本的MAVLink协议实现,为开发者提供了直接使用C++语言进行通信接口开发的能力。这使得开发者可以不依赖于其他编程语言,直接在C++环境下进行MAVLink协议的编程实现,从而控制和获取飞行器的状态信息。使用C++实现的MAVLink协议通常会提供一套丰富的API接口,开发者可以通过这些接口方便地发送和接收MAVLink消息。
PX4飞控是一种先进的开源飞控系统,支持多旋翼、固定翼、垂直起降等各类飞行器。PX4飞控系统通常与MAVLink协议结合使用,以实现与外部设备,如地面站、遥控器、计算机等的通信。通过MAVLink协议,PX4飞控系统能够接收飞行动作指令、发送飞行状态信息,同时也支持飞行器的自动驾驶和导航功能。
在压缩包文件“c_uart_interface_mavlink-master”中,包含了一系列与C++语言版本MAVLink协议相关的源代码文件。这些文件可能涵盖了用于串行通信的UART接口实现、消息的编码和解码逻辑、消息发送和接收的管理、以及与PX4飞控系统通信的具体实现细节。开发者可以通过查看和修改这些文件,来开发符合自己需求的MAVLink通信软件模块,或者在现有的飞控系统中嵌入MAVLink通信功能。
由于MAVLink协议的轻量化特性和C++语言的强大功能,这种组合特别适合资源受限的嵌入式系统,如无人机飞控系统。开发者可以利用C++的面向对象编程特性,封装通信过程中的各种操作,实现更加稳定和高效的通信机制。同时,由于MAVLink协议的广泛使用,开发者可以方便地找到大量相关的技术文档和社区支持,以解决开发中遇到的问题。
MAVLink协议和C++语言版本的结合,为无人机开发者提供了一个稳定、高效且易用的通信解决方案。而“c_uart_interface_mavlink-master”这个压缩包文件,则是实现该通信方案的重要工具之一。通过这个文件,开发者能够快速地在C++环境中实现与PX4飞控系统的MAVLink通信,进而开发出更加智能和可靠的无人机产品。

红叶落水
- 粉丝: 3349
最新资源
- 嵌入式系统及应用-Chapter1-嵌入式系统导论.ppt
- 网络营销精英培训计划.pptx
- 最新毋岩毕业设计正文(基于单片机设计的数字电子钟).doc
- 乐活网电子商务解决方案.doc
- 数字图像处理(冈萨雷斯)第一章-绪论.ppt
- 校园网络维护记录[最终版].pdf
- 循环程序设计陈.doc
- 主流计算机图像技术.doc
- 云计算关键技术与应用技能协作训练组集训专项方案.doc
- 数据库原理与技术课程习题答案.doc
- 网络工程生产实习报告.doc
- 学习]网络科学导论度分布.ppt
- 专升本《计算机软件基础》模拟题试卷.doc
- 互联网应用与安全.ppt
- 项目管理技术在工程成本方面的综合运用.doc
- 网络游戏营销方案.pptx