-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhm_control.py
executable file
·379 lines (329 loc) · 14 KB
/
hm_control.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
#!/usr/bin/env python
import time
import board
import struct
import crcmod
import sys
import threading
import enum
from digitalio import DigitalInOut
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# invalid default values for scoping
SPI_BUS, CSN_PIN, CE_PIN = (None, None, None)
import spidev
SPI_BUS = spidev.SpiDev() # for a faster interface on linux
CSN_PIN = 0 # use CE0 on default bus (even faster than using any pin)
CE_PIN = DigitalInOut(board.D22) # using pin gpio22 (BCM numbering)
nrf = RF24(SPI_BUS, CSN_PIN, CE_PIN)
import hm_control_config
nrf.data_rate = 250
nrf.channel = hm_control_config.channel
nrf.auto_ack = True
nrf.set_auto_retries(3,15)
nrf.crc = 2
nrf.dynamic_payloads = True
nrf.pa_level = hm_control_config.pa_level
nrf.address_length = 5
dtu_ser = hm_control_config.dtu_ser
inverter_ser = hm_control_config.inverter_ser
nrf.open_rx_pipe(1, b'\01' + bytearray.fromhex(str(dtu_ser)[-8:]))
f_crc_m = crcmod.predefined.mkPredefinedCrcFun('modbus')
f_crc8 = crcmod.mkCrcFun(0x101, initCrc=0, xorOut=0)
class CMD:
ON = 0
OFF = 1
RElastAction = 2
LOCK = 3
UNLOCK = 4
ACTIVE_POWER_LIMIT = 11
REACTIVE_POWER_LIMIT = 12
POWER_FACTOR = 13
LOCK_AND_ALARM = 20
SELF_INSPECT = 40
class PacketType:
TX_REQ_INFO = 0x15
TX_REQ_DEVCONTROL = 0x51
# 0x100 -> persistent
# 0x1 -> relative (%)
def setPowerLimit(dst, limit, relative = False, persist = False):
mod = persist * 0x100
mod+= relative
sendControl(dst, CMD.ACTIVE_POWER_LIMIT, limit * 10, mod)
def sendControl(dst, cmd, data = None, mod = 0):
payload = bytearray(2)
payload[0] = cmd
if data is not None:
payload+= data.to_bytes(2, 'big')
payload+= mod.to_bytes(2, 'big')
sendPacket(dst, PacketType.TX_REQ_DEVCONTROL, payload)
def sendPacket(dst, type, payload, frame_id = 0):
packet = bytes([type])
packet += bytearray.fromhex(str(dst)[-8:])
packet += bytearray.fromhex(str(dtu_ser)[-8:])
packet += int(0x80 + frame_id).to_bytes(1, 'big')
packet += payload
if len(payload) > 0:
packet += struct.pack('>H', f_crc_m(payload))
packet += struct.pack('B', f_crc8(packet))
transmitPackage(packet)
mutex = threading.Lock()
def transmitPackage(package):
inv_esb_addr = b'\01' + package[1:5]
mutex.acquire()
nrf.listen = False
nrf.open_tx_pipe(inv_esb_addr)
nrf.auto_ack = True
#print("sending",package[0:1].hex(), package[1:5].hex(),"->",package[5:9].hex(),package[9:10].hex(), ":",package[10:].hex())
result = nrf.send(package)
nrf.auto_ack = False
nrf.listen = True
mutex.release()
def hm_control_load_config_override():
global inverter_power_min, inverter_power_max, power_target, power_target_lower_threshold, power_target_upper_threshold
import os
inverter_power_min = hm_control_config.inverter_power_min
inverter_power_max = hm_control_config.inverter_power_max
power_target = hm_control_config.power_target
power_target_lower_threshold = hm_control_config.power_target_lower_threshold
power_target_upper_threshold = hm_control_config.power_target_upper_threshold
if 'hm_control_config_override' in sys.modules:
sys.modules.pop('hm_control_config_override')
try:
import hm_control_config_override
except ImportError:
pass
else:
try:
hm_control_config_override.override_valid_until
except:
pass
else:
if (time.time() < hm_control_config_override.override_valid_until):
try:
inverter_power_min = hm_control_config_override.inverter_power_min
if (inverter_power_min < hm_control_config.inverter_power_min):
inverter_power_min = hm_control_config.inverter_power_min
except AttributeError:
pass
try:
inverter_power_max = hm_control_config_override.inverter_power_max
if (inverter_power_max > hm_control_config.inverter_power_max):
inverter_power_max = hm_control_config.inverter_power_max
except AttributeError:
pass
try:
power_target = hm_control_config_override.power_target
except AttributeError:
pass
try:
power_target_lower_threshold = hm_control_config_override.power_target_lower_threshold
except AttributeError:
pass
try:
power_target_upper_threshold = hm_control_config_override.power_target_upper_threshold
except AttributeError:
pass
try:
inverter_power_min_mtime = os.path.getmtime('inverter_power_min')
except FileNotFoundError:
pass
else:
if (time.time() < inverter_power_min_mtime + 60):
try:
with open('inverter_power_min', 'r') as file:
inverter_power_min = max(inverter_power_min, int(file.read().rstrip()))
except AttributeError:
pass
try:
inverter_power_max_mtime = os.path.getmtime('inverter_power_max')
except FileNotFoundError:
pass
else:
if (time.time() < inverter_power_max_mtime + 60):
try:
with open('inverter_power_max', 'r') as file:
inverter_power_max = min(inverter_power_max, int(file.read().rstrip()))
except AttributeError:
pass
try:
power_target_mtime = os.path.getmtime('power_target')
except FileNotFoundError:
pass
else:
if (time.time() < power_target_mtime + 60):
try:
with open('power_target', 'r') as file:
power_target = int(file.read().rstrip())
except AttributeError:
pass
try:
power_target_lower_threshold_mtime = os.path.getmtime('power_target_lower_threshold')
except FileNotFoundError:
pass
else:
if (time.time() < power_target_lower_threshold_mtime + 60):
try:
with open('power_target_lower_threshold', 'r') as file:
power_target_lower_threshold = int(file.read().rstrip())
except AttributeError:
pass
try:
power_target_upper_threshold_mtime = os.path.getmtime('power_target_upper_threshold')
except FileNotFoundError:
pass
else:
if (time.time() < power_target_upper_threshold_mtime + 60):
try:
with open('power_target_upper_threshold', 'r') as file:
power_target_upper_threshold = int(file.read().rstrip())
except AttributeError:
pass
if (inverter_power_min > hm_control_config.inverter_power_max):
inverter_power_min = hm_control_config.inverter_power_max
if (inverter_power_max < hm_control_config.inverter_power_min and inverter_power_max != -999999):
inverter_power_max = hm_control_config.inverter_power_min
#if (inverter_power_min > inverter_power_max):
# inverter_power_tmp = inverter_power_min
# inverter_power_min = inverter_power_max
# inverter_power_max = inverter_power_tmp
def hm_control_set_limit(new_limit, power_measured=None):
global limit, skip_counter, on_off, off_counter
if (new_limit > inverter_power_max):
new_limit = inverter_power_max
elif (new_limit < inverter_power_min):
new_limit = inverter_power_min
if (new_limit == -999999):
off_counter += 1
else:
off_counter = 0
if (new_limit < 0):
new_limit = 0
if (new_limit == 0 and on_off == 'on' and off_counter > hm_control_config.power_set_pause*6):
skip_counter = -1
limit = new_limit
on_off = 'off'
sendControl(inverter_ser, CMD.OFF)
print('[CMD.OFF]')
elif (new_limit > 0 and on_off == 'off'):
skip_counter = -1
limit = new_limit
on_off = 'on'
sendControl(inverter_ser, CMD.ON)
print('[CMD.ON]')
elif (limit < inverter_power_min and limit != 0):
skip_counter = -hm_control_config.power_set_pause
limit = inverter_power_min
setPowerLimit(inverter_ser, int(limit*hm_control_config.inverter_power_multiplier))
print('[set - skip next '+str(hm_control_config.power_set_pause)+' second(s)]')
elif (limit > inverter_power_max and limit != 0):
skip_counter = -hm_control_config.power_set_pause
limit = max(0, inverter_power_max)
setPowerLimit(inverter_ser, int(limit*hm_control_config.inverter_power_multiplier))
print('[set - skip next '+str(hm_control_config.power_set_pause)+' second(s)]')
else:
if (power_measured is not None):
print('Intended power generation:\t'+str(new_limit)+' W', end = '')
skip_counter += 1
if (power_measured is None or power_measured < power_target - power_target_lower_threshold or power_measured > power_target + power_target_upper_threshold):
if (skip_counter >= 0 and new_limit != limit):
skip_counter = -hm_control_config.power_set_pause
limit = new_limit
setPowerLimit(inverter_ser, int(limit*hm_control_config.inverter_power_multiplier))
print('\t[set - skip next '+str(hm_control_config.power_set_pause)+' second(s)]')
elif (skip_counter < 0):
# send the limit again, in case it hasn't been received before
setPowerLimit(inverter_ser, int(limit*hm_control_config.inverter_power_multiplier))
print('\t[wait '+str(abs(skip_counter))+' second(s)]* (resend limit)')
else:
if (skip_counter % (hm_control_config.power_set_pause*9) == 0):
# send command to turn inverter off/on again, in case it hasn't been received before
if (new_limit == 0 and on_off == 'off' and off_counter > hm_control_config.power_set_pause*6):
sendControl(inverter_ser, CMD.OFF)
print('\t[skipped: '+str(skip_counter+1)+'x] (+ CMD.OFF)')
elif (new_limit > 0 and on_off == 'on'):
sendControl(inverter_ser, CMD.ON)
print('\t[skipped: '+str(skip_counter+1)+'x] (+ CMD.ON)')
else:
print('\t[skipped: '+str(skip_counter+1)+'x]')
elif (skip_counter % (hm_control_config.power_set_pause*5) == 0):
# send the limit again, in case it still hasn't been received before
setPowerLimit(inverter_ser, int(limit*hm_control_config.inverter_power_multiplier))
print('\t[skipped: '+str(skip_counter+1)+'x]* (resend limit)')
else:
print('\t[skipped: '+str(skip_counter+1)+'x]')
else:
if (skip_counter < 0):
print('\t[wait '+str(abs(skip_counter))+' second(s)]')
else:
print('\t[skipped: '+str(skip_counter+1)+'x]')
time.sleep(1)
try:
limit = sys.argv[1]
except:
limit = hm_control_config.fail_power_limit
else:
if (limit.isnumeric() is False):
limit = hm_control_config.fail_power_limit
else:
limit = int(limit)
print('Setting power limit to known value ('+str(limit)+' W)...\t', end = '')
if (limit > 0):
on_off = 'off' # invert, to trigger on/off condition
else:
on_off = 'on'
off_counter = 0
skip_counter = 0
fail_counter = 0
hm_control_load_config_override()
hm_control_set_limit(limit+1) # send slightly different limit to trigger "new_limit != limit" condition
import requests
import json
while True:
try:
hm_control_load_config_override()
r = requests.get('http://'+hm_control_config.shelly3em+'/status')
if (r.status_code == 200):
fail_counter = 0
data = json.loads(r.text)
print()
print('Inverter power limit:\t\t'+str(limit)+' W', end='')
if (limit == inverter_power_max):
print('\t[max: '+str(inverter_power_max)+' W]')
elif (limit == inverter_power_min):
print('\t[min: '+str(inverter_power_min)+' W]')
else:
print()
power_measured = round(data['total_power'])
print('Measured energy consumption:\t'+str(power_measured)+' W', end='')
if (power_measured >= power_target - power_target_lower_threshold and power_measured <= power_target + power_target_upper_threshold):
print ('\t[tolerated range: '+str(power_target - power_target_lower_threshold)+' W to '+str(power_target + power_target_upper_threshold)+' W]')
else:
print()
power_calculated = power_measured + limit
print('Calculated energy consumption:\t'+str(power_calculated)+' W')
hm_control_set_limit(power_calculated-power_target, power_measured)
else:
fail_counter += 1
if (fail_counter > hm_control_config.fail_threshold):
print('Fail threshold exceeded, setting power limit to '+str(hm_control_config.fail_power_limit)+' W...', end='')
hm_control_set_limit(hm_control_config.fail_power_limit)
else:
print('Failed to get energy consumption, retrying... ['+str(fail_counter)+']')
time.sleep(0.5)
except KeyboardInterrupt:
print()
print('Keyboard interrupt detected, stopping...')
break
except:
print()
fail_counter += 1
if (fail_counter > hm_control_config.fail_threshold):
print('Fail threshold exceeded, setting power limit to '+str(hm_control_config.fail_threshold)+' W...', end='')
hm_control_set_limit(hm_control_config.fail_power_limit)
else:
print('Something went wrong, retrying... ['+str(fail_counter)+']')
print()
time.sleep(0.5)
continue