-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflow.py
More file actions
281 lines (249 loc) · 10.7 KB
/
flow.py
File metadata and controls
281 lines (249 loc) · 10.7 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
import sys
import os
# Check for simulation mode
SIMULATION_MODE = os.getenv("RIO_SIMULATION", "false").lower() == "true"
# Import from sibling module
# Note: We import from the parent package to ensure we get the same module instance
# that main.py uses, avoiding import path issues
import sys as sys_module # noqa: E402
import os as os_module # noqa: E402
parent_dir = os_module.path.dirname(os_module.path.dirname(os_module.path.abspath(__file__)))
if parent_dir not in sys_module.path:
sys_module.path.insert(0, parent_dir)
from drivers import spi_handler # noqa: E402
# Note: spi_handler.spi is a global variable set by spi_init()
# We access it as spi_handler.spi after initialization
# spi_handler handles simulation mode automatically
class PiFlow:
DEVICE_ID = "MICROFLOW"
STX = 2
PRESSURE_SHIFT = 3
PRESSURE_SCALE = 1 << PRESSURE_SHIFT
PACKET_TYPE_GET_ID = 1
PACKET_TYPE_SET_PRESSURE_TARGET = 2
PACKET_TYPE_GET_PRESSURE_TARGET = 3
PACKET_TYPE_GET_PRESSURE_ACTUAL = 4
PACKET_TYPE_SET_FLOW_TARGET = 5
PACKET_TYPE_GET_FLOW_TARGET = 6
PACKET_TYPE_GET_FLOW_ACTUAL = 7
PACKET_TYPE_SET_CONTROL_MODE = 8
PACKET_TYPE_GET_CONTROL_MODE = 9
PACKET_TYPE_SET_FPID_CONSTS = 10
PACKET_TYPE_GET_FPID_CONSTS = 11
NUM_CONTROLLERS = 4
def __init__(self, device_port, reply_pause_s):
self.device_port = device_port
self.reply_pause_s = reply_pause_s
# In simulation mode, use SimulatedFlow directly
self._simulated_flow = None
sim_mode = os.getenv("RIO_SIMULATION", "false").lower() == "true"
if sim_mode:
try:
from simulation.flow_simulated import SimulatedFlow
self._simulated_flow = SimulatedFlow(
device_port=device_port, reply_pause_s=reply_pause_s
)
except ImportError as e:
import logging
logger = logging.getLogger(__name__)
logger.debug(f"Could not import SimulatedFlow: {e}, falling back to SPI")
pass # Fall back to SPI-based communication
def read_bytes(self, bytes):
data = []
# Ensure spi is initialized
if spi_handler.spi is None:
import logging
logger = logging.getLogger(__name__)
logger.error("SPI not initialized! Call spi_init() before using drivers.")
return []
for x in range(bytes):
data.extend(spi_handler.spi.xfer2([0]))
return data
def packet_read(self):
valid = False
type_read = 0 # Initialize type_read
data = []
spi_handler.spi_select_device(self.device_port)
data_bytes = self.read_bytes(1)
if len(data_bytes) > 0 and data_bytes[0] == self.STX:
data_bytes.extend(self.read_bytes(2))
if len(data_bytes) >= 3:
size = data_bytes[1]
type_read = data_bytes[2]
data_bytes.extend(self.read_bytes(size - 3))
checksum = sum(data_bytes) & 0xFF
if checksum == 0:
data = data_bytes[3 : (size - 1)]
valid = True
if not valid:
data = []
return valid, type_read, data
def packet_write(self, type, data):
# Ensure spi is initialized
if spi_handler.spi is None:
import logging
logger = logging.getLogger(__name__)
logger.error("SPI not initialized! Call spi_init() before using drivers.")
return
msg = [2, len(data) + 4, type] + data
checksum = (-(sum(msg) & 0xFF)) & 0xFF
msg.append(checksum)
spi_handler.spi_select_device(self.device_port)
spi_handler.spi.xfer2(msg)
def packet_query(self, type, data):
# In simulation mode, use SimulatedFlow directly
if self._simulated_flow is not None:
return self._simulated_flow.packet_query(type, data)
# Real hardware mode - use SPI communication
valid = False # Initialize before try block
data_read: list[int] = [] # Initialize before try block
try:
spi_handler.spi_lock()
self.packet_write(type, data)
# time.sleep( self.reply_pause_s )
spi_handler.pi_wait_s(self.reply_pause_s)
valid = True
data_read = []
type_read = 0x100
try:
while valid and (type_read != type) and (type_read != 0):
valid, type_read, data_read = self.packet_read()
except Exception:
valid = False
data_read = []
spi_handler.spi_deselect_current()
except Exception as e:
import logging
logger = logging.getLogger(__name__)
logger.error(f"SPI communication error in packet_query: {e}")
valid = False
data_read = []
try:
spi_handler.spi_deselect_current()
except Exception:
pass
finally:
try:
spi_handler.spi_release()
except Exception:
pass
return valid, data_read
def get_id(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_ID, [])
if valid:
id_str = bytes(data[1:-1]).decode("ascii")
id_valid = id_str == self.DEVICE_ID
try:
checksum_okay = data[0] == 0
except Exception:
checksum_okay = False
else:
id_str = ""
id_valid = False
checksum_okay = False
return (valid and checksum_okay, id_str, id_valid)
def set_pressure_all(self, pressures_mbar):
data_bytes = []
for i in range(self.NUM_CONTROLLERS):
mask = 1 << i
pressure_fp = int(pressures_mbar[i] * self.PRESSURE_SCALE)
data_bytes.extend([mask] + list(pressure_fp.to_bytes(2, "little", signed=False)))
valid, data = self.packet_query(self.PACKET_TYPE_SET_PRESSURE_TARGET, data_bytes)
return valid and (data[0] == 0)
def set_pressure(self, indices, pressures_mbar):
data_bytes = []
for i in range(len(indices)):
mask = 1 << indices[i]
pressure_fp = int(pressures_mbar[i] * self.PRESSURE_SCALE)
data_bytes.extend([mask] + list(pressure_fp.to_bytes(2, "little", signed=False)))
valid, data = self.packet_query(self.PACKET_TYPE_SET_PRESSURE_TARGET, data_bytes)
return valid and (data[0] == 0)
def set_control_mode(self, indices, control_modes):
data_bytes = []
for i in range(len(indices)):
mask = 1 << indices[i]
control_mode = control_modes[i]
data_bytes.extend([mask] + list(control_mode.to_bytes(1, "little", signed=False)))
valid, data = self.packet_query(self.PACKET_TYPE_SET_CONTROL_MODE, data_bytes)
return valid and (data[0] == 0)
def get_pressure_target(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_PRESSURE_TARGET, [])
count = int((len(data) - 1) / 2)
pressures_mbar = []
for i in range(count):
index = 1 + (i << 1)
pressure_mbar = (
int.from_bytes(data[index : index + 2], byteorder="little", signed=False)
/ self.PRESSURE_SCALE
)
pressures_mbar.extend([pressure_mbar])
return (valid and (data[0] == 0), pressures_mbar)
def get_pressure_actual(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_PRESSURE_ACTUAL, [])
count = int((len(data) - 1) / 2)
pressures_mbar = []
for i in range(count):
index = 1 + (i << 1)
pressure_mbar = (
int.from_bytes(data[index : index + 2], byteorder="little", signed=True)
/ self.PRESSURE_SCALE
)
pressures_mbar.extend([pressure_mbar])
return (valid and (data[0] == 0), pressures_mbar)
def get_flow_target(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_FLOW_TARGET, [])
count = int((len(data) - 1) / 2)
flows_ul_hr = []
for i in range(count):
index = 1 + (i << 1)
flow_ul_hr = int.from_bytes(data[index : index + 2], byteorder="little", signed=False)
flows_ul_hr.extend([flow_ul_hr])
return (valid and (data[0] == 0), flows_ul_hr)
def get_flow_actual(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_FLOW_ACTUAL, [])
count = int((len(data) - 1) / 2)
flows_ul_hr = []
for i in range(count):
index = 1 + (i << 1)
flow_ul_hr = int.from_bytes(data[index : index + 2], byteorder="little", signed=True)
flows_ul_hr.extend([flow_ul_hr])
return (valid and (data[0] == 0), flows_ul_hr)
def set_flow(self, indices, flows_ul_hr):
data_bytes = []
for i in range(len(indices)):
mask = 1 << indices[i]
flow_ul_hr = int(flows_ul_hr[i])
data_bytes.extend([mask] + list(flow_ul_hr.to_bytes(2, "little", signed=False)))
valid, data = self.packet_query(self.PACKET_TYPE_SET_FLOW_TARGET, data_bytes)
return valid and (data[0] == 0)
def get_control_modes(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_CONTROL_MODE, [])
count = len(data) - 1
control_modes = []
for i in range(count):
index = 1 + i
control_mode = data[index]
control_modes.extend([control_mode])
return (valid and (data[0] == 0), control_modes)
def set_flow_pid_consts(self, indices, pid_consts):
data_bytes = []
for i in range(len(indices)):
mask = 1 << indices[i]
pid_const = pid_consts[i]
data_bytes.extend([mask] + list(pid_const[0].to_bytes(2, "little", signed=False)))
data_bytes.extend(list(pid_const[1].to_bytes(2, "little", signed=False)))
data_bytes.extend(list(pid_const[2].to_bytes(2, "little", signed=False)))
valid, data = self.packet_query(self.PACKET_TYPE_SET_FPID_CONSTS, data_bytes)
return valid and (data[0] == 0)
def get_flow_pid_consts(self):
valid, data = self.packet_query(self.PACKET_TYPE_GET_FPID_CONSTS, [])
count = int((len(data) - 1) / (3 * 2)) # 3 constants * 2 bytes each
pid_consts = []
for i in range(count):
consts = []
for j in range(3): # P, I, D
index = 1 + (2 * (3 * i + j))
const = int.from_bytes(data[index : index + 2], byteorder="little", signed=False)
consts.extend([const])
pid_consts.extend([consts])
return (valid and (data[0] == 0), pid_consts)