im using companion computer as Esp32 board and for that im using Arduino IDE and
And Arducopter code …in that im added the FFAT system to store the data or msg in the file but in that i didnot getting msg id only im getting single msg id i.e 0(Heartbeat)
for i need total msg ids and my code is below>…
#include <HardwareSerial.h>
#include <FFat.h>
#include <MAVLink_ardupilotmega.h>
// Define serial ports
#define FLIGHT_CONTROLLER Serial2
// FFat configuration
#define FFAT_CS_PIN 5 // Define the Chip Select pin for FFat module
// Configuration
const unsigned long INTERVAL = 3600 * 1000; // Interval to pull logs in milliseconds (e.g., every hour)
const char* LOG_PATH = “/logs/”; // Path in FFat to save logs
const char* MSG_LOG_PATH = “/msgLogs/”; // Path in FFat to save MAVLink messages
// const char* DIAG_LOG_PATH = “/diagLogs/”; // Path in FFat to save diagnostic messages
// 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;
void setup() {
// MAVLink interface start
Serial.begin(57600);
Serial.println(“MAVLink starting.”);
FLIGHT_CONTROLLER.begin(57600, SERIAL_8N1, 16, 17); // Configure RX and TX pins
// Initialize FFat filesystem
if (!FFat.begin(true)) {
Serial.println("Failed to initialize FFat");
return;
}
// Create logs directory if it doesn't exist
if (!FFat.exists(LOG_PATH)) {
// Serial.println("LOG_PATH");
FFat.mkdir(LOG_PATH);
}
// Create message logs directory if it doesn't exist
if (!FFat.exists(MSG_LOG_PATH)) {
// Serial.println("MSG_LOG_PATH");
FFat.mkdir(MSG_LOG_PATH);
}
// Create diagnostic logs directory if it doesn't exist
// if (!FFat.exists(DIAG_LOG_PATH)) {
// // Serial.println("DIAG_LOG_PATH");
// // FFat.mkdir(DIAG_LOG_PATH);
// }
// Initial log download
downloadLogs();
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMillisMAVLink = millis();
// MAVLink heartbeat and data request logic
if (currentMillisMAVLink - previousMillisMAVLink >= next_interval_MAVLink) {
previousMillisMAVLink = currentMillisMAVLink;
num_hbs_pasados++;
if (num_hbs_pasados >= num_hbs) {
Mav_Request_Data();
num_hbs_pasados = 0;
}
}
// Check for incoming MAVLink messages
comm_receive();
// Regularly download logs
static unsigned long lastDownload = 0;
if (currentMillis - lastDownload >= INTERVAL) {
downloadLogs();
lastDownload = currentMillis;
}
}
void downloadLogs() {
Serial.println(“Requesting log list…”);
// Request log list (this is a placeholder, replace with actual MAVLink message)
FLIGHT_CONTROLLER.write("log_request_list");
// Wait for log list response
while (FLIGHT_CONTROLLER.available()) {
uint8_t msgId = FLIGHT_CONTROLLER.read();
Serial.print("Flight Controller:");
Serial.println(msgId);
if (msgId == MAVLINK_MSG_ID_LOG_ENTRY)
// if(msgId == MAVLINK_MSG_ID_HEARTBEAT)
{
Serial.println("Received log entry message");
handleLogEntry(3,5);
// Extract log entry details and download logs (replace with actual handling logic)
// uint8_t logId = 0; // Replace with actual log ID extracted
// uint8_t logSize = 0; // Replace with actual log size extracted
// handleLogEntry(logId, logSize);
// void handleLogEntry(uint8_t logId, uint8_t logSize);
}
if (msgId == MAVLINK_MSG_ID_LOG_REQUEST_DATA) {
Serial.println("Received log request send message");
// handleLogEntry(3,5);
}
}
Serial.println("Log download complete");
}
void handleLogEntry(uint8_t logId, uint8_t logSize) {
Serial.print("Downloading log ID: “);
Serial.print(logId);
Serial.print(”, Size: ");
Serial.println(logSize);
// Request the log data
FLIGHT_CONTROLLER.write("log_request_data");
// Save log data to FFat
File logFile = FFat.open(String(LOG_PATH) + String(logId) + ".bin", FILE_WRITE);
if (!logFile) {
Serial.println("Failed to open file for writing");
return;
}
else
Serial.println("Open file for writing");
while (FLIGHT_CONTROLLER.available()) {
uint8_t data = FLIGHT_CONTROLLER.read();
logFile.write(data);
}
logFile.close();
Serial.println("Log saved successfully");
}
void Mav_Request_Data() {
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// To be setup according to the needed information to be requested from the Pixhawk
const int maxStreams = 3;
// const uint8_t MAVStreams[maxStreams] = {0, 1, 2, 3, 4, 6, 10, 11, 12, 13};
const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_ALL,MAV_DATA_STREAM_EXTENDED_STATUS,MAV_DATA_STREAM_EXTRA1};
const uint16_t MAVRates[maxStreams] = {0x02, 0x02,0x02};
// const uint16_t MAVRates[maxStreams] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
// const uint8_t MAVStreams[maxStreams] = {MAV_DATA_STREAM_ALL};
// const uint16_t MAVRates[maxStreams] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
for (int i = 0; i < maxStreams; i++) {
// Serial.println("maxstream");
mavlink_msg_request_data_stream_pack(6, 200, &msg, 1, 0, MAVStreams[i], MAVRates[i], 1);
// mavlink_msg_request_data_stream_pack(1, 1, &msg, 1, 0, MAVStreams[i], MAVRates[i], 1);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial.write(buf, len);
}
}
void comm_receive() {
mavlink_message_t msg;
mavlink_status_t status;
// Create a file to store incoming messages
File msgFile = FFat.open(String(MSG_LOG_PATH) + "mavlink_messages.txt", FILE_APPEND);
if (!msgFile) {
Serial.println("Failed to open MAVLink message log file");
return;
}
// Create a file to store diagnostic messages
// File diagFile = FFat.open(String(DIAG_LOG_PATH) + "diagnostic_messages.txt", FILE_APPEND);
// if (!diagFile) {
// Serial.println("Failed to open diagnostic message log file");
// return;
// }
while (FLIGHT_CONTROLLER.available() > 0) {
uint8_t c = FLIGHT_CONTROLLER.read();
// Try to get a new message
if (mavlink_parse_char(MAVLINK_COMM_0, c, &msg, &status)) {
Serial.print("\n");
Serial.print("message ID:");
Serial.print(msg.msgid);
// Log the message ID and payload to the file
msgFile.print("Message ID: ");
msgFile.print(msg.msgid);
msgFile.print(", Payload: ");
for (int i = 0; i < msg.len; i++) {
msgFile.print(((uint8_t*)(&msg.payload64))[i], HEX); // Access payload as byte array
msgFile.print(" ");
}
msgFile.println();
// Handle message and log specific diagnostic messages
switch (msg.msgid) {
case MAVLINK_MSG_ID_HEARTBEAT:
{
__mavlink_heartbeat_t mavlink_heartbeat_t;
mavlink_msg_heartbeat_decode(&msg, &mavlink_heartbeat_t);
// Log heartbeat message to diagnostic file
// diagFile.print("Heartbeat: ");
// diagFile.print("Type: ");
// diagFile.print(mavlink_heartbeat_t.type);
// diagFile.print(", Autopilot: ");
// diagFile.print(mavlink_heartbeat_t.autopilot);
// diagFile.print(", Base Mode: ");
// diagFile.print(mavlink_heartbeat_t.base_mode);
// diagFile.print(", System Status: ");
// diagFile.println(mavlink_heartbeat_t.system_status);
}
break;
case MAVLINK_MSG_ID_VIBRATION:
{
__mavlink_vibration_t mavlink_vibration_t;
mavlink_msg_vibration_decode(&msg, &mavlink_vibration_t);
}
break;
case MAVLINK_MSG_ID_SYS_STATUS:
{
__mavlink_sys_status_t mavlink_sys_status_t;
mavlink_msg_sys_status_decode(&msg, &mavlink_sys_status_t);
// Log system status message to diagnostic file
// diagFile.print("Sys Status: ");
// diagFile.print("Voltage: ");
// diagFile.print(mavlink_sys_status_t.voltage_battery);
// diagFile.print(", Current: ");
// diagFile.print(mavlink_sys_status_t.current_battery);
// diagFile.print(", Battery Remaining: ");
// diagFile.println(mavlink_sys_status_t.battery_remaining);
}
break;
case MAVLINK_MSG_ID_PROTOCOL_VERSION:
{
__mavlink_protocol_version_t mavlink_protocol_version_t;
mavlink_msg_protocol_version_decode(&msg, &mavlink_protocol_version_t);
}
break;
case MAVLINK_MSG_ID_RAW_IMU:
{
__mavlink_raw_imu_t mavlink_raw_imu_t;
mavlink_msg_raw_imu_decode(&msg, &mavlink_raw_imu_t);
}
break;
case MAVLINK_MSG_ID_ATTITUDE:
{
__mavlink_attitude_t mavlink_attitude_t;
mavlink_msg_attitude_decode(&msg, &mavlink_attitude_t);
}
break;
case MAVLINK_MSG_ID_GLOBAL_POSITION_INT:
{
__mavlink_global_position_int_t mavlink_global_position_int_t;
mavlink_msg_global_position_int_decode(&msg, &mavlink_global_position_int_t);
}
break;
case MAVLINK_MSG_ID_VFR_HUD:
{
__mavlink_vfr_hud_t mavlink_vfr_hud_t;
mavlink_msg_vfr_hud_decode(&msg, &mavlink_vfr_hud_t);
}
break;
case MAVLINK_MSG_ID_POWER_STATUS:
{
__mavlink_power_status_t mavlink_power_status_t;
mavlink_msg_power_status_decode(&msg, &mavlink_power_status_t);
}
break;
case MAVLINK_MSG_ID_NAV_CONTROLLER_OUTPUT:
{
__mavlink_nav_controller_output_t mavlink_nav_controller_output_t;
mavlink_msg_nav_controller_output_decode(&msg, &mavlink_nav_controller_output_t);
}
break;
case MAVLINK_MSG_ID_MISSION_CURRENT:
{
__mavlink_mission_current_t mavlink_mission_current_t;
mavlink_msg_mission_current_decode(&msg, &mavlink_mission_current_t);
}
break;
case MAVLINK_MSG_ID_SERVO_OUTPUT_RAW:
{
__mavlink_servo_output_raw_t mavlink_servo_output_raw_t;
mavlink_msg_servo_output_raw_decode(&msg, &mavlink_servo_output_raw_t);
}
break;
case MAVLINK_MSG_ID_RC_CHANNELS:
{
__mavlink_rc_channels_t mavlink_rc_channels_t;
mavlink_msg_rc_channels_decode(&msg, &mavlink_rc_channels_t);
}
break;
case MAVLINK_MSG_ID_SCALED_IMU2:
{
__mavlink_scaled_imu2_t mavlink_scaled_imu2_t;
mavlink_msg_scaled_imu2_decode(&msg, &mavlink_scaled_imu2_t);
}
break;
case MAVLINK_MSG_ID_SCALED_IMU3:
{
__mavlink_scaled_imu3_t mavlink_scaled_imu3_t;
mavlink_msg_scaled_imu3_decode(&msg, &mavlink_scaled_imu3_t);
}
break;
case MAVLINK_MSG_ID_SCALED_PRESSURE:
{
__mavlink_scaled_pressure_t mavlink_scaled_pressure_t;
mavlink_msg_scaled_pressure_decode(&msg, &mavlink_scaled_pressure_t);
}
break;
case MAVLINK_MSG_ID_GPS_RAW_INT:
{
__mavlink_gps_raw_int_t mavlink_gps_raw_int_t;
mavlink_msg_gps_raw_int_decode(&msg, &mavlink_gps_raw_int_t);
}
break;
case MAVLINK_MSG_ID_SYSTEM_TIME:
{
__mavlink_system_time_t mavlink_system_time_t;
mavlink_msg_system_time_decode(&msg, &mavlink_system_time_t);
}
break;
case MAVLINK_MSG_ID_TERRAIN_REPORT:
{
__mavlink_terrain_report_t mavlink_terrain_report_t;
mavlink_msg_terrain_report_decode(&msg, &mavlink_terrain_report_t);
}
break;
case MAVLINK_MSG_ID_PARAM_VALUE:
{
__mavlink_param_value_t mavlink_param_value_t;
mavlink_msg_param_value_decode(&msg, &mavlink_param_value_t);
}
break;
case MAVLINK_MSG_ID_COMMAND_ACK:
{
__mavlink_command_ack_t mavlink_command_ack_t;
mavlink_msg_command_ack_decode(&msg, &mavlink_command_ack_t);
}
break;
case MAVLINK_MSG_ID_AHRS:
{
__mavlink_ahrs_t mavlink_ahrs_t;
mavlink_msg_ahrs_decode(&msg, &mavlink_ahrs_t);
}
break;
case MAVLINK_MSG_ID_AHRS2:
{
__mavlink_ahrs2_t mavlink_ahrs2_t;
mavlink_msg_ahrs2_decode(&msg, &mavlink_ahrs2_t);
}
break;
case MAVLINK_MSG_ID_MEMINFO:
{
__mavlink_meminfo_t mavlink_meminfo_t;
mavlink_msg_meminfo_decode(&msg, &mavlink_meminfo_t);
}
break;
case MAVLINK_MSG_ID_MCU_STATUS:
{
__mavlink_mcu_status_t mavlink_mcu_status_t;
mavlink_msg_mcu_status_decode(&msg, &mavlink_mcu_status_t);
}
break;
case MAVLINK_MSG_ID_EKF_STATUS_REPORT:
{
__mavlink_ekf_status_report_t mavlink_ekf_status_report_t;
mavlink_msg_ekf_status_report_decode(&msg, &mavlink_ekf_status_report_t);
}
break;
case MAVLINK_MSG_ID_SCALED_PRESSURE2:
{
__mavlink_scaled_pressure2_t mavlink_scaled_pressure2_t;
mavlink_msg_scaled_pressure2_decode(&msg, &mavlink_scaled_pressure2_t);
}
break;
case MAVLINK_MSG_ID_LOG_ENTRY:
{
__mavlink_log_entry_t mavlink_log_entry_t ;
mavlink_msg_log_entry_decode(&msg, &mavlink_log_entry_t);
}
break;
case MAVLINK_MSG_ID_LOG_REQUEST_DATA:
{
__mavlink_log_request_data_t mavlink_log_entry_t;
mavlink_msg_log_request_data_decode(&msg, &mavlink_log_entry_t);
}
break;
}
}
}
// Close the message log file and diagnostic file
msgFile.close();
// diagFile.close();
}
Anyone plz help…