2026-03-30 22:09:23 +01:00
|
|
|
#include "driver/gpio.h"
|
2026-04-04 02:39:41 +01:00
|
|
|
#include "drone.h"
|
2026-03-30 22:09:23 +01:00
|
|
|
#include "drone_comms.h"
|
2026-04-02 18:57:52 +01:00
|
|
|
#include "esp32-hal.h"
|
2026-03-18 21:43:28 +00:00
|
|
|
#include "esp_log.h"
|
2026-03-30 22:09:23 +01:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2026-03-26 23:01:20 +00:00
|
|
|
#include "freertos/idf_additions.h"
|
|
|
|
|
#include "freertos/projdefs.h"
|
2026-04-07 10:48:44 +01:00
|
|
|
#include "freertos/task.h" #include < cstddef>
|
2026-04-02 18:57:52 +01:00
|
|
|
#include <cstdint>
|
2026-04-04 02:39:41 +01:00
|
|
|
#include <optional>
|
2026-03-30 22:09:23 +01:00
|
|
|
|
|
|
|
|
#include "env_sens.h"
|
2026-03-26 23:01:20 +00:00
|
|
|
#include "gps.h"
|
2026-04-03 17:52:02 +01:00
|
|
|
#include "imu.h"
|
2026-03-30 22:09:23 +01:00
|
|
|
#include "nav.h"
|
2026-04-03 17:52:02 +01:00
|
|
|
#include "packet_handler.h"
|
2026-03-30 22:09:23 +01:00
|
|
|
#include "radio.h"
|
2026-04-03 17:52:02 +01:00
|
|
|
#include "sens_fus.h"
|
2026-04-02 18:57:52 +01:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
static const char *TAG = "MAIN";
|
2026-03-23 11:01:23 +00:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
extern "C" void app_main(void) {
|
2026-03-18 15:49:09 +00:00
|
|
|
|
2026-04-03 17:52:02 +01:00
|
|
|
sens_fus_mutex = xSemaphoreCreateMutex();
|
2026-04-07 10:48:44 +01:00
|
|
|
configASSERT(sens_fus_mutex);
|
2026-03-30 22:09:23 +01:00
|
|
|
nav_mutex = xSemaphoreCreateMutex();
|
2026-04-03 17:52:02 +01:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
initArduino();
|
|
|
|
|
gpio_install_isr_service(0);
|
|
|
|
|
Serial.begin(115200);
|
2026-03-26 23:01:20 +00:00
|
|
|
|
2026-04-12 16:52:29 +01:00
|
|
|
// xTaskCreatePinnedToCore(radio_task, // Function name
|
|
|
|
|
// "radio_rxtx", // Name for debugging
|
|
|
|
|
// 4096, // Stack size in bytes
|
|
|
|
|
// NULL, // Parameters
|
|
|
|
|
// 5, // Priority (higher = more urgent)
|
|
|
|
|
// NULL, // Task handle
|
|
|
|
|
// 0 // Core ID
|
|
|
|
|
// );
|
2026-04-06 03:39:08 +01:00
|
|
|
//
|
2026-04-13 14:36:24 +01:00
|
|
|
// xTaskCreatePinnedToCore(env_sens::baro_poll_task, "baro_poll", 8192, NULL,
|
|
|
|
|
// 1,
|
|
|
|
|
// NULL, 0);
|
2026-04-06 03:39:08 +01:00
|
|
|
//
|
2026-04-13 14:36:24 +01:00
|
|
|
// xTaskCreatePinnedToCore(gps_poll_task, "gps_poll", 8192, NULL, 5, NULL, 0);
|
|
|
|
|
|
2026-04-07 10:48:44 +01:00
|
|
|
// xTaskCreatePinnedToCore(drone_controller_task, // Function name
|
|
|
|
|
// "drone_controller_task", // Name for debugging
|
|
|
|
|
// 1024 * 32, // Stack size in bytes
|
|
|
|
|
// NULL, // Parameters
|
|
|
|
|
// 20, // Priority (higher = more urgent)
|
2026-04-06 03:39:08 +01:00
|
|
|
// NULL, // Task handle
|
|
|
|
|
// 1 // Core ID
|
|
|
|
|
// );
|
2026-03-26 23:01:20 +00:00
|
|
|
|
2026-04-12 16:52:29 +01:00
|
|
|
xTaskCreatePinnedToCore(motor_throttles_task, // Function name
|
|
|
|
|
"motor_throttles_task", // Name for debugging
|
|
|
|
|
1024 * 4, // Stack size in bytes
|
|
|
|
|
NULL, // Parameters
|
2026-04-13 14:36:24 +01:00
|
|
|
24, // Priority (higher = more urgent)
|
2026-04-12 16:52:29 +01:00
|
|
|
NULL, // Task handle
|
|
|
|
|
1 // Core ID
|
|
|
|
|
);
|
2026-04-07 10:48:44 +01:00
|
|
|
|
2026-04-13 14:36:24 +01:00
|
|
|
// setup_imu();
|
2026-04-14 16:00:52 +01:00
|
|
|
ESP_LOGI("MAIN", "All tasks spawned. Main loop free.");
|
2026-04-06 03:39:08 +01:00
|
|
|
|
2026-03-30 22:09:23 +01:00
|
|
|
Eigen::Vector3f local_pos = {0, 0, 0};
|
|
|
|
|
Eigen::Vector3f local_vel = {0, 0, 0};
|
|
|
|
|
bool nav_data_ready = false;
|
2026-04-06 00:27:32 +01:00
|
|
|
|
|
|
|
|
uint64_t last_print_time = 0;
|
2026-04-14 16:00:52 +01:00
|
|
|
uint64_t last_broadcast_time = 0;
|
|
|
|
|
|
2026-03-26 23:01:20 +00:00
|
|
|
while (true) {
|
2026-04-06 03:39:08 +01:00
|
|
|
while (packet_tx_queue &&
|
|
|
|
|
xQueueReceive(packet_tx_queue, &packet_data[0], 1)) {
|
2026-04-02 18:57:52 +01:00
|
|
|
handle_packet(&packet_data[0]);
|
|
|
|
|
}
|
2026-03-30 22:09:23 +01:00
|
|
|
|
2026-04-14 16:00:52 +01:00
|
|
|
if (millis() > last_broadcast_time + 100) {
|
|
|
|
|
send_packet_getter(PACKET_TYPE::INFO_DRONE_POSITION);
|
|
|
|
|
last_broadcast_time = millis();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:18:50 +01:00
|
|
|
if (millis() > last_print_time + 5000) {
|
2026-04-06 03:39:08 +01:00
|
|
|
last_print_time = millis();
|
2026-04-06 00:27:32 +01:00
|
|
|
|
|
|
|
|
std::optional<Eigen::Vector3f> coords;
|
|
|
|
|
float lat, lon, alt;
|
2026-04-07 10:48:44 +01:00
|
|
|
bool gps_values = false;
|
|
|
|
|
bool fix = false;
|
|
|
|
|
uint8_t sat_count = 0;
|
|
|
|
|
if (gps_mutex && xSemaphoreTake(gps_mutex, (TickType_t)20) == pdTRUE) {
|
2026-04-06 00:27:32 +01:00
|
|
|
if (gps->gps_avaliable()) {
|
|
|
|
|
coords = gps->get_coordinates();
|
|
|
|
|
lat = gps->gps->latitudeDegrees;
|
|
|
|
|
lon = gps->gps->longitudeDegrees;
|
|
|
|
|
alt = gps->gps->altitude;
|
2026-04-07 10:48:44 +01:00
|
|
|
gps_values = true;
|
2026-04-06 00:27:32 +01:00
|
|
|
}
|
2026-04-07 10:48:44 +01:00
|
|
|
sat_count = gps->gps->satellites;
|
|
|
|
|
fix = gps->gps->fix;
|
2026-04-06 00:27:32 +01:00
|
|
|
xSemaphoreGive(gps_mutex);
|
|
|
|
|
|
2026-04-07 10:48:44 +01:00
|
|
|
if (gps_values) {
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG,
|
|
|
|
|
"loc -> lat: %f, long: %f, height: %f, sat_c: %d, fix: %b",
|
|
|
|
|
lat, lon, alt, sat_count, fix);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 00:27:32 +01:00
|
|
|
if (coords.has_value()) {
|
|
|
|
|
auto D_pos = coords.value();
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, " -> D(pos): (%f, %f, %f)", D_pos[0], D_pos[1],
|
|
|
|
|
D_pos[2]);
|
|
|
|
|
}
|
2026-04-04 02:39:41 +01:00
|
|
|
}
|
2026-04-06 00:27:32 +01:00
|
|
|
env_sens::dbg_sens();
|
|
|
|
|
|
|
|
|
|
if (sens_fus_mutex &&
|
|
|
|
|
xSemaphoreTake(sens_fus_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
|
|
|
|
|
local_pos = sens_fus.position;
|
|
|
|
|
local_vel = sens_fus.velocity;
|
|
|
|
|
nav_data_ready = true;
|
|
|
|
|
xSemaphoreGive(sens_fus_mutex);
|
2026-03-26 23:01:20 +00:00
|
|
|
}
|
2026-03-30 22:09:23 +01:00
|
|
|
|
2026-04-06 00:27:32 +01:00
|
|
|
if (nav_data_ready) {
|
|
|
|
|
ESP_LOGI(TAG, "nav(pos): (%f, %f, %f)", local_pos[0], local_pos[1],
|
|
|
|
|
local_pos[2]);
|
|
|
|
|
ESP_LOGI(TAG, "nav(vel): (%f, %f, %f)", local_vel[0], local_vel[1],
|
|
|
|
|
local_vel[2]);
|
|
|
|
|
}
|
2026-04-07 10:48:44 +01:00
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Throttles: (%f, %f, %f, %f)", motor_throttles[0],
|
|
|
|
|
motor_throttles[1], motor_throttles[2], motor_throttles[3]);
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Controller: (%f, %f), (%f, %f)",
|
|
|
|
|
current_controller_input.lx, current_controller_input.ly,
|
|
|
|
|
current_controller_input.rx, current_controller_input.ry);
|
2026-03-30 22:09:23 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-14 16:00:52 +01:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(50));
|
2026-03-26 23:01:20 +00:00
|
|
|
}
|
2026-03-16 00:49:02 +00:00
|
|
|
}
|