Merge pull request #126 from Genaker/rtl-sdr

Compass ADD
This commit is contained in:
Yegor Shytikov
2025-01-11 23:15:12 -08:00
committed by GitHub
4 changed files with 196 additions and 1 deletions
+8
View File
@@ -336,6 +336,14 @@ Video Out - Video IN <br />
#define OSD_MOSI 34
#define OSD_SCK 26
```
FOR LYLIGO board
```
#define OSD_SCK 38
#define OSD_CS 39
#define OSD_MISO 40
#define OSD_MOSI 41
```
</details>
+2
View File
@@ -25,6 +25,8 @@
#ifdef LILYGO
#define BUZZER_PIN 45
#define COMPASS_SCL 42
#define COMPASS_SDA 46
#endif
#ifdef T3_V1_6_SX1276
#define BUZZER_PIN 35
+32
View File
@@ -248,6 +248,38 @@ build_flags =
-DARDUINO_LILYGO_T3_S3_V1_X
-DARDUINO_USB_MODE=1
[env:lilygo-T3S3-v1-2-lr1121-900-compass]
platform = espressif32
board = t3_s3_v1_x
framework = arduino
upload_speed = 921600
monitor_speed = 115200
board_build.f_cpu = 240000000
board_build.filesystem = littlefs
lib_deps =
ropg/Heltec_ESP32_LoRa_v3@^0.9.1
RadioLib
U8g2
XPowersLib
ESP Async WebServer
https://github.com/Genaker/Adafruit_HMC5883_Unified
build_flags =
-DLILYGO
-DT3_S3_V1_2_LR1121
-DARDUINO_LILYGO_T3S3_LR1121
-DESP32
-DSAMPLES_RSSI=5
-DUSING_LR1121
-DINIT_FREQ=900
-DFREQ_BEGIN=800
-DFREQ_END=950
-DFREQ_RX=2440
-DARDUINO_ARCH_ESP32
-DARDUINO_USB_CDC_ON_BOOT=1
-DARDUINO_LILYGO_T3_S3_V1_X
-DARDUINO_USB_MODE=1
-DCOMPASS_ENABLED
[env:lilygo-T3S3-v1-2-sx1280]
platform = espressif32
board = t3_s3_v1_x
+154 -1
View File
@@ -29,6 +29,7 @@
#include <ESPAsyncWebServer.h>
#include <File.h>
#include <LittleFS.h>
#include <Wire.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <unordered_map>
@@ -112,13 +113,21 @@ unsigned int osdCyclesCount = 0;
#define SIDEBAR_DB_LEVEL 80 // Absolute value without minus
#define SIDEBAR_DB_DELTA 2 // detect changes <> threshold
#ifdef LILYGO
#define OSD_SCK 38
#define OSD_CS 39
#define OSD_MISO 40
#define OSD_MOSI 41
#else
// SPI pins
#define OSD_SCK 26
#define OSD_CS 47
#define OSD_MISO 33
#define OSD_MOSI 34
#define OSD_SCK 26
#endif
#endif // End OSD_ENABLED
#define OSD_WIDTH 30
#define OSD_HEIGHT 16
#define OSD_CHART_WIDTH 15
@@ -152,6 +161,41 @@ DFRobot_OSD osd(OSD_CS);
#include "global_config.h"
#include "ui.h"
#ifdef COMPASS_ENABLED
#include <Adafruit_HMC5883_U.h>
#include <Adafruit_Sensor.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print("Sensor: ");
Serial.println(sensor.name);
Serial.print("Driver Ver: ");
Serial.println(sensor.version);
Serial.print("Unique ID: ");
Serial.println(sensor.sensor_id);
Serial.print("Max Value: ");
Serial.print(sensor.max_value);
Serial.println(" uT");
Serial.print("Min Value: ");
Serial.print(sensor.min_value);
Serial.println(" uT");
Serial.print("Resolution: ");
Serial.print(sensor.resolution);
Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
// Variables for dynamic calibration
float x_min = 1000, x_max = -1000;
float y_min = 1000, y_max = -1000;
float z_min = 1000, z_max = -1000;
#endif
// -----------------------------------------------------------------
// CONFIGURATION OPTIONS
// -----------------------------------------------------------------
@@ -1462,6 +1506,26 @@ void setup(void)
r.addEventListener(ALL_EVENTS, eventListenerForReport, NULL);
#ifdef COMPASS_ENABLED
Serial.println("Compass Init Start");
Wire1.end();
Wire1.begin(46, 42);
Serial.println("Compass BEGIN");
Serial.println("HMC5883 Magnetometer Test");
/* Initialise the sensor */
if (!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
}
/* Display some basic information on this sensor */
displaySensorDetails();
Serial.println("Compass Success!!!");
#endif
#ifdef UPTIME_CLOCK
uptime = new UptimeClock(display, millis());
#endif
@@ -1970,6 +2034,95 @@ void loop(void)
doScan();
reportScan();
}
#ifdef COMPASS_ENABLED
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
#ifdef COMPASS_DEBUG
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: ");
Serial.print(event.magnetic.x);
Serial.print(" ");
Serial.print("Y: ");
Serial.print(event.magnetic.y);
Serial.print(" ");
Serial.print("Z: ");
Serial.print(event.magnetic.z);
Serial.print(" ");
Serial.println("uT");
#endif
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
// float heading = atan2(event.magnetic.y, event.magnetic.x);
// Use Y as the forward axis
// float heading = atan2(event.magnetic.x, event.magnetic.y);
/// If Z-axis is forward and Y-axis points upward:
// float heading = atan2(event.magnetic.x, event.magnetic.y);
// If Z-axis is forward and X-axis points upward:
// float heading = atan2(event.magnetic.y, -event.magnetic.x);
// heading based on the magnetic readings from the Z-axis (forward) and the X-axis
// (perpendicular to Z, horizontal).
// float heading = atan2(event.magnetic.z, event.magnetic.x);
// Dynamicly Calibrated out
// Read raw magnetometer data
float x = event.magnetic.x;
float y = event.magnetic.y;
float z = event.magnetic.z;
// Update min/max values dynamically
x_min = min(x_min, x);
x_max = max(x_max, x);
y_min = min(y_min, y);
y_max = max(y_max, y);
z_min = min(z_min, z);
z_max = max(z_max, z);
// Calculate offsets and scales in real-time
float x_offset = (x_max + x_min) / 2;
float y_offset = (y_max + y_min) / 2;
float z_offset = (z_max + z_min) / 2;
float x_scale = (x_max - x_min) / 2;
float y_scale = (y_max - y_min) / 2;
float z_scale = (z_max - z_min) / 2;
// Apply calibration to raw data
float calibrated_x = (x - x_offset) / x_scale;
float calibrated_y = (y - y_offset) / y_scale;
float calibrated_z = (z - z_offset) / z_scale;
// Calculate heading using Z-axis forward, X-axis horizontal
float heading = atan2(calibrated_z, calibrated_x);
// Once you have your heading, you must then add your 'Declination Angle', which is
// the 'Error' of the magnetic field in your location. Find yours here:
// http://www.magnetic-declination.com/ Mine is: -13* 2' W, which is ~13 Degrees, or
// (which we need) 0.22 radians If you cannot find your Declination, comment out these
// two lines, your compass will be slightly off.
float declinationAngle = 0.22;
heading += declinationAngle;
// Correct for when signs are reversed.
if (heading < 0)
heading += 2 * PI;
// Check for wrap due to addition of declination.
if (heading > 2 * PI)
heading -= 2 * PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180 / M_PI;
Serial.println("Heading (degrees): " + String(headingDegrees));
/// Serial.println();
display.drawString(80, 0, String((int)headingDegrees));
display.display();
#endif
}
void doScan()