/
short-circuit-limiter.ino
688 lines (498 loc) · 16.6 KB
/
short-circuit-limiter.ino
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
#include <EEPROM.h>
#include <U8g2lib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "CTSensor.h"
#define CURRENT_MCB_CUT 16
#define DEFAULT_CURRENT_ENABLE_THRESHOLD 0.15
#define CURRENT_THRESHOLD_STRIDE 0.01
#define MIN_ENABLE_CURRENT_THRESHOLD 0.01
#define MAX_ENABLE_CURRENT_THRESHOLD 10.00
#define ADDRESS_STORE_CURRENT_ENABLE 0
#define TEMP_MAX 85
#define TEMP_WARN 70
#define TEMP_WARNING_RATE 120000 //2 x 60 x 1000ms
#define CT_CALIBRATION_VALUE 60.6 // (100A / 0.05A) / 33 ohms
#define INITIAL_SETTLE_TIME 20000 //20 seconds
#define BUTTON_DEBOUNCE 300
#define PIN_LED 26
#define PIN_CT A0
#define PIN_TEMP 2
#define PIN_BUZZER 4
#define PIN_BUTTON_UP 25
#define PIN_BUTTON_ENTER 24
#define PIN_BUTTON_DOWN 23
#define PIN_RELAY_MCB 5
#define PIN_RELAY_LIMITER 7
#define BUZZER_BEEP_ON_TIME 20
#define BUZZER_BEEP_OFF_TIME 200
#define BUZZER_LONG_BEEP_TIME 2000
#define DELAY_BEFORE_LIMITER_RELAY_ENABLE 1000
//Number of temperature reads to average over
#define TEMP_READS_SET 50
//The MCU can take 5500 samples/s.
//0.05s seconds will require 275 samples/set
#define NUM_CT_SAMPLES 275
//Delay between LCD Print to avoid slowing down data collection
#define INTERVAL_PRINT 300
#define BACKLIGHT_BLINK_RATE 200
#define DALLAS_RESOLUTION 11 //This will give 0.125C precision
#define DALLAS_READING_DELAY 750/ (1 << (12-DALLAS_RESOLUTION))
typedef enum {
STATE_MCB_TRIPPED, STATE_WINDOW_BEFORE, STATE_WINDOW_WITHIN, STATE_WINDOW_EXITED, STATE_TEMP_MAX
} STATE;
//This is to prepare to transition to the STATE_WINDOW_BEFORE at the start
STATE currentState = STATE_MCB_TRIPPED;
STATE nextState = STATE_WINDOW_BEFORE;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(PIN_TEMP);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dallasTemp(&oneWire);
DeviceAddress tempDeviceAddress;
U8G2_UC1701_MINI12864_F_4W_SW_SPI u8g2(U8G2_R2, 21, 20, 19, 22);
CTSensor clamp(PIN_CT, CT_CALIBRATION_VALUE);
double mcbTrippedCurrent;
int beepsLeft = 0;
bool isBacklightAlwaysOn = false;
float currentEnableThreshold;
void setup() {
currentEnableThreshold = getEnableThresholdFromEEProm();
analogReference(EXTERNAL);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_BUZZER, OUTPUT);
pinMode(PIN_RELAY_LIMITER, OUTPUT);
pinMode(PIN_RELAY_MCB, OUTPUT);
pinMode(PIN_BUTTON_UP, INPUT);
pinMode(PIN_BUTTON_ENTER, INPUT);
pinMode(PIN_BUTTON_DOWN, INPUT);
changeDisplayBacklight(true);
passFullCurrentThrough(false);
changeMCBRelayState(false);
u8g2.begin();
dallasTemp.begin();
dallasTemp.getAddress(tempDeviceAddress, 0);
dallasTemp.setResolution(tempDeviceAddress, DALLAS_RESOLUTION);
dallasTemp.setWaitForConversion(false); //I'll read temperatures asynchronously
unsigned long initialTime = millis();
unsigned long timeElapsedSinceStart;
while((timeElapsedSinceStart = (millis() - initialTime)) < INITIAL_SETTLE_TIME){
if(isMiddleButtonPressed()){
break;
}
getCurrentMeasurement();
unsigned long currentTime = millis();
static unsigned long lastDisplayTime = 0;
if((currentTime - lastDisplayTime) >= INTERVAL_PRINT){
lastDisplayTime = currentTime;
unsigned long timeLeft = INITIAL_SETTLE_TIME - timeElapsedSinceStart;
unsigned int timeLeftSeconds = timeLeft / 1000;
updateCurrentThresholdForCertainModes();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(0,10, "Short Circuit Limiter\n");
u8g2.drawStr(0,21, "Starting in: ");
u8g2.setFont(u8g2_font_9x18_tr);
char secondsBuffer[10];
sprintf(secondsBuffer, "%ds", timeLeftSeconds);
u8g2.drawStr(80, 22, secondsBuffer);
u8g2.setFont(u8g2_font_5x8_tr);
char valueBuff[10];
char valueBuff2[10];
char fullBuff[30];
float temperature = getTemperature();
dtostrf(temperature, 3, 1, valueBuff);
sprintf(fullBuff,"Curr Temp: %sC", valueBuff);
u8g2.drawStr(0,31, fullBuff);
dtostrf(TEMP_WARN, 2, 0, valueBuff);
dtostrf(TEMP_MAX, 2, 0, valueBuff2);
sprintf(fullBuff,"Temp Warn/Max : %s/%s C", valueBuff, valueBuff2);
u8g2.drawStr(0,39, fullBuff);
dtostrf(currentEnableThreshold, 4, 2, valueBuff);
sprintf(fullBuff,"Enable thres: %sA", valueBuff);
u8g2.drawStr(0,47, fullBuff);
dtostrf(CURRENT_MCB_CUT, 4, 1, valueBuff);
sprintf(fullBuff,"Trip thres: %sA", valueBuff);
u8g2.drawStr(0,55, fullBuff);
u8g2.drawStr(0,63, "Designer: Yeo Kheng Meng");
u8g2.sendBuffer();
}
}
Serial.begin(115200);
Serial.println("Setup complete");
enterWindowBeforeMode(0);
}
double getCurrentMeasurement(){
for(int sampleIndex = 0; sampleIndex < NUM_CT_SAMPLES; sampleIndex++){
clamp.doIncrementalMeasurement();
}
double currentValue = clamp.getIrmsFromIncrementalMeasurement();
return currentValue;
}
void loop() {
double currentValue = getCurrentMeasurement();
if(currentValue >= CURRENT_MCB_CUT){
nextState = enterMCBTrippedMode(currentValue);
}
switch(nextState){
case STATE_MCB_TRIPPED:
nextState = enterMCBTrippedMode(currentValue);
break;
case STATE_WINDOW_BEFORE:
nextState = enterWindowBeforeMode(currentValue);
break;
case STATE_WINDOW_WITHIN:
nextState = enterWindowWithinMode(currentValue);
break;
case STATE_WINDOW_EXITED:
nextState = enterWindowExitedMode(currentValue);
break;
case STATE_TEMP_MAX:
nextState = enterTempMaxMode();
break;
default:
Serial.println("Something is really wrong here, this mode should not exist!!!");
break;
}
shortBeepRunner();
displayToScreen(currentValue);
}
void displayToScreen(double currentValue){
long long currentTime = millis();
static unsigned long lastDisplayTime;
if(currentState == STATE_MCB_TRIPPED){
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_9x18_tr);
u8g2.drawStr(0,10, "Overcurrent!!!");
u8g2.drawStr(0,31, "Reset MCB >>>>>>>>");
char valueBuff[10];
char fullBuff[30];
dtostrf(mcbTrippedCurrent, 4, 1, valueBuff);
sprintf(fullBuff,"Tripped: %sA", valueBuff);
u8g2.drawStr(0,50, fullBuff);
u8g2.setFont(u8g2_font_5x8_tr);
dtostrf(CURRENT_MCB_CUT, 4, 1, valueBuff);
sprintf(fullBuff,"Trip threshold: %sA", valueBuff);
u8g2.drawStr(0,63, fullBuff);
u8g2.sendBuffer();
} else if(currentState == STATE_TEMP_MAX){
if((currentTime - lastDisplayTime) >= INTERVAL_PRINT){
lastDisplayTime = currentTime;
float temperature = getTemperature();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_9x18_tr);
u8g2.drawStr(0,10, "Overheated!!!");
char valueBuff[10];
char fullBuff[30];
u8g2.setFont(u8g2_font_7x13_tr);
dtostrf(temperature, 3, 1, valueBuff);
sprintf(fullBuff,"Curr Temp: %sC", valueBuff);
u8g2.drawStr(0,30, fullBuff);
u8g2.setFont(u8g2_font_6x10_tr);
dtostrf(TEMP_MAX, 3, 1, valueBuff);
sprintf(fullBuff,"Temp Max : %sC", valueBuff);
u8g2.drawStr(0,50, fullBuff);
dtostrf(TEMP_WARN, 3, 1, valueBuff);
sprintf(fullBuff,"Temp Warn: %sC", valueBuff);
u8g2.drawStr(0,60, fullBuff);
u8g2.sendBuffer();
}
} else {
static double runningTotal = 0;
static int samplesTaken = 0;
runningTotal += currentValue;
samplesTaken++;
//We don't want to keep printing to screen as it is slow so we just do a running average
if((currentTime - lastDisplayTime) >= INTERVAL_PRINT){
lastDisplayTime = currentTime;
float temperature = getTemperature();
double average = runningTotal / samplesTaken;
runningTotal = 0;
samplesTaken = 0;
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_7x13_tr);
switch(currentState){
case STATE_WINDOW_BEFORE:
u8g2.drawStr(0,10, "Limiter: In Effect");
break;
case STATE_WINDOW_WITHIN:
u8g2.drawStr(0,10, "Limiter:");
u8g2.drawStr(0,20, "Ready to Bypass...");
break;
case STATE_WINDOW_EXITED:
u8g2.drawStr(0,10, "Limiter: Bypassed");
break;
case STATE_MCB_TRIPPED:
//Fallthrough
default:
Serial.println("LCD Printing shouldn't come to this mode!");
break;
}
bool tempWarning = temperatureWarningCheck(temperature);
char valueBuff[10];
char fullBuff[30];
dtostrf(currentValue, 4, 2, valueBuff);
sprintf(fullBuff,"Current : %sA", valueBuff);
u8g2.drawStr(0,33, fullBuff);
static bool blinkTempOn = false;
blinkTempOn = !blinkTempOn;
//Start blinking when temperature is warning range
if(!tempWarning || blinkTempOn){
dtostrf(temperature, 3, 1, valueBuff);
sprintf(fullBuff,"Curr Temp: %sC", valueBuff);
u8g2.drawStr(0,47, fullBuff);
}
u8g2.setFont(u8g2_font_5x8_tr);
dtostrf(currentEnableThreshold, 4, 2, valueBuff);
sprintf(fullBuff,"Enable Current: %sA", valueBuff);
u8g2.drawStr(0,63, fullBuff);
u8g2.sendBuffer();
}
}
}
STATE enterMCBTrippedMode(double currentValue){
static bool initialWarningTriggered = false;
static unsigned long initialWarningStart = 0;
unsigned long currentTime = millis();
if(currentState != STATE_MCB_TRIPPED){
changeMCBRelayState(false);
passFullCurrentThrough(false);
currentState = STATE_MCB_TRIPPED;
mcbTrippedCurrent = currentValue;
Serial.print("MCB tripped at: ");
Serial.println(mcbTrippedCurrent);
initialWarningTriggered = true;
initialWarningStart = currentTime;
digitalWrite(PIN_BUZZER, HIGH);
}
//This is to sound the buzzer and blink the Backlight for BACKLIGHT_BLINK_RATE duration
if(initialWarningTriggered){
static bool displayBacklightCurrentlyOn = false;
static unsigned long displayBacklightLastChanged = 0;
if((currentTime - displayBacklightLastChanged) > BACKLIGHT_BLINK_RATE){
displayBacklightLastChanged = currentTime;
displayBacklightCurrentlyOn = !displayBacklightCurrentlyOn;
changeDisplayBacklight(displayBacklightCurrentlyOn);
}
if((currentTime - initialWarningStart) > BUZZER_LONG_BEEP_TIME){
initialWarningTriggered = false;
digitalWrite(PIN_BUZZER, LOW);
changeDisplayBacklight(true);
}
}
if(isMiddleButtonPressed()){
return STATE_WINDOW_BEFORE;
}
return STATE_MCB_TRIPPED;
}
STATE enterWindowBeforeMode(double currentValue){
if(currentState != STATE_WINDOW_BEFORE){
passFullCurrentThrough(false);
changeMCBRelayState(true);
changeDisplayBacklight(false);
//Don't bother beeping if we came from within window. Happens when the current measurement is noisy
if(currentState != STATE_WINDOW_WITHIN){
shortBeepXTimesNoDelay(2);
}
currentState = STATE_WINDOW_BEFORE;
Serial.println("Window Before");
}
updateDisplayBackLightSettingsForCertainModes();
updateCurrentThresholdForCertainModes();
if(currentValue >= currentEnableThreshold){
return STATE_WINDOW_WITHIN;
}
return STATE_WINDOW_BEFORE;
}
STATE enterWindowWithinMode(double currentValue){
unsigned long currentTime = millis();
static unsigned long enableWindowTime = 0;
if(currentState != STATE_WINDOW_WITHIN){
currentState = STATE_WINDOW_WITHIN;
enableWindowTime = currentTime;
Serial.println("Enter window");
}
updateDisplayBackLightSettingsForCertainModes();
updateCurrentThresholdForCertainModes();
if(currentValue < currentEnableThreshold){
return STATE_WINDOW_BEFORE;
} else if((currentTime - enableWindowTime) >= DELAY_BEFORE_LIMITER_RELAY_ENABLE){
//Exit window if no overcurrent is detected in this time
return STATE_WINDOW_EXITED;
} else {
return STATE_WINDOW_WITHIN;
}
}
STATE enterWindowExitedMode(double currentValue){
if(currentState != STATE_WINDOW_EXITED){
currentState = STATE_WINDOW_EXITED;
changeDisplayBacklight(true);
shortBeepXTimesNoDelay(1);
Serial.println("Exited window, bypass resistor");
}
passFullCurrentThrough(true);
updateCurrentThresholdForCertainModes();
if(currentValue < currentEnableThreshold){
return STATE_WINDOW_BEFORE;
} else {
return STATE_WINDOW_EXITED;
}
}
STATE enterTempMaxMode(){
if(currentState != STATE_TEMP_MAX){
currentState = STATE_TEMP_MAX;
changeMCBRelayState(false);
passFullCurrentThrough(false);
changeDisplayBacklight(true);
shortBeepXTimesNoDelay(10);
Serial.println("Enter Max Temp mode");
}
float temperature = getTemperature();
//We remain in this mode until the temperature drops below TEMP_WARN to prevent oscillating into this mode and out
if(temperature >= TEMP_WARN){
return STATE_TEMP_MAX;
} else {
return STATE_WINDOW_BEFORE;
}
}
void updateDisplayBackLightSettingsForCertainModes(){
if(isMiddleButtonPressed()){
isBacklightAlwaysOn = !isBacklightAlwaysOn;
changeDisplayBacklight(isBacklightAlwaysOn);
}
}
float getEnableThresholdFromEEProm(){
float value;
EEPROM.get(ADDRESS_STORE_CURRENT_ENABLE, value);
if(isnan(value)){
return DEFAULT_CURRENT_ENABLE_THRESHOLD;
} else {
return value;
}
}
void updateCurrentThresholdForCertainModes(){
float newThreshold = currentEnableThreshold;
if(isTopButtonPressed()){
newThreshold += CURRENT_THRESHOLD_STRIDE;
}
if(isBottomButtonPressed()){
newThreshold -= CURRENT_THRESHOLD_STRIDE;
}
if(newThreshold != currentEnableThreshold
&& newThreshold >= MIN_ENABLE_CURRENT_THRESHOLD
&& newThreshold <= MAX_ENABLE_CURRENT_THRESHOLD){
currentEnableThreshold = newThreshold;
EEPROM.put(ADDRESS_STORE_CURRENT_ENABLE, currentEnableThreshold);
}
}
void changeMCBRelayState(bool state){
if(state){
digitalWrite(PIN_RELAY_MCB, HIGH);
} else {
digitalWrite(PIN_RELAY_MCB, LOW);
}
}
void passFullCurrentThrough(bool state){
if(state){
digitalWrite(PIN_RELAY_LIMITER, HIGH);
} else {
digitalWrite(PIN_RELAY_LIMITER, LOW);
}
}
void changeDisplayBacklight(bool state){
//This setting will override whatever the modes want
if(isBacklightAlwaysOn){
state = true;
}
//State is flipped for some reason
if(state){
digitalWrite(PIN_LED, LOW);
} else {
digitalWrite(PIN_LED, HIGH);
}
}
float getTemperature(){
static float previousTemperature = -273.15;
static unsigned long previousReadingTime = 0;
unsigned long currentTime = millis();
if((currentTime - previousReadingTime) > DALLAS_READING_DELAY){
previousTemperature = dallasTemp.getTempCByIndex(0);
previousReadingTime = currentTime;
dallasTemp.requestTemperaturesByIndex(0);
}
return previousTemperature;
}
bool isTopButtonPressed(){
int upPressed = digitalRead(PIN_BUTTON_UP);
if(upPressed == LOW){
return buttonDebounceComplete();
} else {
return false;
}
}
bool isMiddleButtonPressed(){
int enterPressed = digitalRead(PIN_BUTTON_ENTER);
if(enterPressed == LOW){
return buttonDebounceComplete();
} else {
return false;
}
}
bool isBottomButtonPressed(){
int downPressed = digitalRead(PIN_BUTTON_DOWN);
if(downPressed == LOW){
return buttonDebounceComplete();
} else {
return false;
}
}
bool buttonDebounceComplete(){
static unsigned long lastPressedTime = 0;
unsigned long currentTime = millis();
if((currentTime - lastPressedTime) > BUTTON_DEBOUNCE){
lastPressedTime = currentTime;
return true;
} else {
return false;
}
}
void shortBeepXTimesNoDelay(int times){
beepsLeft = times;
}
//We don't use delay from beeps to avoid holding up the controller
void shortBeepRunner(){
static bool beepCurrentlyOn = false;
static unsigned long beepChangedTime = 0;
unsigned long currentTime = millis();
if(beepsLeft > 0){
if(beepCurrentlyOn){
if((currentTime - beepChangedTime) > BUZZER_BEEP_ON_TIME){
beepChangedTime = currentTime;
digitalWrite(PIN_BUZZER, LOW);
beepCurrentlyOn = false;
beepsLeft--;
}
} else {
if((currentTime - beepChangedTime) > BUZZER_BEEP_OFF_TIME){
beepChangedTime = currentTime;
digitalWrite(PIN_BUZZER, HIGH);
beepCurrentlyOn = true;
}
}
}
}
//Returns true if temperature threshold has been reached
bool temperatureWarningCheck(float temperature){
if(temperature >= TEMP_MAX){
nextState = STATE_TEMP_MAX;
return true;
} else if(temperature >= TEMP_WARN){
static unsigned long lastTempAlert = 0;
unsigned long currentTime = millis();
if((currentTime - lastTempAlert) > TEMP_WARNING_RATE){
lastTempAlert = currentTime;
shortBeepXTimesNoDelay(5);
}
return true;
}
return false;
}