【类 型】:feat

【原  因】:飞控mavlink发送 ADSB数据
【过  程】:
【影  响】:

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
air 2025-06-19 22:18:07 +08:00
parent 43dfbc91b2
commit 212a9e5da1
2 changed files with 308 additions and 247 deletions

View File

@ -5,7 +5,7 @@
//char* password = "63587839ab"; //wifi密码 //char* password = "63587839ab"; //wifi密码
char* ssid = "flicube"; //wifi帐号 char* ssid = "flicube"; //wifi帐号
char* password = "fxmf0622"; //wifi密码 char* password = "fxmf0622"; //wifi密码
char* mqttServer = "szdot.top"; //mqtt地址 char* mqttServer = "wxsky.com"; //mqtt地址
int mqttPort = 1883; //mqtt端口 int mqttPort = 1883; //mqtt端口
char* mqttName = "admin"; //mqtt帐号 char* mqttName = "admin"; //mqtt帐号
char* mqttPassword = "123456"; //mqtt密码 char* mqttPassword = "123456"; //mqtt密码

View File

@ -1,118 +1,136 @@
#include "FoodDeliveryBase.h" #include "FoodDeliveryBase.h"
/** /**
* @description: * @description:
*/ */
FoodCube::FoodCube(char* userSsid, char* userPassword, char* userMqttServer, int userMqttPort, char* userMqttName, char* userMqttPassword, uint8_t userMavlinkSerial, uint8_t userVoiceSerial, char* userUdpServerIP, uint32_t userUdpServerPort) { FoodCube::FoodCube(char *userSsid, char *userPassword, char *userMqttServer, int userMqttPort, char *userMqttName, char *userMqttPassword, uint8_t userMavlinkSerial, uint8_t userVoiceSerial, char *userUdpServerIP, uint32_t userUdpServerPort)
{
/*初始化*/ /*初始化*/
ssid = userSsid; //wifi帐号 ssid = userSsid; // wifi帐号
password = userPassword; //wifi密码 password = userPassword; // wifi密码
mqttServer = userMqttServer; //mqtt服务器地址 mqttServer = userMqttServer; // mqtt服务器地址
mqttPort = userMqttPort; //mqtt服务器端口 mqttPort = userMqttPort; // mqtt服务器端口
mqttName = userMqttName; //mqtt帐号 mqttName = userMqttName; // mqtt帐号
mqttPassword = userMqttPassword; //mqtt密码 mqttPassword = userMqttPassword; // mqtt密码
mavlinkSerial = userMavlinkSerial; //mavlink用的串口 mavlinkSerial = userMavlinkSerial; // mavlink用的串口
voiceSerial = userVoiceSerial; //声音模块用的串口 voiceSerial = userVoiceSerial; // 声音模块用的串口
udpServerIP = userUdpServerIP; //云台相机ip udpServerIP = userUdpServerIP; // 云台相机ip
udpServerPort = userUdpServerPort; //云台相机端口 udpServerPort = userUdpServerPort; // 云台相机端口
//初始化飞控通讯串口 波特率 // 初始化飞控通讯串口 波特率
switch (mavlinkSerial) { //初始化指定 串口号 switch (mavlinkSerial)
case 0: { // 初始化指定 串口号
Serial.begin(57600); case 0:
break; Serial.begin(57600);
case 1: break;
Serial1.begin(57600); case 1:
break; Serial1.begin(57600);
case 2: break;
Serial2.begin(57600); case 2:
break; Serial2.begin(57600);
break;
} }
//初始化声音模块串口 波特率 // 初始化声音模块串口 波特率
switch (voiceSerial) { //初始化指定 串口号 switch (voiceSerial)
case 0: { // 初始化指定 串口号
Serial.begin(115200); case 0:
break; Serial.begin(115200);
case 1: break;
Serial1.begin(115200); case 1:
break; Serial1.begin(115200);
case 2: break;
Serial2.begin(115200); case 2:
break; Serial2.begin(115200);
break;
} }
/*初始化 连接mqtt对象 赋予mqttClient指针*/ /*初始化 连接mqtt对象 赋予mqttClient指针*/
mqttClient = new PubSubClient(mqttServer, mqttPort, wifiClient); mqttClient = new PubSubClient(mqttServer, mqttPort, wifiClient);
} }
/** /**
* @description: * @description:
* @param {*} val * @param {*} val
*/ */
void FoodCube::log(String val) { void FoodCube::log(String val)
{
Serial.print(val); Serial.print(val);
} }
void FoodCube::log(char* val) { void FoodCube::log(char *val)
{
Serial.print(val); Serial.print(val);
} }
void FoodCube::log(int val) { void FoodCube::log(int val)
{
Serial.print(val); Serial.print(val);
} }
void FoodCube::log(IPAddress val) { void FoodCube::log(IPAddress val)
{
Serial.print(val); Serial.print(val);
} }
void FoodCube::log(bool val) { void FoodCube::log(bool val)
{
Serial.print(val); Serial.print(val);
} }
void FoodCube::logln(String val) { void FoodCube::logln(String val)
{
Serial.println(val); Serial.println(val);
} }
void FoodCube::logln(char* val) { void FoodCube::logln(char *val)
{
Serial.println(val); Serial.println(val);
} }
void FoodCube::logln(int val) { void FoodCube::logln(int val)
{
Serial.println(val); Serial.println(val);
} }
void FoodCube::logln(IPAddress val) { void FoodCube::logln(IPAddress val)
{
Serial.println(val); Serial.println(val);
} }
void FoodCube::logln(bool val) { void FoodCube::logln(bool val)
{
Serial.print(val); Serial.print(val);
} }
/** /**
*@description: *@description:
*/ */
bool FoodCube::getIsInit() { bool FoodCube::getIsInit()
{
return isInit; return isInit;
} }
void FoodCube::setIsInit(bool b) { void FoodCube::setIsInit(bool b)
{
isInit = b; isInit = b;
} }
String FoodCube::getMacAdd() { String FoodCube::getMacAdd()
{
return macAdd; return macAdd;
} }
/** /**
* @description: wifi * @description: wifi
*/ */
void FoodCube::connectWifi() { void FoodCube::connectWifi()
//设置wifi帐号密码 {
// 设置wifi帐号密码
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
//连接wifi // 连接wifi
logln("Connecting Wifi..."); logln("Connecting Wifi...");
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED)
{
log("."); log(".");
delay(150); delay(150);
} }
//获取局域网ip // 获取局域网ip
logln(""); logln("");
logln("WiFi connected"); logln("WiFi connected");
log("IP address: "); log("IP address: ");
logln(WiFi.localIP()); logln(WiFi.localIP());
localIp = WiFi.localIP(); localIp = WiFi.localIP();
//设置开发板为无线终端 获取物理mac地址 // 设置开发板为无线终端 获取物理mac地址
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
macAdd = WiFi.macAddress(); macAdd = WiFi.macAddress();
macAdd.replace(":", ""); //板子的物理地址 并且去除冒号 macAdd.replace(":", ""); // 板子的物理地址 并且去除冒号
log("macAdd: "); log("macAdd: ");
logln(macAdd); logln(macAdd);
playText("歪佛哎,已连接"); playText("歪佛哎,已连接");
@ -120,23 +138,27 @@ void FoodCube::connectWifi() {
} }
/** /**
* @description: mqtt * @description: mqtt
* @param {String} topicSub * @param {String} topicSub
*/ */
void FoodCube::connectMqtt(String topicSub) { void FoodCube::connectMqtt(String topicSub)
{
/*尝试连接mqtt*/ /*尝试连接mqtt*/
if (mqttClient->connect(macAdd.c_str(), mqttName, mqttPassword)) { if (mqttClient->connect(macAdd.c_str(), mqttName, mqttPassword))
{
logln("MQTT Server Connected."); logln("MQTT Server Connected.");
log("Server Address: "); log("Server Address: ");
logln(mqttServer); logln(mqttServer);
log("ClientId :"); log("ClientId :");
logln(macAdd); logln(macAdd);
/*连接成功 订阅主题*/ /*连接成功 订阅主题*/
//订阅主题 PS:连接上mqtt服务器 订阅主题 收到信息触发回调函数 receiveCallback // 订阅主题 PS:连接上mqtt服务器 订阅主题 收到信息触发回调函数 receiveCallback
subscribeTopic(topicSub, 1); subscribeTopic(topicSub, 1);
playText("服务器,已连接"); playText("服务器,已连接");
} else { }
//失败返回状态码 else
{
// 失败返回状态码
log("MQTT Server Connect Failed. Client State:"); log("MQTT Server Connect Failed. Client State:");
logln(mqttClient->state()); logln(mqttClient->state());
delay(3000); delay(3000);
@ -144,87 +166,95 @@ void FoodCube::connectMqtt(String topicSub) {
} }
/** /**
* @description: loop函数里 mqtt连接情况 * @description: loop函数里 mqtt连接情况
* @param {String} topicSub * @param {String} topicSub
*/ */
void FoodCube::mqttLoop(String topicSub) { void FoodCube::mqttLoop(String topicSub)
if (mqttClient->connected()) { //检测 如果开发板成功连接服务器 {
mqttClient->loop(); // 保持心跳 if (mqttClient->connected())
} else { // 如果开发板未能成功连接服务器 { // 检测 如果开发板成功连接服务器
connectMqtt(topicSub); // 则尝试连接服务器 mqttClient->loop(); // 保持心跳
}
else
{ // 如果开发板未能成功连接服务器
connectMqtt(topicSub); // 则尝试连接服务器
} }
} }
/** /**
* @description: * @description:
* @param {String} topicString * @param {String} topicString
* @param {int} Qos 1 ps: * @param {int} Qos 1 ps:
*/ */
void FoodCube::subscribeTopic(String topicString, int Qos = 1) { void FoodCube::subscribeTopic(String topicString, int Qos = 1)
//处理主题字符串 {
// 处理主题字符串
topicString = topicString + "/" + macAdd; topicString = topicString + "/" + macAdd;
char subTopic[topicString.length() + 1]; char subTopic[topicString.length() + 1];
strcpy(subTopic, topicString.c_str()); strcpy(subTopic, topicString.c_str());
//订阅主题 // 订阅主题
mqttClient->subscribe(subTopic, Qos); mqttClient->subscribe(subTopic, Qos);
} }
/** /**
* @description: * @description:
* @param {String} topicString * @param {String} topicString
* @param {String} messageString * @param {String} messageString
*/ */
void FoodCube::pubMQTTmsg(String topicString, String messageString) { void FoodCube::pubMQTTmsg(String topicString, String messageString)
//处理主题字符串 {
// 处理主题字符串
topicString = topicString + "/" + macAdd; topicString = topicString + "/" + macAdd;
char publishTopic[topicString.length() + 1]; char publishTopic[topicString.length() + 1];
strcpy(publishTopic, topicString.c_str()); strcpy(publishTopic, topicString.c_str());
//处理发布内容字符串 // 处理发布内容字符串
char publishMsg[messageString.length() + 1]; char publishMsg[messageString.length() + 1];
strcpy(publishMsg, messageString.c_str()); strcpy(publishMsg, messageString.c_str());
//向指定主题 发布信息 // 向指定主题 发布信息
mqttClient->publish(publishTopic, publishMsg); mqttClient->publish(publishTopic, publishMsg);
} }
/** /**
* @description: * @description:
* @param {uint8_t[]} buf[] * @param {uint8_t[]} buf[]
* @param {int} len * @param {int} len
* @param {uint8_t} swSerial * @param {uint8_t} swSerial
*/ */
void FoodCube::SWrite(uint8_t buf[], int len, uint8_t swSerial) { void FoodCube::SWrite(uint8_t buf[], int len, uint8_t swSerial)
//通过串口发送 {
switch (swSerial) { //初始化指定 串口号 // 通过串口发送
case 0: switch (swSerial)
Serial.write(buf, len); { // 初始化指定 串口号
break; case 0:
case 1: Serial.write(buf, len);
Serial1.write(buf, len); break;
break; case 1:
case 2: Serial1.write(buf, len);
Serial2.write(buf, len); break;
break; case 2:
Serial2.write(buf, len);
break;
} }
} }
/** /**
* @description: * @description:
* @param {String} str * @param {String} str
*/ */
void FoodCube::playText(String str) { void FoodCube::playText(String str)
{
/*消息长度*/ /*消息长度*/
int len = str.length(); //修改信息长度 消息长度 int len = str.length(); // 修改信息长度 消息长度
// if (len >= 3996) { //限制信息长度 超长的不播放 // if (len >= 3996) { //限制信息长度 超长的不播放
// return; // return;
// } // }
/*长度高低位*/ /*长度高低位*/
int frameLength = len + 2; // 5 bytes for header and length bytes int frameLength = len + 2; // 5 bytes for header and length bytes
byte highByte = (frameLength >> 8) & 0xFF; // extract high byte of length byte highByte = (frameLength >> 8) & 0xFF; // extract high byte of length
byte lowByte = frameLength & 0xFF; // extract low byte of length byte lowByte = frameLength & 0xFF; // extract low byte of length
/*帧命令头*/ /*帧命令头*/
uint8_t command[len + 5]; uint8_t command[len + 5];
//已知 帧头0xFD z信息长度高位 x信息长度低位 0x01命令字1是开始命令 0x04utf8编码 先给值 // 已知 帧头0xFD z信息长度高位 x信息长度低位 0x01命令字1是开始命令 0x04utf8编码 先给值
int index = 0; int index = 0;
command[index++] = 0xFD; command[index++] = 0xFD;
command[index++] = highByte; command[index++] = highByte;
@ -232,71 +262,81 @@ void FoodCube::playText(String str) {
command[index++] = 0x01; command[index++] = 0x01;
command[index++] = 0x04; command[index++] = 0x04;
/*帧命令 消息段*/ /*帧命令 消息段*/
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++)
{
command[index++] = (int)str[i]; command[index++] = (int)str[i];
} }
for (int i = 0; i < sizeof(command); i++) { for (int i = 0; i < sizeof(command); i++)
{
log(command[i]); log(command[i]);
log(" "); log(" ");
} }
logln(""); logln("");
//串口发送 播放声音 // 串口发送 播放声音
SWrite(command, sizeof(command), voiceSerial); SWrite(command, sizeof(command), voiceSerial);
} }
/** /**
* @description: * @description:
* @return {uint8_t} 0x4E 78: 0x4F 79: * @return {uint8_t} 0x4E 78: 0x4F 79:
*/ */
uint8_t FoodCube::chekVoiceMcu() { uint8_t FoodCube::chekVoiceMcu()
{
uint8_t serialData; uint8_t serialData;
uint8_t check[] = { 0xFD, 0x00, 0x01, 0x21 }; //检查命令帧 uint8_t check[] = {0xFD, 0x00, 0x01, 0x21}; // 检查命令帧
SWrite(check, sizeof(check), voiceSerial); //发送检查指令 SWrite(check, sizeof(check), voiceSerial); // 发送检查指令
switch (voiceSerial) { switch (voiceSerial)
case 0: {
{ case 0:
while (Serial.available()) { // 当串口接收到信息后 {
serialData = Serial.read(); // 将接收到的信息使用read读取 while (Serial.available())
} { // 当串口接收到信息后
} serialData = Serial.read(); // 将接收到的信息使用read读取
break; }
case 1: }
{ break;
while (Serial1.available()) { // 当串口接收到信息后 case 1:
serialData = Serial1.read(); // 将接收到的信息使用read读取 {
} while (Serial1.available())
} { // 当串口接收到信息后
break; serialData = Serial1.read(); // 将接收到的信息使用read读取
case 2: }
{ }
while (Serial2.available()) { // 当串口接收到信息后 break;
serialData = Serial2.read(); // 将接收到的信息使用read读取 case 2:
} {
} while (Serial2.available())
break; { // 当串口接收到信息后
serialData = Serial2.read(); // 将接收到的信息使用read读取
}
}
break;
} }
return serialData; return serialData;
} }
/** /**
* @description: * @description:
*/ */
void FoodCube::stopVoice() { void FoodCube::stopVoice()
uint8_t stop[] = { 0xFD, 0x00, 0x01, 0x22 }; //停止合成命令帧 {
SWrite(stop, sizeof(stop), voiceSerial); //发送检查指令 uint8_t stop[] = {0xFD, 0x00, 0x01, 0x22}; // 停止合成命令帧
SWrite(stop, sizeof(stop), voiceSerial); // 发送检查指令
} }
/** /**
* @description: * @description:
* @param {uint8_t[]} * @param {uint8_t[]}
* @param {int} len * @param {int} len
*/ */
uint16_t FoodCube::CRC16_cal(uint8_t* ptr, uint32_t len, uint16_t crc_init) { uint16_t FoodCube::CRC16_cal(uint8_t *ptr, uint32_t len, uint16_t crc_init)
{
uint16_t crc, oldcrc16; uint16_t crc, oldcrc16;
uint8_t temp; uint8_t temp;
crc = crc_init; crc = crc_init;
while (len-- != 0) { while (len-- != 0)
{
temp = (crc >> 8) & 0xff; temp = (crc >> 8) & 0xff;
oldcrc16 = crc16_tab[*ptr ^ temp]; oldcrc16 = crc16_tab[*ptr ^ temp];
crc = (crc << 8) ^ oldcrc16; crc = (crc << 8) ^ oldcrc16;
@ -305,58 +345,64 @@ uint16_t FoodCube::CRC16_cal(uint8_t* ptr, uint32_t len, uint16_t crc_init) {
return (crc); return (crc);
} }
void FoodCube::crc_check_16bites(uint8_t* pbuf, uint32_t len, uint16_t* p_result) { void FoodCube::crc_check_16bites(uint8_t *pbuf, uint32_t len, uint16_t *p_result)
{
uint16_t crc_result = 0; uint16_t crc_result = 0;
crc_result = CRC16_cal(pbuf, len, 0); crc_result = CRC16_cal(pbuf, len, 0);
*p_result = crc_result; *p_result = crc_result;
} }
/** /**
* @description: * @description:
* @param {uint8_t[]} * @param {uint8_t[]}
* @param {int} len * @param {int} len
*/ */
void FoodCube::udpSendToCamera(uint8_t* p_command, uint32_t len) { void FoodCube::udpSendToCamera(uint8_t *p_command, uint32_t len)
{
uint16_t result = 0; uint16_t result = 0;
uint16_t* p_result = &result; uint16_t *p_result = &result;
//计算校检码 // 计算校检码
crc_check_16bites(p_command, len, p_result); crc_check_16bites(p_command, len, p_result);
//加上校检码 // 加上校检码
uint8_t bytes[2 + len]; uint8_t bytes[2 + len];
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++)
{
bytes[i] = p_command[i]; bytes[i] = p_command[i];
} }
bytes[len] = static_cast<uint8_t>(result); // 低位 互换 bytes[len] = static_cast<uint8_t>(result); // 低位 互换
bytes[len + 1] = static_cast<uint8_t>(result >> 8); // 高位 互换 bytes[len + 1] = static_cast<uint8_t>(result >> 8); // 高位 互换
//udp发送命令帧 // udp发送命令帧
udp.beginPacket(udpServerIP, udpServerPort); udp.beginPacket(udpServerIP, udpServerPort);
udp.write(bytes, len + 2); udp.write(bytes, len + 2);
udp.endPacket(); udp.endPacket();
} }
/** /**
* @description: 1 or 0 * @description: 1 or 0
* @param {String} str * @param {String} str
* @param {uint8_t} n 0 * @param {uint8_t} n 0
* @param {uint8_t} isOne 1:1 0:0 * @param {uint8_t} isOne 1:1 0:0
*/ */
String FoodCube::setNBit(String str, uint8_t n, uint8_t i) { String FoodCube::setNBit(String str, uint8_t n, uint8_t i)
{
char buf[10]; char buf[10];
uint16_t val = (uint8_t)str.toInt(); uint16_t val = (uint8_t)str.toInt();
if (i) { if (i)
val |= (1u << n); //按位设置成1 {
} else { val |= (1u << n); // 按位设置成1
val &= ~(1u << n); //按位设置成0 }
else
{
val &= ~(1u << n); // 按位设置成0
} }
sprintf(buf, "%d", val); sprintf(buf, "%d", val);
return buf; return buf;
} }
/** /**
* @description: * @description:
*/ */
void FoodCube::mav_request_data() { void FoodCube::mav_request_data()
{
mavlink_message_t msg; mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// 以下注释 把数据流组合到一起 // 以下注释 把数据流组合到一起
@ -373,10 +419,11 @@ void FoodCube::mav_request_data() {
* MAV_DATA_STREAM_ENUM_END=13, * MAV_DATA_STREAM_ENUM_END=13,
*/ */
// 根据从Pixhawk请求的所需信息进行设置 // 根据从Pixhawk请求的所需信息进行设置
const int maxStreams = 1; // 遍历次数 (下面组合的长度) const int maxStreams = 1; // 遍历次数 (下面组合的长度)
const uint8_t MAVStreams[maxStreams] = { MAV_DATA_STREAM_ALL }; // 请求的数据流组合 放到一个对象 后面进行遍历 const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_ALL}; // 请求的数据流组合 放到一个对象 后面进行遍历
const uint16_t MAVRates[maxStreams] = { 0x01 }; // 设定发送频率 分别对应上面数据流 ps:0X01 1赫兹 既每秒发送一次 const uint16_t MAVRates[maxStreams] = {0x01}; // 设定发送频率 分别对应上面数据流 ps:0X01 1赫兹 既每秒发送一次
for (int i = 0; i < maxStreams; i++) { for (int i = 0; i < maxStreams; i++)
{
// 向飞控发送请求 // 向飞控发送请求
mavlink_msg_request_data_stream_pack(2, 200, &msg, 1, 0, MAVStreams[i], MAVRates[i], 1); mavlink_msg_request_data_stream_pack(2, 200, &msg, 1, 0, MAVStreams[i], MAVRates[i], 1);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
@ -388,26 +435,31 @@ void FoodCube::mav_request_data() {
* @description: mavlink数据流 * @description: mavlink数据流
* @param {pFun} pFun * @param {pFun} pFun
*/ */
void FoodCube::comm_receive(void (*pFun)(uint8_t)) { void FoodCube::comm_receive(void (*pFun)(uint8_t))
switch (mavlinkSerial) { {
case 0: switch (mavlinkSerial)
while (Serial.available() > 0) { // 判断串口缓存 是否有数据 {
uint8_t c = Serial.read(); // 从缓存拿到数据 case 0:
pFun(c); while (Serial.available() > 0)
} { // 判断串口缓存 是否有数据
break; uint8_t c = Serial.read(); // 从缓存拿到数据
case 1: pFun(c);
while (Serial1.available() > 0) { // 判断串口缓存 是否有数据 }
uint8_t c = Serial1.read(); // 从缓存拿到数据 break;
pFun(c); case 1:
} while (Serial1.available() > 0)
break; { // 判断串口缓存 是否有数据
case 2: uint8_t c = Serial1.read(); // 从缓存拿到数据
while (Serial2.available() > 0) { // 判断串口缓存 是否有数据 pFun(c);
uint8_t c = Serial2.read(); // 从缓存拿到数据 }
pFun(c); break;
} case 2:
break; while (Serial2.available() > 0)
{ // 判断串口缓存 是否有数据
uint8_t c = Serial2.read(); // 从缓存拿到数据
pFun(c);
}
break;
} }
} }
@ -415,13 +467,14 @@ void FoodCube::comm_receive(void (*pFun)(uint8_t)) {
* @description: * @description:
* @param {uint8_t} taskcount * @param {uint8_t} taskcount
*/ */
void FoodCube::mav_mission_count(uint8_t taskcount) { void FoodCube::mav_mission_count(uint8_t taskcount)
mavlink_message_t msg; //mavlink协议信息(msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; //发送的缓存 mavlink_message_t msg; // mavlink协议信息(msg)
//设置飞行模式的数据包 uint8_t buf[MAVLINK_MAX_PACKET_LEN]; // 发送的缓存
// 设置飞行模式的数据包
mavlink_msg_mission_count_pack(0xFF, 0xBE, &msg, 1, 0, taskcount); mavlink_msg_mission_count_pack(0xFF, 0xBE, &msg, 1, 0, taskcount);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
//通过串口发送 // 通过串口发送
SWrite(buf, len, mavlinkSerial); SWrite(buf, len, mavlinkSerial);
} }
@ -440,26 +493,28 @@ void FoodCube::mav_mission_count(uint8_t taskcount) {
* @param {double} y * @param {double} y
* @param {double} z * @param {double} z
*/ */
void FoodCube::mav_mission_item(int8_t seq, uint8_t frame, uint8_t command, uint8_t current, uint8_t autocontinue, double param1, double param2, double param3, double param4, double x, double y, double z) { void FoodCube::mav_mission_item(int8_t seq, uint8_t frame, uint8_t command, uint8_t current, uint8_t autocontinue, double param1, double param2, double param3, double param4, double x, double y, double z)
mavlink_message_t msg; //mavlink协议信息(msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; //发送的缓存 mavlink_message_t msg; // mavlink协议信息(msg)
//写入航点数据包 uint8_t buf[MAVLINK_MAX_PACKET_LEN]; // 发送的缓存
// 写入航点数据包
mavlink_msg_mission_item_pack(0xFF, 0xBE, &msg, 1, 0, seq, frame, command, current, autocontinue, param1, param2, param3, param4, x, y, z); mavlink_msg_mission_item_pack(0xFF, 0xBE, &msg, 1, 0, seq, frame, command, current, autocontinue, param1, param2, param3, param4, x, y, z);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
//通过串口发送 // 通过串口发送
SWrite(buf, len, mavlinkSerial); SWrite(buf, len, mavlinkSerial);
} }
/** /**
* @description: * @description:
*/ */
void FoodCube::mav_set_mode(uint8_t SysState) { void FoodCube::mav_set_mode(uint8_t SysState)
mavlink_message_t msg; //mavlink协议信息(msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; //发送的缓存 mavlink_message_t msg; // mavlink协议信息(msg)
//设置飞行模式的数据包 uint8_t buf[MAVLINK_MAX_PACKET_LEN]; // 发送的缓存
// 设置飞行模式的数据包
mavlink_msg_set_mode_pack(0xFF, 0xBE, &msg, 1, 209, SysState); mavlink_msg_set_mode_pack(0xFF, 0xBE, &msg, 1, 209, SysState);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
//通过串口发送 // 通过串口发送
SWrite(buf, len, mavlinkSerial); SWrite(buf, len, mavlinkSerial);
} }
@ -468,20 +523,23 @@ void FoodCube::mav_set_mode(uint8_t SysState) {
* @param {uint8_t} controlType 3: * @param {uint8_t} controlType 3:
* @param {uint16_t} param[] * @param {uint16_t} param[]
*/ */
void FoodCube::mav_command(uint8_t controlType, uint16_t param[]) { void FoodCube::mav_command(uint8_t controlType, uint16_t param[])
mavlink_message_t msg; //mavlink协议信息(msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; //发送的缓存 mavlink_message_t msg; // mavlink协议信息(msg)
//设置飞行模式的数据包 uint8_t buf[MAVLINK_MAX_PACKET_LEN]; // 发送的缓存
// 设置飞行模式的数据包
mavlink_command_long_t cmd; mavlink_command_long_t cmd;
cmd.target_system = 1; cmd.target_system = 1;
cmd.target_component = 1; cmd.target_component = 1;
cmd.confirmation = 0; cmd.confirmation = 0;
if (controlType == 3) { //飞机加解锁 if (controlType == 3)
{ // 飞机加解锁
cmd.command = MAV_CMD_COMPONENT_ARM_DISARM; cmd.command = MAV_CMD_COMPONENT_ARM_DISARM;
cmd.param1 = param[0]; // 0:加锁 1解锁 cmd.param1 = param[0]; // 0:加锁 1解锁
cmd.param2 = param[1]; //0:符合条件加解锁 21196:强制加解锁 cmd.param2 = param[1]; // 0:符合条件加解锁 21196:强制加解锁
} }
if (controlType == 6) { //测试起飞 if (controlType == 6)
{ // 测试起飞
float p = (float)param[0]; float p = (float)param[0];
cmd.command = MAV_CMD_NAV_TAKEOFF; cmd.command = MAV_CMD_NAV_TAKEOFF;
cmd.param1 = 0; cmd.param1 = 0;
@ -490,12 +548,14 @@ void FoodCube::mav_command(uint8_t controlType, uint16_t param[]) {
cmd.param4 = 0; cmd.param4 = 0;
cmd.param5 = 0; cmd.param5 = 0;
cmd.param6 = 0; cmd.param6 = 0;
cmd.param7 = p; //起飞高度 cmd.param7 = p; // 起飞高度
} }
if (controlType == 8) { //降落 if (controlType == 8)
{ // 降落
cmd.command = MAV_CMD_NAV_LAND; cmd.command = MAV_CMD_NAV_LAND;
} }
if (controlType == 11) { //磁罗盘校准 if (controlType == 11)
{ // 磁罗盘校准
cmd.command = MAV_CMD_DO_START_MAG_CAL; cmd.command = MAV_CMD_DO_START_MAG_CAL;
cmd.param1 = 0; cmd.param1 = 0;
cmd.param2 = 1; cmd.param2 = 1;
@ -503,19 +563,20 @@ void FoodCube::mav_command(uint8_t controlType, uint16_t param[]) {
} }
mavlink_msg_command_long_encode(0xFF, 0xBE, &msg, &cmd); mavlink_msg_command_long_encode(0xFF, 0xBE, &msg, &cmd);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
//通过串口发送 // 通过串口发送
SWrite(buf, len, mavlinkSerial); SWrite(buf, len, mavlinkSerial);
} }
/** /**
* @description: * @description:
* @param {uint16_t} chan[] * @param {uint16_t} chan[]
*/ */
void FoodCube::mav_channels_override(uint16_t chan[]) { void FoodCube::mav_channels_override(uint16_t chan[])
mavlink_message_t msg; //mavlink协议信息(msg) {
uint8_t buf[MAVLINK_MAX_PACKET_LEN]; //发送的缓存 mavlink_message_t msg; // mavlink协议信息(msg)
//控制油门 uint8_t buf[MAVLINK_MAX_PACKET_LEN]; // 发送的缓存
// 控制油门
mavlink_msg_rc_channels_override_pack(0xFF, 0xBE, &msg, 1, 1, chan[0], chan[1], chan[2], chan[3], 0xffff, 0xffff, 0xffff, 0xffff); mavlink_msg_rc_channels_override_pack(0xFF, 0xBE, &msg, 1, 1, chan[0], chan[1], chan[2], chan[3], 0xffff, 0xffff, 0xffff, 0xffff);
int len = mavlink_msg_to_send_buffer(buf, &msg); int len = mavlink_msg_to_send_buffer(buf, &msg);
//通过串口发送 // 通过串口发送
SWrite(buf, len, mavlinkSerial); SWrite(buf, len, mavlinkSerial);
} }