Skip to content

Commit

Permalink
Merge pull request #83 from jgauchia/35-compass-calibration
Browse files Browse the repository at this point in the history
35 compass calibration

* Update SPIFFS filesystem
  • Loading branch information
jgauchia authored Jul 14, 2023
2 parents 1c3fae5 + 1507190 commit 2e9ba0e
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 95 deletions.
Binary file added data/turn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion lib/lv_CUSTOMBOARD_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
#define LV_DISP_DEF_REFR_PERIOD 30 /*[ms]*/

/*Input device read period in milliseconds*/
#define LV_INDEV_DEF_READ_PERIOD 30 /*[ms]*/
#define LV_INDEV_DEF_READ_PERIOD 60 /*[ms]*/

/*Use a custom tick source that tells the elapsed time in milliseconds.
*It removes the need to manually update the tick with `lv_tick_inc()`)*/
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default_envs = CUSTOMBOARD
platform = espressif32
framework = arduino
version = 0.1.6
revision = 35
revision = 36
monitor_speed = 115200
monitor_rts = 0
monitor_dtr = 0
Expand Down
2 changes: 1 addition & 1 deletion src/gui/screens/Main/events/main_scr.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static void update_main_screen(lv_timer_t *t)
{
case COMPASS:
#ifdef ENABLE_COMPASS
heading = read_compass();
heading = get_heading();
lv_event_send(compass_heading, LV_EVENT_VALUE_CHANGED, NULL);
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/gui/screens/Main/events/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void update_map(lv_event_t *event)
map_rot.pushSprite(0, 27);

#ifdef ENABLE_COMPASS
heading = read_compass();
heading = get_heading();
map_spr.pushRotated(&map_rot, 360 - heading, TFT_TRANSPARENT);
map_rot.fillRectAlpha(TFT_WIDTH - 48, 0, 48, 48, 95, TFT_BLACK);
map_rot.pushImageRotateZoom(TFT_WIDTH - 24, 24, 24, 24, 360 - heading, 1, 1, 48, 48, (uint16_t *)mini_compass, TFT_BLACK);
Expand Down
66 changes: 24 additions & 42 deletions src/gui/screens/Notify_Bar/events/notify_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ static void update_batt(lv_event_t *event)
*/
static void update_fix_mode(lv_event_t *event)
{
switch (atoi(fix_mode.value()))
lv_obj_t *mode = lv_event_get_target(event);
if (fix_mode.isValid())
{
case 1:
lv_label_set_text(gps_fix_mode, "--");
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", 0);
break;
case 2:
lv_label_set_text(gps_fix_mode, "2D");
break;
case 3:
lv_label_set_text(gps_fix_mode, "3D");
break;
default:
lv_label_set_text(gps_fix_mode, "--");
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", 0);
break;
switch (atoi(fix_mode.value()))
{
case 1:
lv_label_set_text(mode, "--");
break;
case 2:
lv_label_set_text(mode, "2D");
break;
case 3:
lv_label_set_text(mode, "3D");
break;
default:
lv_label_set_text(mode, "--");
break;
}
}
}

Expand All @@ -77,7 +79,11 @@ static void update_time(lv_event_t *event)
*/
static void update_gps_count(lv_event_t *event)
{
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", GPS.satellites.value());
lv_obj_t *gps_num = lv_event_get_target(event);
if (GPS.satellites.isValid())
lv_label_set_text_fmt(gps_num, LV_SYMBOL_GPS "%2d", GPS.satellites.value());
else
lv_label_set_text_fmt(gps_num, LV_SYMBOL_GPS "%2d", 0);
}

/**
Expand All @@ -87,32 +93,8 @@ static void update_gps_count(lv_event_t *event)
void update_notify_bar(lv_timer_t *t)
{
lv_event_send(gps_time, LV_EVENT_VALUE_CHANGED, NULL);

lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", GPS.satellites.value());

switch (atoi(fix.value()))
{
case 0:
lv_led_off(gps_fix);
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", 0);
break;
case 1:
lv_led_toggle(gps_fix);
break;
case 2:
lv_led_toggle(gps_fix);
break;
default:
lv_led_off(gps_fix);
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", 0);
break;
}

if (atoi(fix_mode.value()) != fix_mode_old)
{
lv_event_send(gps_fix_mode, LV_EVENT_VALUE_CHANGED, NULL);
fix_mode_old = atoi(fix_mode.value());
}
lv_event_send(gps_count, LV_EVENT_VALUE_CHANGED, NULL);
lv_event_send(gps_fix_mode, LV_EVENT_VALUE_CHANGED, NULL);

batt_level = battery_read();
if (batt_level != batt_level_old)
Expand Down
6 changes: 1 addition & 5 deletions src/gui/screens/Notify_Bar/notify_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ void create_notify_bar()

gps_count = lv_label_create(notifyBar);
lv_label_set_text_fmt(gps_count, LV_SYMBOL_GPS "%2d", 0);
lv_obj_set_width(gps_count,40);
lv_obj_add_event_cb(gps_count, update_gps_count, LV_EVENT_VALUE_CHANGED, NULL);

gps_fix = lv_led_create(notifyBar);
lv_led_set_color(gps_fix, lv_palette_main(LV_PALETTE_RED));
lv_obj_set_size(gps_fix, 7, 7);
lv_led_off(gps_fix);

gps_fix_mode = lv_label_create(notifyBar);
lv_obj_set_style_text_font(gps_fix_mode, &lv_font_montserrat_10, 0);
lv_label_set_text(gps_fix_mode, "--");
Expand Down
15 changes: 14 additions & 1 deletion src/gui/screens/Settings/events/settings_scr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void load_main_screen();
*/
static void back(lv_event_t *event)
{
load_main_screen();
load_main_screen();
}

/**
Expand All @@ -31,4 +31,17 @@ static void touch_calib(lv_event_t *event)
REPEAT_CAL = false;
is_main_screen = false;
lv_scr_load(settingsScreen);
}

/**
* @brief Compass Calibration
*
* @param event
*/
static void compass_calib(lv_event_t *event)
{
tft.fillScreen(TFT_BLACK);
compass_calibrate();
is_main_screen = false;
lv_scr_load(settingsScreen);
}
9 changes: 9 additions & 0 deletions src/gui/screens/Settings/settings_scr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ void create_settings_scr()

lv_obj_t *but_label;

// Compass Calibration
lv_obj_t *compass_calib_but = lv_btn_create(settingsScreen);
lv_obj_set_size(compass_calib_but, TFT_WIDTH - 30, 40);
but_label = lv_label_create(compass_calib_but);
lv_obj_set_style_text_font(but_label, &lv_font_montserrat_20, 0);
lv_label_set_text(but_label, "Compass Calibration");
lv_obj_center(but_label);
lv_obj_add_event_cb(compass_calib_but, compass_calib, LV_EVENT_CLICKED, NULL);

// Touch Calibration
lv_obj_t *touch_calib_but = lv_btn_create(settingsScreen);
lv_obj_set_size(touch_calib_but, TFT_WIDTH - 30, 40);
Expand Down
1 change: 1 addition & 0 deletions src/gui/screens/Splash/splash_scr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
void splash_scr()
{
tft.fillScreen(TFT_BLACK);
millis_actual = millis();
set_brightness(0);
tft.drawPngFile(SPIFFS, PSTR("/BOOTLOGO.png"), (tft.width() / 2) - 150 , (tft.height() / 2) - 70);
Expand Down
139 changes: 117 additions & 22 deletions src/hardware/compass.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Adafruit_HMC5883_Unified compass = Adafruit_HMC5883_Unified(12345);
MPU9250 IMU(Wire, 0x68);
#endif

#define COMPASS_CAL_TIME 16000
static void save_compass_cal(float offset_x, float offset_y);

/**
* @brief Magnetic declination
*
Expand All @@ -30,7 +33,7 @@ MPU9250 IMU(Wire, 0x68);
float declinationAngle = 0.22;

/**
* @brief Compass Heading Angle
* @brief Compass Heading Angle and Smooth factors
*
*/
int heading = 0;
Expand All @@ -40,16 +43,41 @@ float heading_previous = 0.0;
#define SMOOTH_PREVIOUS_FACTOR 0.60

/**
* @brief Read compass data
* @brief Calibration variables
*
* @return compass heading
*/
int read_compass()
float minx, maxx, miny, maxy, offx = 0.0, offy = 0.0;

/**
* @brief Init Compass
*
*/
void init_compass()
{
float y = 0.0;
float x = 0.0;
float z = 0.0;

#ifdef CUSTOMBOARD
compass.begin();
#endif
#ifdef MAKERF_ESP32S3
int status = IMU.begin();
if (status < 0)
{
log_e("IMU initialization unsuccessful");
log_e("Check IMU wiring or try cycling power");
log_e("Status: %i", status);
}
#endif
}

/**
* @brief Read compass values
*
* @param x
* @param y
* @param z
*/
static void read_compass(float &x, float &y, float &z)
{
#ifdef CUSTOMBOARD
sensors_event_t event;
compass.getEvent(&event);
Expand All @@ -64,11 +92,26 @@ int read_compass()
y = IMU.getMagY_uT();
z = IMU.getMagZ_uT();
#endif
float heading_no_filter = atan2(y, x);
}

/**
* @brief Get compass heading
*
* @return compass heading
*/
int get_heading()
{
float y = 0.0;
float x = 0.0;
float z = 0.0;

read_compass(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;
// heading_smooth = (heading_no_filter * SMOOTH_FACTOR) + (heading_previous * SMOOTH_PREVIOUS_FACTOR);
// heading_previous = heading_smooth;

if (heading_smooth < 0)
heading_smooth += 2 * M_PI;
Expand All @@ -77,19 +120,71 @@ int read_compass()
return (int)(heading_smooth * 180 / M_PI);
}

void init_compass()
/**
* @brief Compass calibration
*
*/
static void compass_calibrate()
{
bool cal = 1;
float y = 0.0;
float x = 0.0;
float z = 0.0;
uint16_t touchX, touchY;

#ifdef CUSTOMBOARD
compass.begin();
#endif
#ifdef MAKERF_ESP32S3
int status = IMU.begin();
if (status < 0)
tft.drawCenterString("ROTATE THE DEVICE", 160, 10, &fonts::DejaVu18);
tft.drawPngFile(SPIFFS, PSTR("/turn.png"), (tft.width() / 2) - 50, 60);
tft.drawCenterString("TOUCH TO START", 160, 200, &fonts::DejaVu18);
tft.drawCenterString("COMPASS CALIBRATION", 160, 230, &fonts::DejaVu18);

while (!tft.getTouch(&touchX, &touchY))
{
log_e("IMU initialization unsuccessful");
log_e("Check IMU wiring or try cycling power");
log_e("Status: %i", status);
};
delay(1000);

unsigned long calTimeWas = millis();

read_compass(x, y, z);

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

while (cal)
{

read_compass(x, y, z);

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);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(3);
tft.setTextPadding(tft.textWidth("88"));
tft.drawNumber((COMPASS_CAL_TIME - secmillis) / 1000, (tft.width() >> 1), 280);

if (secs == 0)
{
offx = (maxx + minx) / 2;
offy = (maxy + miny) / 2;
cal = 0;
}
}
#endif
}

tft.setTextSize(1);
tft.drawCenterString("DONE!", 160, 340, &fonts::DejaVu40);
tft.drawCenterString("TOUCH TO CONTINUE.", 160, 380, &fonts::DejaVu18);

while (!tft.getTouch(&touchX, &touchY))
{
};

save_compass_cal(offx,offy);
}
3 changes: 0 additions & 3 deletions src/hardware/gps.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
HardwareSerial *gps = &Serial2;
TinyGPSPlus GPS;
bool is_gps_fixed = false;
uint8_t fix_mode_old = 0;
uint8_t fix_old = 0;

/**
Expand Down Expand Up @@ -42,7 +41,6 @@ struct GSV
TinyGPSCustom pdop(GPS, PSTR("GNGSA"), 15); // $GNGSA sentence, 15th element
TinyGPSCustom hdop(GPS, PSTR("GNGSA"), 16); // $GNGSA sentence, 16th element
TinyGPSCustom vdop(GPS, PSTR("GNGSA"), 17); // $GNGSA sentence, 17th element
TinyGPSCustom fix(GPS, PSTR("GNGGA"), 6);
TinyGPSCustom fix_mode(GPS, PSTR("GNGSA"), 2);

// GPS Satellites in view
Expand All @@ -60,7 +58,6 @@ GSV BD_GSV;
TinyGPSCustom pdop(GPS, PSTR("GPGSA"), 15); // $GPGSA sentence, 15th element
TinyGPSCustom hdop(GPS, PSTR("GPGSA"), 16); // $GPGSA sentence, 16th element
TinyGPSCustom vdop(GPS, PSTR("GPGSA"), 17); // $GPGSA sentence, 17th element
TinyGPSCustom fix(GPS, PSTR("GPGGA"), 6);
TinyGPSCustom fix_mode(GPS, PSTR("GPGSA"), 2);

// GPS Satellites in view
Expand Down
Loading

0 comments on commit 2e9ba0e

Please sign in to comment.