Skip to content

Commit

Permalink
Merge pull request #109 from jgauchia/v.0.1.8_dev
Browse files Browse the repository at this point in the history
V.0.1.8 dev - IMU
  • Loading branch information
jgauchia committed Apr 24, 2024
2 parents 808cac8 + 7e06225 commit d5ef10a
Show file tree
Hide file tree
Showing 109 changed files with 6,206 additions and 5,747 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ For the moment we have two Icenav models, with the next hardware setup and specs
RST GPIO32
CS GPIO2
LED GPIO33
TCH GPIO18
TCS GPIO18
TIRQ GPIO5

Pinout (ESP32S3-MakerFabs)
Expand Down Expand Up @@ -142,6 +142,17 @@ If the GPS module supports multiple GNSS, uncomment the following flag in the pl
-D MULTI_GNSS=1
```

### TO DO

- [ ] LVGL 9 Integration
- [ ] GPX Integration
- [ ] Multiple IMU's and Compass module implementation
- [ ] Power saving
- [ ] Vector maps
- [ ] Google Maps navigation style
- [ ] Optimize code
- [ ] Fix bugs!

## Credits

* Added support to [Makerfabs ESP32-S3 Parallel TFT with Touch 3.5" ILI9488](https://www.makerfabs.com/esp32-s3-parallel-tft-with-touch-ili9488.html) (thanks to [@hpsaturn](https://github.com/hpsaturn))
Expand Down
Binary file modified data/altit.bin
Binary file not shown.
Binary file modified data/arrow.bin
Binary file not shown.
Binary file modified data/delete.bin
Binary file not shown.
Binary file modified data/load.bin
Binary file not shown.
Binary file modified data/pin.bin
Binary file not shown.
Binary file modified data/sat.bin
Binary file not shown.
Binary file modified data/save.bin
Binary file not shown.
Binary file modified data/settings.bin
Binary file not shown.
Binary file modified data/speed.bin
Binary file not shown.
Binary file modified data/track.bin
Binary file not shown.
Binary file modified data/wpt.bin
Binary file not shown.
62 changes: 62 additions & 0 deletions include/hal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @file hal.hpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief Pin definitions
* @version 0.1.8
* @date 2024-04
*/

#ifndef HAL_HPP
#define HAL_HPP

#ifdef CUSTOMBOARD
/**
* @brief GPS pin definition
*
*/
extern const uint8_t GPS_TX = 25;
extern const uint8_t GPS_RX = 26;

/**
* @brief SD pin definition
*
*/
extern const uint8_t SD_CS = 4;
extern const uint8_t SD_MISO = 19;
extern const uint8_t SD_MOSI = 23;
extern const uint8_t SD_CLK = 12;
#endif

#ifdef MAKERF_ESP32S3 // TODO: we need find the right pins for this board
/**
* @brief GPS pin definition
*
*/
extern const uint8_t GPS_TX = 17;
extern const uint8_t GPS_RX = 18;

#define LCD_CS 37
#define LCD_BLK 45

#define I2C_SDA_PIN 38
#define I2C_SCL_PIN 39

extern const uint8_t SD_CS = 1;
extern const uint8_t SD_MISO = 41;
extern const uint8_t SD_MOSI = 2;
extern const uint8_t SD_CLK = 42;
#endif

/**
* @brief Battery monitor pin
*
*/
#define ADC_BATT_PIN 34

/**
* @brief BME280 Address
*
*/
#define BME_ADDRESS 0x76

#endif
27 changes: 9 additions & 18 deletions src/hardware/battery.h → lib/battery/battery.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
/**
* @file battery.h
* @author Jordi Gauchía (jgauchia@jgauchia.com)
* @file battery.cpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief Battery monitor definition and functions
* @version 0.1.6
* @date 2023-06-14
* @version 0.1.8
* @date 2024-04
*/

#include <driver/adc.h>
#include <esp_adc_cal.h>

uint8_t batt_level = 0;
uint8_t batt_level_old = 0;

esp_adc_cal_characteristics_t characteristics;
#define V_REF 3.9 // ADC reference voltage

float battery_max = 4.20; // 4.2; // maximum voltage of battery
float battery_min = 3.40; // 3.6; // minimum voltage of battery before shutdown
#include "battery.hpp"

/**
* @brief Configurate ADC Channel for battery reading
*
*/
void init_ADC()
void initADC()
{
// When VDD_A is 3.3V:
// 0dB attenuaton (ADC_ATTEN_DB_0) gives full-scale voltage 1.1V
Expand All @@ -38,7 +29,7 @@ void init_ADC()
*
* @return float -> % Charge
*/
float battery_read()
float batteryRead()
{
long sum = 0; // sum of samples taken
float voltage = 0.0; // calculated voltage
Expand All @@ -55,9 +46,9 @@ float battery_read()
voltage = (voltage * V_REF) / 4096.0;
voltage = voltage / (R2 / (R1 + R2));
voltage = roundf(voltage * 100) / 100;
output = ((voltage - battery_min) / (battery_max - battery_min)) * 100;
output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100;
if (output <= 160)
return output;
else
return 0.0f;
}
}
25 changes: 25 additions & 0 deletions lib/battery/battery.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file battery.hpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief Battery monitor definition and functions
* @version 0.1.8
* @date 2024-04
*/

#ifndef BATTERY_HPP
#define BATTERY_HPP

#include <Arduino.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>

static esp_adc_cal_characteristics_t characteristics;
#define V_REF 3.9 // ADC reference voltage

static float batteryMax = 4.20; // 4.2; // maximum voltage of battery
static float batteryMin = 3.40; // 3.6; // minimum voltage of battery before shutdown

void initADC();
float batteryRead();

#endif
11 changes: 11 additions & 0 deletions lib/bme/bme.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @file bme.cpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief BME280 Sensor functions
* @version 0.1.8
* @date 2024-04
*/

#include "bme.hpp"

Adafruit_BME280 bme = Adafruit_BME280();
17 changes: 17 additions & 0 deletions lib/bme/bme.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file bme.hpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief BME280 Sensor functions
* @version 0.1.8
* @date 2024-04
*/

#ifndef BME_HPP
#define BME_HPP

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

extern Adafruit_BME280 bme;

#endif
95 changes: 44 additions & 51 deletions src/hardware/compass.h → lib/compass/compass.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
/**
* @file compass.h
* @author Jordi Gauchía (jgauchia@jgauchia.com)
* @file compass.cpp
* @author Jordi Gauchía (jgauchia@gmx.es)
* @brief Compass definition and functions
* @version 0.1.6
* @date 2023-06-14
* @version 0.1.8
* @date 2024-04
*/

#ifdef CUSTOMBOARD
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include "compass.hpp"

#ifdef CUSTOMBOARD
Adafruit_HMC5883_Unified compass = Adafruit_HMC5883_Unified(12345);
#endif

#ifdef MAKERF_ESP32S3
#include <MPU9250.h>

MPU9250 IMU(Wire, 0x68);
#endif

#define COMPASS_CAL_TIME 16000
static void save_compass_cal(float offset_x, float offset_y);
// #ifdef MAKERF_ESP32S3
// MPU9250 IMU(Wire, 0x68);
// #endif

/**
* @brief Magnetic declination
Expand All @@ -32,28 +26,27 @@ static void save_compass_cal(float offset_x, float offset_y);
// Substitute your magnetic declination for the "declinationAngle" shown below.
float declinationAngle = 0.22;


/**
* @brief Compass Heading Angle and Smooth factors
*
*/
int heading = 0;
int map_heading = 0;
float heading_smooth = 0.0;
float heading_previous = 0.0;
int mapHeading = 0;
#define SMOOTH_FACTOR 0.40
#define SMOOTH_PREVIOUS_FACTOR 0.60

/**
* @brief Calibration variables
*
* @brief Compass offset calibration
*
*/
float minx, maxx, miny, maxy;
float offX = 0.0, offY = 0.0;

/**
* @brief Init Compass
*
*/
void init_compass()
void initCompass()
{

#ifdef CUSTOMBOARD
Expand All @@ -77,7 +70,7 @@ void init_compass()
* @param y
* @param z
*/
static void read_compass(float &x, float &y, float &z)
void readCompass(float &x, float &y, float &z)
{
#ifdef CUSTOMBOARD
sensors_event_t event;
Expand All @@ -100,32 +93,32 @@ static void read_compass(float &x, float &y, float &z)
*
* @return compass heading
*/
int get_heading()
int getHeading()
{
float y = 0.0;
float x = 0.0;
float z = 0.0;

read_compass(x, y, z);
readCompass(x, y, z);

float heading_no_filter = atan2(y - offy, x - offx);
heading_no_filter += declinationAngle;
heading_smooth = heading_no_filter;
// heading_smooth = (heading_no_filter * SMOOTH_FACTOR) + (heading_previous * SMOOTH_PREVIOUS_FACTOR);
// heading_previous = heading_smooth;
float headingNoFilter = atan2(y - offY, x - offX);
headingNoFilter += declinationAngle;
headingSmooth = headingNoFilter;
// headingSmooth = (headingNoFilter * SMOOTH_FACTOR) + (headingPrevious * SMOOTH_PREVIOUS_FACTOR);
// headingPrevious = headingSmooth;

if (heading_smooth < 0)
heading_smooth += 2 * M_PI;
if (heading_smooth > 2 * M_PI)
heading_smooth -= 2 * M_PI;
return (int)(heading_smooth * 180 / M_PI);
if (headingSmooth < 0)
headingSmooth += 2 * M_PI;
if (headingSmooth > 2 * M_PI)
headingSmooth -= 2 * M_PI;
return (int)(headingSmooth * 180 / M_PI);
}

/**
* @brief Compass calibration
*
*/
static void compass_calibrate()
void compassCalibrate()
{
bool cal = 1;
float y = 0.0;
Expand All @@ -145,24 +138,24 @@ static void compass_calibrate()

unsigned long calTimeWas = millis();

read_compass(x, y, z);
readCompass(x, y, z);

maxx = minx = x; // Set initial values to current magnetometer readings.
maxy = miny = y;
maxX = minX = x; // Set initial values to current magnetometer readings.
maxY = minY = y;

while (cal)
{

read_compass(x, y, z);
readCompass(x, y, z);

if (x > maxx)
maxx = x;
if (x < minx)
minx = x;
if (y > maxy)
maxy = y;
if (y < miny)
miny = y;
if (x > maxX)
maxX = x;
if (x < minX)
minX = x;
if (y > maxY)
maxY = y;
if (y < minY)
minY = y;

int secmillis = millis() - calTimeWas;
int secs = (int)((COMPASS_CAL_TIME - secmillis + 1000) / 1000);
Expand All @@ -173,8 +166,8 @@ static void compass_calibrate()

if (secs == 0)
{
offx = (maxx + minx) / 2;
offy = (maxy + miny) / 2;
offX = (maxX + minX) / 2;
offY = (maxY + minY) / 2;
cal = 0;
}
}
Expand All @@ -187,5 +180,5 @@ static void compass_calibrate()
{
};

save_compass_cal(offx,offy);
saveCompassCal(offX,offY);
}
Loading

0 comments on commit d5ef10a

Please sign in to comment.