如何通过Mavlink从Pixhawk请求数据到Arduino ?



我将arduino (serial1)连接到pixhawk S(serial2)。

有人能解释我为什么我有心跳,我可以重写RC。但我不能向我的像素鹰索要数据。我的像素鹰系统是1号车,1号舱输入图片描述

#include "mavlink.h"
// Mavlink variables
int sysid = 1;                   ///< ID 20 for this airplane. 1 PX, 255 ground station
int compid = 158;                ///< The component sending the message
uint8_t target_system = 1;
uint8_t target_component = 1;
int type = MAV_TYPE_GROUND_ROVER;   ///< This system is an airplane / fixed wing
uint8_t autopilot_type = MAV_AUTOPILOT_PIXHAWK;
uint8_t system_mode = MAV_MODE_PREFLIGHT; ///< Booting up
uint32_t custom_mode = 0;                 ///< Custom mode, can be defined by user/adopter
uint8_t system_state = MAV_STATE_STANDBY; ///< System ready for flight
unsigned long request_timer = millis();
unsigned long heartbeat_timer = millis();
void setup() {
// MAVLink interface start
Serial1.begin(57600);
Serial.begin(115200);
}
void loop() {
doSendHeartbeat();
dorequest();
comm_receive() ;
rc_override(1200, 1200);
}
void doSendHeartbeat() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
if (heartbeat_timer < millis()) {
mavlink_msg_heartbeat_pack(sysid, compid, &msg, type, autopilot_type, system_mode, custom_mode, system_state);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
heartbeat_timer = millis() + 100;
}
}
void dorequest() {
if (request_timer < millis()) {
Mav_Request_Data();
request_timer = millis() + 5000;
}
}
void Mav_Request_Data()
{
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_request_data_stream_pack(target_system, target_component, &msg, sysid, compid, MAV_DATA_STREAM_ALL, 0x01, 1);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
}

void comm_receive() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_status_t status;
while (Serial1.available() > 0) {
uint8_t c = Serial1.read();
// Try to get a new message
if (mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {
// Handle message
Serial.println(msg.msgid);
switch (msg.msgid) {
case MAVLINK_MSG_ID_HEARTBEAT:  // #0: Heartbeat
{
// E.g. read GCS heartbeat and go into
// comm lost mode if timer times out
}
break;
case MAVLINK_MSG_ID_SYS_STATUS:  // #1: SYS_STATUS
{
/* Message decoding: PRIMITIVE
mavlink_msg_sys_status_decode(const mavlink_message_t* msg, mavlink_sys_status_t* sys_status)
*/
//mavlink_message_t* msg;
mavlink_sys_status_t sys_status;
mavlink_msg_sys_status_decode(&msg, &sys_status);
Serial.println("2-");
}
break;
case MAVLINK_MSG_ID_PARAM_VALUE:  // #22: PARAM_VALUE
{
/* Message decoding: PRIMITIVE
mavlink_msg_param_value_decode(const mavlink_message_t* msg, mavlink_param_value_t* param_value)
*/
//mavlink_message_t* msg;
mavlink_param_value_t param_value;
mavlink_msg_param_value_decode(&msg, &param_value);
Serial.println("3-");
}
break;
case MAVLINK_MSG_ID_RAW_IMU:  // #27: RAW_IMU
{
/* Message decoding: PRIMITIVE
static inline void mavlink_msg_raw_imu_decode(const mavlink_message_t* msg, mavlink_raw_imu_t* raw_imu)
*/
mavlink_raw_imu_t raw_imu;
mavlink_msg_raw_imu_decode(&msg, &raw_imu);
Serial.println(raw_imu.zgyro);
}
break;
case MAVLINK_MSG_ID_REQUEST_DATA_STREAM :  // #66
{

}
break;
case MAVLINK_MSG_ID_ATTITUDE:  // #30
{
/* Message decoding: PRIMITIVE
mavlink_msg_attitude_decode(const mavlink_message_t* msg, mavlink_attitude_t* attitude)
*/
mavlink_attitude_t attitude;
mavlink_msg_attitude_decode(&msg, &attitude);
Serial.println("5-");
}
break;

default:
break;
}
}
}
}
void rc_override(int mav_steer, int mav_speed) {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
mavlink_msg_rc_channels_override_pack(sysid, compid, &msg, 0, 0,
mav_steer,
mav_speed,
65535, 65535, 65535, 65535, 65535, 65535);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial1.write(buf, len);
}

我更改了目标id, sys id。我试过Mavlink1和2,不同的SERIAL2_BAUD。我期待从不同的msg. msgstr中得到pixhawk循环的答案。现在只有0和66了。心跳和请求。输入图片描述

我也面临同样的问题。pixhawk的serial2端口是TELEM2端口,请确保引脚顺序正确连接。如果您在Arduino中使用mavlink.h,则此库的旧版本可与mavlink1协议一起使用。消息id 0表示您正在获取心跳。如果您需要更多的信息,请告诉我。

你可以在不发送任何请求的情况下从pixhawk获得数据流。

设置SRx_参数(然后重新启动自动驾驶仪)将使自动驾驶仪主动向地面站发送消息组。(https://ardupilot.org/dev/docs/mavlink-requesting-data.html)

查看是否可以在设置SRx参数后获得任何数据。

顺便说一句,正如Hemanth Narayan提到的,mavlink.h库只适用于mavlink 1。我尝试了mavlink 2,没有数据拾取。

最新更新