From f0f0cdd96f16b9f17db889814939d026ead3a7fe Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sun, 15 Jan 2017 02:10:44 -0800 Subject: [PATCH] Configuration fixes, configure AVR --- Makefile | 3 +++ scripts/check-config-vars.py | 3 +++ src/py/bbctrl/AVR.py | 14 +++++----- src/py/bbctrl/Config.py | 37 ++++++++++++++------------- src/py/bbctrl/Ctrl.py | 2 ++ src/resources/config-defaults.json | 18 +++++++++++++ src/resources/config-template.json | 41 ++++++++++++++++-------------- 7 files changed, 74 insertions(+), 44 deletions(-) create mode 100644 src/resources/config-defaults.json diff --git a/Makefile b/Makefile index 97805f7..13dd22e 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,9 @@ publish: pkg echo -n $(VERSION) > dist/latest.txt rsync $(RSYNC_OPTS) dist/$(PKG_NAME).tar.bz2 dist/latest.txt $(PUB_PATH)/ +install: pkg + rsync dist/$(PKG_NAME).tar.bz2 bbmc@bbctrl.local:update.tar.bz2 + mount: mkdir -p $(DEST) sshfs bbmc@bbctrl.local: $(DEST) diff --git a/scripts/check-config-vars.py b/scripts/check-config-vars.py index 5b9c484..6fd567a 100755 --- a/scripts/check-config-vars.py +++ b/scripts/check-config-vars.py @@ -17,6 +17,9 @@ def check(section): if 'type' in entry: ok = False + # TODO check that defaults are valid + # TODO check that types match + if 'code' in entry and not entry['code'] in vars: print('"%s" with code "%s" not found' % (name, entry['code'])) diff --git a/src/py/bbctrl/AVR.py b/src/py/bbctrl/AVR.py index f947c0e..dab01d3 100644 --- a/src/py/bbctrl/AVR.py +++ b/src/py/bbctrl/AVR.py @@ -61,8 +61,6 @@ class AVR(): self.i2c_bus = None log.warning('Failed to open device: %s', e) - self._i2c_connect() - def _start_sending_gcode(self, path): if self.stream is not None: @@ -78,14 +76,15 @@ class AVR(): self.stream = None - def _i2c_connect(self): + def connect(self): try: # Reset AVR communication self.stop(); - self.report() + self.ctrl.config.config_avr() - except: - self.ctrl.ioloop.call_later(1, self._i2c_connect) + except Exception as e: + log.warning('Connect failed: %s', e) + self.ctrl.ioloop.call_later(1, self.connect) def _i2c_command(self, cmd, byte = None, word = None): @@ -206,8 +205,7 @@ class AVR(): if update: if 'firmware' in msg: log.error('AVR rebooted') - self.stop(); - self.report() + self.connect() if 'x' in msg and msg['x'] == 'ESTOPPED': self._stop_sending_gcode() diff --git a/src/py/bbctrl/Config.py b/src/py/bbctrl/Config.py index eed411b..5590300 100644 --- a/src/py/bbctrl/Config.py +++ b/src/py/bbctrl/Config.py @@ -62,6 +62,8 @@ class Config(object): def encode_cmd(self, index, value, spec): + if not 'code' in spec: return + if spec['type'] == 'enum': value = spec['values'].index(value) elif spec['type'] == 'bool': value = 1 if value else 0 elif spec['type'] == 'percent': value /= 100.0 @@ -69,28 +71,29 @@ class Config(object): self.ctrl.avr.set(index, spec['code'], value) - def encode_category(self, index, config, category): + def encode_category(self, index, config, category, with_defaults): for key, spec in category.items(): - if key in config: - self.encode_cmd(index, config[key], spec) + if key in config: value = config[key] + elif with_defaults: value = spec['default'] + else: continue + + self.encode_cmd(index, value, spec) - def encode(self, index, config, tmpl): + def encode(self, index, config, tmpl, with_defaults): for category in tmpl.values(): - self.encode_category(index, config, category) + self.encode_category(index, config, category, with_defaults) + + def update(self, config, with_defaults = False): + for name, tmpl in self.template.items(): + if name == 'motors': + for index in range(len(config['motors'])): + self.encode(index, config['motors'][index], tmpl, + with_defaults) - def update(self, config): - # Motors - tmpl = self.template['motors'] - for index in range(len(config['motors'])): - self.encode(index, config['motors'][index], tmpl) + else: self.encode_category('', config.get(name, {}), tmpl, + with_defaults) - # Switches - tmpl = self.template['switches'] - for index in range(len(config['switches'])): - self.encode_category(index, config['switches'][index], tmpl) - # Spindle - tmpl = self.template['spindle'] - self.encode_category('', config['spindle'], tmpl) + def config_avr(self): self.update(self.load(), True) diff --git a/src/py/bbctrl/Ctrl.py b/src/py/bbctrl/Ctrl.py index 97b186a..6fa9fbb 100644 --- a/src/py/bbctrl/Ctrl.py +++ b/src/py/bbctrl/Ctrl.py @@ -16,3 +16,5 @@ class Ctrl(object): self.avr = bbctrl.AVR(self) self.jog = bbctrl.Jog(self) self.lcd = bbctrl.LCD(self) + + self.avr.connect() diff --git a/src/resources/config-defaults.json b/src/resources/config-defaults.json new file mode 100644 index 0000000..8593c1d --- /dev/null +++ b/src/resources/config-defaults.json @@ -0,0 +1,18 @@ +{ + "motors": [ + { + "axis": "X" + }, + { + "axis": "Y" + }, + { + "axis": "Z" + }, + { + "axis": "A" + }, + ], + + "spindle": {}, +} diff --git a/src/resources/config-template.json b/src/resources/config-template.json index ca2f684..7a78304 100644 --- a/src/resources/config-template.json +++ b/src/resources/config-template.json @@ -13,7 +13,7 @@ "power-mode": { "type": "enum", "values": ["disabled", "always-on", "in-cycle", "when-moving"], - "default": "in-cycle", + "default": "when-moving", "code": "pm" }, "max-current": { @@ -25,13 +25,13 @@ "min-current": { "type": "percent", "unit": "%", - "default": 30, + "default": 20, "code": "lc" }, "idle-current": { "type": "percent", "unit": "%", - "default": 10, + "default": 1, "code": "ic" } }, @@ -49,14 +49,14 @@ "travel-per-rev": { "type": "float", "unit": "mm", - "default": 3.175, + "default": 5, "code": "tr" }, "microsteps": { "type": "int", - "values": [1, 2, 4, 8, 16, 32, 64, 128, 256], + "values": [1, 2, 4, 8, 16, 32, 64, 128], "unit": "per full step", - "default": 16, + "default": 32, "code": "mi" }, "reverse": { @@ -68,21 +68,14 @@ "type": "float", "min": 0, "unit": "mm/min", - "default": 16000, + "default": 5000, "code": "vm" }, - "max-feedrate": { - "type": "float", - "min": 0, - "unit": "mm/min", - "default": 16000, - "code": "fr" - }, "max-jerk": { "type": "float", "min": 0, "unit": "mm/min³", - "default": 20, + "default": 10, "code": "jm" } }, @@ -97,7 +90,7 @@ "max-soft-limit": { "type": "float", "unit": "mm", - "default": 150, + "default": 0, "code": "tm" }, "min-switch": { @@ -117,7 +110,9 @@ "homing": { "homing-mode": { "type": "enum", - "values": ["disabled", "stall", "min-switch", "max-switch"], + "values": [ + "disabled", "stall-min", "stall-max", "switch-min", "switch-max" + ], "default": "disabled", "code": "ho" }, @@ -155,8 +150,8 @@ "spindle": { "spindle-type": { "type": "enum", - "values": ["PWM", "HUNAYANG"], - "default": "PWM", + "values": ["HUNAYANG", "PWM"], + "default": "HUNAYANG", "code": "st" }, "spin-reversed": { @@ -203,6 +198,14 @@ "min": 0, "default": 48000, "code": "sd" + }, + "pwm-freq": { + "type": "int", + "unit": "Hz", + "min": 0, + "max": 65535, + "default": 100, + "code": "sf" } }, -- 2.27.0