Browse Source

Added code to measure voltages using an ADS1015 (wip)

master
Youen 6 months ago
parent
commit
0637250f10
  1. 1
      ESP32/platformio.ini
  2. 14
      ESP32/src/vehicle-monitor.cpp

1
ESP32/platformio.ini

@ -18,3 +18,4 @@ monitor_speed = 115200
upload_speed = 921600 upload_speed = 921600
build_type = debug build_type = debug
monitor_filters = esp32_exception_decoder monitor_filters = esp32_exception_decoder
lib_deps = adafruit/Adafruit ADS1X15@^2.4.0

14
ESP32/src/vehicle-monitor.cpp

@ -14,6 +14,8 @@
#include <Dps310.h> #include <Dps310.h>
#include <Adafruit_ADS1X15.h>
#include "wifi-credentials.h" #include "wifi-credentials.h"
#include <limits> #include <limits>
@ -26,6 +28,7 @@ AsyncWebServer server(80);
ADC currentSensor(36); ADC currentSensor(36);
ADC batterySensor(39); ADC batterySensor(39);
Adafruit_ADS1015 currentSensor2;
Dps310 pressureSensor = Dps310(); Dps310 pressureSensor = Dps310();
const int8_t speedSensorPin = 13; const int8_t speedSensorPin = 13;
const int8_t debugLedPin = 2; 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); const uint32_t wheelCircumferenceMillimeters = (uint32_t)(wheelCircumferenceMeters * 1000.0f + 0.5f);
uint16_t batteryVoltage = 0; // in mV 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 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) 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); pressureSensor.begin(Wire);
currentSensor2.begin(0x48, &Wire);
server.on("/api/debug/log", HTTP_GET, [](AsyncWebServerRequest *request){ server.on("/api/debug/log", HTTP_GET, [](AsyncWebServerRequest *request){
AsyncResponseStream *response = request->beginResponseStream("text/plain"); AsyncResponseStream *response = request->beginResponseStream("text/plain");
int logPartIdx = 0; int logPartIdx = 0;
@ -351,6 +357,12 @@ void handle_ADC_measures()
batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f); batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f);
batteryOutputCurrent = (uint16_t)(averageC * 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 #if DUMMY_DATA
batteryVoltage = (uint16_t)((float)random(4000, 4020) / 100.0f * 1000.0f + 0.5f); 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); batteryOutputCurrent = (uint16_t)(max(0.0f, sinf((float)millis()/30000.0f)) * 25.0f * 1000.0f + 0.5f);

Loading…
Cancel
Save