MAVLink and Arduino: step by step

But this make a Web Server, I would like to connect with another telemetry 915Mhz Lora with Mavlink and connect to Mission Planner

can Anybody connect in ESP 32 + Lora like a Telemetry?

Hello how are you doing? I am investigating exactly the same thing, I have been stuck for several days looking for a possible solution to the problem. Maybe we can help each other.
From what this blog says he can separate the MavLink messages, I want to take only the current mode Latitude Longitude, battery and other important data to be transmitted by LoRa.
Layers can you help us.
Thank you so much

I would like to congratulate and thank you as this is the first (and only so far i found) practical and clear example and how to use mavlink (for a non programmer expert like me)!

may I ask about how to display roll, pitch, and yaw values ​​in Arduino from the data taken in Pixhawk, do we just write “Serial.print (roll / pitch / yaw);” or something to add?
please be willing to reply

sorry i wanna ask, why the result of roll pitch yaw not like -180 untill 180 degress, why just until 2 ?

Hi @PittRBM and @jplopezll , I am trying to implement this but stuck at the step 2. The SERIAL_1, SERIAL_2 etc. parameters are not visible in my Mission Planner. Could you help me out? I tried running the 1.3.52 firmware but still no success.

Would greatly appreciate any assistance.

Hi, @abhilash-ingale:

That screen you have attached looks strange. I mean it looks like you are not really connected to the flight controller. If you check the Ardupilot pages here, the serial comms parameters are in the “full parameter tree” menu on the left side and that menu will only show up once connected.

The only unusual thing I can see in your screen capture is that the connection speed is very slow. It should not matter, but I would try with at least 57600 bauds. Please check again and revert.

Kind regards.

Hi @farhan_ashegaf:

Sorry, I was not able to connect for a long time. Do you still need help with this?

Kind regards.

Hello friend,
I have your problem,
Can you give me your code and how you fixed it plz ?

Hi, @Yousef_Shehada:

Same problem? Have you checked your comms with the flight controller?

KR

Hello @jplopezll,

Thank you so much for your work,
I established connection between Arduino Mega and Pixhawk 2.4.8,
received heartbeat and messages,
I’m trying to get these Data:
Lat,Long,GroundSpeed,VericalSpeed (from Acc.),Heading,Altitude

This is my Code:

#include “mavlink.h”
//#include “common/mavlink_msg_request_data_stream.h”

// Mavlink variables
unsigned long previousMillisMAVLink = 0; // will store last time MAVLink was transmitted and listened
unsigned long next_interval_MAVLink = 1000; // next interval to count
const int num_hbs = 60; // # of heartbeats to wait before activating STREAMS from Pixhawk. 60 = one minute.
int num_hbs_pasados = num_hbs;

// FastLed setup
// How many leds in your strip?
#define NUM_LEDS 8
#define BRIGHTNESS 255 // Put 0 to switch off all leds

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 2
//#define MODE_PIN 3
//#define CLOCK_PIN 13

// Define the array of leds
//CRGB leds[NUM_LEDS];

// Status: 0, 2, 4 for lights off, 1, 3 for lights on
// Pulses: flashing with two 20 ms pulses, 80 ms in between pulses and 1 Hz frequency
int leds_status = 0;

// Light modes: 1, normal mode. 0, off. 2, low battery.
#define NUM_MODOS 3
int leds_modo = 1;

// Lights flashing adjustment
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long next_interval = 0; // next interval
const long tiempo_on = 20;
const long tiempo_off = 80;
const long tiempo_descanso = 880;
int test_led_tipo = 4;

void setup() {
// MAVLink interface start
Serial.begin(57600);

// LEDs setup
//delay(5000); // sanity delay
// FastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);
// FastLED.setBrightness( BRIGHTNESS );

// [DEB] Soft serial port start
// Serial.begin(57600);
Serial.println(“MAVLink starting.”);
Serial2.begin(57600);
}

