-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsand_drawing.py
269 lines (239 loc) · 8.54 KB
/
sand_drawing.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
# pylint: disable=E0401
from umqtt.simple import MQTTClient
import time
import os
import random
from utime import ticks_us, ticks_ms, sleep_ms, sleep_us, ticks_diff, time
import machine
import network
import secrets
if secrets.wifi_ssid == 'my_ssid':
import secrets_real as secrets
from constants import *
from stepper import stepper
from cnc import cnc
MQTT_ENABLED = True
WIFI_CONNECT_WAIT_MAX_S = 10
NEW_PATTERN_CHECK_INTERVAL_MS = 2000
SAND_DRAWING_TOPIC = secrets.mqtt_root+"/sand_drawing"
# These are volatile variables written to from callbacks.
g_shuffle_generator_interval_s = -1
g_last_generator_shuffle_s = 0
g_stepper_config = ''
def get_generator_file_list():
return [filename for filename in os.listdir() if filename.endswith(".pat")]
def do_pattern(msg):
global G_PATTERN
global g_shuffle_generator_interval_s
G_PATTERN = str(bytearray(msg), "utf-8")
g_shuffle_generator_interval_s = -1
def do_generator(msg):
global G_GENERATOR
global g_shuffle_generator_interval_s
G_GENERATOR = str(bytearray(msg), "utf-8")
g_shuffle_generator_interval_s = -1
def do_save_generator(msg):
msg = str(bytearray(msg), "utf-8")
filename, generator = msg.split(' ',1)
if not filename.endswith(".pat"):
return
with open(str(filename), 'wb') as f:
f.write(generator)
def do_run_generator(msg):
filename = str(bytearray(msg), "utf-8")
return do_run_generator_internal(filename)
def do_run_generator_internal(filename):
global G_GENERATOR
print("Running generator: "+filename)
print("Running generator decoded: "+filename)
if not filename in get_generator_file_list():
print("Generator not found")
return
print("Running generator from flash {}".format(filename))
with open(str(filename), 'r') as f:
G_GENERATOR = f.read()
def do_list_generators(filename):
global G_PUBLISH
gen_list = str(get_generator_file_list())
G_PUBLISH = (bytes(SAND_DRAWING_TOPIC+"/generator_list", "utf-8"), bytes(gen_list, "utf-8"))
def do_delete_generator(filename):
if not filename.endswith(".pat"):
return
os.remove(filename)
def do_shuffle_generators(msg=''):
print("Shuffling generators")
global g_shuffle_generator_interval_s
global g_last_generator_shuffle_s
g_last_generator_shuffle_s = time()
generators = get_generator_file_list()
print("Got list")
if not generators:
print("No generators found")
return
print("picking random generator")
random_generator = random.choice(generators)
print("Random generator: "+random_generator)
do_run_generator_internal(random_generator)
print("Ran generator")
if len(msg) > 0:
print("Set shuffle interval: "+str(msg))
g_shuffle_generator_interval_s = int(msg)
print("Shuffle interval: {}".format(g_shuffle_generator_interval_s))
def do_stepper_config(msg=''):
if not MICROCONTROLLER.startswith("board_v1_2"):
return
global g_stepper_config
new_config = str(bytearray(msg), "utf-8").split(',')
if len(new_config) != 4:
g_stepper_config = ''
else:
g_stepper_config = new_config
def save_pattern(id, pattern):
with open("pattern_"+str(id), 'w') as f:
f.write(pattern)
def load_pattern(id):
try:
with open("pattern_"+str(id), 'r') as f:
return f.read()
except:
return ""
# A list of tuples of (<topic>, <callback function>)
SUBSCRIPTIONS = []
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/pattern", "utf-8"), do_pattern)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/generator", "utf-8"), do_generator)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/save_generator", "utf-8"), do_save_generator)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/list_generators", "utf-8"), do_list_generators)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/delete_generator", "utf-8"), do_delete_generator)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/run_generator", "utf-8"), do_run_generator)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/shuffle_generators", "utf-8"), do_shuffle_generators)]
SUBSCRIPTIONS += [(bytes(SAND_DRAWING_TOPIC+"/stepper_config", "utf-8"), do_stepper_config)]
G_PATTERN = ""
G_GENERATOR = ""
G_PUBLISH = ('','')
def main():
global G_PATTERN
global G_GENERATOR
global G_PUBLISH
global g_shuffle_generator_interval_s
global g_last_generator_shuffle_s
global g_stepper_config
mqtt = None
generator = None
s1 = stepper(A1PINS, False, "X", ARM1_HOME_INDEX, ARM1_HOME_ANGLE)
s2 = stepper(A2PINS, False, "Y", ARM2_HOME_INDEX, ARM2_HOME_ANGLE)
my_cnc = cnc(s1, s2)
pattern = [ "G1 X1 Y1",
"G1 X1000 Y1000",
"J0 0",
]
last_pattern_check_ticks_ms = 0
print("about to loop")
# Force a home on startup
my_cnc.set_gcode("G28 X")
my_cnc.block_until_done()
my_cnc.set_gcode("G28 Y")
my_cnc.block_until_done()
# Set cartesian mode
my_cnc.set_gcode("G16 2")
my_cnc.set_pattern(pattern)
while True:
if g_shuffle_generator_interval_s > 0:
if (time() - g_last_generator_shuffle_s) > g_shuffle_generator_interval_s:
do_shuffle_generators()
if ticks_diff(ticks_ms(), last_pattern_check_ticks_ms) > NEW_PATTERN_CHECK_INTERVAL_MS:
mqtt = mqtt_check(mqtt)
last_pattern_check_ticks_ms = ticks_ms()
if G_PATTERN != "":
generator = ""
print(G_PATTERN)
pattern = G_PATTERN.split(',')
pattern = [a.strip() for a in pattern]
G_PATTERN = ""
my_cnc.set_pattern(pattern)
if G_GENERATOR != "":
if generator != G_GENERATOR:
generator = G_GENERATOR
pattern = []
my_cnc.set_generator(generator)
G_GENERATOR = ""
if G_PUBLISH != ('',''):
try:
mqtt.publish(G_PUBLISH[0], G_PUBLISH[1])
finally:
G_PUBLISH = ('','')
if g_stepper_config != '':
try:
if g_stepper_config[0] == '1':
s1.config(g_stepper_config[1:])
elif g_stepper_config[0] == '2':
s2.config(g_stepper_config[1:])
finally:
g_stepper_config = ''
my_cnc.tick()
def mqtt_check(mqtt):
if not MQTT_ENABLED:
return None
wifi_connect()
if mqtt == None:
try:
mqtt = mqtt_connect()
mqtt.check_msg()
print("Successfully connected to broker")
except Exception as e :
print("Couldn't connect to broker. Try again later: {}".format(e))
mqtt = None
else:
try:
mqtt.check_msg()
except:
mqtt = None
return mqtt
def robust_publish(broker, topic, message):
if broker == None:
return None
try:
# Skip pylint error because we're running micropython
# pylint: disable=E1101
broker.publish(topic, b'{}'.format(message))
return broker
except Exception as e:
print("Couldn't publish to broker: {}".format(e))
return None
def mqtt_connect():
print("Attempting connection to MQTT broker")
c = MQTTClient( secrets.mqtt_clientid,
secrets.mqtt_host, 1883,
secrets.mqtt_username,
secrets.mqtt_password, 0, ssl=False)
c.set_callback(mqtt_callback)
c.connect(clean_session=False)
for topic, callback in SUBSCRIPTIONS:
c.subscribe(topic)
c.check_msg()
return c
def mqtt_callback(topic, msg):
print("Got callback: {} {}".format(topic, msg))
for cb_topic, callback in SUBSCRIPTIONS:
if cb_topic == topic:
callback(msg)
return
def wifi_connect():
# Lots of potential for improvement here. We should cache the wifi object, for starters.
sta_if = network.WLAN(network.STA_IF)
if sta_if.isconnected():
return
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
sta_if.active(True)
sta_if.connect(secrets.wifi_ssid, secrets.wifi_password)
start_time = ticks_ms()
while ticks_diff(ticks_ms(), start_time) < WIFI_CONNECT_WAIT_MAX_S*1000:
if sta_if.isconnected():
break
sleep_ms(100)
else:
# We failed to connect.
# Skip pylint error because we're running micropython
# pylint: disable=E702
raise(Exception("Connection failed"))
print("Connected to wifi")