Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
391 lines (351 loc) · 13.9 KB
/
Copy pathconfig.py
File metadata and controls
391 lines (351 loc) · 13.9 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# -*- coding: UTF-8 -*-
# Configuration of DriveboardApp
#
# NOTE!
# -----
# To change config parameters create a file named
# config.name_of_config.json in the config directory
# (use --list-configs to find it)
# and write something like this:
#
# {
# "network_port": 4411,
# "serial_port": "COM3"
# }
#
import copy
import glob
import importlib.util
import json
import os
import sys
import tempfile
import threading
# AVR toolchain constants for the Driveboard's atmega328 (build + flash)
AVR_DEVICE = "atmega328p"
AVR_PROGRAMMER = "arduino" # bootloader
AVR_BITRATE = "115200"
AVR_CLOCK = "16000000"
conf = {
"appname": "driveboardapp",
"version": "26.08.1",
"company_name": "com.nortd.labs",
"network_host": "", # '' for all nics
"network_port": 4444,
"serial_port": "", # set to '' for auto (req. firmware)
"baudrate": 57600,
"rootdir": None, # defined further down (../)
"confdir": None, # defined further down
"stordir": None, # defined further down
"hardware": None, # defined further down
"firmware": None, # defined further down
"tolerance": 0.01,
"workspace": [1220, 610, 0],
"jog_soft_limits": True,
"grid_mm": 100,
"seekrate": 6000,
"feedrate": 2000,
"intensity": 0,
"pxsize": 0.2, # size (mm) of beam for rastering
"pierce_time": 0.0, # seconds to burn in place before starting a cut
"pulse_intensity": 10,
"pulse_duration": 0.1,
"max_jobs_in_list": 20,
"usb_reset_hack": False,
"print_serial_data": False,
"raster_invert": False, # Set to True for materials which etch light on dark (eg slate, black marble)
"raster_levels": 128,
"raster_mode": "Bidirectional", # 'Forward', 'Reverse', 'Bidirectional', 'NearestNeighbor'
"raster_leadin": 10,
"fill_mode": "Bidirectional", # 'Forward', 'Reverse', 'Bidirectional', 'NearestNeighbor'
"fill_leadin": 10,
"split_closed_paths": True, # let seek optimization leave a closed contour part-done
"skip_pierce_on_resume": True, # no pierce dwell when resuming a split contour
"users": {
"laser": "laser",
},
"enable_gzip": True, # allow gzip upload of files / jobs
"force_gzip": False, # compress uploads even when the link looks fast
"home_on_startup": False,
"mill_mode": False,
"mill_max_rpm": 18000,
"alignment_host": None,
"alignment_port": 80,
"require_unit": False,
}
conf_defaults = copy.deepcopy(conf)
# Settings editable via config file only (require restart):
# - network_host, network_port: Server connection settings
# - serial_port, firmware: Hardware connection settings
# - mill_mode: Changes frontend, requires restart
# - users: Security-sensitive, excluded from API
userconfigurable = {
"workspace": "[x,y,z] dimensions of machine's work area in mm.",
"jog_soft_limits": "Hold jog moves inside the work area so they cannot run into a limit switch.",
"grid_mm": "Visual grid of UI in mm.",
"seekrate": "Seek rate in mm/min.",
"feedrate": "Default feed rate in mm/min.",
"intensity": "Default intensity 0-100%.",
"pxsize": "Default pixel size in mm for rastering and filling.",
"pierce_time": "Default seconds to burn in place before starting a cut, 0 to disable.",
"pulse_intensity": "Pulse intensity 0-100%.",
"pulse_duration": "Pulse duration in seconds.",
"max_jobs_in_list": "Jobs to keep in the history list.",
"print_serial_data": "Print all raw serial communications to the debug window.",
"raster_invert": r"If true, laser will set black pixels to 0% intensity and white pixels to 100% intensity.",
"raster_levels": "Number of raster dithering levels, from 2 for complete on/off dithering, to 128 for a smooth image",
"raster_mode": "Pathing to use when rastering: 'Forward', 'Reverse', 'Bidirectional', or 'NearestNeighbor' (reorders segments to minimize seek travel, best for sparse images).",
"raster_leadin": "Leadin for raster fills in mm. Note: rastering may fail if this is 0",
"fill_mode": "Pathing to use when engraving a fill area: 'Forward', 'Reverse', 'Bidirectional', or 'NearestNeighbor'.",
"fill_leadin": "Leadin for vector fills in mm.",
"split_closed_paths": "Allow seek optimization to burn a closed contour in separate arcs when that saves travel (eg the far sides of a row of circles on the way back). Adjacent arcs still burn as one continuous move. Disable for delicate cuts where an interrupted contour could resume misaligned.",
"skip_pierce_on_resume": "Skip the pierce dwell when a split contour resumes at a point that is already cut through. Only affects arcs created by split_closed_paths.",
"home_on_startup": "Automatically perform a homing cycle when the machine first connects.",
"mill_max_rpm": "Maximum spindle RPM.",
"require_unit": "Whether a physical unit (cm, mm, in) should be required to load SVG files.",
"force_gzip": "Always compress uploaded jobs. Compression is otherwise used only over a connection the browser reports as slow, since it costs more time than it saves on a local or lan link.",
}
### make some 'smart' default setting choices
### rootdir
# This is to be used with all relative file access.
# _MEIPASS is a special location for data files when creating
# standalone, single file python apps with pyInstaller.
# Standalone is created by calling from 'other' directory:
# python pyinstaller/pyinstaller.py --onefile app.spec
if hasattr(sys, "_MEIPASS"):
conf["rootdir"] = sys._MEIPASS
else:
# root is one up from this file
conf["rootdir"] = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "../")
)
#
###
### stordir
# This is to be used to store queue files and similar
conf["stordir"] = tempfile.mkdtemp("driveboardapp")
#
###
### confdir
# This is to be used to store the configuration file
if sys.platform == "darwin":
directory = os.path.join(
os.path.expanduser("~"),
"Library",
"Application Support",
conf["company_name"],
conf["appname"],
)
elif sys.platform == "win32":
directory = os.path.join(os.path.expandvars("%APPDATA%"), conf["company_name"], conf["appname"])
else:
directory = os.path.join(os.path.expanduser("~"), "." + conf["appname"])
if not os.path.exists(directory):
os.makedirs(directory)
conf["confdir"] = directory
#
###
### auto-check hardware
#
conf["hardware"] = "standard"
if sys.platform.startswith("linux"):
try:
if importlib.util.find_spec("RPi.GPIO") is not None:
conf["hardware"] = "raspberrypi"
except ImportError:
# os.uname() on BBB:
# ('Linux', 'lasersaur', '3.8.13-bone20',
# '#1 SMP Wed May 29 06:14:59 UTC 2013', 'armv7l')
if os.uname()[4].startswith("arm"):
conf["hardware"] = "beaglebone"
#
###
if conf["hardware"] == "standard":
if not conf["firmware"]:
conf["firmware"] = "driveboardusb"
elif conf["hardware"] == "beaglebone":
if not conf["firmware"]:
conf["firmware"] = "driveboard1403"
conf["serial_port"] = "/dev/ttyO1"
# if running as root
if os.geteuid() == 0:
conf["network_port"] = 80
# Beaglebone white specific
if os.path.exists("/sys/kernel/debug/omap_mux/uart1_txd"):
# we are not on the beaglebone black, setup uart1
# echo 0 > /sys/kernel/debug/omap_mux/uart1_txd
with open("/sys/kernel/debug/omap_mux/uart1_txd", "w") as fw:
fw.write(f"{0:X}")
# echo 20 > /sys/kernel/debug/omap_mux/uart1_rxd
with open("/sys/kernel/debug/omap_mux/uart1_rxd", "w") as fw:
fw.write(f"{(1 << 5) | 0:X}")
### if running on BBB/Ubuntu 14.04, setup pin muxing UART1
pin24list = glob.glob("/sys/devices/ocp.*/P9_24_pinmux.*/state")
for pin24 in pin24list:
os.system(f"echo uart > {pin24}")
pin26list = glob.glob("/sys/devices/ocp.*/P9_26_pinmux.*/state")
for pin26 in pin26list:
os.system(f"echo uart > {pin26}")
### Set up atmega328 reset control
# The reset pin is connected to GPIO2_7 (2*32+7 = 71).
# Setting it to low triggers a reset.
# echo 71 > /sys/class/gpio/export
### if running on BBB/Ubuntu 14.04, setup pin muxing GPIO2_7 (pin 46)
pin46list = glob.glob("/sys/devices/ocp.*/P8_46_pinmux.*/state")
for pin46 in pin46list:
os.system(f"echo gpio > {pin46}")
try:
with open("/sys/class/gpio/export", "w") as fw:
fw.write(f"{71:d}")
except OSError:
# probably already exported
pass
# set the gpio pin to output
# echo out > /sys/class/gpio/gpio71/direction
with open("/sys/class/gpio/gpio71/direction", "w") as fw:
fw.write("out")
# set the gpio pin high
# echo 1 > /sys/class/gpio/gpio71/value
with open("/sys/class/gpio/gpio71/value", "w") as fw:
fw.write("1")
fw.flush()
### Set up atmega328 reset control - BeagleBone Black
# The reset pin is connected to GPIO2_9 (2*32+9 = 73).
# Setting it to low triggers a reset.
# echo 73 > /sys/class/gpio/export
### if running on BBB/Ubuntu 14.04, setup pin muxing GPIO2_9 (pin 44)
pin44list = glob.glob("/sys/devices/ocp.*/P8_44_pinmux.*/state")
for pin44 in pin44list:
os.system(f"echo gpio > {pin44}")
try:
with open("/sys/class/gpio/export", "w") as fw:
fw.write(f"{73:d}")
except OSError:
# probably already exported
pass
# set the gpio pin to output
# echo out > /sys/class/gpio/gpio73/direction
with open("/sys/class/gpio/gpio73/direction", "w") as fw:
fw.write("out")
# set the gpio pin high
# echo 1 > /sys/class/gpio/gpio73/value
with open("/sys/class/gpio/gpio73/value", "w") as fw:
fw.write("1")
fw.flush()
### read stepper driver configure pin GPIO2_12 (2*32+12 = 76).
# Low means Geckos, high means SMC11s
### if running on BBB/Ubuntu 14.04, setup pin muxing GPIO2_12 (pin 39)
pin39list = glob.glob("/sys/devices/ocp.*/P8_39_pinmux.*/state")
for pin39 in pin39list:
os.system(f"echo gpio > {pin39}")
try:
with open("/sys/class/gpio/export", "w") as fw:
fw.write(f"{76:d}")
except OSError:
# probably already exported
pass
# set the gpio pin to input
with open("/sys/class/gpio/gpio76/direction", "w") as fw:
fw.write("in")
# set the gpio pin high
with open("/sys/class/gpio/gpio76/value") as fw:
ret = fw.read()
# print "Stepper driver configure pin is: " + str(ret)
elif conf["hardware"] == "raspberrypi":
if not conf["firmware"]:
conf["firmware"] = "driveboard1403"
conf["serial_port"] = "/dev/ttyAMA0"
# if running as root
if os.geteuid() == 0:
conf["network_port"] = 80
import RPi.GPIO as GPIO
# GPIO.setwarnings(False) # surpress warnings
GPIO.setmode(GPIO.BCM) # use chip pin number
pinSense = 7
pinReset = 2
pinExt1 = 3
pinExt2 = 4
pinExt3 = 17
pinTX = 14
pinRX = 15
# read sens pin
GPIO.setup(pinSense, GPIO.IN)
isSMC11 = GPIO.input(pinSense)
# atmega reset pin
GPIO.setup(pinReset, GPIO.OUT)
GPIO.output(pinReset, GPIO.HIGH)
# no need to setup the serial pins
# although /boot/cmdline.txt and /etc/inittab needs
# to be edited to deactivate the serial terminal login
# (basically anything related to ttyAMA0)
configpath = ""
_config_lock = threading.RLock()
def write_json_atomic(path, value, **dump_kwargs):
"""Durably replace a JSON file without exposing a partial write."""
directory = os.path.dirname(os.path.abspath(path))
fd, tmppath = tempfile.mkstemp(prefix=".config-", suffix=".tmp", dir=directory)
try:
with os.fdopen(fd, "w") as fp:
json.dump(value, fp, **dump_kwargs)
fp.flush()
os.fsync(fp.fileno())
os.replace(tmppath, path)
except Exception:
try:
os.unlink(tmppath)
except FileNotFoundError:
pass
raise
def load(configname):
if configname:
path = os.path.join(conf["confdir"], "config." + configname + ".json")
else:
path = os.path.join(conf["confdir"], "config.json")
global configpath
configpath = path
# load
if os.path.exists(path):
# print "CONFIG: " + path
# apply user config
with open(path) as fp:
try:
userconf = json.load(fp)
for k in list(userconfigurable.keys()):
if k in userconf:
conf[k] = userconf[k]
except ValueError:
print("ERROR: failed to read config file")
else:
if not configname:
# special case: default config not present, create
print("INFO: creating default config file")
with open(path, "w") as fp:
confout = {k: v for k, v in list(conf.items()) if k in userconfigurable}
json.dump(confout, fp, indent=4)
else:
print("ERROR: invalid config specified")
sys.exit()
def write_config_fields(subconfigdict):
# Keep the read/modify/replace transaction together so simultaneous API
# updates cannot silently discard one another.
with _config_lock:
# Make the in-memory and persisted views one linearizable update. API
# callers used to mutate conf before taking the file lock, allowing two
# writes of the same field to leave memory and disk disagreeing.
conf.update(subconfigdict)
if os.path.exists(configpath):
with open(configpath) as fp:
conftemp = json.load(fp)
else:
conftemp = {}
conftemp.update(subconfigdict)
write_json_atomic(configpath, conftemp, indent=4)
def list_configs():
print("Config files in " + conf["confdir"] + ":")
tempdir = os.getcwd()
os.chdir(conf["confdir"])
cfiles = glob.glob("config.*.json")
for cfile in cfiles:
confname = cfile.split(".")[1]
print(f"{confname} - ({cfile})")
os.chdir(tempdir)