-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummyTSUN.py
More file actions
350 lines (285 loc) · 12.2 KB
/
dummyTSUN.py
File metadata and controls
350 lines (285 loc) · 12.2 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
#!/usr/bin/python3
#
# For use with PiCAN boards on the Raspberry Pi
# http://skpang.co.uk/catalog/pican2-canbus-board-for-raspberry-pi-2-p-1475.html
#
# Make sure Python-CAN is installed first http://skpang.co.uk/blog/archives/1220
#
# August 2021 James Stulen
# Holy Sh*t it's been a year since we started this thing!
# samples commands for testing
#
# ./cansend can1 010#cafeface
# sudo /sbin/ip link set can1 up type can bitrate 500000
# ./candump can0
# message logging to file batterydummy.log in local directory
#Niall testing git abilities. I cloned the git to get it from github onto our Pi3. I'm now going to attempt to merge it back into github with this mod.
import os
#import logging
import can
import time
import queue
#import pickle
from threading import Thread
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#from gpiozero import LED #, OutputDevice
#------------------------------------------------------------------------------
PID_INVERTER_QUERY = 0x4200 #from inverter
PID_SLEEP_AWAKE_COMMAND = 0x8201 #from inverter, sent to battery 1
PID_SLEEP_AWAKE_COMMAND_Req_Sleep = 0x55
PID_SLEEP_AWAKE_COMMAND_Quit_sleep = 0xAA
BASIC_STATUS_SLEEP = 0
BASIC_STATUS_CHARGE = 1
BASIC_STATUS_DISCHARGE = 2
BASIC_STATUS_IDLE = 3
# initialize BasicStatus
global BasicStatus
BasicStatus = BASIC_STATUS_IDLE
#------------------------------------------------------------------------------
GPIO_POS_RELAY = 5
GPIO_NEG_RELAY = 6
GPIO.setup(GPIO_POS_RELAY, GPIO.OUT)
GPIO.setup(GPIO_NEG_RELAY, GPIO.OUT)
global ContactorsOpen
def OpenContactors():
global ContactorsOpen
GPIO.output(GPIO_NEG_RELAY, GPIO.HIGH)
GPIO.output(GPIO_POS_RELAY, GPIO.HIGH)
ContactorsOpen = True
print('CONTACTORS OPEN.')
def CloseContactors():
global ContactorsOpen
print('CONTACTORS Closing.')
GPIO.output(GPIO_NEG_RELAY, GPIO.LOW)
time.sleep(2)
GPIO.output(GPIO_POS_RELAY, GPIO.LOW)
ContactorsOpen = False
print('CONTACTORS CLOSED.')
#------------------------------------------------------------------------------
class cSendMsg:
def __init__(self, id, msgdata, interval, time):
self.id = id
self.msgdata = msgdata
self.interval = interval
self.time = time
#------------------------------------------------------------------------------
class cCanRead:
def __init__(self, msg_id, msg_type):
self.msg_id = msg_id
self.msg_type = msg_type
#------------------------------------------------------------------------------
def can_rx_task(): # Receive thread
while True:
message = bus.recv()
#if message.arbitration_id == PID_REPLY:
q.put(message) # Put message into queue
#------------------------------------------------------------------------------
def read_can():
# initiallise byte array for all the group data
group_data = bytearray()
try:
message = q.get(False)
# if the q is empty an exception is raised
except queue.Empty:
#print("Empty")
return cCanRead(0,0)
for x in range(message.dlc):
group_data.append(message.data[x])
byte_str = format(message.arbitration_id,'03x') + ': '
for x in range(len(group_data)):
value = format(group_data[x],'02x')
byte_str = byte_str + value + ' '
return cCanRead( message.arbitration_id, group_data[0] )
#------------------------------------------------------------------------------
def LoByte(value):
lobyte = value%256
return int(lobyte)
def HiByte(value):
hibyte = int(value/256)
return int(hibyte)
#------------------------------------------------------------------------------
# LIST OF MESSAGES TO SEND IN RESPONSE TO INVERTER QUERY 'ENSEMBILE INFORMATION' byte 0 = 0
# TSUN doc says LSB so we will put lo byte first in the 2 byte values
# this is opposite of the AOS/SMA and done this way because LSB was not checked
# on SavvyCAN Signals from the AOS/SMA
ensemblerspmsg = []
sysinfomsg = []
# Battery Info
BatPileTotVolt = 380
BatPileCur = 0
SecLvlBMSTemp = 15
BatSOC = 55
BatSOH = 60
# 0x421+1
msg = cSendMsg( 0x4211, [ LoByte( BatPileTotVolt *10 ), HiByte( BatPileTotVolt *10) , LoByte((3000 + BatPileCur) *10 ), HiByte((3000 + BatPileCur) *10 ), LoByte((100 + SecLvlBMSTemp) *10 ), HiByte((100 + SecLvlBMSTemp) *10), int(BatSOC), int(BatSOH) ], 10, 0)
ensemblerspmsg.append(msg)
# Charge Limits
ChargeCutoffVolt = 390
DischargeCutoffVolt = 340 #Set to see if TSUN cuts out at 40 less (ie 300)
MaxChargeCur = 3.5
MaxDischargeCur = 1.5 #5.25 is max we should do through the 10A socket it is currently wired into (2000W @380V)
# 0x422+1
msg = cSendMsg( 0x4221, [ LoByte( ChargeCutoffVolt*10 ), HiByte( ChargeCutoffVolt*10) , LoByte( DischargeCutoffVolt *10 ), HiByte( DischargeCutoffVolt *10 ), LoByte((+3000 + MaxChargeCur) *10 ), HiByte((+3000 + MaxChargeCur) *10), LoByte((+3000 + MaxDischargeCur) *10), HiByte((+3000 + MaxDischargeCur) *10) ], 10, 0)
ensemblerspmsg.append(msg)
# Cell Data
MaxSingleCellVolt = 3.980
MinSingleCellVolt = 3.978
MaxSingleCellNumber = 1
MinSingleCellNumber = 2
# 0x423+1
msg = cSendMsg( 0x4231, [ LoByte( MaxSingleCellVolt *1000 ), HiByte( MaxSingleCellVolt *1000 ) , LoByte( MinSingleCellVolt *1000 ), HiByte( MinSingleCellVolt * 1000 ), LoByte( MaxSingleCellNumber ), HiByte( MaxSingleCellNumber), LoByte( MinSingleCellNumber ), HiByte( MinSingleCellNumber) ], 10, 0)
ensemblerspmsg.append(msg)
# Cell Temperatures
MaxCellTemp = 16
MinCellTemp = 14
MaxCellTempNumber = 3
MinCellTempNumber = 4
# 0x424+1
msg = cSendMsg( 0x4241, [ LoByte((+100 + MaxCellTemp) *10 ), HiByte((+100 + MaxCellTemp) *10 ) , LoByte((+100 + MinCellTemp) *10 ), HiByte((+100 + MinCellTemp) *10 ), LoByte( MaxCellTempNumber ), HiByte( MaxCellTempNumber ), LoByte( MinCellTempNumber ), HiByte( MinCellTempNumber ) ], 10, 0)
ensemblerspmsg.append(msg)
# Status,Error,Alarm,Protection
#BasicStatus = initilzied above and modified by PID_SLEEP_AWAKE_COMMAND message
CyclePeriod = 0 #WTF is this?
Error = 0
Alarm = 0
Protection = 0
# 0x425+1
msg = cSendMsg( 0x4251, [ BasicStatus, LoByte( CyclePeriod), HiByte( CyclePeriod ) , Error , LoByte( Alarm ), HiByte( Alarm ), LoByte( Protection ), HiByte( Protection ) ], 10, 0)
ensemblerspmsg.append(msg)
# Module Volts
ModuleMaxVolt = 2 * MaxSingleCellVolt
ModuleMinVolt = 2 * MinSingleCellVolt
ModuleMaxVoltNumber = 1
ModuleMinVoltNumber = 2
# 0x426+1
msg = cSendMsg( 0x4261, [ LoByte( ModuleMaxVolt *1000 ), HiByte( ModuleMaxVolt *1000 ) , LoByte( ModuleMinVolt *1000 ), HiByte( ModuleMinVolt *1000 ), LoByte( ModuleMaxVoltNumber ), HiByte( ModuleMaxVoltNumber ), LoByte( ModuleMinVoltNumber ), HiByte( ModuleMinVoltNumber ) ], 10, 0)
ensemblerspmsg.append(msg)
# Module Temps
ModuleMaxTemp = 16
ModuleMinTemp = 14
ModuleMaxTempNumber = 3
ModuleMinTempNumber = 4
# 0x427+1
msg = cSendMsg( 0x4271, [ LoByte((+100 + ModuleMaxTemp) *10 ), HiByte((+100 + ModuleMaxTemp) *10 ) , LoByte((+100 + ModuleMinTemp) *10 ), HiByte((+100 + ModuleMinTemp) *10 ), LoByte( ModuleMaxTempNumber ), HiByte( ModuleMaxTempNumber ), LoByte( ModuleMinTempNumber ), HiByte( ModuleMinTempNumber ) ], 10, 0)
ensemblerspmsg.append(msg)
# Charge/Dis command
ChargeForbidden = 0 # 170 (0xAA) for effect
DischargeForbidden = 0 # 170 (0xAA) for effect
# 0x428+1
msg = cSendMsg( 0x4281, [ LoByte( ChargeForbidden ), LoByte( DischargeForbidden ), 0,0,0,0,0,0 ], 10, 0)
ensemblerspmsg.append(msg)
#------------------------------------------------------------------------------
# LIST OF MESSAGES TO SEND IN RESPONSE TO INVERTER QUERY 'SYSTEM EQUIPMENT INFORMATION' byte 0 = 2
# Version Info - see documentaion
# 0x731+1
msg = cSendMsg( 0x7311, [ 0X10, 0X10, 0X10, 0X10, 0X10, 0X10, 0X10, 0 ], 10, 0)
sysinfomsg.append(msg)
# 0x732+1
BatteryModuleQuantity = 48
BatteryModuleInSeries = 48
CellQuantityInModule = 2
VoltLevel = 4
AHNumber = 66 * BatSOH/100 # 66 AH Leaf battery capacity
# last 1 byte reserve
# 0x732+1
msg = cSendMsg( 0x7321, [ LoByte( BatteryModuleQuantity ), HiByte( BatteryModuleQuantity ) , BatteryModuleInSeries, CellQuantityInModule, LoByte( VoltLevel ), HiByte( VoltLevel ), int(AHNumber), 0 ], 10, 0)
sysinfomsg.append(msg)
# 0x4220
# 0x4220+0
#msg = cSendMsg( 0x42200, [ LoByte( ), HiByte( ) , LoByte( ), HiByte( ), LoByte( ), HiByte( ), LoByte( ), HiByte() ], 10, 0)
#ensemblerspmsg.append(msg)
# LIST OF MESSAGES TO BE SENT AT FIXED INTERVALS TO LEAF LBC CONTROLLER
#msgFi = []
#msg = cSendMsg( 0x50B, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00], 100, 0)
#msgFi.append(msg)
#------------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------------
##loggingbasicConfig(filename='can.log',format='%(levelname)s:%(message)s', level=#loggingWARN) # INFO-WARN
#loggingbasicConfig(filename='batterydummy.log',format='%(asctime)s %(levelname)s:%(message)s', level=#loggingINFO)
#loggingdebug('Debug Message')
#logginginfo('Info Message')
#loggingwarning('Warning Message')
# open contactors on startup
OpenContactors()
# Bring up can interface at 500kbps
#print('Bring up can0 and can1 interfaces....')
#logginginfo('Bring up can0 interface....')
os.system("sudo /sbin/ip link set can0 up type can bitrate 500000")
#os.system("sudo /sbin/ip link set can1 up type can bitrate 500000")
time.sleep(0.1)
print('Ready')
try:
#bus = can.interface.Bus('can0', bustype='virtual') #TESTING
bus = can.interface.Bus(channel='can0', bustype='socketcan')
#bus2 = can.interface.Bus(channel='can1', bustype='socketcan')
#bus.set_filters([{"can_id": PID_REPLY, "can_mask": 0x00}])
except OSError:
print('Cannot find PiCAN board.')
exit()
q = queue.Queue()
rx = Thread(target=can_rx_task)
rx.start()
# Main loop
try:
while True:
timenow = int(round(time.time() * 1000))
# for x in range(len(msgFi)):
# if (timenow - msgFi[x].time > msgFi[x].interval):
# msgFi[x].time = int(round(time.time() * 1000))
# msg = can.Message(arbitration_id=msgFi[x].id, data=msgFi[x].msgdata, extended_id=False)
# bus2.send(msg)
# time.sleep(0.001)
# non-blocking read, returns 0 if nothing read
rx_data = read_can()
#print (rx_data)
if (rx_data.msg_id != 0):
print ('Message Received ' + format(rx_data.msg_id,' 02x'))
if (rx_data.msg_id == PID_INVERTER_QUERY):
print('Received: ',format(rx_data.msg_id, '02x'),' msg_type: ', rx_data.msg_type)
#check first byte is 0 or 2
if (rx_data.msg_type == 0):
if (ContactorsOpen):
CloseContactors()
for x in range(len(ensemblerspmsg)):
msg = can.Message(arbitration_id=ensemblerspmsg[x].id, data=ensemblerspmsg[x].msgdata, extended_id=True)
bus.send(msg)
# send for loop delay
time.sleep(ensemblerspmsg[x].interval/1000) # interval is in milliseconds
print ('Ensemble Response Sent ')
elif (rx_data.msg_type == 2):
for x in range(len(sysinfomsg)):
msg = can.Message(arbitration_id=sysinfomsg[x].id, data=sysinfomsg[x].msgdata, extended_id=True)
bus.send(msg)
# send for loop delay
time.sleep(sysinfomsg[x].interval/1000) # interval is in milliseconds
print ('System Info Response Sent ')
elif (rx_data.msg_id == PID_SLEEP_AWAKE_COMMAND):
if (rx_data.msg_type == PID_SLEEP_AWAKE_COMMAND_Req_Sleep):
BasicStatus = BASIC_STATUS_SLEEP
ensemblerspmsg[4].msgdata[0] = BasicStatus
print('Status set to SLEEP')
elif (rx_data.msg_type == PID_SLEEP_AWAKE_COMMAND_Quit_sleep):
BasicStatus = BASIC_STATUS_IDLE
ensemblerspmsg[4].msgdata[0] = BasicStatus
print('Status set to IDLE')
# for x in range(len(sendmsg)):
# if (timenow - sendmsg[x].time > sendmsg[x].interval):
# sendmsg[x].time = int(round(time.time() * 1000))
# msg = can.Message(arbitration_id=sendmsg[x].id, data=sendmsg[x].msgdata, extended_id=False)
# bus.send(msg)
# ##print ('Message Sent ' + format(x,' 02x') + format(sendmsg[x].id,' 02x'))
# # send for loop delay
# time.sleep(0.001)
# main loop delay
time.sleep(0.001)
except KeyboardInterrupt:
print('\n\rKeyboard interrupt')
#Catch keyboard interrupt
OpenContactors()
# Reset GPIO settings
GPIO.cleanup()
os.system("sudo /sbin/ip link set can0 down")
#os.system("sudo /sbin/ip link set can1 down")
print('\n\rShutdown')