void loop() {
// Lights management
// Light pulses: 2 quick flashes per second. 100 ms each cycle
unsigned long currentMillis = millis();
int i=0;

// Normal mode, lights on.
if (currentMillis - previousMillis >= next_interval) {
// Keep time last mode changed
previousMillis = currentMillis;

// Toggle lights depending on status

// Show leds

// FastLED.show();

// Cycle status from 0 to 3

}

// MAVLink
/* The default UART header for your MCU */
int sysid = 1; ///< ID 20 for this airplane. 1 PX, 255 ground station
int compid = 158; ///< The component sending the message
int type = MAV_TYPE_QUADROTOR; ///< This system is an airplane / fixed wing

// Define the system type, in this case an airplane -> on-board controller
// uint8_t system_type = MAV_TYPE_FIXED_WING;
uint8_t system_type = MAV_TYPE_GENERIC;
uint8_t autopilot_type = MAV_AUTOPILOT_INVALID;

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
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];

// Pack the message
//mavlink_msg_heartbeat_pack(sysid,compid, &msg, type, autopilot_type, system_mode, custom_mode, system_state);
mavlink_msg_heartbeat_pack(1,0, &msg, type, autopilot_type, system_mode, custom_mode, system_state);

// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);

// Send the message with the standard UART send function
// uart0_send might be named differently depending on
// the individual microcontroller / library in use.
unsigned long currentMillisMAVLink = millis();
if (currentMillisMAVLink - previousMillisMAVLink >= next_interval_MAVLink) {
// Guardamos la última vez que se cambió el modo
previousMillisMAVLink = currentMillisMAVLink;

Serial2.write(buf,len);
Serial.println("Ardu HB");


//Mav_Request_Data();
num_hbs_pasados++;
if(num_hbs_pasados>=num_hbs) {
  // Request streams from Pixhawk
  Serial.println("Streams requested!");
  Mav_Request_Data();
  num_hbs_pasados=0;
}

}

// Check reception buffer
comm_receive();
}

void Mav_Request_Data()
{
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];

// STREAMS that can be requested
/*

  • Definitions are in common.h: enum MAV_DATA_STREAM
  • MAV_DATA_STREAM_ALL=0, // Enable all data streams
  • MAV_DATA_STREAM_RAW_SENSORS=1, /* Enable IMU_RAW, GPS_RAW, GPS_STATUS packets.
  • MAV_DATA_STREAM_EXTENDED_STATUS=2, /* Enable GPS_STATUS, CONTROL_STATUS, AUX_STATUS
  • MAV_DATA_STREAM_RC_CHANNELS=3, /* Enable RC_CHANNELS_SCALED, RC_CHANNELS_RAW, SERVO_OUTPUT_RAW
  • MAV_DATA_STREAM_RAW_CONTROLLER=4, /* Enable ATTITUDE_CONTROLLER_OUTPUT, POSITION_CONTROLLER_OUTPUT, NAV_CONTROLLER_OUTPUT.
  • MAV_DATA_STREAM_POSITION=6, /* Enable LOCAL_POSITION, GLOBAL_POSITION/GLOBAL_POSITION_INT messages.
  • MAV_DATA_STREAM_EXTRA1=10, /* Dependent on the autopilot
  • MAV_DATA_STREAM_EXTRA2=11, /* Dependent on the autopilot
  • MAV_DATA_STREAM_EXTRA3=12, /* Dependent on the autopilot
  • MAV_DATA_STREAM_ENUM_END=13,
  • Data in PixHawk available in:
    • Battery, amperage and voltage (SYS_STATUS) in MAV_DATA_STREAM_EXTENDED_STATUS
    • Gyro info (IMU_SCALED) in MAV_DATA_STREAM_EXTRA1
      */

// To be setup according to the needed information to be requested from the Pixhawk
const int maxStreams = 2;
const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_EXTENDED_STATUS, MAV_DATA_STREAM_EXTRA1};
const uint16_t MAVRates[maxStreams] = {0x02,0x05};

