From 09e87ad70b25a593b9ba8cdf388e9b87e6f51bba Mon Sep 17 00:00:00 2001 From: Maxim Marshalov Date: Tue, 16 Apr 2024 16:47:22 +0300 Subject: [PATCH] fix battery code --- etc/arduino/arduino.ino | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/etc/arduino/arduino.ino b/etc/arduino/arduino.ino index 52800fe..610f0ff 100644 --- a/etc/arduino/arduino.ino +++ b/etc/arduino/arduino.ino @@ -15,8 +15,8 @@ #define R_S 0.01 #define R_L 49900 #define V_REF 2.5 -#define V_R1 2000 -#define V_R2 1000 +#define V_R1 2000.0 +#define V_R2 1000.0 #define VOLTAGE_PIN A1 #define CURRENT_PIN A0 #define ADC_TO_VOLTAGE 5.0 / 1024 @@ -32,7 +32,7 @@ #define ECHO_REG 3 #define CURRENT_REG 4 #define VOLTAGE_REG 5 -#define BATTAREY_REG 6 +#define BATTERY_REG 6 // ModbusSerial object ModbusSerial mb (MODBUS_SERIAL, SLAVE_ID, -1); @@ -60,7 +60,7 @@ void setup() { mb.addHreg (LEFT_THRUSTER_REG, 1500); mb.addHreg (RIGHT_THRUSTER_REG, 1500); mb.addHreg (ECHO_REG, 0); - mb.addHreg(BATTAREY_REG, 0); + mb.addHreg(BATTERY_REG, 0); mb.addHreg(CURRENT_REG, 0); mb.addHreg(VOLTAGE_REG, 0); pinMode(CURRENT_PIN, INPUT); @@ -90,14 +90,14 @@ void ReadDepth(){ } } } -void BattareyRead(){ +void BatteryRead(){ - float voltage = (analogRead(VOLTAGE_PIN) * ADC_TO_VOLTAGE) / (V_R2 / (V_R1 + V_R2)); - float current = (analogRead(CURRENT_PIN) * ADC_TO_VOLTAGE) / ((R_S * R_L) / 1000); - float battarey = voltage * V_REF; + int voltage = (analogRead(VOLTAGE_PIN) * ADC_TO_VOLTAGE) / (V_R2 / (V_R1 + V_R2)); + float current = 1000 * (analogRead(CURRENT_PIN) * ADC_TO_VOLTAGE) / ((R_S * R_L) / 1000); + int battery = (int)(voltage * V_REF); mb.setHreg(VOLTAGE_REG, voltage); mb.setHreg(CURRENT_REG, current); - mb.setHreg(BATTAREY_REG, battarey); + mb.setHreg(BATTERY_REG, battery); delay(30); } @@ -105,7 +105,7 @@ void BattareyRead(){ void loop() { ping(); ReadDepth(); - BattareyRead(); + BatteryRead(); mb.task(); thruster_left.writeMicroseconds(constrain(mb.Hreg(LEFT_THRUSTER_REG), PWM_LOW, PWM_HIGH)); thruster_right.writeMicroseconds(constrain(mb.Hreg(RIGHT_THRUSTER_REG), PWM_LOW, PWM_HIGH)); -- GitLab