Configuration fixes, configure AVR
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 15 Jan 2017 10:10:44 +0000 (02:10 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 15 Jan 2017 10:10:44 +0000 (02:10 -0800)
Makefile
scripts/check-config-vars.py
src/py/bbctrl/AVR.py
src/py/bbctrl/Config.py
src/py/bbctrl/Ctrl.py
src/resources/config-defaults.json [new file with mode: 0644]
src/resources/config-template.json

index 97805f7255e795f8d25fe313904d2be79f78a2b0..13dd22e7d66860c5bab9fdba6ee46d6b6622459f 100644 (file)
--- 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)
index 5b9c4841af5f8b1c8a954e7f0fdbedb8f98958ef..6fd567a9b5e5af63735fbf42a8be6e94c3fb6824 100755 (executable)
@@ -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']))
 
index f947c0ecc68ee0763aa16d0a0170598fc6a9ae05..dab01d3f2af8e6f0e0425488485440229a8c9fd4 100644 (file)
@@ -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()
index eed411b36ce45d044103c01027f9e2e86a9df80a..559030095ba2f9fe41bee4ce6dc45c50e9e8b127 100644 (file)
@@ -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)
index 97b186ab2299291fbbe82c305c7420874caf89ea..6fa9fbb3aeb24e478c1b7af176ab4637142a9282 100644 (file)
@@ -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 (file)
index 0000000..8593c1d
--- /dev/null
@@ -0,0 +1,18 @@
+{
+  "motors": [
+    {
+      "axis": "X"
+    },
+    {
+      "axis": "Y"
+    },
+    {
+      "axis": "Z"
+    },
+    {
+      "axis": "A"
+    },
+  ],
+
+  "spindle": {},
+}
index ca2f6842bd333274b77d86a40afa0fa197b1a35d..7a7830495103da91f24a24b080aaae72f2e668b8 100644 (file)
@@ -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": {
       "min-current": {
         "type": "percent",
         "unit": "%",
-        "default": 30,
+        "default": 20,
         "code": "lc"
       },
       "idle-current": {
         "type": "percent",
         "unit": "%",
-        "default": 10,
+        "default": 1,
         "code": "ic"
       }
     },
       "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": {
         "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": {
     "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"
       },
   "spindle": {
     "spindle-type": {
       "type": "enum",
-      "values": ["PWM", "HUNAYANG"],
-      "default": "PWM",
+      "values": ["HUNAYANG", "PWM"],
+      "default": "HUNAYANG",
       "code": "st"
     },
     "spin-reversed": {
       "min": 0,
       "default": 48000,
       "code": "sd"
+    },
+    "pwm-freq": {
+      "type": "int",
+      "unit": "Hz",
+      "min": 0,
+      "max": 65535,
+      "default": 100,
+      "code": "sf"
     }
   },