-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardware.py
295 lines (180 loc) · 8.89 KB
/
hardware.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
from machine import ADC
from machine import Pin
import ujson
from stepper import Stepper
#---------------------------------------------------------
#class analogmultiplexI2Ccontrol:
class analogmultiplexGPIOcontrol:
# ============================================================================
# ===( Constants )============================================================
# ============================================================================
# ============================================================================
# ===( Class globals )=======================================================
# ============================================================================
# ============================================================================
# ===( Utils )===============================================================
# ============================================================================
# ============================================================================
# ===( Constructor )==========================================================
# ============================================================================
def __init__(self, configFile = "hardware.cfg",config = None):
#---------------------------------------------------------
if config == None:
f = open('hardware.cfg')
hardconfobj = ujson.loads(f.read())
config = hardconfobj["input"]
f.close()
#init all adc pins
self.adc = []
x = 0
for pin in config["analogInputs"]:
self.adc[x]=ADC(Pin(pin)) # create ADC object on ADC pin
self.adc[x].atten(ADC.ATTN_11DB)# set 11dB input attenuation
self.adc[x].width(ADC.WIDTH_9BIT)
x +=1
self.mplex = []
x = 0
for pin in config["multiplexerPins"]:
self.mplex[x]=Pin(pin, Pin.OUT)
self.mplex[x].off()
x +=1
# ============================================================================
# ===( Functions )============================================================
# ============================================================================
def readInput(self,number):
"""set multiplexergpios and read gpioadc"""
mod = number % 5 # =index of analogpin
multiplexarray = '{0:04b}'.format((number - mod)/5)# get the binary array of the
i=0
for x in multiplexarray:
self.mplex[i].value(int(x))
i +=1
return self.adc[mod].read()
class valvecontrol:
# ============================================================================
# ===( Constants )============================================================
# ============================================================================
_m5_steigung = 0.8
# ============================================================================
# ===( Class globals )=======================================================
# ============================================================================
# ============================================================================
# ===( Utils )===============================================================
# ============================================================================
# ============================================================================
# ===( Constructor )==========================================================
# ============================================================================
def __init__(self, configFile = "hardware.cfg",config = None):
if config == None:
f = open('hardware.cfg')
hardconfobj = ujson.loads(f.read())
self.config = hardconfobj["valve"]
f.close()
else:
self.config = config
#self.config["homeSencePin"]
#self.config["homePosToValve"] in mm
#self.config["valveToValve"] in mm
#self.config["valveAmmount"]
#self.config["maxSteps"]
# mm von ventil zu ventil * schritte pro umdrehung / gewindesteigung
self.stepsValveToValve = int(self.config["valveToValve"] * ((4075.7728395061727/2)/self._m5_steigung) )
print(self.stepsValveToValve)
self.stepshomePosToValve = int(self.config["homePosToValve"] * ((4075.7728395061727/2)/self._m5_steigung) )
self.homingIsrunning = False
self.valveReachedCB = None
self.homingCB = None
self.homepin = Pin(self.config["homeSencePin"], Pin.IN)
pin = self.config["stepperPins"]
self.valveStepper = Stepper(pin[0],pin[1],pin[2],pin[3],self.config["steps-s"],self.config["maxSteps"])
def setValveReachedCB(self,cb):
"""set callback that was calles if the actuator reached the wanted valve"""
self.valveReachedCB = cb
def setHomeFinishedCB(self,cb):
"""set callback that was calles if the actuator reached the homing sensor"""
self.homingCB = cb
def _internalHomingCallback(self,e):
"""called from interrupt(see homing()) if homingpoint is arrived """
if self.homingIsrunning == True:
self.homingIsrunning = False
self.valveStepper.stop()
self.valveStepper.resetPos()
if self.homingCB is not None:
self.homingCB()
def homing(self,speed = None):
"""starts the homing procedure"""
self.homingIsrunning = True
#
#print(self.homepin.value())
#todo: test if homepin is triggered
#if true go in postitive direction and home after
#if false home
self.homepin.irq(trigger=Pin.IRQ_RISING , handler=self._internalHomingCallback)
self.valveStepper.run(-1000000,1,speed)
def openValve(self,valve):
"""drive the actor to the right valve"""
if valve > self.config["valveAmmount"]:
return False
stepsToRun = self.stepshomePosToValve + ((valve-1) * self.stepsValveToValve) - self.valveStepper.getPos()
self.valveStepper.setMotionEndCB(self.valveReachedCB)
self.valveStepper.run(stepsToRun)
def run(self , steps , dire = 1, speed = None):
"""run a specifiv amount of steps"""
self.valveStepper.run(steps,dire,speed)
def closeAll(self):
"""run the aktor behind the homingpoint to close all valves"""
self.valveStepper.setMotionEndCB(self.valveReachedCB)
self.valveStepper.run(self.config["valveToValve"]/2)
def readHomePin(self):
"""manual read the input pin of the homing sensor (vor testing and homingstart)"""
return self.homepin.value()
def closeInHome(self):
"""the same as closeAll()"""
stepsToRun = -(self.stepshomePosToValve + self.valveStepper.getPos())
print("stepsToRun")
print(stepsToRun)
class pumpcontrol:
# ============================================================================
# ===( Constants )============================================================
# ============================================================================
# ============================================================================
# ===( Class globals )=======================================================
# ============================================================================
# ============================================================================
# ===( Utils )===============================================================
# ============================================================================
# ============================================================================
# ===( Constructor )==========================================================
# ============================================================================
def __init__(self, configFile = "hardware.cfg",config = None):
pass
#adc[].read() # read value, 0-511 across voltage range 0.0v - 3.6v
#---hardware.cfg---
#inhalt:
# config der analogeingänge:
# multiplexer signalpins 0-3
# genutze analogeingänge 0-4
"""
ADC1_CH3 >>> GPIO39
ADC1_CH6 >>> GPIO34
ADC1_CH7 >>> GPIO35
ADC1_CH4 >>> GPIO32
ADC1_CH5 >>> GPIO33
"""
"""
"""
# (16 multiplexereingänge x 5= 80 eingänge)
# ------------------------
# gemessener Spannungsbereich
# auflösung
# anzahl an verfügbaren eingängen (duch multiplexer oder i2c auch viel mehr)
# ventile:
# anzahl ventile
# stepps von homepos zu 1.ventil
# stepps von ventil zu ventil
# maximale stepps bis ende
# max geschwindigkeit
# max beschleunigung
# pumpe:
# stepps pro ml oder t/ml
# betriebsmodus(stepper oder dc motor)