mirror of
https://github.com/Luzifer/lorabattery.git
synced 2024-12-21 02:11:15 +00:00
Improve measurement stability
This commit is contained in:
parent
279541f3c3
commit
a3f1e62d4d
4 changed files with 56 additions and 11 deletions
39
src/main.cpp
39
src/main.cpp
|
@ -1,14 +1,18 @@
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
#include <TTN_esp32.h>
|
#include <TTN_esp32.h>
|
||||||
#include "heltec.h"
|
#include "heltec.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "main.h"
|
#include "main.hh"
|
||||||
|
#include "math.hh"
|
||||||
|
|
||||||
TTN_esp32 ttn;
|
TTN_esp32 ttn;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Read voltage and add as uint8 to transmission
|
// Read voltage and add as uint8 to transmission
|
||||||
float voltage = readVoltage();
|
float voltage = readVoltage();
|
||||||
uint8_t payload = (uint8_t)(int)(voltage * 10);
|
uint8_t payload = (uint8_t)(int)(round(voltage * 10));
|
||||||
|
|
||||||
if (ENABLE_SEND) {
|
if (ENABLE_SEND) {
|
||||||
sendVoltage(payload);
|
sendVoltage(payload);
|
||||||
|
@ -35,14 +39,31 @@ void loop() {
|
||||||
float readVoltage() {
|
float readVoltage() {
|
||||||
Serial.println("Reading voltage...");
|
Serial.println("Reading voltage...");
|
||||||
|
|
||||||
int rawSum = 0;
|
int rawValues[MEAS_COUNT], sum;
|
||||||
for (int i = 0; i < MEAS_COUNT; i++) {
|
float rawSum = 0.0, dev = 9, mean;
|
||||||
rawSum += analogRead(PIN_VOLTAGE_READ);
|
|
||||||
delay(MEAS_WAIT_MS);
|
while (dev > 2) {
|
||||||
|
sum = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < MEAS_COUNT; i++) {
|
||||||
|
rawValues[i] = analogRead(PIN_VOLTAGE_READ);
|
||||||
|
sum += rawValues[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
dev = deviation(rawValues, MEAS_COUNT);
|
||||||
|
mean = sum / MEAS_COUNT;
|
||||||
}
|
}
|
||||||
|
|
||||||
float raw = rawSum / float(MEAS_COUNT);
|
int count = 0;
|
||||||
float vout = raw * 3.3 / 4094.0;
|
for (int i = 0; i < MEAS_COUNT; i++) {
|
||||||
|
if (rawValues[i] >= mean - dev && rawValues[i] <= mean + dev) {
|
||||||
|
rawSum += rawValues[i];
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float raw = rawSum / count;
|
||||||
|
float vout = raw * 3.3 / 1024;
|
||||||
float value = vout / (7500.0 / (30000.0 + 7500.0)) + CORRECT_DIFF;
|
float value = vout / (7500.0 / (30000.0 + 7500.0)) + CORRECT_DIFF;
|
||||||
|
|
||||||
Serial.printf("Voltage read: %.1f %.2f\r\n", raw, value);
|
Serial.printf("Voltage read: %.1f %.2f\r\n", raw, value);
|
||||||
|
@ -67,7 +88,7 @@ void sendVoltage(uint8_t payload) {
|
||||||
void setup() {
|
void setup() {
|
||||||
Heltec.begin(true /* DisplayEnable */, false /* LoRaEnable */, true /* SerialEnable */, false /* PABOOST */, BAND /* long BAND */);
|
Heltec.begin(true /* DisplayEnable */, false /* LoRaEnable */, true /* SerialEnable */, false /* PABOOST */, BAND /* long BAND */);
|
||||||
|
|
||||||
analogReadResolution(12);
|
analogReadResolution(10);
|
||||||
|
|
||||||
// Initialize display for usage
|
// Initialize display for usage
|
||||||
Heltec.display->init();
|
Heltec.display->init();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef MAIN_H_INCLUDED
|
#ifndef MAIN_HH_INCLUDED
|
||||||
#define MAIN_H_INCLUDED
|
#define MAIN_HH_INCLUDED
|
||||||
|
|
||||||
float readVoltage();
|
float readVoltage();
|
||||||
void sendVoltage(uint8_t payload);
|
void sendVoltage(uint8_t payload);
|
18
src/math.cpp
Normal file
18
src/math.cpp
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#include <cmath>
|
||||||
|
#include "math.hh"
|
||||||
|
|
||||||
|
float deviation(int x[], int size) {
|
||||||
|
float deviation = 0.0, mean, sum = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
sum += x[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
mean = sum / size;
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
deviation += pow((x[i] - mean), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqrt(deviation / size);
|
||||||
|
}
|
6
src/math.hh
Normal file
6
src/math.hh
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef MATH_HH_INCLUDED
|
||||||
|
#define MATH_HH_INCLUDED
|
||||||
|
|
||||||
|
float deviation(int x[], int size);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue