From 0637250f104949d1311b157f4538a2492abd25c3 Mon Sep 17 00:00:00 2001 From: Youen Date: Sat, 25 May 2024 00:04:54 +0200 Subject: [PATCH] Added code to measure voltages using an ADS1015 (wip) --- ESP32/platformio.ini | 1 + ESP32/src/vehicle-monitor.cpp | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ESP32/platformio.ini b/ESP32/platformio.ini index 5c5f4a3..8c26fa3 100644 --- a/ESP32/platformio.ini +++ b/ESP32/platformio.ini @@ -18,3 +18,4 @@ monitor_speed = 115200 upload_speed = 921600 build_type = debug monitor_filters = esp32_exception_decoder +lib_deps = adafruit/Adafruit ADS1X15@^2.4.0 diff --git a/ESP32/src/vehicle-monitor.cpp b/ESP32/src/vehicle-monitor.cpp index 895294c..0945db2 100644 --- a/ESP32/src/vehicle-monitor.cpp +++ b/ESP32/src/vehicle-monitor.cpp @@ -14,6 +14,8 @@ #include +#include + #include "wifi-credentials.h" #include @@ -26,6 +28,7 @@ AsyncWebServer server(80); ADC currentSensor(36); ADC batterySensor(39); +Adafruit_ADS1015 currentSensor2; Dps310 pressureSensor = Dps310(); const int8_t speedSensorPin = 13; const int8_t debugLedPin = 2; @@ -38,7 +41,8 @@ const float wheelCircumferenceMeters = wheelDiameterInches * 0.0254f * 3.1415f / const uint32_t wheelCircumferenceMillimeters = (uint32_t)(wheelCircumferenceMeters * 1000.0f + 0.5f); uint16_t batteryVoltage = 0; // in mV -uint16_t batteryOutputCurrent = 0; // in mV +uint16_t batteryOutputCurrent = 0; // in mA +uint16_t batteryOutputCurrent2 = 0; // in mA int16_t temperature = 0; // in tenth of °C int32_t altitude = 0; // in mm above sea level (can be negative if below sea level, or depending on atmospheric conditions) @@ -197,6 +201,8 @@ void setup() pressureSensor.begin(Wire); + currentSensor2.begin(0x48, &Wire); + server.on("/api/debug/log", HTTP_GET, [](AsyncWebServerRequest *request){ AsyncResponseStream *response = request->beginResponseStream("text/plain"); int logPartIdx = 0; @@ -351,6 +357,12 @@ void handle_ADC_measures() batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f); batteryOutputCurrent = (uint16_t)(averageC * 1000.0f + 0.5f); + int16_t batteryOutputCurrent2Int = currentSensor2.readADC_Differential_0_1(); + float batteryOutputCurrent2Volts = (float)batteryOutputCurrent2Int * 3.0f / 1000.0f; + const float shuntResistanceValue = 0.01f; // in ohms + float batteryOutputCurrent2Amps = batteryOutputCurrent2Volts / shuntResistanceValue; + batteryOutputCurrent2 = (uint16_t)(batteryOutputCurrent2Amps * 1000.0f + 0.5f); + #if DUMMY_DATA batteryVoltage = (uint16_t)((float)random(4000, 4020) / 100.0f * 1000.0f + 0.5f); batteryOutputCurrent = (uint16_t)(max(0.0f, sinf((float)millis()/30000.0f)) * 25.0f * 1000.0f + 0.5f);