-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInteractive.cpp
More file actions
201 lines (151 loc) · 6.11 KB
/
Interactive.cpp
File metadata and controls
201 lines (151 loc) · 6.11 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
#include "Interactive.h"
Interactive::Interactive( RMSSensor *vac_in, RMSSensor *vac_out, Sensor *ac_out, Sensor *v_bat) {
// link to sensors
_vac_in = vac_in;
_vac_out = vac_out;
_ac_out = ac_out;
_v_bat = v_bat;
// set relays to output
pinMode(INTERACTIVE_INPUT_RLY_OUT, OUTPUT);
pinMode(INTERACTIVE_OUTPUT_RLY_OUT, OUTPUT);
pinMode(INTERACTIVE_LEFT_RLY_OUT, OUTPUT);
pinMode(INTERACTIVE_RIGHT_RLY_OUT, OUTPUT);
pinMode(INTERACTIVE_INVERTER_OUT, OUTPUT);
pinMode(INTERACTIVE_ERROR_OUT, OUTPUT);
// type of the UPS
writeStatus( LINE_INTERACTIVE, true );
// beeper enabled at the start
writeStatus( BEEPER_IS_ACTIVE, true );
}
RegulateStatus Interactive::regulate(unsigned long ticks) {
// read the sensors
_battery_level = max(min((_v_bat->reading() - INTERACTIVE_MIN_V_BAT) / INTERACTIVE_V_BAT_DELTA, 1.0F), 0.0F) ;
float ac_out = _ac_out->reading();
float vac_input = _vac_in->reading();
bool bad_sine = _vac_in->bad_sine();
float abs_deviation = abs(_nominal_vac_input - vac_input);
float nominal_deviation = _deviation * _nominal_vac_input;
float nominal_hysteresis = _hysteresis * _nominal_vac_input;
writeStatus(BATTERY_DEAD, _v_bat->reading() < INTERACTIVE_MIN_V_BAT );
writeStatus(BATTERY_LOW, _battery_level < INTERACTIVE_BATTERY_LOW );
// If output is disconnected but the load is present then it may
// point at the defective output relay or the load sensor
writeStatus(UNUSUAL_STATE, !readStatus( OUTPUT_CONNECTED ) && ( ac_out > INTERACTIVE_MIN_AC_OUT ) );
// overload protection
writeStatus(OVERLOAD, ac_out > INTERACTIVE_MAX_AC_OUT);
// input voltage is far off the regulation limits (X2)
writeStatus(UTILITY_FAIL, (abs_deviation > 2 * (nominal_deviation - nominal_hysteresis * ( _batteryMode? 1 : - 1 ))) || bad_sine );
// stop self-test if the battery is low
writeStatus(SELF_TEST, _selfTestMode && !readStatus(BATTERY_LOW) );
if(_batteryMode ) {
// check output voltage after inverter.
float out_deviation = _vac_out->reading() - _nominal_vac_input;
// if output is wrong post grace period, report failure
if( abs(ticks - _last_time) > INVERTER_GRACE_PERIOD ) {
if( abs(out_deviation) > nominal_deviation )
writeStatus(UPS_FAULT, true);
}
}
else
_last_time = ticks;
if(readStatus(UTILITY_FAIL)) {
_last_fail_time = ticks;
writeStatus(SELF_TEST, false);
if(!_batteryMode) {
_last_fault_input_voltage = vac_input;
}
}
if(_shutdownMode) {
// writeStatus(UTILITY_FAIL, utility_fail);
return update_state(REGULATE_STATUS_SHUTDOWN);
}
else {
if( readStatus( SHUTDOWN_ACTIVE ) ) {
return update_state(REGULATE_STATUS_WAKEUP);
}
}
// if the state is overload or output voltage is wrong, no regulation, need cold reset.
if( _status & (( 1U << OVERLOAD ) | ( 1U << UPS_FAULT )) ) {
return update_state(REGULATE_STATUS_ERROR);
}
if( ( _status & (( 1U << UTILITY_FAIL ) | ( 1U << SELF_TEST )) ) || (ticks - _last_fail_time < TIMER_ONE_SEC * 2) ) {
toggleInput(false);
adjustOutput(REGULATE_NONE);
if(readStatus(BATTERY_DEAD))
return update_state(REGULATE_STATUS_ERROR);
else
return update_state(REGULATE_STATUS_FAIL);
}
// input voltage is within the regulation limits
// regulate
if( abs_deviation > nominal_deviation - nominal_hysteresis * (bitRead(_status, REGULATED)? 1 : -1 ) ) {
// input voltage deviation exceeds the limit
if(vac_input > _nominal_vac_input )
// input voltage is higher than limit, step down
adjustOutput(REGULATE_DOWN);
else
// input voltage is lower than limit, step up
adjustOutput(REGULATE_UP);
}
else {
// input voltage is within the limits, pass on to the load
adjustOutput(REGULATE_NONE);
}
return update_state(REGULATE_STATUS_SUCCESS);
}
void Interactive::toggleInverter(bool mode) {
digitalWrite(INTERACTIVE_INVERTER_OUT, mode);
_batteryMode = mode;
}
void Interactive::toggleOutput( bool mode ) {
digitalWrite(INTERACTIVE_OUTPUT_RLY_OUT, mode);
writeStatus(OUTPUT_CONNECTED, mode);
}
void Interactive::toggleInput(bool mode) {
digitalWrite(INTERACTIVE_INPUT_RLY_OUT, mode);
writeStatus(INPUT_CONNECTED, mode);
}
void Interactive::toggleError(bool mode) {
digitalWrite(INTERACTIVE_ERROR_OUT, mode);
}
void Interactive::adjustOutput(RegulateMode mode) {
if(mode) {
writeStatus(REGULATED, true);
digitalWrite(INTERACTIVE_LEFT_RLY_OUT, mode == REGULATE_DOWN ? HIGH : LOW);
digitalWrite(INTERACTIVE_RIGHT_RLY_OUT, mode == REGULATE_UP ? HIGH : LOW);
}
else {
writeStatus(REGULATED, false);
digitalWrite(INTERACTIVE_LEFT_RLY_OUT, LOW);
digitalWrite(INTERACTIVE_RIGHT_RLY_OUT, LOW);
}
}
void Interactive::writeStatus(uint16_t nbit, bool value) {
if(value)
bitSet(_status, nbit);
else
bitClear(_status, nbit);
}
void Interactive::sleep(uint32_t timeout) {
wdt_disable();
ADCSRA &= ~ (1 << ADEN); // Disable ADC
ACSR |= (1 << ACD); // Disable comparator
while (timeout > 0) {
wdt_enable(WDTO_250MS); // Enable WDT
WDTCSR |= (1 << WDIE); // Режим ISR+RST
wdt_reset(); // Reset WDT
set_sleep_mode(SLEEP_MODE);
sleep_enable(); // Enable sleep mode
sleep_cpu(); // Put the CPU to sleep
sleep_disable(); // Disable sleep mode
wdt_disable(); // Disable WDT
wdt_reset(); // Reset WDT
timeout --;
}
ADCSRA |= (1 << ADEN); // Enable ADC
ACSR &= ~ (1 << ACD); // Enable comparator
wdt_enable(WDTO_2S);
}
ISR(WDT_vect) {
for(int i=0; i<1000;i++);
}