-
Notifications
You must be signed in to change notification settings - Fork 2
/
Battery.m
135 lines (127 loc) · 5.26 KB
/
Battery.m
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
123
124
125
126
127
128
129
130
131
132
133
134
135
classdef Battery < handle % handle class
properties (Constant)
S = 6
P = 1
RATED_CAPACITY = 3200 % mAh
NOMINAL_VOLTAGE = 3600 % mV
STD_CH_CURRENT = 1.625 % A
MAX_CH_CURRENT = 1700 % A
MAX_DS_CURRENT = 3200 % mA
CUTOFF_CURRENT = 65 % mA
MAX_SECURITY_CELL_VOLTAGE = 4150 % mV used for security check
MAX_CELL_VOLTAGE = 4110 % mV used for SetPoint Estimation alghoritm
MIN_CELL_VOLTAGE = 2500 % mV
CELL_VOLTAGE_START_SP_CH_REDUCTION = 4000 % mV
CELL_VOLTAGE_START_BALANCING = 4000 % mV
DELTA_VOLTAGE_EOB = 30 % mV
MAX_CELL_TEMPERATURE = 40 % °C
MIN_CELL_TEMPERATURE = 0 % °C
MAX_BMS_TEMPERATURE = 75 % °C
MANUFACTURER = 'Panasonic'
PART_NUMBER = 'NCR18650B'
DELTA_VOLTAGE_EOC = 5 % mV
R_BAL = 10 %ohm
NOTE = '6s1p battery made with 18650 lithium cells. This battery is used to test BMSino'
end
properties
BatteryName = 'Unnamed Battery'
CellsVoltages = NaN*zeros(6,1)
CellsTemperatures = NaN*zeros(6,1)
CellsBalancingStatus = NaN*zeros(6,1) %left justified
% e.g. cell 1 and 2 on balancing: 110000
BMSTemperature = NaN
TotalCurrent = NaN
TotalVoltage = NaN
BatteryFullyCharged = NaN
StateOfCharge = NaN % not yet implemented
StateOfHealth = NaN % not yet implemented
R_INT = NaN*ones(6,1) .* 38 %mOhm
SerialObj
end
% properties (Access = private)
% SerialObj
% end
methods
% Constructor
function obj = Battery(name)
if (nargin > 0 && ischar(name))
obj.BatteryName = name;
else
error('Battery wants a name. Name must be a string.');
end
end
% Destructor
% function delete(obj)
% fclose(obj.SerialObj);
% end
% Initialize serial communication
function COMinit(obj, baudrate, COMport)
if (nargin == 3 && ischar(COMport) && isnumeric(baudrate))
obj.SerialObj = serial(COMport);
set(obj.SerialObj, 'BaudRate',baudrate);
fopen(obj.SerialObj);
disp('waiting for serial communication...');
pause(2);
% ask for instrument name and check if it responds
% correctly
% obj.BatteryName = namefprintf(obj.SerialObj, 'ask for name'); %change code here
% if obj.BatteryName == 'Nome previsto'
disp('Battery serial communication initializated');
% else
% disp('Battery name is not correct: Serial communication issues?');
% end
else
error('Inputs arguments are wrong');
end
end
% Close serial communication
function COMclose(obj)
fclose(obj.SerialObj);
end
% Get Voltages
function getVoltages(obj)
fprintf(obj.SerialObj, 'mVCELL A');
obj.CellsVoltages = cell2mat(textscan(fscanf(obj.SerialObj),'%f'));
obj.TotalVoltage = sum(obj.CellsVoltages);
flushinput(obj.SerialObj);
end
% Get Temperatures
function getTemperatures(obj)
fprintf(obj.SerialObj, 'TCELL A');
obj.CellsTemperatures = cell2mat(textscan(fscanf(obj.SerialObj),'%f'));
flushinput(obj.SerialObj);
end
% Get Temperature of BMS
function getBMSTemperature(obj)
fprintf(obj.SerialObj, 'TBMS');
obj.BMSTemperature = cell2mat(textscan(fscanf(obj.SerialObj),'%f'));
flushinput(obj.SerialObj);
end
% Get Balancing Status
function getBalancingStatus(obj)
fprintf(obj.SerialObj, 'RBCELL');
obj.CellsBalancingStatus = string2binarray(fscanf((obj.SerialObj),'%s'));
flushinput(obj.SerialObj);
end
% Set Balancing Status
function setBalancingStatus(obj, bitmask_binarray)
bitmask_string = binarray2string(bitmask_binarray);
string = strcat('SBCELL' ,32, bitmask_string);
fprintf(obj.SerialObj, string);
% pause(0.3);
% fprintf(obj.SerialObj, 'RBCELL ');
% obj.CellsBalancingStatus = string2binarray(fscanf((obj.SerialObj),'%s'));
% if obj.CellsBalancingStatus == bitmask_binarray
% disp('CellsBalancingStatus wrote!')
% status = 1;
% return
% else
% disp('CellsBalancingStatus Error!')
% status = 0;
% return
% end
flushinput(obj.SerialObj);
flushoutput(obj.SerialObj);
end
end
end