for (int i=0; i < maxStreams; i++) {
/*
* mavlink_msg_request_data_stream_pack(system_id, component_id,
* &msg,
* target_system, target_component,
* MAV_DATA_STREAM_POSITION, 10000000, 1);
*
* mavlink_msg_request_data_stream_pack(uint8_t system_id, uint8_t component_id,
* mavlink_message_t* msg,
* uint8_t target_system, uint8_t target_component, uint8_t req_stream_id,
* uint16_t req_message_rate, uint8_t start_stop)
*
*/
mavlink_msg_request_data_stream_pack(2, 200, &msg, 1, 0, MAVStreams[i], MAVRates[i], 1);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial2.write(buf,len);
}

// Request: PARAM_REQUEST_LIST. Only for full log recording
/*

  • Primitive: mavlink_msg_param_request_list_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
    uint8_t target_system, uint8_t target_component)
    /
    /

    // Configure
    uint8_t system_id=2;
    uint8_t component_id=200;
    // mavlink_message_t* msg;
    uint8_t target_system=1;
    uint8_t target_component=0;

// Pack
mavlink_msg_param_request_list_pack(system_id, component_id, &msg,
target_system, target_component);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);

// Send
#ifdef SOFT_SERIAL_DEBUGGING
pxSerial.write(buf,len);
#else
Serial.write(buf, len);
#endif
*/
}

void comm_receive() {

mavlink_message_t msg;
mavlink_status_t status;

// Echo for manual debugging
// Serial.println("—Start—");

while(Serial2.available()>0) {
uint8_t c = Serial2.read();

// Try to get a new message
if(mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {

  // Handle message
  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
        Serial.println("PX HB");

      }
      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.print("PX SYS STATUS: ");
        Serial.print("[Bat (V): ");
        Serial.print(sys_status.voltage_battery);
        Serial.print("], [Bat (A): ");
        Serial.print(sys_status.current_battery);
        Serial.print("], [Comms loss (%): ");
        Serial.print(sys_status.drop_rate_comm);
        Serial.println("]");
      }
      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("PX PARAM_VALUE");
        Serial.println(param_value.param_value);
        Serial.println(param_value.param_count);
        Serial.println(param_value.param_index);
        Serial.println(param_value.param_id);
        Serial.println(param_value.param_type);
        Serial.println("------ Fin -------");
      }
      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("PX RAW IMU");
        Serial.println(raw_imu.xacc);
      }
      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("PX ATTITUDE");
        Serial.println(attitude.roll);
        if(attitude.roll>1) leds_modo = 0;
        else if(attitude.roll<-1) leds_modo = 2;
        else leds_modo=1;

      }
      break;

    
   default:

      Serial.print("--- Otros: ");
      Serial.print("[ID: ");
      Serial.print(msg.msgid);
      Serial.print("], [seq: ");
      Serial.print(msg.seq);
      Serial.println("]");
      break;
  }
}

}
}

