From 5e3b513f7c07731aa349bcd62685e3e87245ad21 Mon Sep 17 00:00:00 2001 From: Maxim Marshalov Date: Tue, 16 Apr 2024 16:02:28 +0300 Subject: [PATCH 1/3] relocate arduino code --- etc/{ => arduino}/arduino.ino | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename etc/{ => arduino}/arduino.ino (100%) diff --git a/etc/arduino.ino b/etc/arduino/arduino.ino similarity index 100% rename from etc/arduino.ino rename to etc/arduino/arduino.ino -- GitLab From 7c5c7699c45d47055daaf1a321a8cc09504f63e8 Mon Sep 17 00:00:00 2001 From: Maxim Marshalov Date: Tue, 16 Apr 2024 16:03:47 +0300 Subject: [PATCH 2/3] add battarey registers to arduino code --- etc/arduino/arduino.ino | 128 +++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 53 deletions(-) diff --git a/etc/arduino/arduino.ino b/etc/arduino/arduino.ino index 6f2d9bb..52800fe 100644 --- a/etc/arduino/arduino.ino +++ b/etc/arduino/arduino.ino @@ -3,89 +3,111 @@ #include #include -const int thruster_left_pin = 11; -const int thruster_right_pin = 10; -const byte slave_id = 1; - -const int reg_thruster_left = 1; -const int reg_thruster_right = 2; -const int reg_echo = 3; - +#define THRUSTER_LEFT_PIN 11 +#define THRUSTER_RIGHT_PIN 10 +#define SLAVE_ID 1 #define PWM_LOW 1100 #define PWM_HIGH 1900 +#define MODBUS_SERIAL Serial +#define BAUDRATE 115200 +#define TIMEOUT 200 +//Battarey parameters +#define R_S 0.01 +#define R_L 49900 +#define V_REF 2.5 +#define V_R1 2000 +#define V_R2 1000 +#define VOLTAGE_PIN A1 +#define CURRENT_PIN A0 +#define ADC_TO_VOLTAGE 5.0 / 1024 -#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available +// echosounder uart +#define RX_PIN 9 +#define TX_PIN 6 +#define ECHO_PACKAGE_SIZE 4 -const unsigned long baudrate = 115200; +//Modbus registers +#define LEFT_THRUSTER_REG 1 +#define RIGHT_THRUSTER_REG 2 +#define ECHO_REG 3 +#define CURRENT_REG 4 +#define VOLTAGE_REG 5 +#define BATTAREY_REG 6 // ModbusSerial object -ModbusSerial mb (MySerial, slave_id, -1); -// Servo object +ModbusSerial mb (MODBUS_SERIAL, SLAVE_ID, -1); + +// Motor objects Servo thruster_left; Servo thruster_right; -// echosounder -#define rxPin 9 -#define txPin 6 -SoftwareSerial softSerial = SoftwareSerial(rxPin, txPin); -unsigned long lastPing; -const int timeout = 200; -#define ECHO_PACKAGE_SIZE 4 +SoftwareSerial softSerial = SoftwareSerial(RX_PIN, TX_PIN); int echoPackage[ECHO_PACKAGE_SIZE]; int depth = 0; +unsigned long lastPing; + void setup() { - MySerial.begin (baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard + thruster_left.attach(THRUSTER_LEFT_PIN); + thruster_right.attach(THRUSTER_RIGHT_PIN); + softSerial.begin(BAUDRATE); + pinMode(RX_PIN, INPUT); + pinMode(TX_PIN, OUTPUT); + MODBUS_SERIAL.begin (BAUDRATE); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard + lastPing = millis(); wdt_disable(); - mb.config (baudrate); - mb.setAdditionalServerData("Waterstrider low level"); // for Report Server ID function (0x11) + mb.config (BAUDRATE); + mb.setAdditionalServerData("Waterstrider low level"); + mb.addHreg (LEFT_THRUSTER_REG, 1500); + mb.addHreg (RIGHT_THRUSTER_REG, 1500); + mb.addHreg (ECHO_REG, 0); + mb.addHreg(BATTAREY_REG, 0); + mb.addHreg(CURRENT_REG, 0); + mb.addHreg(VOLTAGE_REG, 0); + pinMode(CURRENT_PIN, INPUT); + pinMode(VOLTAGE_PIN, INPUT); + - thruster_left.attach(thruster_left_pin); - thruster_right.attach(thruster_right_pin); - mb.addHreg (reg_thruster_left, 1500); - mb.addHreg (reg_thruster_right, 1500); - mb.addHreg (reg_echo, 0); - - pinMode(rxPin, INPUT); - pinMode(txPin, OUTPUT); - - // Set the baud rate for the SoftwareSerial object - softSerial.begin(115200); - lastPing = millis(); } - -void ping() { - if ((millis() - lastPing)> timeout) - { - digitalWrite(txPin, LOW); - delay(2); - digitalWrite(txPin, HIGH); - lastPing = millis(); - } +void ping(){ + if(millis() - lastPing > TIMEOUT){ + digitalWrite(RX_PIN, LOW); + delay(2); + digitalWrite(TX_PIN, HIGH); + lastPing = millis(); + } + } - -void parseEchosounder() -{ +void ReadDepth(){ if (softSerial.available() > ECHO_PACKAGE_SIZE) { if (softSerial.read() == 0xFF) { for (int i = 1; i < ECHO_PACKAGE_SIZE; i++) { echoPackage[i] = softSerial.read(); } - depth = echoPackage[1] * 256 + echoPackage[2]; - mb.setHreg(reg_echo, constrain(depth,0,10000)); + mb.setHreg(ECHO_REG, constrain(depth,0,10000)); softSerial.flush(); } } } +void BattareyRead(){ + + 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; + mb.setHreg(VOLTAGE_REG, voltage); + mb.setHreg(CURRENT_REG, current); + mb.setHreg(BATTAREY_REG, battarey); + delay(30); +} void loop() { ping(); - parseEchosounder(); + ReadDepth(); + BattareyRead(); mb.task(); - - thruster_left.writeMicroseconds(constrain(mb.Hreg(reg_thruster_left), PWM_LOW, PWM_HIGH)); - thruster_right.writeMicroseconds(constrain(mb.Hreg(reg_thruster_right), PWM_LOW, PWM_HIGH)); - delay (15); + 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)); + delay(15); } \ No newline at end of file -- GitLab From 0a5aad60f7a124a3cea98552618759cde2350143 Mon Sep 17 00:00:00 2001 From: Maxim Marshalov Date: Tue, 16 Apr 2024 16:05:17 +0300 Subject: [PATCH 3/3] add topics to modbus config json --- config/modbus-mapping.json | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/config/modbus-mapping.json b/config/modbus-mapping.json index 07a4fe5..193c4af 100644 --- a/config/modbus-mapping.json +++ b/config/modbus-mapping.json @@ -27,7 +27,19 @@ "echo": { "address": 3, "type": "UINT" + }, + "current":{ + "address": 4, + "type":"UINT" + }, + "voltage":{ + "address": 5, + "type": "UINT" + }, + "charge":{ + "address": 6, + "type": "UINT" } } } -} +} \ No newline at end of file -- GitLab