From f9ccf97e811d05900114f1a9530f53a93dc7e3e3 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sun, 10 Mar 2024 16:07:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B4=D0=B0=D1=82=D1=87=D0=B8=D0=BA=20=D1=82=D0=BE=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- charger/charger.ino | 58 +++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/charger/charger.ino b/charger/charger.ino index fc54eac..11b24f1 100644 --- a/charger/charger.ino +++ b/charger/charger.ino @@ -6,8 +6,6 @@ ModbusRTU mb; #define I_ACC_LOW_REGISTER 0 #define I_ACC_HIGH_REGISTER 1 -#define I_LAMP_LOW_REGISTER 2 -#define I_LAMP_HIGH_REGISTER 3 #define ACC_STATE_REGISTER 4 #define LAMP_STATE_REGISTER 5 #define U_ACC_LOW_REGISTER 6 @@ -28,6 +26,7 @@ float R_lamp; void setup() { Serial.begin(115200); + pinMode(A7, INPUT); pinMode(A4, INPUT); pinMode(A5, INPUT); pinMode(A2, INPUT); @@ -39,8 +38,6 @@ void setup() mb.slave(MODBUS_ADDRESS); mb.addHreg(I_ACC_LOW_REGISTER); mb.addHreg(I_ACC_HIGH_REGISTER); - mb.addHreg(I_LAMP_LOW_REGISTER); - mb.addHreg(I_LAMP_HIGH_REGISTER); mb.addHreg(ACC_STATE_REGISTER); mb.addHreg(LAMP_STATE_REGISTER); for (int i = 0; i < 4; i++) @@ -50,12 +47,11 @@ void setup() } } -float I_lamp; float I_acc; -float U1_lamp; -float U2_lamp; -float U1_acc; -float U2_acc; +float U4; +float U5; +float U2; +float U3; void ReadLamp() { @@ -63,9 +59,8 @@ void ReadLamp() int a5 = analogRead(A5); // multiply by 5 as reference current, // multiply by 3 because of the divider - U1_lamp = a4 / 1023.0 * 5 * 3; - U2_lamp = a5 / 1023.0 * 5 * 3; - I_lamp = (U1_lamp - U2_lamp) / R_lamp; + U4 = a4 / 1023.0 * 5 * 3; + U5 = a5 / 1023.0 * 5 * 3; } void ReadAcc() @@ -74,9 +69,32 @@ void ReadAcc() int a3 = analogRead(A3); // multiply by 5 as reference current, // multiply by 3 because of the divider - U1_acc = a2 / 1023.0 * 5 * 3; - U2_acc = a3 / 1023.0 * 5 * 3; - I_acc = (U1_acc - U2_acc) / R_LIMITER; + U2 = a2 / 1023.0 * 5 * 3; + U3 = a3 / 1023.0 * 5 * 3; +} + +const float CURRENT_SENSOR_MAX = 30; + +void ReadI() +{ + static int reads[10]; + for (int i = 0; i < 10; i++) + { + reads[i] = analogRead(A7); + } + int imin = 0, imax = 0; + for (int i = 0; i < 10; i++) + { + if (reads[i] < reads[imin]) imin = i; + if (reads[i] < reads[imax]) imax = i; + } + int total = 0; + for (int i = 0; i < 10; i++) + { + if (i == imin || i == imax) continue; + total += reads[i]; + } + I_acc = ((total / 8.0 + 0.0) / 1023.0 - 0.5) * CURRENT_SENSOR_MAX; } void WriteFloatReg(int start, float val) @@ -93,12 +111,12 @@ void loop() { ReadLamp(); ReadAcc(); + ReadI(); WriteFloatReg(I_ACC_LOW_REGISTER, I_acc); - WriteFloatReg(U_ACC_LOW_REGISTER, U1_acc); - WriteFloatReg(U_ACC_LOW_REGISTER + 2, U2_acc); - WriteFloatReg(I_LAMP_LOW_REGISTER, I_lamp); - WriteFloatReg(U_LAMP_LOW_REGISTER, U1_lamp); - WriteFloatReg(U_LAMP_LOW_REGISTER + 2, U2_lamp); + WriteFloatReg(U_ACC_LOW_REGISTER, U2); + WriteFloatReg(U_ACC_LOW_REGISTER + 2, U3); + WriteFloatReg(U_LAMP_LOW_REGISTER, U4); + WriteFloatReg(U_LAMP_LOW_REGISTER + 2, U5); mb.task(); lamp_state = mb.Hreg(LAMP_STATE_REGISTER); acc_state = mb.Hreg(ACC_STATE_REGISTER);