======================================================================
This is the result from my code:
MAVLink starting.
PX RAW IMU
2
— Otros: [ID: 29], [seq: 193]
— Otros: [ID: 24], [seq: 194]
— Otros: [ID: 2], [seq: 195]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX ATTITUDE
0.03
— Otros: [ID: 33], [seq: 208]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 212]
— Otros: [ID: 42], [seq: 213]
— Otros: [ID: 74], [seq: 214]
— Otros: [ID: 36], [seq: 215]
— Otros: [ID: 35], [seq: 217]
PX RAW IMU
6
— Otros: [ID: 29], [seq: 220]
— Otros: [ID: 24], [seq: 221]
— Otros: [ID: 2], [seq: 222]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX HB
PX HB
— Otros: [ID: 33], [seq: 234]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
Ardu HB
Streams requested!
— Otros: [ID: 62], [seq: 238]
— Otros: [ID: 42], [seq: 239]
— Otros: [ID: 74], [seq: 240]
— Otros: [ID: 36], [seq: 241]
— Otros: [ID: 35], [seq: 243]
PX RAW IMU
2
— Otros: [ID: 29], [seq: 246]
— Otros: [ID: 24], [seq: 247]
— Otros: [ID: 2], [seq: 248]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX ATTITUDE
0.03
— Otros: [ID: 33], [seq: 5]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 9]
— Otros: [ID: 42], [seq: 10]
— Otros: [ID: 74], [seq: 11]
— Otros: [ID: 36], [seq: 12]
— Otros: [ID: 35], [seq: 14]
PX RAW IMU
1
— Otros: [ID: 29], [seq: 17]
— Otros: [ID: 24], [seq: 18]
— Otros: [ID: 2], [seq: 19]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX HB
PX HB
— Otros: [ID: 33], [seq: 31]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 35]
Ardu HB
— Otros: [ID: 42], [seq: 36]
— Otros: [ID: 74], [seq: 37]
— Otros: [ID: 36], [seq: 38]
— Otros: [ID: 35], [seq: 40]
PX RAW IMU
1
— Otros: [ID: 29], [seq: 43]
— Otros: [ID: 24], [seq: 44]
— Otros: [ID: 2], [seq: 45]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 62]
— Otros: [ID: 42], [seq: 63]
— Otros: [ID: 74], [seq: 64]
— Otros: [ID: 36], [seq: 65]
— Otros: [ID: 35], [seq: 67]
PX RAW IMU
0
— Otros: [ID: 29], [seq: 70]
— Otros: [ID: 24], [seq: 71]
— Otros: [ID: 2], [seq: 72]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX HB
— Otros: [ID: 33], [seq: 84]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 88]
PX HB
— Otros: [ID: 42], [seq: 89]
— Otros: [ID: 74], [seq: 90]
— Otros: [ID: 36], [seq: 91]
Ardu HB
— Otros: [ID: 35], [seq: 93]
PX RAW IMU
4
— Otros: [ID: 29], [seq: 96]
— Otros: [ID: 24], [seq: 98]
— Otros: [ID: 2], [seq: 99]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX ATTITUDE
0.03
— Otros: [ID: 33], [seq: 113]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 117]
— Otros: [ID: 42], [seq: 118]
— Otros: [ID: 74], [seq: 119]
— Otros: [ID: 36], [seq: 120]
— Otros: [ID: 35], [seq: 122]
PX RAW IMU
2
— Otros: [ID: 29], [seq: 125]
— Otros: [ID: 24], [seq: 126]
— Otros: [ID: 2], [seq: 127]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX HB
PX HB
— Otros: [ID: 33], [seq: 139]
PX SYS STATUS: [Bat (V): 0], [Bat (A): -1], [Comms loss (%): 0]
— Otros: [ID: 62], [seq: 143]
— Otros: [ID: 42], [seq: 144]
Ardu HB
— Otros: [ID: 74], [seq: 145]
— Otros: [ID: 36], [seq: 146]
— Otros: [ID: 35], [seq: 148]
PX RAW IMU
2
— Otros: [ID: 29], [seq: 151]
— Otros: [ID: 24], [seq: 152]
— Otros: [ID: 2], [seq: 153]
PX ATTITUDE
0.03
PX ATTITUDE
0.03
PX ATTITUDE
0.03

I cannot understand the results at all,
this is your code with removing Led thing and changing Serial only,
Please help me through.

Hi, @eizex:

I do not fully understand your question. I would suggest you to read again “step 5” of the post.

Each message can have a specific data structure that you must know to be able to properly decode the message and access the packed variables. For instance, attitude messages have ID 30 and the structure is like this:

#define MAVLINK_MSG_ID_ATTITUDE 30

typedef struct __mavlink_attitude_t
{
 uint32_t time_boot_ms; ///< Timestamp (milliseconds since system boot)
 float roll; ///< Roll angle (rad)
 float pitch; ///< Pitch angle (rad)
 float yaw; ///< Yaw angle (rad)
 float rollspeed; ///< Roll angular speed (rad/s)
 float pitchspeed; ///< Pitch angular speed (rad/s)
 float yawspeed; ///< Yaw angular speed (rad/s)
} mavlink_attitude_t;

So, once you have decoded the message, you can access the variables using attitude.roll, or attitude.pitch, etc

In the output you have attached I can see you are already getting some info. There are many messages you are not decoding and in my code I had a default case to know the ID of all those mesages whose decoding I had not implemented. For those messages I was printing just the ID and the sequence number with the “Otros” (means “others”) just in front to be able to differentiate them.

