From 5f9bb289cd9c3529570a0cd5aca403754882fef8 Mon Sep 17 00:00:00 2001 From: franchioping Date: Sun, 12 Apr 2026 16:52:29 +0100 Subject: [PATCH] wip --- main/drone.cpp | 8 +++++--- main/drone.h | 4 ++-- main/env_sens.cpp | 22 +++++++++++++++++++--- main/gps.h | 4 ++-- main/imu.cpp | 14 +++++++++++--- main/main.cpp | 34 +++++++++++++++++----------------- main/radio.cpp | 21 ++++++++++++--------- 7 files changed, 68 insertions(+), 39 deletions(-) diff --git a/main/drone.cpp b/main/drone.cpp index 449f6c1..5545b63 100644 --- a/main/drone.cpp +++ b/main/drone.cpp @@ -148,21 +148,23 @@ void drone_controller_task(void *params) { } } -const gpio_num_t motor_pins[4] = {GPIO_NUM_15, GPIO_NUM_NC, GPIO_NUM_NC, - GPIO_NUM_NC}; +const gpio_num_t motor_pins[4] = {GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, + GPIO_NUM_46}; void motor_throttles_task(void *params) { DShotRMT *motors[4]; for (int i = 0; i < 4; i++) { motors[i] = new DShotRMT(motor_pins[i], DSHOT150, false); } + motors[0]->sendCommand(3); // ARM unsigned long armTime = millis(); - while (millis() - armTime < 4000) { + while (millis() - armTime < 5000) { for (int i = 0; i < 4; i++) { motors[i]->sendThrottlePercent(0); } + vTaskDelay(1); } uint8_t wait_ms = 1000.0 / CONTROLLER_TASK_FREQUENCY; diff --git a/main/drone.h b/main/drone.h index da3c505..0005ce8 100644 --- a/main/drone.h +++ b/main/drone.h @@ -24,5 +24,5 @@ void drone_controller_task(void *params); void motor_throttles_task(void *params); -inline float motor_throttles[4] = {0.5, 0.5, 0.5, 0.5}; -inline bool killswitch_active; +inline float motor_throttles[4] = {0.05, 0.05, 0.05, 0.05}; +inline bool killswitch_active = false; diff --git a/main/env_sens.cpp b/main/env_sens.cpp index 690f3d4..0fa5014 100644 --- a/main/env_sens.cpp +++ b/main/env_sens.cpp @@ -18,12 +18,19 @@ Adafruit_Sensor *bme_humidity = bme.getHumiditySensor(); static const constexpr char *TAG = "BARO"; +#define BARO_SDA GPIO_NUM_47 +#define BARO_SCL GPIO_NUM_48 + namespace env_sens { void setup() { baro_mutex = xSemaphoreCreateMutex(); - if (!bme.begin()) { + TwoWire *wire = new TwoWire(0); + + wire->begin(BARO_SDA, BARO_SCL); + + if (!bme.begin(119, wire)) { ESP_LOGE(TAG, "Couldn't find a valid sensor"); @@ -43,14 +50,23 @@ void setup() { float get_temperature() { sensors_event_t temp_event; - bme_temp->getEvent(&temp_event); + if (baro_mutex && xSemaphoreTake(baro_mutex, 30)) { + + bme_temp->getEvent(&temp_event); + xSemaphoreGive(baro_mutex); + } + return temp_event.temperature; } float get_pressure() { sensors_event_t e; - bme_pressure->getEvent(&e); + if (baro_mutex && xSemaphoreTake(baro_mutex, 30)) { + + bme_pressure->getEvent(&e); + xSemaphoreGive(baro_mutex); + } return e.pressure; } diff --git a/main/gps.h b/main/gps.h index 90c58d8..7b1a6d5 100644 --- a/main/gps.h +++ b/main/gps.h @@ -21,7 +21,7 @@ const float TO_RAD = M_PI / 180.0f; const float KNOTS_TO_M_SEC = 0.5144444f; const float earth_radius = 6371000.0f; -#define GPS_RX_PIN 16 +#define GPS_RX_PIN 18 #define GPS_TX_PIN 17 struct GPS { @@ -94,7 +94,7 @@ struct GPS { char c = this->gps->read(); if (this->gps->newNMEAreceived()) { char *line = this->gps->lastNMEA(); - // ESP_LOGI("GPS", "NMEA LINE: %s", line); + ESP_LOGI("GPS", "NMEA LINE: %s", line); this->gps->parse(line); } } diff --git a/main/imu.cpp b/main/imu.cpp index 154ec8c..ed4d7da 100644 --- a/main/imu.cpp +++ b/main/imu.cpp @@ -20,13 +20,21 @@ static const char *TAG = "IMU"; +#define IMU_CS GPIO_NUM_3 +#define IMU_MOSI GPIO_NUM_2 // DI +#define IMU_RST GPIO_NUM_1 + +#define IMU_INT GPIO_NUM_4 +#define IMU_MISO GPIO_NUM_5 // SDA +#define IMU_SCLK GPIO_NUM_6 // SCL + void setup_imu() { imu_state *local_state = new imu_state; imu_state_mutex = xSemaphoreCreateMutex(); - BNO08x *imu = new BNO08x(bno08x_config_t( - SPI2_HOST, GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_25, // TODO: FIX - GPIO_NUM_33, GPIO_NUM_36, GPIO_NUM_32, 2000000, false)); + BNO08x *imu = + new BNO08x(bno08x_config_t(SPI2_HOST, IMU_MOSI, IMU_MISO, IMU_SCLK, + IMU_CS, IMU_INT, IMU_RST, 2000000, false)); if (!imu->initialize()) { ESP_LOGE(TAG, "BNO08x Init failure."); diff --git a/main/main.cpp b/main/main.cpp index 40b244a..7984015 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -30,15 +30,15 @@ extern "C" void app_main(void) { gpio_install_isr_service(0); Serial.begin(115200); - 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 - ); - // + // 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 + // ); + // // xTaskCreate(env_sens::baro_poll_task, "baro_poll", 8192, NULL, 1, NULL); // xTaskCreate(gps_poll_task, "gps_poll", 8192, NULL, 5, NULL); @@ -52,14 +52,14 @@ extern "C" void app_main(void) { // 1 // Core ID // ); - // xTaskCreatePinnedToCore(motor_throttles_task, // Function name - // "motor_throttles_task", // Name for debugging - // 1024 * 4, // Stack size in bytes - // NULL, // Parameters - // 30, // Priority (higher = more urgent) - // NULL, // Task handle - // 1 // Core ID - // ); + xTaskCreatePinnedToCore(motor_throttles_task, // Function name + "motor_throttles_task", // Name for debugging + 1024 * 4, // Stack size in bytes + NULL, // Parameters + 30, // Priority (higher = more urgent) + NULL, // Task handle + 1 // Core ID + ); ESP_LOGI("MAIN", "All tasks spawned. Main loop free."); diff --git a/main/radio.cpp b/main/radio.cpp index 2a99a4a..1b9aae8 100644 --- a/main/radio.cpp +++ b/main/radio.cpp @@ -2,6 +2,7 @@ #include "Esp.h" #include "esp32-hal-gpio.h" +#include "esp32-hal-spi.h" #include "esp_log.h" #include "freertos/idf_additions.h" #include "packet_handler.h" @@ -12,12 +13,14 @@ #include -#define RFM69_RST 14 -#define RFM69_CS 12 -#define RFM69_INT 39 -#define SPI_SCLK 18 -#define SPI_MISO 19 -#define SPI_MOSI 23 +// Right to left on hardware. + +#define RFM69_MOSI GPIO_NUM_11 +#define RFM69_SCLK GPIO_NUM_12 +#define RFM69_MISO GPIO_NUM_13 +#define RFM69_CS GPIO_NUM_10 +#define RFM69_INT GPIO_NUM_9 +#define RFM69_RST GPIO_NUM_8 #define FREQUENCY RF69_433MHZ #define NODEID 1 @@ -43,8 +46,8 @@ void radio_task(void *pvParameters) { pinMode(RFM69_CS, OUTPUT); pinMode(RFM69_INT, INPUT); - SPIClass vspi(VSPI); - vspi.begin(SPI_SCLK, SPI_MISO, SPI_MOSI, 34); + SPIClass hspi(HSPI); + hspi.begin(RFM69_SCLK, RFM69_MISO, RFM69_MOSI, RFM69_CS); pinMode(RFM69_RST, OUTPUT); digitalWrite(RFM69_RST, HIGH); @@ -52,7 +55,7 @@ void radio_task(void *pvParameters) { digitalWrite(RFM69_RST, LOW); vTaskDelay(pdMS_TO_TICKS(50)); - RFM69 radio(RFM69_CS, RFM69_INT, true, &vspi); + RFM69 radio(RFM69_CS, RFM69_INT, true, &hspi); if (radio.initialize(FREQUENCY, NODEID, NETWORKID)) { radio.setHighPower(true);