From 41ec005680fa25540e190a69a595c5c960fb1d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Fri, 11 Dec 2020 14:09:20 +0100 Subject: [PATCH] Implement battery voltage to charge conversion Voltage to charge nonlinear function is being linearly approximated in the operating range. --- meson.build | 3 +- openrtx/include/battery.h | 30 +++++++++++++++ openrtx/src/battery.c | 45 ++++++++++++++++++++++ openrtx/src/ui.c | 5 ++- platform/targets/GD77/hwconfig.h | 5 ++- platform/targets/MD-380/hwconfig.h | 4 +- platform/targets/MD-390/hwconfig.h | 4 +- platform/targets/MD-UV380/hwconfig.h | 6 +-- platform/targets/linux/emulator/emulator.c | 2 +- platform/targets/linux/hwconfig.h | 6 +-- 10 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 openrtx/include/battery.h create mode 100644 openrtx/src/battery.c diff --git a/meson.build b/meson.build index 21330cb6..4f3a9df5 100644 --- a/meson.build +++ b/meson.build @@ -14,7 +14,8 @@ project('OpenRTX', 'c', openrtx_src = ['openrtx/src/bootstrap.c', 'openrtx/src/state.c', 'openrtx/src/ui.c', - 'openrtx/src/threads.c'] + 'openrtx/src/threads.c', + 'openrtx/src/battery.c'] ## Replace main executable with platform test diff --git a/openrtx/include/battery.h b/openrtx/include/battery.h new file mode 100644 index 00000000..61d51191 --- /dev/null +++ b/openrtx/include/battery.h @@ -0,0 +1,30 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef BATTERY_H +#define BATTERY_H + +/* This function uses battery charge tables to convert a battery voltage into a + * charge percentage. + * @param vbat: the voltage read from the battery in volt + * @return the charge percentage + */ +float battery_getCharge(float vbat); + +#endif /* BATTERY_H */ diff --git a/openrtx/src/battery.c b/openrtx/src/battery.c new file mode 100644 index 00000000..65664a60 --- /dev/null +++ b/openrtx/src/battery.c @@ -0,0 +1,45 @@ +/*************************************************************************** + * Copyright (C) 2020 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN, * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#include +#include +#include + +/* This array acts as a lookup table for converting Li-Po voltage into + * charge percentage, elements range from 0% to 100% (included) with 5% steps. + * Data is taken from (https://blog.ampow.com/lipo-voltage-chart/). + */ +#define V_LUT_STEPS 21 +#if defined BAT_LIPO_1S +float bat_v_min = 3.61f; +float bat_v_max = 4.20f; +#elif defined BAT_LIPO_2S +float bat_v_min = 7.22f; +float bat_v_max = 8.40f; +#elif defined BAT_LIPO_3S +float bat_v_min = 10.83; +float bat_v_max = 12.60; +#else +#error Please define a battery type into platform/targets/.../hwconfig.h +#endif + +float battery_getCharge(float vbat) { + // Perform a linear interpolation between minimum and maximum charge values + return (vbat - bat_v_min) / (bat_v_max - bat_v_min); +} diff --git a/openrtx/src/ui.c b/openrtx/src/ui.c index 3582f38d..08c6e6e2 100644 --- a/openrtx/src/ui.c +++ b/openrtx/src/ui.c @@ -72,6 +72,7 @@ #include #include #include +#include const char *menuItems[MENU_NUM] = { @@ -229,12 +230,12 @@ void _ui_drawTopBar(state_t* last_state) color_white); // Print battery icon on top bar, use 4 px padding - float percentage = last_state->v_bat / MAX_VBAT; + float charge = battery_getCharge(last_state->v_bat); uint16_t bat_width = SCREEN_WIDTH / 9; uint16_t bat_height = layout.top_h - layout.vertical_pad; point_t bat_pos = {SCREEN_WIDTH - bat_width - layout.horizontal_pad, layout.vertical_pad / 2}; - gfx_drawBattery(bat_pos, bat_width, bat_height, percentage); + gfx_drawBattery(bat_pos, bat_width, bat_height, charge); // Print radio mode on top bar char mode[4] = ""; diff --git a/platform/targets/GD77/hwconfig.h b/platform/targets/GD77/hwconfig.h index 80f5af11..f2cf591c 100644 --- a/platform/targets/GD77/hwconfig.h +++ b/platform/targets/GD77/hwconfig.h @@ -31,6 +31,9 @@ #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 +/* Battery type */ +#define BAT_LIPO_2S + /* Display */ #define LCD_BKLIGHT GPIOC,4 #define LCD_CS GPIOC,8 @@ -56,4 +59,4 @@ #define FUNC2_SW GPIOB,1 #define MONI_SW GPIOB,9 -#endif \ No newline at end of file +#endif diff --git a/platform/targets/MD-380/hwconfig.h b/platform/targets/MD-380/hwconfig.h index b8fc08e5..1f03bf71 100644 --- a/platform/targets/MD-380/hwconfig.h +++ b/platform/targets/MD-380/hwconfig.h @@ -29,8 +29,8 @@ /* Screen needs x-axis mirroring */ #define DISPLAY_MIRROR_X -/* Maximum battery voltage */ -#define MAX_VBAT 8.4f +/* Battery type */ +#define BAT_LIPO_2S /* Display */ #define LCD_D0 GPIOD,14 diff --git a/platform/targets/MD-390/hwconfig.h b/platform/targets/MD-390/hwconfig.h index 9994a7d3..96bed2fa 100644 --- a/platform/targets/MD-390/hwconfig.h +++ b/platform/targets/MD-390/hwconfig.h @@ -26,8 +26,8 @@ #define SCREEN_WIDTH 160 #define SCREEN_HEIGHT 128 -/* Maximum battery voltage */ -#define MAX_VBAT 8.4f +/* Battery type */ +#define BAT_LIPO_2S /* Display */ #define LCD_D0 GPIOD,14 diff --git a/platform/targets/MD-UV380/hwconfig.h b/platform/targets/MD-UV380/hwconfig.h index 438e2aff..f9870033 100644 --- a/platform/targets/MD-UV380/hwconfig.h +++ b/platform/targets/MD-UV380/hwconfig.h @@ -26,6 +26,9 @@ #define SCREEN_WIDTH 160 #define SCREEN_HEIGHT 128 +/* Battery type */ +#define BAT_LIPO_2S + /* Display */ #define LCD_D0 GPIOD,14 #define LCD_D1 GPIOD,15 @@ -76,9 +79,6 @@ #define FLASH_SDO GPIOB,4 #define FLASH_SDI GPIOB,5 -/* Maximum battery voltage */ -#define MAX_VBAT 8.4f - /* * To enable pwm for display backlight dimming uncomment this directive. * diff --git a/platform/targets/linux/emulator/emulator.c b/platform/targets/linux/emulator/emulator.c index 31f1085b..e31a1dd3 100644 --- a/platform/targets/linux/emulator/emulator.c +++ b/platform/targets/linux/emulator/emulator.c @@ -25,7 +25,7 @@ #include #include -radio_state Radio_State = {12, 7, 3, 4, 1, false}; +radio_state Radio_State = {12, 8.2f, 3, 4, 1, false}; void systemBootstrap(); diff --git a/platform/targets/linux/hwconfig.h b/platform/targets/linux/hwconfig.h index 84370f0e..623dc462 100644 --- a/platform/targets/linux/hwconfig.h +++ b/platform/targets/linux/hwconfig.h @@ -18,6 +18,9 @@ #define PLATFORM_LINUX +/* Battery type */ +#define BAT_LIPO_2S + #define GPIOA "PA" #define GPIOB "PB" #define GPIOC "PC" @@ -48,6 +51,3 @@ /* Push-to-talk switch */ #define PTT_SW "PTT_SW",11 - -/* Maximum battery voltage */ -#define MAX_VBAT 8.4f