If you have any doubt, please be more specific regarding the exact line of code or output you do not understand. I will try to explain the best I can.

KR.

Thank you bro,

Since I wrote the reply I managed to get GPS data but still didn’t get them all good, I got the details from :

case MAVLINK_MSG_ID_GPS_RAW_INT:
{
iob_lat = mavlink_msg_gps_raw_int_get_lat(&msg) / 10000000.0f;
iob_hdop=(mavlink_msg_gps_raw_int_get_eph(&msg)/100);
iob_lon = mavlink_msg_gps_raw_int_get_lon(&msg) / 10000000.0f;
iob_gps_alt = mavlink_msg_gps_raw_int_get_alt(&msg) / 1000.0f;
iob_fix_type = mavlink_msg_gps_raw_int_get_fix_type(&msg);
iob_satellites_visible = mavlink_msg_gps_raw_int_get_satellites_visible(&msg);
}

Still didn’t get GPS speed.

https://mavlink.io/en/messages/common.html#GPS_RAW_INT

Explains the structure of that message.
Velocity is only present if available.

Hi! I’m now using arduino to parse data from a fake gps that outputs NMEA-0183 data then send it into the flight controller.
I found out that the mavlink.zip doesn’t include <mavlink_msg_gps_input.h>
then i found this https://github.com/langrind/MavlinkTeensyLib and copy it into my arduino libraries.
But the flight controller didn’t receive the message. (By checking the mavlink inspector in Mission Planner)
Can anyone help me out???

Here’s the code

#include <mavlink2.h>

//#include <SoftwareSerial.h>
//#include <mavlink.h> // Mavlink interface

//#include <common/mavlink_msg_gps_input.h>
#include <TinyGPS++.h>

//Nooploop Linktrack 標籤機連接
//SoftwareSerial LinkTrackGPS(10, 11); // RX, TX

// The TinyGPS++ object
TinyGPSPlus gps;

double lat_A0_origin = 22.5180977;
double lon_A0_origin = 113.9007239;

//A0現實座標
double lat_A0_real = 25.64541232;
double lon_A0_real = 159.464646;

//A0座標修正值
double lat_correction = lat_A0_real - lat_A0_origin;
double lon_correction = lon_A0_real - lon_A0_origin;

//比例尺
float alt_scale = 0.01; //cm to m
float hdop_scale = 0.01; //cm to m

mavlink_system_t mavlink_system = {
1, // System ID (1-255)
158 // Component ID (a MAV_COMPONENT value)
};

void setup() {

// Open serial communications and wait for port to open:
Serial.begin(115200);

// set the data rate for the SoftwareSerial port
//LinkTrackGPS.begin(115200);
Serial1.begin(115200);
Serial2.begin(115200);

delay(500);
}

// Mavlink Setup

void command_heartbeat() {
int system_id = 100; //< ID 1 for this system (?)
int component_id = 195; //MAV_COMP_ID_PERIPHERAL; //< (221) MAV_COMP_ID_GPS2 GPS #2. The component sending the message. (?) https://mavlink.io/en/messages/common.html#MAV_COMPONENT
uint8_t system_type = MAV_TYPE_GCS; // Define the system type, in this case ground control station
uint8_t autopilot_type = MAV_AUTOPILOT_INVALID;
uint8_t system_mode = 0;
uint32_t custom_mode = 0;
uint8_t system_state = 0;

// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];

// Pack the message
mavlink_msg_heartbeat_pack(system_id,component_id, &msg, system_type, autopilot_type, system_mode, custom_mode, system_state);

// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);

}

// GPS_INPUT Message Setup

