forked from Dr-Gigavolt/dbus-aggregate-batteries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregatebatteries.py
1113 lines (944 loc) · 66.6 KB
/
aggregatebatteries.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Service to aggregate multiple serial batteries https://github.com/Louisvdw/dbus-serialbattery
to one virtual battery.
Python location on Venus:
/usr/bin/python3.8
/usr/lib/python3.8/site-packages/
References:
https://dbus.freedesktop.org/doc/dbus-python/tutorial.html
https://github.com/victronenergy/venus/wiki/dbus
https://github.com/victronenergy/velib_python
"""
VERSION = '1.0'
from gi.repository import GLib
import logging
import sys
import os
import dbus
import re
from settings import *
from functions import *
from datetime import datetime as dt # for UTC time stamps for logging
import time as tt # for charge measurement
from dbusmon import DbusMon
from threading import Thread
sys.path.append('/opt/victronenergy/dbus-systemcalc-py/ext/velib_python')
from vedbus import VeDbusService
# localsettings: see also https://github.com/victronenergy/localsettings
from settingsdevice import SettingsDevice # available in the velib_python repository
#class DbusVariable(object):
#
# def __init__(self, service):
# self._path: str = ''
# self._dbusservice: VeDbusService = service._dbusservice
# self._add_params: list = {}
class DbusAggBatService(object):
def __init__(self, servicename='com.victronenergy.battery.aggregate'):
self._fn = Functions()
self._batteries_dict = {} # marvo2011
self._multi = None
self._grid = None
self._mppts_list = []
self._smartShunt = None
self._searchTrials = 0
self._readTrials = 0
self._MaxChargeVoltage_old = 0
self._MaxChargeCurrent_old = 0
self._MaxDischargeCurrent_old = 0
self._fullyDischarged = False # implementing hysteresis for allowing discharge
self._dbusservice = VeDbusService(servicename)
self._dbusConn = dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.SystemBus()
self._timeOld = tt.time()
self._DCfeedActive = False # written when dynamic CVL limit activated
self._balancing = 0 # 0: inactive; 1: goal reached, waiting for discharging under nominal voltage; 2: nominal voltage reached
self._lastBalancing = 0 # Day in year
self._dynamicCVL = False # set if the CVL needs to be reduced due to peaking
self._logTimer = 0 # measure logging period in seconds
self._EssActive = 0
self._SmoothFilter = 250
self._MaxChargeCurrentSm = 0
self._MinSocLimit = 0
self._CorrectionI = 0
# read initial charge from text file
try:
self._charge_file = open('/data/dbus-aggregate-batteries/charge', 'r') # read
self._ownCharge = float(self._charge_file.readline().strip())
self._charge_file.close()
self._ownCharge_old = self._ownCharge
logging.info('%s: Initial Ah read from file: %.0fAh' % ((dt.now()).strftime('%c'), self._ownCharge))
except Exception:
logging.error('%s: Charge file read error. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
if OWN_CHARGE_PARAMETERS: # read the day of the last balancing from text file
try:
self._lastBalancing_file = open('/data/dbus-aggregate-batteries/last_balancing', 'r') # read
self._lastBalancing = int(self._lastBalancing_file.readline().strip())
self._lastBalancing_file.close()
time_unbalanced = int((dt.now()).strftime('%j')) - self._lastBalancing # in days
if time_unbalanced < 0:
time_unbalanced += 365 # year change
logging.info('%s: Last balancing done at the %d. day of the year' % ((dt.now()).strftime('%c'), self._lastBalancing))
logging.info('Batteries balanced %d days ago.' % time_unbalanced)
except Exception:
logging.error('%s: Last balancing file read error. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
# Create the mandatory objects
self._dbusservice.add_mandatory_paths(processname = __file__, processversion = '0.0', connection = 'Virtual',
deviceinstance = 0, productid = 0, productname = 'AggregateBatteries', firmwareversion = VERSION,
hardwareversion = '0.0', connected = 1)
# Create DC paths
self._dbusservice.add_path('/Dc/0/Voltage', None, writeable=True, gettextcallback=lambda a, x: "{:.2f}V".format(x))
self._dbusservice.add_path('/Dc/0/Current', None, writeable=True, gettextcallback=lambda a, x: "{:.2f}A".format(x))
self._dbusservice.add_path('/Dc/0/Power', None, writeable=True, gettextcallback=lambda a, x: "{:.0f}W".format(x))
# Create capacity paths
self._dbusservice.add_path('/Soc', None, writeable=True)
self._dbusservice.add_path('/Capacity', None, writeable=True, gettextcallback=lambda a, x: "{:.0f}Ah".format(x))
self._dbusservice.add_path('/InstalledCapacity', None, gettextcallback=lambda a, x: "{:.0f}Ah".format(x))
self._dbusservice.add_path('/ConsumedAmphours', None, gettextcallback=lambda a, x: "{:.0f}Ah".format(x))
# Create temperature paths
self._dbusservice.add_path('/Dc/0/Temperature', None, writeable=True)
self._dbusservice.add_path('/System/MinCellTemperature', None, writeable=True)
self._dbusservice.add_path('/System/MaxCellTemperature', None, writeable=True)
# Create extras paths
self._dbusservice.add_path('/System/MinCellVoltage', None, writeable=True, gettextcallback=lambda a, x: "{:.3f}V".format(x)) # marvo2011
self._dbusservice.add_path('/System/MinVoltageCellId', None, writeable=True)
self._dbusservice.add_path('/System/MaxCellVoltage', None, writeable=True, gettextcallback=lambda a, x: "{:.3f}V".format(x)) # marvo2011
self._dbusservice.add_path('/System/MaxVoltageCellId', None, writeable=True)
self._dbusservice.add_path('/System/NrOfCellsPerBattery', None, writeable=True)
self._dbusservice.add_path('/System/NrOfModulesOnline', None, writeable=True)
self._dbusservice.add_path('/System/NrOfModulesOffline', None, writeable=True)
self._dbusservice.add_path('/System/NrOfModulesBlockingCharge', None, writeable=True)
self._dbusservice.add_path('/System/NrOfModulesBlockingDischarge', None, writeable=True)
self._dbusservice.add_path('/Voltages/Sum', None, writeable=True, gettextcallback=lambda a, x: "{:.3f}V".format(x))
self._dbusservice.add_path('/Voltages/Diff', None, writeable=True, gettextcallback=lambda a, x: "{:.3f}V".format(x))
self._dbusservice.add_path('/TimeToGo', None, writeable=True)
# Create alarm paths
self._dbusservice.add_path('/Alarms/LowVoltage', None, writeable=True)
self._dbusservice.add_path('/Alarms/HighVoltage', None, writeable=True)
self._dbusservice.add_path('/Alarms/LowCellVoltage', None, writeable=True)
#self._dbusservice.add_path('/Alarms/HighCellVoltage', None, writeable=True)
self._dbusservice.add_path('/Alarms/LowSoc', None, writeable=True)
self._dbusservice.add_path('/Alarms/HighChargeCurrent', None, writeable=True)
self._dbusservice.add_path('/Alarms/HighDischargeCurrent', None, writeable=True)
self._dbusservice.add_path('/Alarms/CellImbalance', None, writeable=True)
self._dbusservice.add_path('/Alarms/InternalFailure', None, writeable=True)
self._dbusservice.add_path('/Alarms/HighChargeTemperature', None, writeable=True)
self._dbusservice.add_path('/Alarms/LowChargeTemperature', None, writeable=True)
self._dbusservice.add_path('/Alarms/HighTemperature', None, writeable=True)
self._dbusservice.add_path('/Alarms/LowTemperature', None, writeable=True)
self._dbusservice.add_path('/Alarms/BmsCable', None, writeable=True)
# Create control paths
self._dbusservice.add_path('/Info/MaxChargeCurrent', None, writeable=True, gettextcallback=lambda a, x: "{:.1f}A".format(x))
self._dbusservice.add_path('/Info/MaxDischargeCurrent', None, writeable=True, gettextcallback=lambda a, x: "{:.1f}A".format(x))
self._dbusservice.add_path('/Info/MaxChargeVoltage', None, writeable=True, gettextcallback=lambda a, x: "{:.2f}V".format(x))
self._dbusservice.add_path('/Io/AllowToCharge', None, writeable=True)
self._dbusservice.add_path('/Io/AllowToDischarge', None, writeable=True)
self._dbusservice.add_path('/Io/AllowToBalance', None, writeable=True)
# Create battery current control paths
self._dbusservice.add_path('/Ess/Active', 0, writeable=True, onchangecallback=self._onDbusUpdate)
self._dbusservice.add_path('/Ess/BatteryP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/BatteryI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/BatteryCalcI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/MpptP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/MpptI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/AcInP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/AcInI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/AcOutP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/AcOutI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/InverterP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/InverterI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/MaxChargeP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/MaxChargeI', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/MaxChargeIsm', None, writeable=False, gettextcallback=lambda a, x: "{:.2f} A".format(x))
self._dbusservice.add_path('/Ess/GridSetpoint', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/GridP', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/AcPowerSetpoint', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} W".format(x))
self._dbusservice.add_path('/Ess/MaxChrgCellVoltage', None, writeable=False, gettextcallback=lambda a, x: "{:.3f} V".format(x))
self._dbusservice.add_path('/Ess/SmoothFilter', self._SmoothFilter, writeable=True, onchangecallback=self._onDbusUpdate)
self._dbusservice.add_path('/Ess/ConsumptionInputL1', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/ConsumptionInputL2', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/ConsumptionInputL3', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/ConsumptionInput', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/PvOnGridL1', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/PvOnGridL2', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/PvOnGridL3', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/PvOnGrid', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/AcLoadL1', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/AcLoadL2', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/AcLoadL3', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/AcLoad', None, writeable=False, gettextcallback=lambda a, x: "{:.1f} W".format(x))
self._dbusservice.add_path('/Ess/CorrectionI', None, writeable=False, gettextcallback=lambda a, x: "{:.3f} A".format(x))
self._dbusservice.add_path('/Ess/MinimumSocLimit', None, writeable=False, gettextcallback=lambda a, x: "{:.0f} %".format(x))
# add settings #
self.settings = SettingsDevice(
bus=dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.SystemBus(),
supportedSettings={
'Active': ['/Settings/MyEss/Active', 4, 0, 5],
'MinSocLimit': ['/Settings/MyEss/MinSocLimit', 20, 0, 100],
'CorrectionI': ['/Settings/MyEss/CorrectionI', 0.419, -10.0, 10.0],
'SmoothFilter' : ['/Settings/MyEss/SmoothFilter', 250, 0, 1000],
},
eventCallback=self._handle_changed_setting)
x = Thread(target = self._startMonitor)
x.start()
GLib.timeout_add(10000, self._find_settings) # search com.victronenergy.settings
##############################################################################################################
##############################################################################################################
### Starting battery dbus monitor in external thread (otherwise collision with AggregateBatteries service) ###
##############################################################################################################
##############################################################################################################
def _startMonitor(self):
logging.info('%s: Starting battery monitor.' % (dt.now()).strftime('%c'))
self._dbusMon = DbusMon()
#####################################################################
#####################################################################
### switch Ess test features on/off ###
#####################################################################
#####################################################################
def _onDbusUpdate(self, path, value):
return True
#####################################################################
#####################################################################
### local setting changed callback funtion ###
#####################################################################
#####################################################################
def _handle_changed_setting(self, setting, oldvalue, newvalue):
if setting == 'Active':
if newvalue == 0:
self._EssActive = newvalue
self._dbusMon.dbusmon.set_value('com.victronenergy.settings', '/Settings/CGwacs/Hub4Mode', 1)
logging.info('%s: Hub4Mode set to normal control!' % ((dt.now()).strftime('%c')))
elif newvalue > 0 and newvalue <=5:
self._EssActive = newvalue
self._dbusMon.dbusmon.set_value('com.victronenergy.settings', '/Settings/CGwacs/Hub4Mode', 3)
logging.info('%s: Hub4Mode set to external control!' % ((dt.now()).strftime('%c')))
else:
logging.info('%s: wrong value! Reset to old value!' % ((dt.now()).strftime('%c')))
elif setting == 'CorrectionI':
self._CorrectionI = newvalue
elif setting == 'MinSocLimit':
self._MinSocLimit = newvalue
elif setting == 'SmoothFilter':
self._SmoothFilter = newvalue
logging.info('%s: /Ess/SmoothFilter manually set to %d' % ((dt.now()).strftime('%c'), self._SmoothFilter))
logging.info('%s: setting changed, setting: %s, old: %s, new: %s' % ((dt.now()).strftime('%c'), setting, oldvalue, newvalue))
return
#####################################################################
#####################################################################
### search Settings, to maintain CCL during dynamic CVL reduction ###
# https://www.victronenergy.com/upload/documents/Cerbo_GX/140558-CCGX__Venus_GX__Cerbo_GX__Cerbo-S_GX_Manual-pdf-en.pdf, P72
#####################################################################
#####################################################################
def _find_settings(self):
logging.info('%s: Searching Settings: Trial Nr. %d' % ((dt.now()).strftime('%c'),(self._searchTrials + 1)))
try:
for service in self._dbusConn.list_names():
if 'com.victronenergy.settings' in service:
self._settings = service
logging.info('%s: com.victronenergy.settings found.' % (dt.now()).strftime('%c'))
except Exception:
pass
if (self._settings != None):
self._searchTrials = 0
GLib.timeout_add(1000, self._find_batteries) # search batteries on DBus if present ##### !!! was 5000 cg
return False # all OK, stop calling this function
elif self._searchTrials < SEARCH_TRIALS:
self._searchTrials += 1
return True # next trial
else:
logging.error('%s: com.victronenergy.settings not found. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
#####################################################################
#####################################################################
### search physical batteries and optional SmartShunt on DC loads ###
#####################################################################
#####################################################################
def _find_batteries(self):
self._batteries_dict = {} # Marvo2011
batteriesCount = 0
productName = ''
logging.info('%s: Searching batteries: Trial Nr. %d' % ((dt.now()).strftime('%c'),(self._searchTrials + 1)))
try: # if Dbus monitor not running yet, new trial instead of exception
for service in self._dbusConn.list_names():
if BATTERY_SERVICE_NAME in service:
productName = self._dbusMon.dbusmon.get_value(service, BATTERY_PRODUCT_NAME_PATH)
if BATTERY_PRODUCT_NAME in productName:
# Custom name, if exists, Marvo2011
try:
BatteryName = self._dbusMon.dbusmon.get_value(service, BATTERY_INSTANCE_NAME_PATH)
except Exception:
BatteryName = 'Battery%d' % (batteriesCount + 1)
# Check if all batteries have custom names
if BatteryName in self._batteries_dict:
BatteryName = '%s%d' %(BatteryName, batteriesCount + 1)
self._batteries_dict[BatteryName] = service
logging.info('%s: %s found, named as: %s.' % ((dt.now()).strftime('%c'),(self._dbusMon.dbusmon.get_value(service, '/ProductName')), BatteryName))
batteriesCount += 1
# Create voltage paths with battery names
if SEND_CELL_VOLTAGES == 1:
for cellId in range(1, (NR_OF_CELLS_PER_BATTERY) + 1):
self._dbusservice.add_path('/Voltages/%s_Cell%d' % (re.sub('[^A-Za-z0-9_]+', '', BatteryName), cellId), None, writeable=True, gettextcallback=lambda a, x: "{:.3f}V".format(x))
# Check if Nr. of cells is equal
if self._dbusMon.dbusmon.get_value(service, '/System/NrOfCellsPerBattery') != NR_OF_CELLS_PER_BATTERY:
logging.error('%s: Number of cells of batteries is not correct. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
# end of section, Marvo2011
elif SMARTSHUNT_NAME_KEY_WORD in productName: # if SmartShunt found, can be used for DC load current
self._smartShunt = service
except Exception:
pass
logging.info('%s: %d batteries found.' % ((dt.now()).strftime('%c'), batteriesCount))
if batteriesCount == NR_OF_BATTERIES:
if CURRENT_FROM_VICTRON:
self._searchTrials = 0
GLib.timeout_add(1000, self._find_multis) # if current from Victron stuff search multi/quattro on DBus
else:
self._timeOld = tt.time()
GLib.timeout_add(UPDATE_INTV, self._update) # if current from BMS start the _update loop
return False # all OK, stop calling this function
elif self._searchTrials < SEARCH_TRIALS:
self._searchTrials += 1
return True # next trial
else:
logging.error('%s: Required number of batteries not found. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
##########################################################################
##########################################################################
### search Multis or Quattros (if selected for DC current measurement) ###
##########################################################################
##########################################################################
def _find_multis(self):
logging.info('%s: Searching Multi/Quatro VEbus: Trial Nr. %d' % ((dt.now()).strftime('%c'),(self._searchTrials + 1)))
try:
for service in self._dbusConn.list_names():
if MULTI_KEY_WORD in service:
self._multi = service
logging.info('%s: %s found.' % ((dt.now()).strftime('%c'),(self._dbusMon.dbusmon.get_value(service, '/ProductName'))))
except Exception:
pass
if (self._multi != None):
if (NR_OF_MPPTS > 0):
self._searchTrials = 0
GLib.timeout_add(1000, self._find_mppts) # search MPPTs on DBus if present
else:
self._timeOld = tt.time()
GLib.timeout_add(UPDATE_INTV, self._update) # if no MPPTs start the _update loop
return False # all OK, stop calling this function
elif self._searchTrials < SEARCH_TRIALS:
self._searchTrials += 1
return True # next trial
else:
logging.error('%s: Multi/Quattro not found. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
#############################################################
#############################################################
### search MPPTs (if selected for DC current measurement) ###
#############################################################
#############################################################
def _find_mppts(self):
self._mppts_list = []
mpptsCount = 0
logging.info('%s: Searching MPPTs: Trial Nr. %d' % ((dt.now()).strftime('%c'),(self._searchTrials + 1)))
try:
for service in self._dbusConn.list_names():
if MPPT_KEY_WORD in service:
self._mppts_list.append(service)
logging.info('%s: %s found.' % ((dt.now()).strftime('%c'),(self._dbusMon.dbusmon.get_value(service, '/ProductName'))))
mpptsCount += 1
except Exception:
pass
logging.info('%s: %d MPPT(s) found.' % ((dt.now()).strftime('%c'), mpptsCount))
if mpptsCount == NR_OF_MPPTS:
self._timeOld = tt.time()
GLib.timeout_add(1000, self._find_grid)
return False # all OK, stop calling this function
elif self._searchTrials < SEARCH_TRIALS:
self._searchTrials += 1
return True # next trial
else:
logging.error('%s: Required number of MPPTs not found. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
#############################################################
#############################################################
### search grid meter ###
#############################################################
#############################################################
def _find_grid(self):
logging.info('%s: Searching grid meter: Trial Nr. %d' % ((dt.now()).strftime('%c'),(self._searchTrials + 1)))
try:
for service in self._dbusConn.list_names():
# logging.error('%s: service=%s' % ((dt.now()).strftime('%c'),service))
if GRID_KEY_WORD in service:
self._grid = service
logging.info('%s: %s found.' % ((dt.now()).strftime('%c'),(self._dbusMon.dbusmon.get_value(service, '/ProductName'))))
except Exception:
logging.info('%s: Exception' % (dt.now()).strftime('%c'))
if (self._grid != None):
self._timeOld = tt.time()
GLib.timeout_add(UPDATE_INTV, self._update) # if no MPPTs start the _update loop
return False # all OK, stop calling this function
elif self._searchTrials < SEARCH_TRIALS:
self._searchTrials += 1
return True # next trial
else:
logging.error('%s: Grid meter not found. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
##################################################################################
##################################################################################
#### aggregate values of physical batteries, perform calculations, update Dbus ###
##################################################################################
##################################################################################
def _update(self):
# DC
Voltage = 0
Current = 0
Power = 0
# Capacity
Soc = 0
Capacity = 0
InstalledCapacity = 0
ConsumedAmphours = 0
TimeToGo = 0
# Temperature
Temperature = 0
MaxCellTemp_list = [] # list, maxima of all physical batteries
MinCellTemp_list = [] # list, minima of all physical batteries
# Extras
cellVoltages_dict = {}
MaxCellVoltage_dict = {} # dictionary {'ID' : MaxCellVoltage, ... } for all physical batteries
MinCellVoltage_dict = {} # dictionary {'ID' : MinCellVoltage, ... } for all physical batteries
NrOfModulesOnline = 0
NrOfModulesOffline = 0
NrOfModulesBlockingCharge = 0
NrOfModulesBlockingDischarge = 0
VoltagesSum_dict = {} # battery voltages from sum of cells, Marvo2011
chargeVoltageReduced_list = []
# Alarms
LowVoltage_alarm_list = [] # lists to find maxima
HighVoltage_alarm_list = []
LowCellVoltage_alarm_list = []
LowSoc_alarm_list = []
HighChargeCurrent_alarm_list = []
HighDischargeCurrent_alarm_list = []
CellImbalance_alarm_list = []
InternalFailure_alarm_list = []
HighChargeTemperature_alarm_list = []
LowChargeTemperature_alarm_list = []
HighTemperature_alarm_list = []
LowTemperature_alarm_list = []
BmsCable_alarm_list = []
# Charge/discharge parameters
MaxChargeCurrent_list = [] # the minimum of MaxChargeCurrent * NR_OF_BATTERIES to be transmitted
MaxDischargeCurrent_list = [] # the minimum of MaxDischargeCurrent * NR_OF_BATTERIES to be transmitted
MaxChargeVoltage_list = [] # if some cells are above MAX_CELL_VOLTAGE, store here the sum of differences for each battery
AllowToCharge_list = [] # minimum of all to be transmitted
AllowToDischarge_list = [] # minimum of all to be transmitted
AllowToBalance_list = [] # minimum of all to be transmitted
ChargeMode_list = [] # Bulk, Absorption, Float, Keep always max voltage
# Ess stuff
BatteryPower = 0
BatteryCurrent = 0
MpptCurrent = 0
MpptPower = 0
AcInPower = 0
AcInCurrent = 0
AcOutPower = 0
AcOutCurrent = 0
InverterPower = 0
InverterCurrent = 0
MaxChargePower = 0
MaxChargeCurrent = 0
MaxChargeVoltage = 0
MaxDischargePower = 0
MaxDischargeCurrent = 0
GridSetpoint = 0
GridPower = 0
GridL1 = 0
GridL2 = 0
GridL3 = 0
AcPowerSetpoint = 0
BatteryCurrentCalc = 0
MaxChrgCellVoltage = 0
ConsumptionInputL1 = 0
ConsumptionInputL2 = 0
ConsumptionInputL3 = 0
ConsumptionInput = 0
PvOnGridL1 = 0
PvOnGridL2 = 0
PvOnGridL3 = 0
PvOnGrid = 0
AcLoadL1 = 0
AcLoadL2 = 0
AcLoadL3 = 0
AcLoad = 0
MinimumSocLimit = 0
####################################################
# Get DBus values from all SerialBattery instances #
####################################################
try:
for i in self._batteries_dict: # Marvo2011
# DC
step = 'Read V, I, P' # to detect error
Voltage += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Dc/0/Voltage')
Current += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Dc/0/Current')
Power += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Dc/0/Power')
# Capacity
step = 'Read and calculate capacity, SoC, Time to go'
InstalledCapacity += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/InstalledCapacity')
if not OWN_SOC:
ConsumedAmphours += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/ConsumedAmphours')
Capacity += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Capacity')
Soc += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Soc') * self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Capacity')
ttg = self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/TimeToGo')
if (ttg != None) and (TimeToGo != None):
TimeToGo += ttg * self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Capacity')
else:
TimeToGo = None
# Temperature
step = 'Read temperatures'
Temperature += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Dc/0/Temperature')
MaxCellTemp_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MaxCellTemperature'))
MinCellTemp_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MinCellTemperature'))
# Cell voltages
step = 'Read max. and min cell voltages and voltage sum' # cell ID : its voltage
MaxCellVoltage_dict['%s_%s' % (i, self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MaxVoltageCellId'))]\
= self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MaxCellVoltage')
MinCellVoltage_dict['%s_%s' % (i, self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MinVoltageCellId'))]\
= self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/MinCellVoltage')
VoltagesSum_dict[i] = self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Voltages/Sum')
# Battery state
step = 'Read battery state'
NrOfModulesOnline += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/NrOfModulesOnline')
NrOfModulesOffline += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/NrOfModulesOffline')
NrOfModulesBlockingCharge += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/NrOfModulesBlockingCharge')
NrOfModulesBlockingDischarge += self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/System/NrOfModulesBlockingDischarge') # sum of modules blocking discharge
step = 'Read cell voltages'
for j in range(NR_OF_CELLS_PER_BATTERY): # Marvo2011
cellVoltages_dict['%s_Cell%d' % (i, j+1)] = self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Voltages/Cell%d' % (j+1))
# Alarms
step = 'Read alarms'
LowVoltage_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/LowVoltage'))
HighVoltage_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/HighVoltage'))
LowCellVoltage_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/LowCellVoltage'))
LowSoc_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/LowSoc'))
HighChargeCurrent_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/HighChargeCurrent'))
HighDischargeCurrent_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/HighDischargeCurrent'))
CellImbalance_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/CellImbalance'))
InternalFailure_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/InternalFailure_alarm'))
HighChargeTemperature_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/HighChargeTemperature'))
LowChargeTemperature_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/LowChargeTemperature'))
HighTemperature_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/HighTemperature'))
LowTemperature_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/LowTemperature'))
BmsCable_alarm_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Alarms/BmsCable'))
if OWN_CHARGE_PARAMETERS: # calculate reduction of charge voltage as sum of overvoltages of all cells
step = 'Calculate CVL reduction'
cellOvervoltage = 0
for j in range (NR_OF_CELLS_PER_BATTERY): # Marvo2011
cellVoltage = self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Voltages/Cell%d' % (j+1))
if (cellVoltage > MAX_CELL_VOLTAGE):
cellOvervoltage += (cellVoltage - MAX_CELL_VOLTAGE)
chargeVoltageReduced_list.append(VoltagesSum_dict[i] - cellOvervoltage)
else: # Aggregate charge/discharge parameters
step = 'Read charge parameters'
MaxChargeCurrent_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Info/MaxChargeCurrent')) # list of max. charge currents to find minimum
MaxDischargeCurrent_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Info/MaxDischargeCurrent')) # list of max. discharge currents to find minimum
MaxChargeVoltage_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Info/MaxChargeVoltage')) # list of max. charge voltages to find minimum
ChargeMode_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Info/ChargeMode')) # list of charge modes of batteries (Bulk, Absorption, Float, Keep always max voltage)
step = 'Read Allow to'
AllowToCharge_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Io/AllowToCharge')) # list of AllowToCharge to find minimum
AllowToDischarge_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Io/AllowToDischarge')) # list of AllowToDischarge to find minimum
AllowToBalance_list.append(self._dbusMon.dbusmon.get_value(self._batteries_dict[i], '/Io/AllowToBalance')) # list of AllowToBalance to find minimum
step = 'Find max. and min. cell voltage of all batteries'
# placed in try-except structure for the case if some values are of None. The _max() and _min() don't work with dictionaries
MaxVoltageCellId = max(MaxCellVoltage_dict, key = MaxCellVoltage_dict.get)
MaxCellVoltage = MaxCellVoltage_dict[MaxVoltageCellId]
MinVoltageCellId = min(MinCellVoltage_dict, key = MinCellVoltage_dict.get)
MinCellVoltage = MinCellVoltage_dict[MinVoltageCellId]
except Exception as err:
self._readTrials += 1
logging.error('%s: Error: %s.' % ((dt.now()).strftime('%c'), err))
logging.error('Occured during step %s, Battery %s.' % (step, i))
logging.error('Read trial nr. %d' % self._readTrials)
if (self._readTrials > READ_TRIALS):
logging.error('%s: DBus read failed. Exiting.' % (dt.now()).strftime('%c'))
sys.exit()
else:
return True # next call allowed
self._readTrials = 0 # must be reset after try-except
#####################################################
# Process collected values (except of dictionaries) #
#####################################################
# averaging
Voltage = Voltage / NR_OF_BATTERIES
Temperature = Temperature / NR_OF_BATTERIES
VoltagesSum = sum(VoltagesSum_dict.values()) / NR_OF_BATTERIES # Marvo2011
# find max and min cell temperature (have no ID)
MaxCellTemp = self._fn._max(MaxCellTemp_list)
MinCellTemp = self._fn._min(MinCellTemp_list)
# find max in alarms
LowVoltage_alarm = self._fn._max(LowVoltage_alarm_list)
HighVoltage_alarm = self._fn._max(HighVoltage_alarm_list)
LowCellVoltage_alarm = self._fn._max(LowCellVoltage_alarm_list)
LowSoc_alarm = self._fn._max(LowSoc_alarm_list)
HighChargeCurrent_alarm = self._fn._max(HighChargeCurrent_alarm_list)
HighDischargeCurrent_alarm = self._fn._max(HighDischargeCurrent_alarm_list)
CellImbalance_alarm = self._fn._max(CellImbalance_alarm_list)
InternalFailure_alarm = self._fn._max(InternalFailure_alarm_list)
HighChargeTemperature_alarm = self._fn._max(HighChargeTemperature_alarm_list)
LowChargeTemperature_alarm = self._fn._max(LowChargeTemperature_alarm_list)
HighTemperature_alarm = self._fn._max(HighTemperature_alarm_list)
LowTemperature_alarm = self._fn._max(LowTemperature_alarm_list)
BmsCable_alarm = self._fn._max(BmsCable_alarm_list)
# find max. charge voltage (if needed)
#if not OWN_CHARGE_PARAMETERS:
if self._fn._min(MaxChargeVoltage_list):
MaxChargeVoltage = self._fn._min(MaxChargeVoltage_list) # add KEEP_MAX_CVL
if self._fn._min(MaxChargeCurrent_list):
MaxChargeCurrent = self._fn._min(MaxChargeCurrent_list) * NR_OF_BATTERIES
if self._fn._min(MaxDischargeCurrent_list):
MaxDischargeCurrent = self._fn._min(MaxDischargeCurrent_list) * NR_OF_BATTERIES
AllowToCharge = self._fn._min(AllowToCharge_list)
AllowToDischarge = self._fn._min(AllowToDischarge_list)
AllowToBalance = self._fn._min(AllowToBalance_list)
####################################
# Measure current by Victron stuff #
####################################
if CURRENT_FROM_VICTRON:
try:
Current_VE = self._dbusMon.dbusmon.get_value(self._multi, '/Dc/0/Current') # get DC current of multi/quattro (or system of them)
for i in range(NR_OF_MPPTS):
MpptCurrent += self._dbusMon.dbusmon.get_value(self._mppts_list[i], '/Dc/0/Current') # add DC current of all MPPTs (if present)
Current_VE += MpptCurrent
MpptPower = MpptCurrent * Voltage
if DC_LOADS:
if INVERT_SMARTSHUNT:
Current_VE += self._dbusMon.dbusmon.get_value(self._smartShunt, '/Dc/0/Current') # SmartShunt is monitored as a battery
else:
Current_VE -= self._dbusMon.dbusmon.get_value(self._smartShunt, '/Dc/0/Current')
if Current_VE is not None:
Current = Current_VE # BMS current overwritten only if no exception raised
Power = Voltage * Current_VE # calculate own power (not read from BMS)
else:
logging.error('%s: Victron current is None. Using BMS current and power instead.' % (dt.now()).strftime('%c')) # the BMS values are not overwritten
except Exception:
logging.error('%s: Victron current read error. Using BMS current and power instead.' % (dt.now()).strftime('%c')) # the BMS values are not overwritten
####################################################################################################
# Calculate own charge/discharge parameters (overwrite the values received from the SerialBattery) #
####################################################################################################
if OWN_CHARGE_PARAMETERS:
CVL_NORMAL = NR_OF_CELLS_PER_BATTERY * CHARGE_VOLTAGE_LIST[int((dt.now()).strftime('%m')) - 1]
CVL_BALANCING = NR_OF_CELLS_PER_BATTERY * BALANCING_VOLTAGE
ChargeVoltageBattery = CVL_NORMAL
time_unbalanced = int((dt.now()).strftime('%j')) - self._lastBalancing # in days
if time_unbalanced < 0:
time_unbalanced += 365 # year change
if (CVL_BALANCING > CVL_NORMAL): # if the normal charging voltage is lower then 100% SoC
# manage balancing voltage
if (self._balancing == 0) and (time_unbalanced >= BALANCING_REPETITION):
self._balancing = 1 # activate increased CVL for balancing
logging.info('%s: CVL increase for balancing activated.' % (dt.now()).strftime('%c'))
if self._balancing == 1:
ChargeVoltageBattery = CVL_BALANCING
if (Voltage >= 0.999 * CVL_BALANCING):
self._ownCharge = InstalledCapacity # reset Coulumb counter to 100%
if ((MaxCellVoltage - MinCellVoltage) < CELL_DIFF_MAX):
self._balancing = 2;
logging.info('%s: Balancing goal reached.' % (dt.now()).strftime('%c'))
if self._balancing >= 2:
ChargeVoltageBattery = CVL_BALANCING # keep balancing voltage at balancing day until decrease of solar powers and
if Voltage <= CVL_NORMAL: # the charge above "normal" is consumed
self._balancing = 0;
self._lastBalancing = int((dt.now()).strftime('%j'))
self._lastBalancing_file = open('/data/dbus-aggregate-batteries/last_balancing', 'w')
self._lastBalancing_file.write('%s' % self._lastBalancing)
self._lastBalancing_file.close()
logging.info('%s: CVL increase for balancing de-activated.' % (dt.now()).strftime('%c'))
if self._balancing == 0:
ChargeVoltageBattery = CVL_NORMAL
elif (time_unbalanced > 0) and (Voltage >= 0.999 * CVL_BALANCING) and ((MaxCellVoltage - MinCellVoltage) < CELL_DIFF_MAX): # if normal charging voltage is 100% SoC and balancing is finished
self._ownCharge = InstalledCapacity # reset Coulumb counter to 100%
logging.info('%s: Balancing goal reached with full charging set as normal. Updating last_balancing file.' % (dt.now()).strftime('%c'))
self._lastBalancing = int((dt.now()).strftime('%j'))
self._lastBalancing_file = open('/data/dbus-aggregate-batteries/last_balancing', 'w')
self._lastBalancing_file.write('%s' % self._lastBalancing)
self._lastBalancing_file.close()
# manage dynamic CVL reduction
if MaxCellVoltage >= MAX_CELL_VOLTAGE:
if not self._dynamicCVL:
self._dynamicCVL = True
logging.info('%s: Dynamic CVL reduction started.' % (dt.now()).strftime('%c'))
if self._DCfeedActive == False: # avoid periodic readout if once set True
self._DCfeedActive = self._dbusMon.dbusmon.get_value('com.victronenergy.settings', '/Settings/CGwacs/OvervoltageFeedIn') # check if DC-feed enabled
self._dbusMon.dbusmon.set_value('com.victronenergy.settings', '/Settings/CGwacs/OvervoltageFeedIn', 0) # disable DC-coupled PV feed-in
MaxChargeVoltage = min((min(chargeVoltageReduced_list)), ChargeVoltageBattery) # avoid exceeding MAX_CELL_VOLTAGE
else:
MaxChargeVoltage = ChargeVoltageBattery
if self._dynamicCVL:
self._dynamicCVL = False
logging.info('%s: Dynamic CVL reduction finished.' % (dt.now()).strftime('%c'))
if ((MaxCellVoltage - MinCellVoltage) < CELL_DIFF_MAX) and self._DCfeedActive: # re-enable DC-feed if it was enabled before
self._dbusMon.dbusmon.set_value('com.victronenergy.settings', '/Settings/CGwacs/OvervoltageFeedIn', 1) # enable DC-coupled PV feed-in
logging.info('%s: DC-coupled PV feed-in re-activated.' % (dt.now()).strftime('%c'))
self._DCfeedActive = False #reset to prevent permanent logging and activation of /Settings/CGwacs/OvervoltageFeedIn
if (MinCellVoltage <= MIN_CELL_VOLTAGE) and ZERO_SOC:
self._ownCharge = 0 # reset Coulumb counter to 0%
# manage charge current
if NrOfModulesBlockingCharge > 0:
MaxChargeCurrent = 0
else:
MaxChargeCurrent = MAX_CHARGE_CURRENT * self._fn._interpolate(CELL_CHARGE_LIMITING_VOLTAGE, CELL_CHARGE_LIMITED_CURRENT, MaxCellVoltage)
# manage discharge current
if MinCellVoltage <= MIN_CELL_VOLTAGE:
self._fullyDischarged = True
elif MinCellVoltage > MIN_CELL_VOLTAGE + MIN_CELL_HYSTERESIS:
self._fullyDischarged = False
if (NrOfModulesBlockingDischarge > 0) or (self._fullyDischarged):
MaxDischargeCurrent = 0
else:
MaxDischargeCurrent = MAX_DISCHARGE_CURRENT * self._fn._interpolate(CELL_DISCHARGE_LIMITING_VOLTAGE, CELL_DISCHARGE_LIMITED_CURRENT, MinCellVoltage)
else: # OWN_CHARGE_PARAMETERS = false!
if (VoltagesSum >= 0.995 * MaxChargeVoltage):
if (self._ownCharge < 0.95 * InstalledCapacity ):
self._ownCharge = 0.95 * InstalledCapacity # reset Coulumb counter to 95%
if ((MaxCellVoltage - MinCellVoltage) < CELL_DIFF_MAX): # if normal charging voltage is 100% SoC and balancing is finished
self._ownCharge = InstalledCapacity # reset Coulumb counter to 100%
###########################################################
# own Coulomb counter (runs even the BMS values are used) #
###########################################################
deltaTime = tt.time() - self._timeOld
self._timeOld = tt.time()
if Current > 0:
self._ownCharge += Current * (deltaTime / 3600) * BATTERY_EFFICIENCY # charging (with efficiency)
else:
self._ownCharge += Current * (deltaTime / 3600) # discharging
self._ownCharge = max(self._ownCharge, 0)
self._ownCharge = min(self._ownCharge, InstalledCapacity)
# store the charge into text file if changed significantly (avoid frequent file access)
if abs(self._ownCharge - self._ownCharge_old) >= (CHARGE_SAVE_PRECISION * InstalledCapacity):
self._charge_file = open('/data/dbus-aggregate-batteries/charge', 'w')
self._charge_file.write('%.3f' % self._ownCharge)
self._charge_file.close()
self._ownCharge_old = self._ownCharge
# overwrite BMS charge values
if OWN_SOC:
Capacity = self._ownCharge
Soc = 100 * self._ownCharge / InstalledCapacity
ConsumedAmphours = InstalledCapacity - self._ownCharge
if (self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/SystemState/LowSoc') == 0) and (Current < 0):
TimeToGo = -3600 * self._ownCharge / Current
else:
TimeToGo = None
else:
Soc = Soc / Capacity # weighted sum
if TimeToGo != None:
TimeToGo = TimeToGo / Capacity # weighted sum
###########################################################
# my ESS test code here #
###########################################################
AcInPower = self._dbusMon.dbusmon.get_value(self._multi, '/Devices/0/Ac/In/P')
AcInCurrent = AcInPower / 230 if AcInPower is not None else 0
AcOutPower = self._dbusMon.dbusmon.get_value(self._multi, '/Devices/0/Ac/Out/P')
AcOutCurrent = AcOutPower / 230 if AcOutPower is not None else 0
InverterPower = self._dbusMon.dbusmon.get_value(self._multi, '/Devices/0/Ac/Inverter/P')
InverterCurrent = InverterPower / Voltage if InverterPower is not None else 0
GridSetpoint = self._dbusMon.dbusmon.get_value('com.victronenergy.settings', '/Settings/CGwacs/AcPowerSetPoint')
MinimumSocLimit = self._dbusMon.dbusmon.get_value('com.victronenergy.settings', '/Settings/CGwacs/BatteryLife/MinimumSocLimit')
GridPower = self._dbusMon.dbusmon.get_value(self._grid, '/Ac/Power')
GridL1 = self._dbusMon.dbusmon.get_value(self._grid, '/Ac/L1/Power')
GridL2 = self._dbusMon.dbusmon.get_value(self._grid, '/Ac/L2/Power')
GridL3 = self._dbusMon.dbusmon.get_value(self._grid, '/Ac/L3/Power')
ConsumptionInputL1 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/ConsumptionOnInput/L1/Power')
ConsumptionInputL2 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/ConsumptionOnInput/L2/Power')
ConsumptionInputL3 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/ConsumptionOnInput/L3/Power')
ConsumptionInput = ConsumptionInputL1 + ConsumptionInputL2 + ConsumptionInputL3
PvOnGridL1 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/PvOnGrid/L1/Power')
PvOnGridL2 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/PvOnGrid/L2/Power')
PvOnGridL3 = self._dbusMon.dbusmon.get_value('com.victronenergy.system', '/Ac/PvOnGrid/L3/Power')
PvOnGrid = PvOnGridL1 + PvOnGridL2 + PvOnGridL3
AcLoadL1 = GridL1 + PvOnGridL1 - AcInPower
AcLoadL2 = GridL2 + PvOnGridL2
AcLoadL3 = GridL3 + PvOnGridL3
AcLoad = AcLoadL1 + AcLoadL2 + AcLoadL3
BatteryPower = Power
BatteryCurrent = Current
BatteryCurrentCalc = MpptCurrent + InverterCurrent
MaxChargePower = MaxChargeCurrent * Voltage
MaxChrgCellVoltage = MaxChargeVoltage / NR_OF_CELLS_PER_BATTERY
if MaxChargeCurrent > self._MaxChargeCurrentSm:
self._MaxChargeCurrentSm = ((self._SmoothFilter * self._MaxChargeCurrentSm) + MaxChargeCurrent) / (self._SmoothFilter + 1)
else:
self._MaxChargeCurrentSm = MaxChargeCurrent #((self._SmoothFilter * self._MaxChargeCurrentSm) + MaxChargeCurrent) / (self._SmoothFilter + 1)
MaxChargePowerSmooth = self._MaxChargeCurrentSm * Voltage
CorrectionCurrent = BatteryCurrentCalc - BatteryCurrent
CorrectionPower = CorrectionCurrent * Voltage
###############################################################################
# ESS magic
#
# Calculation of AcPowerSetpoint
# positive AcPowerSetpoint means MP2 is consuming power from the AC input side
# negative AcPowerSetpoint means MP2 is sourcing power to the AC input side
###############################################################################
# APSp1: compensate AC out power and charge battery with grid and MPPTs using maximum charge power
APSp1 = AcOutPower - MpptPower + MaxChargePowerSmooth + CorrectionPower
# APSp2: maintain gridsetpoint using PvOnGrid and battery (?)
APSp2 = GridSetpoint + PvOnGrid - ConsumptionInput
# APSp4: maintain gridsepoint and charge battery using PvOnGrid and MPPTs
APSp4 = GridSetpoint + PvOnGrid - AcLoad
# APSp5: maintain gridsepoint and charge battery using MPPTs only
APSp5 = AcOutPower - MpptPower + min(MpptPower,(MaxChargePowerSmooth + CorrectionPower))
# APSp_noDischarge: prohibit battery discharge
APSp_noDischarge = AcOutPower
if (self._EssActive > 0):
if (self._EssActive == 1):
AcPowerSetpoint = APSp1
elif (self._EssActive == 2):
AcPowerSetpoint = APSp2
elif (self._EssActive == 3):
AcPowerSetpoint = min(APSp1,APSp2)
elif (self._EssActive == 4):
AcPowerSetpoint = min(APSp1,APSp4)
if (Soc < MinimumSocLimit):
AcPowerSetpoint = APSp_noDischarge
elif (self._EssActive == 5):
AcPowerSetpoint = min(APSp5,APSp4)
if (Soc < MinimumSocLimit):
AcPowerSetpoint = APSp_noDischarge
self._dbusMon.dbusmon.set_value(self._multi, '/Hub4/L1/AcPowerSetpoint',AcPowerSetpoint)
else:
AcPowerSetpoint = self._dbusMon.dbusmon.get_value(self._multi, '/Hub4/L1/AcPowerSetpoint')
#######################
# Send values to DBus #
#######################
with self._dbusservice as bus:
# send DC
bus['/Dc/0/Voltage'] = Voltage #round(Voltage, 2)
bus['/Dc/0/Current'] = Current #round(Current, 1)
bus['/Dc/0/Power'] = Power #round(Power, 0)
# send charge
bus['/Soc'] = Soc
bus['/TimeToGo'] = TimeToGo
bus['/Capacity'] = Capacity
bus['/InstalledCapacity'] = InstalledCapacity
bus['/ConsumedAmphours'] = ConsumedAmphours
# send temperature
bus['/Dc/0/Temperature'] = Temperature
bus['/System/MaxCellTemperature'] = MaxCellTemp
bus['/System/MinCellTemperature'] = MinCellTemp
# send cell voltages
bus['/System/MaxCellVoltage'] = MaxCellVoltage
bus['/System/MaxVoltageCellId'] = MaxVoltageCellId
bus['/System/MinCellVoltage'] = MinCellVoltage
bus['/System/MinVoltageCellId'] = MinVoltageCellId
bus['/Voltages/Sum']= VoltagesSum
bus['/Voltages/Diff']= round(MaxCellVoltage - MinCellVoltage, 3) # Marvo2011
if SEND_CELL_VOLTAGES == 1: # Marvo2011
for cellId,currentCell in enumerate(cellVoltages_dict):
bus['/Voltages/%s' % (re.sub('[^A-Za-z0-9_]+', '', currentCell))] = cellVoltages_dict[currentCell]
# send battery state
bus['/System/NrOfCellsPerBattery'] = NR_OF_CELLS_PER_BATTERY
bus['/System/NrOfModulesOnline'] = NrOfModulesOnline
bus['/System/NrOfModulesOffline'] = NrOfModulesOffline
bus['/System/NrOfModulesBlockingCharge'] = NrOfModulesBlockingCharge
bus['/System/NrOfModulesBlockingDischarge'] = NrOfModulesBlockingDischarge
# send alarms
bus['/Alarms/LowVoltage'] = LowVoltage_alarm