-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChargeController.h
119 lines (95 loc) · 4.52 KB
/
ChargeController.h
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
#include "mbed.h"
#define open 0
#define close 1
float Ibat, Vbat, Iload, Vload, Pbat; // Variables for current, voltage for battery and load, and power from battery
float SoC; // Variable for State of Charge of the battery pack
const float VbMax = 8; //[v] Maximum Battery voltage (SoC = 100) //// Need to verify value
const int SoCMax = 95; // Maximum allowed battery State of Charge //// Need to verify value
const int SoCDanger = 97; // Dangerously high SoC //// Need to verify value
const int SoCMin = 20; // Minimum allowed battery SoC //// Need to verify value
const float BatCap = 3800; // [mAh] //// Need to verify value
const float IbatDanger = 2*BatCap/1000; // [A] maximum safe charging current
const float IbatMax = IbatDanger*0.8;
const float LOADCURRENT_SENSOR_MAX = 11; // [A] maximum current the sensor is able to sense properly
const float BATVOLTAGE_SENSOR_MAX = 9; // [V] maximum voltage the sensor is able to sense properly
/*
Control the power flow in the system using switches
*/
class ChargeController{
private:
/* Control signal to be read by the main code. Will tell it how to control the MPPT.
0 = track MPPT
1 = pause MPPT
2 = track Pload
3 = track Pload + PbatMax (overcurrent protection)
4 = reset MPPT
*/
int Control;
float Pload; // Power used by the load
float PbatMax; // Maximum allowed battery charging power
AnalogIn *BatVoltageSensor; // Pointer to the Battery Voltage Sensor object
AnalogIn *LoadCurrentSensor; // Pointer to the Load Current Sensor object
DigitalOut *BatSwitch; // This switch can disconnect the Batteries from the PV system, not allowing them to charge
float readVbat(){
return BatVoltageSensor->read()*BATVOLTAGE_SENSOR_MAX;
};
float readIload(){
return LoadCurrentSensor->read()*LOADCURRENT_SENSOR_MAX;
};
public:
float Ppv; // Incoming power from solar panels
ChargeController(PinName BatVPin, PinName LoadIPin, PinName BatSwPin);
void run(); // Execute charge controller algorithm
int readControl(); // Allows the control signal to be read
float readPload(); // Allows the load power to be read
float readPbatMax(); // Allows PbatMax to be read
};
ChargeController::ChargeController(PinName BatVPin, PinName LoadIPin, PinName BatSwPin){
BatVoltageSensor = new AnalogIn(BatVPin);
LoadCurrentSensor = new AnalogIn(LoadIPin);
BatSwitch = new DigitalOut(BatSwPin);
};
int ChargeController::readControl(){
return Control; // Return control signal
}
float ChargeController::readPload(){
return Pload; // Return Pload
}
float ChargeController::readPbatMax(){
return PbatMax;
}
void ChargeController::run(){
Vbat = readVbat(); // read battery voltage
SoC = Vbat/VbMax*100; // Determine state of charge [%] //// This formula is incorrect, need to change
Iload = readIload(); // read load current
Vload = Vbat; // Load is in parallel with the batteries
Pload = Iload*Vload; // Calculate power used by the load
Ibat = (Ppv - Pload)/Vbat; // Calculate current flowing into battery
PbatMax = IbatMax*Vbat; // Calculate maximum allowed battery charging power
if(Ibat >= IbatDanger){ // Battery charging current is dangerously high
Control = 4; // Reset MPPT (open MOSFET and reset duty cycle to zero)
BatSwitch->write(open); // Disconnect battery
}
if (SoC >= SoCDanger) { // Battery charge is dangerously high
Control = 1; // Pause MPPT (open MOSFET and pause algorithm)
BatSwitch->write(open); // Disconnect battery
}
else if (SoC >= SoCMax && Ppv >= Pload) { // Both SoC and Ppv are too high
Control = 2; // Tell main loop to read Pload. MPPTs must try to set Ppv equal to Pload
BatSwitch->write(close); // Keep Battery connected to absorb excess current
}
else if (Ibat >= IbatMax){ // Battery charging current is very high, but within limits
Control = 3; // Track lower power point
BatSwitch->write(close); // charge battery
}
/* Might need to implement some control algorithm in case of low voltage */
else if (SoC <= SoCMin){ // Battery charge is too low
Control = 0;
BatSwitch->write(open); // prevent further discharging
}
else
{
Control = 0;
BatSwitch->write(close);
}
}