void command_GPS( uint8_t fix_type_read , int32_t lat_fixed , int32_t lon_fixed , int32_t alt_read , float hdop_read , uint8_t sat_read ){

//Mavlink GPS Message

uint8_t system_id = 1; //無人機 sysid_this_mav
uint8_t component_id = 158; //MAV_COMP_ID_PERIPHERAL; // 221 MAV_COMP_ID_GPS2 GPS #2.
uint64_t time_usec = 0; //Timestamp (UNIX Epoch time or time since system boot).
uint8_t gps_id = 0;
uint16_t ignore_flags = 253; //1+4+8+16+32+64+128=253
uint32_t time_week_ms = 0;
uint16_t time_week = 0;
float vdop = 0;
float vn = 0;
float ve = 0;
float vd = 0;
float speed_accuracy = 0;
float horiz_accuracy = 0;
float vert_accuracy = 0;
uint16_t yaw = 0;

uint8_t fix_type = fix_type_read; //將fix_type改成讀取到且判斷後的數值
int32_t lat = lat_fixed * 10000000; //將經度改成讀取到且經過修正後的數值(degE7)
int32_t lon = lon_fixed * 10000000; //將緯度改成讀取到且經過修正後的數值(degE7)
float alt = alt_read * alt_scale; //將高度改成讀取到且經過修正後的數值(m)
float hdop = hdop_read * hdop_scale; //將平面精度HDOP改成讀取到且經過修正後的數值(cm)
uint8_t satellites_visible = sat_read; //將衛星數量改成讀取到且經過修正後的數值

// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];

// Pack the message
uint16_t mavlink_msg_gps_input_pack(system_id,component_id,&msg,time_usec,gps_id,ignore_flags,time_week_ms,time_week,fix_type,lat,lon,alt,hdop,vdop,vn,ve,vd,speed_accuracy,horiz_accuracy,vert_accuracy,satellites_visible,yaw);

// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);

// Send the message (.write sends as bytes)
Serial2.write(buf, len);
}

void loop() {

//while ((LinkTrackGPS.available() > 0)){
while ((Serial1.available() > 0)){
gps.encode(Serial1.read());

  if (gps.time.isUpdated()){
//讀取NMEA格式原始資料
double lat_read = gps.location.lat();    //double
double lon_read = gps.location.lng();    //double
uint8_t sat_read = gps.satellites.value();  //u32
int32_t alt_read = gps.altitude.value(); //int32 (cm)
float hdop_read = gps.hdop.value();    //(100ths-i32)

//修正GPS資料
double lat_fixed = lat_read + lat_correction;
double lon_fixed = lon_read + lon_correction;
uint8_t fix_type_read = 0;


if (sat_read > 2){
  uint8_t fix_type_read = 3;
}
if (sat_read <= 2){
  uint8_t fix_type_read = 0;
}

command_GPS(fix_type_read,lat_fixed,lon_fixed,alt_read,hdop_read,sat_read);

I would say your ignore_flags is incorrect. For example, you have set GPS_INPUT_IGNORE_FLAG_ALT (==1) yet you set an altitude.

Thank you for your advice. I change the code that it didn’t set an altitude. But the flight controller still can’t get the message. I’m assuming the problem might go with the arduino library that i kind of mess up or the connection between arduino and FC. Can anyone provide a arduino mavlink library with GPS_INPUT in it? I’m new to coding and not sure how to create a library that is “USABLE” :sweat_smile:

BTW, the combination I tried setting the flight controller
Serial1_Protocol, 1(Mavlink1) / 2(Mavlink2) / 5(GPS)
Serial1_Baudrate, 115(115200)
GPS_TYPE1, 14(MAV)
GPS_TYPE2, 14(MAV)

SoftwareSerial mySerial(0,1); // RX, TX
SoftwareSerial pxSerial(9,10); // RX, TX

Hello, where is the interface on PIXHAWK used in your PX SERIAL (9,10)? I don’t feel right.

Hi, @SilaLiu:

In my post, I connect to Telem2 port of the PixHawk. The lines you mention are pins of the Arduino. SoftwareSerial is a library that emulates serial comms using pins of the Arduino.

In case you need further explanations, please PM me with your layout and needs. I will try to help.

KR
Juan Pedro

Hi, Juan Pedro.

I have solved the last problem, but thank you very much, and now I have a more difficult problem.

I use mavlink_parse_CHAR (MAVLINK_COMM_0, c, & MSG, &status);

Switch (MSg. msgid), the MSG value is always 66, can you help me solve it?

Thank you

Hi, @SilaLiu:

You have to post your code. I cannot tell you with the info you provide here.

KR,
Juan Pedro