-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
869 lines (815 loc) · 32.7 KB
/
Copy pathfunctions.php
File metadata and controls
869 lines (815 loc) · 32.7 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
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
<?php /* Copyright 2017 Seth Mos <seth.mos@dds.nl>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function open_shm($shm_key, $seg_size, $mode) {
global $state;
/* try opening first */
$shm_id = @shmop_open($shm_key, $mode, 0, 0);
// echo "found shm id $shm_id \n";
if(!$shm_id){
$state = log_message($state, "Couldn't find shared memory segment for key {$shm_key}, creating");
$shm_id = shmop_open($shm_key, "c", 0664, $seg_size);
}
return $shm_id;
}
function close_shm ($shm_id) {
global $state;
//Now lets delete the block and close the shared memory segment
if (!shmop_delete($shm_id)) {
$state = log_message($state, "Couldn't mark shared memory block for deletion.");
}
shmop_close($shm_id);
}
function write_state_shm($shm_id, $state) {
// Get shared memory block's size
$state['time'] = time();
$shm_size = shmop_size($shm_id);
$string = str_pad(serialize($state), $shm_size, "\0");
$shm_bytes_written = shmop_write($shm_id, $string, 0);
if ($shm_bytes_written != strlen($string)) {
$state = log_message($state, "The serialized state array data is too large for shm id '{$shm_id}', wrote '{$shm_bytes_written}', size is '{$shm_size}'");
exit(2);
}
return($state);
}
function write_p1_shm($shm_id, $shm_raw_id, $array, $datagram) {
global $state;
// Get shared memory block's size
$shm_size = shmop_size($shm_id);
$shm_raw_size = shmop_size($shm_raw_id);
$string = str_pad(serialize($array), $shm_size, "\0");
$shm_bytes_written = shmop_write($shm_id, $string, 0);
if ($shm_bytes_written != strlen($string)) {
$state = log_message($state,"The serialized array data is too large for shm");
exit(2);
}
$datagram = str_pad($datagram, $shm_raw_size, "\0");
$shm_bytes_written = shmop_write($shm_raw_id, $datagram, 0);
if ($shm_bytes_written != strlen($datagram)) {
$state = log_message($state,"The P1 datagram is too large for shm");
exit(2);
}
shmop_close($shm_id);
shmop_close($shm_raw_id);
return($state);
}
function read_p1_shm($shm_key, $seg_size) {
global $state;
$data = array();
$shm_id = open_shm($shm_key, $seg_size, "a");
$my_string = shmop_read($shm_id, 0, $seg_size);
if(empty($my_string)) {
$state = log_message($state,"Couldn't read from shared memory block");
return false;
}
$data = @unserialize($my_string);
if(!is_array($data))
return false;
shmop_close($shm_id);
return($data);
}
function return_chargers_power($cfg) {
$chargers = array();
$chargers['count'] = count($cfg['chargers']);
foreach($cfg['chargers'] as $idx => $charger) {
$power_max[$idx] = ($charger['power'] * $charger['pwm_max'])/100;
$power_min[$idx] = ($charger['power'] * $charger['pwm_min'])/100;
$power_max_abs[$idx] = ($charger['power']);
$power_min_abs[$idx] = ($charger['power']);
}
/* find smallest charger */
asort($power_min);
arsort($power_max);
$chargers['power_min'] = reset($power_min);
foreach($power_min as $idx => $value) {
$chargers['charger_min'] = $idx;
break;
}
foreach($power_max as $idx => $value) {
$chargers['charger_max'] = $idx;
break;
}
$chargers['power_max'] = array_sum($power_max);
$chargers['power_max_abs'] = array_sum($power_max_abs);
$chargers['power_min_abs'] = array_sum($power_min_abs);
return $chargers;
}
function return_inverters_power($cfg) {
$inverters = array();
$inverters['count'] = count($cfg['inverters']);
foreach($cfg['inverters'] as $idx => $inverter) {
$power_max[$idx] = ($inverter['power'] * $inverter['pwm_max'])/100;
$power_min[$idx] = ($inverter['power'] * $inverter['pwm_min'])/100;
$power_max_abs[$idx] = ($inverter['power']);
$power_min_abs[$idx] = ($inverter['power']);
}
/* find smallest inverter */
asort($power_min);
arsort($power_max);
$inverters['power_min'] = reset($power_min);
foreach($power_min as $idx => $value) {
$inverters['inverter_min'] = $idx;
break;
}
foreach($power_max as $idx => $value) {
$inverters['inverter_max'] = $idx;
break;
}
$inverters['power_max'] = array_sum($power_max);
$inverters['power_max_abs'] = array_sum($power_max_abs);
$inverters['power_min_abs'] = array_sum($power_min_abs);
return $inverters;
}
function controller($cfg, $state, $power, $battstate, $dev) {
/* blink that we are here */
$dev->getLeds()[7]->turnOn();
$inverters = return_inverters_power($cfg);
$chargers = return_chargers_power($cfg);
/* calculate current charger power */
$cur_charger_power = $state['charger_power'];
$cur_inverter_power = $state['inverter_power'];
$previous_state = previous_state($state);
/* predictable logical charger and inverter state */
switch($state['operation']) {
case -2:
$state[$state['operation']] = time();
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[-1];
/* if the battery was disconnected we need to connect it before charging */
if($state['battery_connect'] === false)
toggle_battery(true);
/* calculate how much we can drive the PWM, the actual percentage is determined in the driver */
$power_diff = 0 - $power['power_gen_cur'] + $power['power_cons_cur'] - $cur_inverter_power;
// $state = log_message($state, "Consumption power diff = {$power_diff}, inverter power {$cur_inverter_power}");
/* Calculate PWM based on current diff but add current inverter power */
$state['inverter_power'] = round(($power_diff + $cur_inverter_power) - $cfg['pow_cons_min']);
// $state = log_message($state, "Consumption power diff = {$power_diff}, inverter power {$cur_inverter_power}, state inverter power '{$state['inverter_power']}'");
$state = drive_chargers($state);
$state = drive_inverters($state);
$state['battery'] = "discharging";
if(($power_diff - $cfg['pow_cons_min'] + $cur_inverter_power) < $inverters['power_min']) {
$state['operation'] = $state['operation'] + 1;
$state = log_message($state,"Not enough consumption to drive the Inverter with minimum of {$inverters['power_min']} Watt moving to state {$state['operation']}");
}
break;
case -1:
$state[$state['operation']] = time();
$state = drive_inverters($state);
$state = drive_chargers($state);
$state['battery'] = "idle";
if($power['power_cons_cur'] > ($cfg['pow_cons_min'] + $inverters['power_min'])) {
$state['inverter_power'] = 0;
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[0];
$state['operation'] = $state['operation'] - 1;
$state = log_message($state,"Consumption power {$power['power_cons_cur']} exceeding minimum of {$cfg['pow_cons_min']}, move to state {$state['operation']}");
}
if(($power['power_gen_cur'] > $cfg['pow_gen_min']) || ($power['power_cons_cur'] < $cfg['pow_cons_min'])) {
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[-2];
$state['operation'] = $state['operation'] + 1;
$state = log_message($state,"We don't have enough consumption move to idle state {$state['operation']}");
}
break;
case 0:
$state[$state['operation']] = time();
if($state['battery'] == "") {
$state = log_message($state,"Startup, disable everything for now, just to be sure");
}
/* reset PWM to 0 */
$state['charger_power'] = 0;
$state['inverter_power'] = 0;
$state['battery'] = "idle";
$state = drive_inverters($state);
$state = drive_chargers($state);
if($state[$previous_state] > 0)
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[$previous_state];
/* with that done, let's see about that power levels */
if((($power['power_gen_cur'] - $cur_charger_power) > 0)) {
$state['operation'] = $state['operation'] + 1;
$state = log_message($state,"We have some generation move to standby {$state['operation']}");
}
if((($power['power_cons_cur'] - $cur_inverter_power) > 0)) {
$state['operation'] = $state['operation'] - 1;
$state = log_message($state,"We have some consumption move to standby {$state['operation']}");
}
break;
case 1:
$state[$state['operation']] = time();
$state = drive_inverters($state);
$state = drive_chargers($state);
$state['battery'] = "idle";
if($power['power_gen_cur'] > ($cfg['pow_gen_min'] + $chargers['power_min'])) {
$state['charger_power'] = 0;
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[0];
$state['operation'] = $state['operation'] + 1;
$state = log_message($state,"Generation exceeding minimum, enable AC, move to Charge state {$state['operation']}");
}
if(($power['power_cons_cur'] > $cfg['pow_cons_min']) || ($power['power_gen_cur'] < $cfg['pow_gen_min'])) {
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[2];
$state['operation'] = $state['operation'] - 1;
$state = log_message($state,"Not enough generation to start move to idle {$state['operation']}");
}
break;
case 2:
$state[$state['operation']] = time();
$state['duration'][$state['operation']] = $state[$state['operation']] - $state[1];
/* if the battery was disconnected we need to connect it before charging */
if($state['battery_connect'] === false)
toggle_battery(true);
$power_diff = 0 - $power['power_cons_cur'] + $power['power_gen_cur'] - $cur_charger_power;
/* calculate how much we can drive the PWM, the actual percentage is determined in the driver */
$state['charger_power'] = round(($power_diff + $cur_charger_power) - $cfg['pow_gen_min']);
// $state = log_message($state, "Generation power diff = {$power_diff}, charger power {$cur_charger_power}");
$state = drive_chargers($state);
$state = drive_inverters($state);
$state['battery'] = "charging";
if(($power_diff - $cfg['pow_gen_min'] + $cur_charger_power) < $chargers['power_min']) {
$state['operation'] = $state['operation'] - 1;
$state = log_message($state,"Not enough generation to drive the Charger with minium of {$chargers['power_min']} Watt moving to state {$state['operation']}");
}
break;
}
//sleep(0.5);
$dev->getLeds()[7]->turnOff();
return($state);
}
function previous_state($state) {
$times = array();
$times[-2] = $state[-2];
$times[-1] = $state[-1];
$times[0] = $state[0];
$times[1] = $state[1];
$times[2] = $state[2];
/* remove our current state, it's always last ;) */
unset($times[$state['operation']]);
arsort($times);
$previous = array();
foreach($times as $state => $value) {
// $previous[$state] = $value;
return $state;
}
}
function toggle_battery($bool) {
global $state;
global $dev;
global $cfg;
if($bool === true) {
if($state['battery_connect'] === false) {
$state['battery_connect'] = true;
$state = log_message($state,"Enable battery relay");
$dev->getLeds()[$cfg['batt_dcpin']]->turnOn();
$dev->getOutputPins()[$cfg['batt_dcpin']]->turnOn(); // 1
}
}
if($bool === false) {
if($state['battery_connect'] === true) {
$state['battery_connect'] = false;
$state = log_message($state,"Disable battery relay");
$dev->getLeds()[$cfg['batt_dcpin']]->turnOff();
$dev->getOutputPins()[$cfg['batt_dcpin']]->turnOff(); // 0
}
}
return($state);
}
function get_cell_voltages($cfg, $battstate) {
global $state;
global $dev;
/* pretend a dry run with simulate and previous battstate */
if($cfg['simulate']) {
if($state['battery'] == "") {
$cells[1] = 3.95;
$cells[2] = 3.96;
$cells[3] = 3.93;
$cells[4] = 3.95;
$cells[5] = 3.94;
$cells[6] = 3.95;
$cells[7] = 3.95;
$cells[8] = 3.97;
$battstate['cells'] = $cells;
}
$cells = $battstate['cells'];
switch($state['operation']) {
case 2:
/* pretend we are charging */
foreach($cells as $idx => $cell) {
$cells[$idx] = $cell + $cfg['simulate_step'];
}
break;
case -2:
/* pretend we are discharging */
foreach($cells as $idx => $cell) {
$cells[$idx] = $cell - $cfg['simulate_step'];
}
break;
}
$battstate['cells'] = $cells;
return $battstate['cells'];
}
$cells = array();
exec("{$cfg['batt_cell_cmd']}", $out, $ret);
if($ret > 0) {
if((time() - $battstate['time']) < 10) {
$state = log_message($state, "Failed to get battery cell voltages, using previous values");
return $battstate['cells'];
}
$state = log_message($state, "Failed to get battery cell voltages");
$battstate['cells'] = array();
return $battstate['cells'];
}
$previous = 0;
$i = 0;
foreach($out as $line) {
/* only use configured number of cells */
if(count($cells) >= $cfg['batt_cells'])
continue;
if(!empty(trim($line))) {
$c_arr = explode(":", $line);
$previous = array_sum($cells);
$cells[$c_arr[0]] = floatval(trim($c_arr[1])) * floatval($cfg['batt_voltage_div'] * $cfg['batt_cell_correction'][$i]) - $previous;
$i++;
}
}
$battstate['cells'] = $cells;
$battstate['time'] = time();
return $battstate['cells'];
}
function battery_status($cfg, $battstate) {
global $state;
$cells = get_cell_voltages($cfg, $battstate);
$battstate['total'] = array_sum($cells);
$battstate['cells'] = $cells;
arsort($cells);
$battstate['cell_min'] = end($cells);
$battstate['cell_max'] = reset($cells);
$cellcount = count($cells);
/* set 0% at $cfg['batt_volt_crit_min'] and 100% at $cfg['batt_volt_crit_max'] */
/* use the lowest cell voltage for the battery level, that way you can see it never charges "fully" again */
$range_diff = floatval($cfg['batt_cell_crit_max']) - floatval($cfg['batt_cell_crit_min']);
$cell_diff = floatval($battstate['cell_min']) - floatval($cfg['batt_cell_crit_min']);
$battstate['level'] = round(($cell_diff / $range_diff) * 100);
if((($battstate['cell_min'] > $cfg['batt_cell_crit_min']) && ($battstate['cell_min'] < $cfg['batt_cell_min'])) && ($state['battery_connect'] === false)) {
if($state['maintenance'] === false)
$state = log_message($state,"Battery cell voltage below minimum {$cfg['batt_cell_min']} but above critical {$cfg['batt_cell_crit_min']}, continue");
$state['charger_throttle'] = 1;
$state['inverter_throttle'] = 0;
if($state['operation'] <> 0)
$state = toggle_battery(true);
}
if($battstate['total'] < $cfg['batt_volt_crit_min']) {
$state = log_message($state,"Battery voltage {$battstate['total']} below critical {$cfg['batt_volt_crit_min']}, abort");
$state['charger_throttle'] = 0;
$state['inverter_throttle'] = 0;
$state = toggle_battery(false);
shutdown();
return($battstate);
}
if((($battstate['cell_max'] < $cfg['batt_cell_crit_max']) && ($battstate['cell_max'] > $cfg['batt_cell_max'])) && ($state['battery_connect'] === false)){
if($state['maintenance'] === false)
$state = log_message($state,"Battery cell voltage above maximum {$cfg['batt_cell_max']} but below critical {$cfg['batt_cell_crit_max']}, continue");
$state['charger_throttle'] = 0;
$state['inverter_throttle'] = 1;
if($state['operation'] <> 0)
$state = toggle_battery(true);
}
if($battstate['total'] > $cfg['batt_volt_crit_max']) {
$state = log_message($state,"Battery voltage {$battstate['total']} above critical {$cfg['batt_volt_crit_max']}, abort");
$state['charger_throttle'] = 0;
$state['inverter_throttle'] = 0;
$state = toggle_battery(false);
shutdown();
return($battstate);
}
/* normal operating conditions */
if(((($battstate['cell_max']-$cfg['batt_hysteresis']) < $cfg['batt_cell_max']) && (($battstate['cell_min']+$cfg['batt_hysteresis']) > $cfg['batt_cell_min'])) && ($state['battery_connect'] === false) && ($state['operation'] <> 0)) {
if($state['operation'] <> 0) {
$state = toggle_battery(true);
}
}
if($state['battery_connect'] === true) {
/* calculate charge and invert throttle based on voltage difference from maximum or minimum */
$state['charger_throttle'] = round((($cfg['batt_cell_max'] - ($battstate['cell_max'] - $cfg['batt_hysteresis'])) * $cfg['batt_charge_taper']), 2);
// $state = log_message($state, "Charge throttle is {$state['charger_throttle']}");
if($state['charger_throttle'] > 1)
$state['charger_throttle'] = 1;
//if(($state['charger_throttle'] < 1) && ($state['charger_throttle'] > 0))
// $state = log_message($state, "Battery almost Full, limiter effective");
if($state['charger_throttle'] <= 0)
$state['charger_throttle'] = 0;
$state['inverter_throttle'] = round(((($battstate['cell_min'] + $cfg['batt_hystersis'])- $cfg['batt_cell_min']) * $cfg['batt_discharge_taper']), 2);
// $state = log_message($state, "Invert throttle is {$state['inverter_throttle']}");
if($state['inverter_throttle'] > 1)
$state['inverter_throttle'] = 1;
// if(($state['inverter_throttle'] < 1) && ($state['inverter_throttle'] > 0))
// $state = log_message($state, "Battery almost empty, limiter effective");
if($state['inverter_throttle'] <= 0)
$state['inverter_throttle'] = 0;
}
return($battstate);
}
function disable_gpio($state) {
$state = log_message($state,"Disable all GPIO to defaults (off)");
return($state);
}
function enable_gpio($state) {
$state = log_message($state,"Enable all GPIO");
return($state);
}
function drive_chargers($state) {
global $cfg;
global $dev;
$chargers = return_chargers_power($cfg);
switch($state['operation']) {
case -2:
case -1:
case 0:
// Disable AC after standby, disable DC immediate
foreach($cfg['chargers'] as $idx => $charger) {
$state = power_device_ac($cfg, $state, "chargers", $idx, 0);
$state = power_device_dc($cfg, $state, "chargers", $idx, 0);
}
break;
case 1:
/* close the PWM before switching relays */
if($cfg['batt_pwm_shared'] === true)
$state = drive_pwm($state, $cfg, $cfg['batt_pwm_channel'], 0);
foreach($cfg['chargers'] as $idx => $charger) {
// Enable AC, keep disabled DC
$state = power_device_ac($cfg, $state, "chargers", $idx, 1);
$state = power_device_dc($cfg, $state, "chargers", $idx, 0);
}
break;
case 2:
// drive PWM
$state['battery'] = "charging";
if($state['charger_power'] > ($chargers['power_max'] * $state['charger_throttle']))
$state['charger_power'] = ($chargers['power_max'] * $state['charger_throttle']);
if($state['charger_power'] < ($chargers['power_min']))
$state['charger_power'] = $chargers['power_min'];
/* Determine which to run */
$enabled = array();
$pwm = array();
$cpower = $state['charger_power'];
/* Calculate battery shared PWM, we really need to take current/battery voltege into account, meh */
$pwm['battery'] = ($state['charger_power']/$chargers['power_max_abs']);
$c = 0;
foreach($cfg['chargers'] as $idx => $charger) {
$pwm[$idx] = 0;
if($cpower >= 0) {
$enabled[$idx] = true;
$c++;
} else {
$enabled[$idx] = false;
}
$cpower = $cpower - (($charger['power'] * $charger['pwm_max'])/100);
}
if($c == 1) {
foreach($enabled as $idx => $value) {
$pwm[$idx] = (($state['charger_power']/$c)/$cfg['chargers'][$idx]['power']);
}
}
if($c > 1) {
foreach($enabled as $idx => $value) {
$factor = (($state['charger_power']/$c) / $cfg['chargers'][$idx]['power']);
$pwm[$idx] = (($state['charger_power']/$c) / $factor) / $cfg['chargers'][$idx]['power'];
}
}
// Enable AC, Enable DC on demand
foreach($cfg['chargers'] as $idx => $charger) {
$state = power_device_ac($cfg, $state, "chargers", $idx, 1);
if($enabled[$idx] === true) {
$state = power_device_dc($cfg, $state, "chargers", $idx, $pwm[$idx]);
} else {
$state = power_device_ac($cfg, $state, "chargers", $idx, 0);
$state = power_device_dc($cfg, $state, "chargers", $idx, 0);
}
}
/* open up the PWM after switching relays */
if($cfg['batt_pwm_shared'] === true)
$state = drive_pwm($state, $cfg, $cfg['batt_pwm_channel'], $pwm['battery']);
break;
}
return($state);
}
function power_device_ac($cfg, $state, $category, $idx, $toggle) {
global $dev;
$previousstate = previous_state($state);
if($toggle == 0) {
/* Power off */
switch($category) {
case "inverters":
$statetime = $state[-2];
$dstatetime = $state[$category][$idx]['time'];
break;
case "chargers":
$statetime = $state[2];
$dstatetime = $state[$category][$idx]['time'];
break;
default:
$statetime = $state[$previousstate];
$dstatetime = $state[$category][$idx]['time'];
break;
}
if(((time() - $statetime) > $cfg[$category][$idx]['standby']) && ($state[$category][$idx]['ac'] === true)) {
$state = log_message($state, "Disable {$category} index {$idx} AC after standby time ".(time() - $statetime)." exceeds {$cfg[$category][$idx]['standby']}");
$dev->getLeds()[$cfg[$category][$idx]['acpin']]->turnOff();
$dev->getOutputPins()[$cfg[$category][$idx]['acpin']]->turnOff();
$state[$category][$idx]['ac'] = false;
}
if((((time() - $dstatetime) > $cfg[$category][$idx]['standby']) && ($state[$category][$idx]['ac'] === true)) && ($dstatetime > 0)) {
$state = log_message($state, "Disable {$category} index {$idx} AC after standby time ".(time() - $state[$category][$idx]['time'])." exceeds {$cfg[$category][$idx]['standby']}");
$dev->getLeds()[$cfg[$category][$idx]['acpin']]->turnOff();
$dev->getOutputPins()[$cfg[$category][$idx]['acpin']]->turnOff();
$state[$category][$idx]['ac'] = false;
}
}
/* Let's go */
if(($toggle > 0) && ($state[$category][$idx]['ac'] === false)) {
$state = log_message($state,"Enable {$category} index $idx AC");
$dev->getLeds()[$cfg[$category][$idx]['acpin']]->turnOn();
$dev->getOutputPins()[$cfg[$category][$idx]['acpin']]->turnOn();
$state[$category][$idx]['ac'] = true;
}
return($state);
}
function power_device_dc($cfg, $state, $category, $idx, $pwm) {
global $dev;
if(($pwm == 0)){
if($state[$category][$idx]['dc'] === true) {
$state = log_message($state,"Disable {$category} index $idx DC");
/* close the PWM before switching the relay */
if($cfg[$category][$idx]['pwm_shared'] === false)
$state = drive_pwm($state, $cfg, $cfg[$category][$idx]['pwm_channel'], $pwm);
sleep(0.1);
$dev->getLeds()[$cfg[$category][$idx]['dcpin']]->turnOff();
$dev->getOutputPins()[$cfg[$category][$idx]['dcpin']]->turnOff();
$state[$category][$idx]['dc'] = false;
}
if($state[$category][$idx]['pwm'] != $pwm)
if($cfg[$category][$idx]['pwm_shared'] === false)
$state = drive_pwm($state, $cfg, $cfg[$category][$idx]['pwm_channel'], $pwm);
$state[$category][$idx]['pwm'] = $pwm;
}
if(($pwm > 0)){
if($state[$category][$idx]['dc'] === false) {
$state = log_message($state,"Enable {$category} index $idx DC");
$dev->getLeds()[$cfg[$category][$idx]['dcpin']]->turnOn();
$dev->getOutputPins()[$cfg[$category][$idx]['dcpin']]->turnOn();
sleep(0.1);
/* Drive the PWM after then relay is on */
if($cfg[$category][$idx]['pwm_shared'] === false)
$state = drive_pwm($state, $cfg, $cfg[$category][$idx]['pwm_channel'], $pwm);
$state[$category][$idx]['dc'] = true;
}
if($state[$category][$idx]['pwm'] != $pwm)
if($cfg[$category][$idx]['pwm_shared'] === false)
$state = drive_pwm($state, $cfg, $cfg[$category][$idx]['pwm_channel'], $pwm);
$state[$category][$idx]['pwm'] = $pwm;
}
return($state);
}
function drive_inverters($state) {
global $cfg;
global $dev;
$inverters = return_inverters_power($cfg);
switch($state['operation']) {
case 2:
case 1:
case 0:
// Disable AC after standby, disable DC immediate
foreach($cfg['inverters'] as $idx => $inverter) {
$state = power_device_ac($cfg, $state, "inverters", $idx, 0);
$state = power_device_dc($cfg, $state, "inverters", $idx, 0);
}
break;
case -1:
/* Close the PWM before the relay is off */
if($cfg['batt_pwm_shared'] === true)
$state = drive_pwm($state, $cfg, $cfg['batt_pwm_channel'], 0);
foreach($cfg['inverters'] as $idx => $inverter) {
// Enable AC, keep disabled DC
$state = power_device_ac($cfg, $state, "inverters", $idx, 1);
$state = power_device_dc($cfg, $state, "inverters", $idx, 0);
}
break;
case -2:
// drive PWM
$state['battery'] = "discharging";
if($state['inverter_power'] > ($inverters['power_max'] * $state['inverter_throttle']))
$state['inverter_power'] = ($inverters['power_max'] * $state['inverter_throttle']);
if($state['inverter_power'] < ($inverters['power_min']))
$state['inverter_power'] = $inverters['power_min'];
/* Determine which to run */
$enabled = array();
$pwm = array();
$cpower = $state['inverter_power'];
/* Calculate battery shared PWM, we really need to take current/battery voltege into account, meh */
$pwm['battery'] = ($state['inverter_power']/$inverters['power_max_abs']);
/* Calculate individual PWM values */
$c = 0;
foreach($cfg['inverters'] as $idx => $inverter) {
if($cpower >= 0) {
$enabled[$idx] = true;
$c++;
} else {
$enabled[$idx] = false;
}
$cpower = $cpower - (($inverter['power'] * $inverter['pwm_max'])/100);
}
if($c == 1) {
foreach($enabled as $idx => $value) {
$pwm[$idx] = (($state['inverter_power']/$c)/$cfg['inverters'][$idx]['power']);
}
}
if($c > 1) {
foreach($enabled as $idx => $value) {
$factor = (($state['inverter_power']/$c) / $cfg['inverters'][$idx]['power']);
$pwm[$idx] = (($state['inverter_power']/$c) / $factor) / $cfg['inverters'][$idx]['power'];
}
}
// Enable AC, Enable DC on demand
foreach($cfg['inverters'] as $idx => $inverter) {
$state = power_device_ac($cfg, $state, "inverters", $idx, 1);
if($enabled[$idx] === true) {
$state = power_device_dc($cfg, $state, "inverters", $idx, $pwm[$idx]);
} else {
$state = power_device_ac($cfg, $state, "inverters", $idx, 0);
$state = power_device_dc($cfg, $state, "inverters", $idx, 0);
}
}
/* Drive the PWM after the relay is on */
if($cfg['batt_pwm_shared'] === true)
$state = drive_pwm($state, $cfg, $cfg['batt_pwm_channel'], $pwm['battery']);
break;
}
return($state);
}
function drive_pwm($state, $cfg, $channel, $pwm) {
// echo "Calling drive_pwm ". print_r($state, true) . print_r($cfg, true) ." {$channel} {$pwm}\n";
if(($channel > 16) || ($channel < 0) || (!is_numeric($channel))) {
$state = log_message($state,"We are passed an invalid channel '{$channel}'");
return($state);
}
if(($pwm > 1) || ($pwm < 0) || (!is_numeric($pwm))) {
$state = log_message($state,"We are passed an invalid pwm value '{$pwm}'");
return($state);
}
$pwmstep = round($pwm * 4096);
if($pwmstep == 4096)
$pwmstep = 4095;
if($pwmstep == 0)
$pwmstep = 1;
if(!isset($state['pwm'][$channel]))
$state['pwm'][$channel] = -1;
// fetch previous value to see if we need to fire */
if($state['pwm'][$channel] <> $pwm) {
$state = log_message($state,"Set pwm channel $channel to step $pwmstep value $pwm");
exec("{$cfg['pwm_command']} {$channel} {$pwmstep}", $out, $ret);
if($ret > 0)
$state = log_message($state,"Failed to set pwm to $pwm channel $channel");
if(count($out) > 0)
$state = log_message($state,"We got unexpected results $out");
$state['pwm'][$channel] = $pwm;
}
return($state);
}
function shutdown() {
global $dev;
global $cfg;
global $shm_state_id;
global $state;
$state = log_message($state,"Shutdown, move to state 0, disable charger(s) and inverter(s). Disconnect Battery. Reset IO.");
$state['operation'] = 0;
$state['charger_power'] = 0;
$state['inverter_power'] = 0;
$state['available_power'] = 0;
$state['duration'][2] = 0;
$state['duration'][-2] = 0;
write_state_shm($shm_state_id, $state);
foreach($cfg['inverters'] as $idx => $inverter) {
$state = power_device_ac($cfg, $state, "inverters", $idx, 0);
$state = power_device_dc($cfg, $state, "inverters", $idx, 0);
}
foreach($cfg['chargers'] as $idx => $charger) {
$state = power_device_ac($cfg, $state, "chargers", $idx, 0);
$state = power_device_dc($cfg, $state, "chargers", $idx, 0);
}
$state = drive_inverters($state);
$state = drive_chargers($state);
write_state_shm($shm_state_id, $state);
sleep($cfg['timer_loop']);
$state = maintenance_charger_disable($cfg, $battstate, $state);
toggle_battery(false);
// reset gpio
$dev->init();
write_state_shm($shm_state_id, $state);
sleep($cfg['timer_loop']);
write_state_shm($shm_state_id, $state);
exit(0);
}
function log_message($state = array(), $message = "") {
if(!empty($state) && ($state['message'] == $message))
return $state;
syslog(LOG_NOTICE, $message);
$date = date("Y-m-d H:i:s");
echo "{$date} {$message}\n";
$state['message'] = "{$message}";
$state['message_time'] = "{$date}";
return $state;
}
function maintenance_charger_disable($cfg, $battstate, $state) {
global $dev;
$state = log_message($state,"Disable maintenance charger.");
$state['maintenance'] = false;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOff();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOff();
return $state;
}
function maintenance_charge($cfg, $battstate, $state) {
global $dev;
$state[$state['operation']] = time();
if(($state['operation'] <> 0) && ($state['maintenance'] === true)) {
$state = log_message($state,"Disable maintenance charger");
$state['maintenance'] = false;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOff();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOff();
}
$cell_diff = round(($battstate['cell_max'] - $battstate['cell_min']), 3);
if(($cell_diff > $cfg['maintenance_diff']) && ($state['maintenance'] === false) && ($state['operation'] == 0) && ($state['battery'] == "full")) {
$state = log_message($state,"Enable maintenance charger, cell difference '{$cell_diff}' exceeds '{$cfg['maintenance_diff']}' limit.");
$state['maintenance'] = true;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOn();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOn();
}
if(($cell_diff < $cfg['batt_hysteresis']) && ($state['maintenance'] === true) && ($state['operation'] == 0)) {
$state = log_message($state,"Disable maintenance charger, cell difference '{$cell_diff}' within hysteresis '{$cfg['batt_hysteresis']}'.");
$state['maintenance'] = false;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOff();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOff();
}
/* low watermark rescue charging, must be dark outside */
if((($battstate['cell_min']+($cfg['batt_hysteresis']/2)) < $cfg['batt_cell_crit_min']) && ($state['maintenance'] === false) && ($state['operation'] == 0) && ($state['battery'] == "empty")) {
$state = log_message($state,"Enable maintenance charger, minimum cell '{$battstate['cell_min']}' below critical.");
$state['maintenance'] = true;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOn();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOn();
}
if((($battstate['cell_min']-($cfg['batt_hysteresis']/2)) > $cfg['batt_cell_crit_min']) && ($state['maintenance'] === true) && ($state['operation'] == 0)) {
$state = log_message($state,"Disable maintenance charger, minimum cell '{$battstate['cell_min']}' above critical.");
$state['maintenance'] = false;
$dev->getLeds()[$cfg['maintenance_charger_acpin']]->turnOff();
$dev->getOutputPins()[$cfg['maintenance_charger_acpin']]->turnOff();
}
return $state;
}
function timeDiff($time, $opt = array()) {
// The default values
$defOptions = array(
'to' => 0,
'parts' => 1,
'precision' => 'second',
'distance' => TRUE,
'separator' => ', '
);
$opt = array_merge($defOptions, $opt);
// Default to current time if no to point is given
(!$opt['to']) && ($opt['to'] = time());
// Init an empty string
$str = '';
// To or From computation
$diff = ($opt['to'] > $time) ? $opt['to']-$time : $time-$opt['to'];
// An array of label => periods of seconds;
$periods = array(
'decade' => 315569260,
'year' => 31556926,
'month' => 2629744,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
// Round to precision
if ($opt['precision'] != 'second')
$diff = round(($diff/$periods[$opt['precision']])) * $periods[$opt['precision']];
// Report the value is 'less than 1 ' precision period away
(0 == $diff) && ($str = 'less than 1 '.$opt['precision']);
// Loop over each period
foreach ($periods as $label => $value) {
// Stitch together the time difference string
(($x=floor($diff/$value))&&$opt['parts']--) && $str.=($str?$opt['separator']:'').($x.' '.$label.($x>1?'s':''));
// Stop processing if no more parts are going to be reported.
if ($opt['parts'] == 0 || $label == $opt['precision']) break;
// Get ready for the next pass
$diff -= $x*$value;
}
$opt['distance'] && $str.=($str&&$opt['to']>$time)?' ago':' away';
return $str;
}
?>