Добавил датчик тока
This commit is contained in:
+38
-20
@@ -6,8 +6,6 @@ ModbusRTU mb;
|
|||||||
|
|
||||||
#define I_ACC_LOW_REGISTER 0
|
#define I_ACC_LOW_REGISTER 0
|
||||||
#define I_ACC_HIGH_REGISTER 1
|
#define I_ACC_HIGH_REGISTER 1
|
||||||
#define I_LAMP_LOW_REGISTER 2
|
|
||||||
#define I_LAMP_HIGH_REGISTER 3
|
|
||||||
#define ACC_STATE_REGISTER 4
|
#define ACC_STATE_REGISTER 4
|
||||||
#define LAMP_STATE_REGISTER 5
|
#define LAMP_STATE_REGISTER 5
|
||||||
#define U_ACC_LOW_REGISTER 6
|
#define U_ACC_LOW_REGISTER 6
|
||||||
@@ -28,6 +26,7 @@ float R_lamp;
|
|||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
pinMode(A7, INPUT);
|
||||||
pinMode(A4, INPUT);
|
pinMode(A4, INPUT);
|
||||||
pinMode(A5, INPUT);
|
pinMode(A5, INPUT);
|
||||||
pinMode(A2, INPUT);
|
pinMode(A2, INPUT);
|
||||||
@@ -39,8 +38,6 @@ void setup()
|
|||||||
mb.slave(MODBUS_ADDRESS);
|
mb.slave(MODBUS_ADDRESS);
|
||||||
mb.addHreg(I_ACC_LOW_REGISTER);
|
mb.addHreg(I_ACC_LOW_REGISTER);
|
||||||
mb.addHreg(I_ACC_HIGH_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(ACC_STATE_REGISTER);
|
||||||
mb.addHreg(LAMP_STATE_REGISTER);
|
mb.addHreg(LAMP_STATE_REGISTER);
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
@@ -50,12 +47,11 @@ void setup()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float I_lamp;
|
|
||||||
float I_acc;
|
float I_acc;
|
||||||
float U1_lamp;
|
float U4;
|
||||||
float U2_lamp;
|
float U5;
|
||||||
float U1_acc;
|
float U2;
|
||||||
float U2_acc;
|
float U3;
|
||||||
|
|
||||||
void ReadLamp()
|
void ReadLamp()
|
||||||
{
|
{
|
||||||
@@ -63,9 +59,8 @@ void ReadLamp()
|
|||||||
int a5 = analogRead(A5);
|
int a5 = analogRead(A5);
|
||||||
// multiply by 5 as reference current,
|
// multiply by 5 as reference current,
|
||||||
// multiply by 3 because of the divider
|
// multiply by 3 because of the divider
|
||||||
U1_lamp = a4 / 1023.0 * 5 * 3;
|
U4 = a4 / 1023.0 * 5 * 3;
|
||||||
U2_lamp = a5 / 1023.0 * 5 * 3;
|
U5 = a5 / 1023.0 * 5 * 3;
|
||||||
I_lamp = (U1_lamp - U2_lamp) / R_lamp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadAcc()
|
void ReadAcc()
|
||||||
@@ -74,9 +69,32 @@ void ReadAcc()
|
|||||||
int a3 = analogRead(A3);
|
int a3 = analogRead(A3);
|
||||||
// multiply by 5 as reference current,
|
// multiply by 5 as reference current,
|
||||||
// multiply by 3 because of the divider
|
// multiply by 3 because of the divider
|
||||||
U1_acc = a2 / 1023.0 * 5 * 3;
|
U2 = a2 / 1023.0 * 5 * 3;
|
||||||
U2_acc = a3 / 1023.0 * 5 * 3;
|
U3 = a3 / 1023.0 * 5 * 3;
|
||||||
I_acc = (U1_acc - U2_acc) / R_LIMITER;
|
}
|
||||||
|
|
||||||
|
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)
|
void WriteFloatReg(int start, float val)
|
||||||
@@ -93,12 +111,12 @@ void loop()
|
|||||||
{
|
{
|
||||||
ReadLamp();
|
ReadLamp();
|
||||||
ReadAcc();
|
ReadAcc();
|
||||||
|
ReadI();
|
||||||
WriteFloatReg(I_ACC_LOW_REGISTER, I_acc);
|
WriteFloatReg(I_ACC_LOW_REGISTER, I_acc);
|
||||||
WriteFloatReg(U_ACC_LOW_REGISTER, U1_acc);
|
WriteFloatReg(U_ACC_LOW_REGISTER, U2);
|
||||||
WriteFloatReg(U_ACC_LOW_REGISTER + 2, U2_acc);
|
WriteFloatReg(U_ACC_LOW_REGISTER + 2, U3);
|
||||||
WriteFloatReg(I_LAMP_LOW_REGISTER, I_lamp);
|
WriteFloatReg(U_LAMP_LOW_REGISTER, U4);
|
||||||
WriteFloatReg(U_LAMP_LOW_REGISTER, U1_lamp);
|
WriteFloatReg(U_LAMP_LOW_REGISTER + 2, U5);
|
||||||
WriteFloatReg(U_LAMP_LOW_REGISTER + 2, U2_lamp);
|
|
||||||
mb.task();
|
mb.task();
|
||||||
lamp_state = mb.Hreg(LAMP_STATE_REGISTER);
|
lamp_state = mb.Hreg(LAMP_STATE_REGISTER);
|
||||||
acc_state = mb.Hreg(ACC_STATE_REGISTER);
|
acc_state = mb.Hreg(ACC_STATE_REGISTER);
|
||||||
|
|||||||
Reference in New Issue
Block a user