-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplc-conveyor
executable file
·279 lines (221 loc) · 7.16 KB
/
plc-conveyor
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
#!/usr/bin/python3
# Copyright (c) 2022 RS Components Ltd
# SPDX-License-Identifier: Apache License 2.0
import sys
import time
import json
import brainboxes
import paho.mqtt.publish as publish
from binascii import unhexlify
# MQTT configuration
mqtt_broker = 'localhost'
sensor_topic = 'conveyor/sensors'
state_topic = 'conveyor/state'
# ASCII TCP servers
bb400ip = '127.0.0.1'
ed538ip = '192.168.127.254'
ed549ip = '192.168.127.253'
# Sensor constants
volts_mult = 3
amps_mult = 0.5
temp_mult = 10
vib_mult = 10
meas_dp = 2
# Beacon amber > green transition delay (s)
beaconDelay = 2
# Initialise variables
enst = runst = prevRunst = False
step = 0
# Check if a bit is set
def isBitSet(resp, pos):
intval = int.from_bytes(unhexlify((resp[3:])), byteorder='big')
return intval & 1 << pos != 0
# Read BB-400 digital inputs (stop and start buttons)
def readBB400():
try:
with brainboxes.AsciiIo(ipaddr=bb400ip, port=9500, timeout=1.0) as bb400:
rxdata = bb400.command_response(b'@01')
except:
print(' *** BB-400 read: Failed to connect!')
if rxdata is None:
print(' *** BB-400 read: No response received!')
return
elif len(rxdata) != 5:
print(' *** BB-400 read: Bad response received!')
return
else:
pb_stop = isBitSet(rxdata, 1)
pb_start = isBitSet(rxdata, 0)
return(pb_stop, pb_start)
# Read ED-538 digital inputs (system enable state + fault push buttons)
def readED538():
try:
with brainboxes.AsciiIo(ipaddr=ed538ip, port=9500, timeout=1.0) as ed538:
rxdata = ed538.command_response(b'@01')
except:
print(' *** ED-538 read: Failed to connect!')
return
if rxdata is None:
print(' *** ED-538 read: No response received!')
return
elif len(rxdata) != 5:
print(' *** ED-538 read: Bad response received!')
return
else:
enable = isBitSet(rxdata, 0)
pb_volt = isBitSet(rxdata, 4)
pb_cur = isBitSet(rxdata, 5)
pb_temp = isBitSet(rxdata, 6)
pb_vib = isBitSet(rxdata, 7)
return(enable, pb_volt, pb_cur, pb_temp, pb_vib)
# Read ED-549 analogue inputs (supply voltage + current, temperature, vibration)
def readED549():
try:
with brainboxes.AsciiIo(ipaddr=ed549ip, port=9500, timeout=1.0) as ed549:
rxdata = ed549.command_response(b'#01')
except:
print(' *** ED-549 read: Failed to connect!')
if rxdata is None:
print(' *** ED-549 read: No response received!')
return
elif len(rxdata) != 57:
print(' *** ED-549 read: Bad response received!')
return
else:
sup_volts = round(float(rxdata[2:6]) * volts_mult, meas_dp)
sup_amps = round(float(rxdata[9:15]) * amps_mult, meas_dp)
motor_tempc = round(float(rxdata[16:22]) * temp_mult, meas_dp)
motor_vib = round(float(rxdata[23:27]) * vib_mult, meas_dp)
return(sup_volts, sup_amps, motor_tempc, motor_vib)
# Evaluate run state
def getRun():
try:
stop, start = readBB400()
if stop == False and start == True:
return False
elif stop == True and start == False:
return True
else:
return
except:
print(' *** getRun: Failed to read BB-400!')
# Set the beacon colour
def setBeacon(colour):
if colour == 'red':
msgs = (b'#011600', b'#011500', b'#011401')
elif colour == 'amber':
msgs = (b'#011600', b'#011400', b'#011501')
elif colour == 'green':
msgs = (b'#011400', b'#011500', b'#011601')
else:
print(' *** BB-400 setBeacon invalid colour: {0}'.format(colour))
return
try:
with brainboxes.AsciiIo(ipaddr=bb400ip, port=9500, timeout=1.0) as bb400:
for txdata in msgs:
data = bb400.command_noresponse(txdata)
time.sleep(0.01)
except:
print(' *** BB-400 setBeacon: Failed to connect!')
# Set the conveyor state
def setConveyor(state):
if state == 'stop':
txdata = b'#010A00'
elif state == 'run':
txdata = b'#010A01'
else:
print(' *** ED-538 setConveyor invalid state: {0}'.format(state))
return
try:
with brainboxes.AsciiIo(ipaddr=ed538ip, port=9500, timeout=1.0) as ed538:
data = ed538.command_noresponse(txdata)
except:
print(' *** ED-538 setConveyor: Failed to connect!')
# Set process step
def setStep():
global step, tick
print(' Running step: {0}'.format(step))
if step == 0:
setConveyor('stop')
setBeacon('red')
elif step == 1:
setBeacon('amber')
elif step == 2:
setBeacon('green')
setConveyor('run')
else:
step = 0
tick = time.time()
# Initialise process
setStep()
# Main process loop
while True:
try:
# Save previous run state
prevRunst = runst
# Read ED-538 inputs (enable circuit and fault push buttons)
try:
enable, pb_volt, pb_cur, pb_temp, pb_vib = readED538()
except:
print(' *** Failed to read ED-538')
# Set the enable state
if enable != None:
enst = enable
# Read and obey start/stop buttons if we're enabled
if enst == True:
run = getRun()
if run != None:
runst = run
else:
runst = False
# Stop if required
if runst == False and prevRunst == True:
print(' Stopping! Run: {0} // Enable: {1}'.format(runst, enst))
runst = False
step = 0
setStep()
# Run the plant process
if runst == True:
if step == 0:
step = 1
setStep()
elif step == 1:
if time.time()-tick > beaconDelay:
step = 2
setStep()
else:
pass
else:
print(' Run: {0} // Enable: {1}'.format(runst, enst))
# Read the analogue inputs
try:
volts, amps, tempc, vib = readED549()
except:
print(' *** Failed to read ED-549!')
# Fomat process data and publish
try:
process_state = {
'enable_state': enst,
'run_state': runst,
'step': step,
'voltage_fault': pb_volt,
'current_fault': pb_cur,
'temperature_fault': pb_temp,
'vibration_fault': pb_vib
}
process_sensors = {
'supply_voltage': volts,
'supply_current': amps,
'motor_temperature': tempc,
'motor_vibration': vib
}
msgs = [{'topic':state_topic, 'payload':json.dumps(process_state)},
(sensor_topic, json.dumps(process_sensors), 0, False)]
publish.multiple(msgs, hostname=mqtt_broker, client_id='conveyor')
except:
print(' *** Failed to publish to MQTT broker!')
# Don't scan too fast
time.sleep(0.5)
except KeyboardInterrupt:
print('CTRL+C pressed - Exit!')
sys.exit()