ESP32-CAN/main/main.cpp

93 lines
2.8 KiB
C++
Raw Normal View History

2026-03-30 22:09:23 +01:00
#include "driver/gpio.h"
#include "drone_comms.h"
#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-03-30 22:09:23 +01:00
#include "freertos/task.h"
#include "imu.h"
#include <Arduino.h>
#include "env_sens.h"
2026-03-26 23:01:20 +00:00
#include "gps.h"
2026-03-30 22:09:23 +01:00
#include "nav.h"
#include "radio.h"
2026-03-16 00:49:02 +00:00
2026-03-30 22:09:23 +01:00
static const char *TAG = "MAIN";
2026-03-30 22:09:23 +01:00
extern "C" void app_main(void) {
2026-03-30 22:09:23 +01:00
nav_mutex = xSemaphoreCreateMutex();
initArduino();
gpio_install_isr_service(0);
Serial.begin(115200);
2026-03-26 23:01:20 +00:00
2026-03-30 22:09:23 +01:00
static imu_state imu_state;
auto _ = setup_imu(&imu_state);
2026-03-26 23:01:20 +00:00
2026-03-30 22:09:23 +01:00
// xTaskCreatePinnedToCore(radio_rx_task, // Function name
// "radio_rx", // Name for debugging
// 4096, // Stack size in bytes
// NULL, // Parameters
// 1, // Priority (higher = more urgent)
// NULL, // Task handle
// 1 // Core ID
// );
//
xTaskCreate(env_sens::baro_poll_task, "baro_poll", 8192, NULL, 1, NULL);
xTaskCreate(gps_poll_task, "gps_poll", 8192, NULL, 1, NULL);
2026-03-26 23:01:20 +00:00
2026-03-30 22:09:23 +01:00
ESP_LOGI("MAIN", "All tasks spawned. Main loop free.");
Eigen::Vector3f local_pos = {0, 0, 0};
Eigen::Vector3f local_vel = {0, 0, 0};
bool nav_data_ready = false;
2026-03-26 23:01:20 +00:00
while (true) {
2026-03-30 22:09:23 +01:00
// if (controller_input_semaphore &&
// xSemaphoreTake(controller_input_semaphore, (TickType_t)30) == pdTRUE)
// {
// inp = current_controller_input;
// xSemaphoreGive(controller_input_semaphore);
//
// ESP_LOGI(TAG, "CONT INPUT: (%f, %f), (%f,%f)", inp.lx, inp.ly, inp.rx,
// inp.ry);
// }
if (gps_mutex && xSemaphoreTake(gps_mutex, (TickType_t)10) == pdTRUE) {
2026-03-26 23:01:20 +00:00
if (gps->gps_avaliable()) {
2026-03-30 22:09:23 +01:00
ESP_LOGI(TAG, "loc -> lat: %f, long: %f, height: %f",
gps->gps->latitudeDegrees, gps->gps->longitudeDegrees,
gps->gps->altitude);
auto D_pos_cond = gps->get_coordinates();
if (D_pos_cond.has_value()) {
auto D_pos = D_pos_cond.value();
2026-03-26 23:01:20 +00:00
2026-03-30 22:09:23 +01:00
ESP_LOGI(TAG, " -> D(pos): (%f, %f, %f)", D_pos[0], D_pos[1],
D_pos[2]);
}
2026-03-26 23:01:20 +00:00
}
xSemaphoreGive(gps_mutex);
}
2026-03-30 22:09:23 +01:00
env_sens::dbg_sens();
if (nav_mutex && xSemaphoreTake(nav_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
local_pos = nav_filter.position;
local_vel = nav_filter.velocity;
nav_data_ready = true;
xSemaphoreGive(nav_mutex); // RELEASE IMMEDIATELY
}
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]);
}
vTaskDelay(pdMS_TO_TICKS(1000));
2026-03-26 23:01:20 +00:00
}
2026-03-16 00:49:02 +00:00
}