-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
180 lines (148 loc) · 5.05 KB
/
main.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
import config
import gc
import json
import utime
import neopixel
from machine import Pin, I2C
from ina219 import INA219
from steppers import Stepper, Axis
from logging import ERROR
from letters import characters
from microWebSrv import MicroWebSrv
# lock
lock1 = False
# ina initialization
i2c = I2C(-1, Pin(config.device['ina_scl']), Pin(config.device['ina_sda']))
ina = INA219(config.device['shunt_ohms'], i2c, log_level=ERROR)
ina.configure()
# leds
np = neopixel.NeoPixel(machine.Pin(27), 25)
# steppers initialization
m1 = Stepper(0, Pin(config.device['m1_dir']),
Pin(config.device['m1_step']),
Pin(config.device['m1_enable']),
config.device['pwm_freq'])
m0 = Stepper(1, Pin(config.device['m2_dir']),
Pin(config.device['m2_step']),
Pin(config.device['m2_enable']),
config.device['pwm_freq'])
# axis initialization
aperture = Axis(m0, ina,
config.device['max_ma_aperture'], config.device['margin'])
focus = Axis(m1, ina,
config.device['max_ma_focus'], config.device['margin'])
def write_2leds(letter, color):
rgb = color
char_matrix = characters.get(letter)
led_counter = 0
for row in char_matrix:
for led in row:
if(led):
np[led_counter] = rgb
else:
np[led_counter] = (0, 0, 0)
led_counter += 1
np.write()
# axis calibration
write_2leds(".", (3, 0, 0))
current = ina.current()
write_2leds(".", (0, 0, 5))
aperture.calibration()
utime.sleep_ms(1000)
write_2leds(".", (0, 3, 0))
focus.calibration()
write_2leds(" ", (0, 0, 0))
# webserver functions
def _httpHandlerMemory(httpClient, httpResponse, routeArgs):
print("In Memory HTTP variable route :")
query = str(routeArgs['query'])
if 'gc' in query or 'collect' in query:
gc.collect()
content = """\
{}
""".format(gc.mem_free())
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=content)
def _httpHandlerGetStatus(httpClient, httpResponse, routeArgs):
global focus, aperture
mtype = routeArgs['mtype']
if 'focus' in mtype:
max_steps = focus.max_steps
calibrated = focus.calibrated
actual_position = focus.actual_position
elif 'aperture' in mtype:
max_steps = aperture.max_steps
calibrated = aperture.calibrated
actual_position = aperture.actual_position
data = {
'mtype': mtype,
'max_steps': max_steps,
'calibrated': calibrated,
'position': actual_position
}
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=json.dumps(data))
gc.collect()
def _httpHandlerSetCalibration(httpClient, httpResponse, routeArgs):
global focus, aperture
mtype = routeArgs['mtype']
if 'focus' in mtype:
max_steps = focus.calibration()
position = focus.actual_position
calibrated = focus.calibrated
elif 'aperture' in mtype:
max_steps = aperture.calibration()
position = aperture.actual_position
calibrated = aperture.calibrated
data = {
'mtype': mtype,
'max_steps': max_steps,
'calibrated': calibrated,
'position': position
}
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=json.dumps(data))
gc.collect()
def _httpHandlerSetMove(httpClient, httpResponse, routeArgs):
global focus, aperture, lock1
mtype = routeArgs['mtype']
steps = int(routeArgs['steps'])
clockwise = -1 if int(routeArgs['clockwise']) == 0 else 1
status = 0
position = 0
if 'focus' in mtype:
write_2leds(".", (0, 3, 0))
status = focus.move(clockwise * steps, 1)
position = focus.actual_position
elif 'aperture' in mtype:
write_2leds(".", (0, 0, 5))
status = aperture.move(clockwise * steps, 1)
position = aperture.actual_position
write_2leds(" ", (0, 0, 0))
data = {
'mtype': mtype,
'steps': steps,
'status': status,
'clockwise': clockwise,
'position': position
}
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=json.dumps(data))
gc.collect()
routeHandlers = [
("/memory/<query>", "GET", _httpHandlerMemory),
("/status/<mtype>", "GET", _httpHandlerGetStatus),
("/calibration/<mtype>", "GET", _httpHandlerSetCalibration),
("/move/<mtype>/<steps>/<clockwise>", "GET", _httpHandlerSetMove)
]
mws = MicroWebSrv(routeHandlers=routeHandlers, webPath="www/")
mws.Start(threaded=True)
gc.collect()