-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarTracker_BMS.ino
More file actions
122 lines (111 loc) · 2.12 KB
/
SolarTracker_BMS.ino
File metadata and controls
122 lines (111 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <Wire.h>
#include "INA3221.h"
#include <Servo.h>
INA3221 ina[2]=
{
INA3221(0x40,&Wire),
INA3221(0x41,&Wire)
};
const int switching=26;
const float upper=25.2;
const float lower=19.5;
int charging=1;
Servo pan,tilt;
int panPos=90;
int tiltPos=90;
float angle=1;
float tolerance=200;
float cellVoltage[6];
float cellCurrent[6];
void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000);
for(int i=0;i<2;i++)
{
ina[i].begin();
ina[i].setShuntR(0,0.1);
ina[i].setShuntR(1,0.1);
ina[i].setShuntR(2,0.1);
}
pinMode(switching,OUTPUT);
digitalWrite(switching,LOW);
pan.attach(18);
tilt.attach(19);
pan.write(90);
tilt.write(90);
Serial.println("6 Cell BMS + Solar Tracker Started");
}
void loop()
{
int index=0;
float packVoltage=0;
for(int i=0;i<2;i++)
{
for(int cell=0;cell<3;cell++)
{
float volt=ina[i].getBusVoltage(cell);
float current=ina[i].getCurrent_mA(cell)/1000.0;
cellVoltage[index]=volt;
cellCurrent[index]=current;
packVoltage+=volt;
Serial.print("Cell ");
Serial.print(index+1);
Serial.print(": ");
Serial.print(volt,3);
Serial.print(" V ");
Serial.print(current,3);
Serial.println(" A");
index++;
}
}
float batteryPercent=(packVoltage/25.2)*100;
Serial.print("Pack Voltage: ");
Serial.print(packVoltage,3);
Serial.println(" V");
Serial.print("Battery Percentage: ");
Serial.print(batteryPercent);
Serial.println(" %");
Serial.println();
if(packVoltage>=upper&&charging==1)
{
digitalWrite(switching,HIGH);
charging=0;
}
if(packVoltage<=lower&&charging==0)
{
digitalWrite(switching,LOW);
charging=1;
}
if(packVoltage>lower)
{
DualAxis();
}
delay(100);
}
void DualAxis()
{
float ldrUp=analogRead(34);
float ldrDown=analogRead(35);
float ldrRight=analogRead(32);
float ldrLeft=analogRead(33);
if(abs(ldrLeft-ldrRight)>tolerance)
{
if(ldrLeft>ldrRight)
panPos-=angle;
else
panPos+=angle;
panPos=constrain(panPos,0,180);
pan.write(panPos);
}
if(abs(ldrUp-ldrDown)>tolerance)
{
if(ldrUp>ldrDown)
tiltPos+=angle;
else
tiltPos-=angle;
tiltPos=constrain(tiltPos,0,180);
tilt.write(tiltPos);
}
}