commit 214784c27be8cfe03e0a42f0e85a41842a10d0cb Author: Denis Denisov Date: Sun Feb 25 13:04:32 2024 +0300 Initial charger version diff --git a/charger.rrb b/charger.rrb new file mode 100644 index 0000000..0fbb9d5 Binary files /dev/null and b/charger.rrb differ diff --git a/charger/charger.ino b/charger/charger.ino new file mode 100644 index 0000000..25e8ce5 --- /dev/null +++ b/charger/charger.ino @@ -0,0 +1,111 @@ +#include + +ModbusRTU mb; + +#define MODBUS_ADDRESS 1 + +#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 ACC_PIN 2 +#define LAMP_PIN 3 + +// Мощность лампочки (Вт) +const float LAMP_POWER = 21.0; +const float LAMP_REFERENCE_CURRENT = 12.0; + +// Сопротивление токоограничивающего резистора +const float R_LIMITER = 4.7; + +float R_lamp; + +void setup() +{ + Serial.begin(115200); + pinMode(A4, INPUT); + pinMode(A5, INPUT); + pinMode(A2, INPUT); + pinMode(A3, INPUT); + pinMode(ACC_PIN, OUTPUT); + pinMode(LAMP_PIN, OUTPUT); + R_lamp = LAMP_REFERENCE_CURRENT / (LAMP_POWER / LAMP_REFERENCE_CURRENT); + mb.begin(&Serial); + 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); +} + +float I_lamp; +float I_acc; + +void ReadLamp() +{ + int a4 = analogRead(A4); + int a5 = analogRead(A5); + // multiply by 5 as reference current, + // multiply by 3 because of the divider + float v1 = a4 / 1023.0 * 5 * 3; + float v2 = a5 / 1023.0 * 5 * 3; + I_lamp = (v1 - v2) / R_lamp; +} + +void ReadAcc() +{ + int a2 = analogRead(A2); + int a3 = analogRead(A3); + // multiply by 5 as reference current, + // multiply by 3 because of the divider + float v1 = a2 / 1023.0 * 5 * 3; + float v2 = a3 / 1023.0 * 5 * 3; + I_acc = (v1 - v2) / R_LIMITER; +} + +void WriteFloatReg(int start, float val) +{ + uint16_t* d = (uint16_t*)(&val); + mb.Hreg(start, *d); + mb.Hreg(start + 1, *(d + 1)); +} + +uint16_t acc_state = 0; +uint16_t lamp_state = 0; + +void loop() +{ + ReadLamp(); + ReadAcc(); + WriteFloatReg(I_ACC_LOW_REGISTER, I_acc); + WriteFloatReg(I_LAMP_LOW_REGISTER, I_lamp); + Serial.print("I_acc = "); + Serial.print(I_acc); + Serial.print(" I_lamp = "); + Serial.print(I_lamp); + Serial.println(); + mb.task(); + lamp_state = mb.Hreg(lamp_state); + acc_state = mb.Hreg(acc_state); + if (lamp_state) + { + digitalWrite(LAMP_PIN, 1); + } + else + { + digitalWrite(LAMP_PIN, 0); + } + if (acc_state) + { + digitalWrite(ACC_PIN, 1); + } + else + { + digitalWrite(ACC_PIN, 0); + } +} diff --git a/charger_new.rrb b/charger_new.rrb new file mode 100644 index 0000000..40d7b22 Binary files /dev/null and b/charger_new.rrb differ