I also want to know the current GPS ID in the Mission Planner open-source project. When I search for MAV_COM_GPS
in the mavlink.lua
file, it shows GPS 1 as 220 and GPS 2 as 221. However, when I tested it, the component IDs appeared to be random. I only connected two components—one for ArduPilot and the other for GPS. Here are the results:
yaml
CopyEdit
Heartbeat received from Component ID: 1
Custom Mode: 1
Type: 16
Autopilot: 0
Base Mode: 0
System Status: 0
MAVLink Version: 1
Active Components: 2
Component IDs: 115, 1
Here is the code I tested:
csharp
CopyEdit
private string ProcessMavLinkMessage(byte[] buffer)
{
try
{
// Ensure that the MAVLink message format is correct
if (buffer.Length < 10) // Minimum length based on MAVLink protocol
{
return "Error: Buffer too small to contain a valid MAVLink message.";
}
// Extract common MAVLink headers from the buffer
byte header = buffer[0];
byte payloadlength = buffer[1];
byte incompat_flags = buffer[2];
byte compat_flags = buffer[3];
byte seq = buffer[4];
byte sysid = buffer[5]; // System ID
byte compid = buffer[6]; // Component ID
uint msgid = (uint)((buffer[9] << 16) + (buffer[8] << 8) + buffer[7]); // Message ID
// Display the basic info of the message
string messageInfo = $"Message received: SysID: {sysid}, CompID: {compid}, MsgID: {msgid}";
// Track unique component IDs
componentIds.Add(compid);
// Handle different message types based on msgid
switch (msgid)
{
case 0: // HEARTBEAT message ID
return ProcessHeartbeatMessage(buffer, compid);
case 115: // STATUSTEXT message ID
return ProcessStatusTextMessage(buffer, compid);
case 24: // GPS_RAW_INT message ID
return ProcessGpsRawIntMessage(buffer, compid);
case 232: // GPS_INPUT message ID
return ProcessGpsInputMessage(buffer, compid);
case 30: // ATTITUDE message ID
return ProcessAttitudeMessage(buffer, compid);
case 74: // VFR_HUD message ID
return ProcessVfrHudMessage(buffer, compid);
case 147: // BATTERY_STATUS message ID
return ProcessBatteryStatusMessage(buffer, compid);
default:
// Return a generic message for unhandled message IDs
return $"{messageInfo} (Unhandled message ID)";
}
}
catch (Exception ex)
{
return $"Error parsing MAVLink message: {ex.Message}";
}
}