Skip to content

Commit

Permalink
Improved ESP32 battery voltage meter. Good both for Standalone and Pr…
Browse files Browse the repository at this point in the history
…ime Mk2. Voltage readings are about 0.1V higher than actual value - but it Ok for now.
  • Loading branch information
lyusupov committed Jul 28, 2018
1 parent cee531d commit d8f2458
Show file tree
Hide file tree
Showing 9 changed files with 523 additions and 8 deletions.
4 changes: 2 additions & 2 deletions software/firmware/source/SoftRF/BatteryHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

void Battery_setup()
{

SoC->Battery_setup();
}

float Battery_voltage()
{
return analogRead (SOC_GPIO_PIN_BATTERY) / SOC_A0_VOLTAGE_DIVIDER ;
return SoC->Battery_voltage();
}
20 changes: 17 additions & 3 deletions software/firmware/source/SoftRF/Platform_ESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "LEDHelper.h"
#include "GNSSHelper.h"

#include <battery.h>
#include <U8x8lib.h>

// RFM95W pin mapping
Expand Down Expand Up @@ -82,9 +83,6 @@ static void ESP32_setup()

ledcSetup(LEDC_CHANNEL_BUZZER, 0, LEDC_RESOLUTION_BUZZER);

analogReadResolution(10);
analogSetPinAttenuation(SOC_GPIO_PIN_BATTERY, ADC_11db);

/* Pre-init 1st ESP32 I2C bus to stick on these pins */
Wire.begin(SOC_GPIO_PIN_SDA, SOC_GPIO_PIN_SCL);

Expand Down Expand Up @@ -423,6 +421,20 @@ static void ESP32_OLED_loop()
}
}

static void ESP32_Battery_setup()
{
calibrate_voltage(esp32_board == ESP32_TTGO_T_BEAM ?
ADC1_GPIO35_CHANNEL : ADC1_GPIO36_CHANNEL);
}

static float ESP32_Battery_voltage()
{
float voltage = ((float) read_voltage()) * 0.001 ;

/* T-Beam has voltage divider 100k/100k on board */
return (esp32_board == ESP32_TTGO_T_BEAM ? 2 * voltage : voltage);
}

static void IRAM_ATTR ESP32_GNSS_PPS_Interrupt_handler() {
portENTER_CRITICAL_ISR(&GNSS_PPS_mutex);
PPS_TimeMarker = millis(); /* millis() has IRAM_ATTR */
Expand Down Expand Up @@ -461,6 +473,8 @@ SoC_ops_t ESP32_ops = {
ESP32_swSer_enableRx,
&ESP32_Bluetooth_ops,
ESP32_OLED_loop,
ESP32_Battery_setup,
ESP32_Battery_voltage,
ESP32_GNSS_PPS_Interrupt_handler,
ESP32_get_PPS_TimeMarker
};
Expand Down
4 changes: 2 additions & 2 deletions software/firmware/source/SoftRF/Platform_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ extern NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip;
extern Adafruit_NeoPixel strip;
#endif /* USE_NEOPIXELBUS_LIBRARY */

#define SOC_A0_VOLTAGE_DIVIDER (1023.0 / 3.5 /* 3.9 */)

#define LEDC_CHANNEL_BUZZER 0
#define LEDC_RESOLUTION_BUZZER 8

Expand Down Expand Up @@ -98,6 +96,8 @@ extern Adafruit_NeoPixel strip;
/* TTGO T-BEAM GPS module */
#define SOC_GPIO_PIN_TBEAM_RX 12
#define SOC_GPIO_PIN_TBEAM_TX 15
/* TTGO T-BEAM battery voltage */
#define SOC_GPIO_PIN_TBEAM_BATTERY 35

#define SSD1306_OLED_I2C_ADDR 0x3C

Expand Down
12 changes: 12 additions & 0 deletions software/firmware/source/SoftRF/Platform_ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ static void ESP8266_OLED_loop()

}

static void ESP8266_Battery_setup()
{

}

static float ESP8266_Battery_voltage()
{
return analogRead (SOC_GPIO_PIN_BATTERY) / SOC_A0_VOLTAGE_DIVIDER ;
}

void ESP8266_GNSS_PPS_Interrupt_handler() {
PPS_TimeMarker = millis();
}
Expand Down Expand Up @@ -265,6 +275,8 @@ SoC_ops_t ESP8266_ops = {
ESP8266_swSer_enableRx,
NULL, /* ESP8266 has no built-in Bluetooth */
ESP8266_OLED_loop,
ESP8266_Battery_setup,
ESP8266_Battery_voltage,
ESP8266_GNSS_PPS_Interrupt_handler,
ESP8266_get_PPS_TimeMarker
};
Expand Down
2 changes: 2 additions & 0 deletions software/firmware/source/SoftRF/SoCHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ typedef struct SoC_ops_struct {
void (*swSer_enableRx)(boolean);
Bluetooth_ops_t *Bluetooth;
void (*OLED_loop)();
void (*Battery_setup)();
float (*Battery_voltage)();
void (*GNSS_PPS_handler)();
unsigned long (*get_PPS_TimeMarker)();
} SoC_ops_t;
Expand Down
2 changes: 1 addition & 1 deletion software/firmware/source/SoftRF/SoftRF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ void setup()
Serial.println(SoC->getResetInfo()); Serial.println("");

EEPROM_setup();
Battery_setup();

ThisAircraft.addr = SoC->getChipId() & 0x00FFFFFF;

Expand All @@ -126,6 +125,7 @@ void setup()
ThisAircraft.stealth = settings->stealth;
ThisAircraft.no_track = settings->no_track;

Battery_setup();
Traffic_setup();

SoC->swSer_enableRx(false);
Expand Down
Loading

0 comments on commit d8f2458

Please sign in to comment.