Fix homing switch to motor channel mapping with non-standard axis order, Added switch...
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 11 Mar 2019 21:27:12 +0000 (14:27 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 11 Mar 2019 21:27:12 +0000 (14:27 -0700)
CHANGELOG.md
package.json
src/avr/src/config.h
src/avr/src/switch.c
src/avr/src/vars.def
src/pug/templates/io-view.pug
src/py/bbctrl/Cmd.py
src/py/bbctrl/Planner.py
src/py/bbctrl/State.py
src/resources/config-template.json

index 1d395cac5a3d6e33dd97f76c96c0cb178b9c426d..254e571f66eb217619fd240fd28f97f542c02c8b 100644 (file)
@@ -1,6 +1,10 @@
 Buildbotics CNC Controller Firmware Changelog
 =============================================
 
+## v0.4.7
+ - Fix homing switch to motor channel mapping with non-standard axis order.
+ - Added ``switch-debounce`` and ``switch-lockout`` config options.
+
 ## v0.4.6
  - Fixed a rare ``Negative s-curve time`` error.
  - Don't allow manual axis homing when soft limits are not set.
index 1e0e46260fef799082d0bae8ba8edb6213da8e30..e936b177d887406c246cf071da78f930170cab3e 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "bbctrl",
-  "version": "0.4.6",
+  "version": "0.4.7",
   "homepage": "http://buildbotics.com/",
   "repository": "https://github.com/buildbotics/bbctrl-firmware",
   "license": "GPL-3.0+",
index 791996e79e9e64371a2d6d4fd9b52fade0400305..9b4d5f63925153cc2a984fc618f154598787a6da 100644 (file)
@@ -99,8 +99,10 @@ enum {
 #define VFDREG                  32 // number of supported VFD modbus registers
 
 // Switch settings.  See switch.c
-#define SWITCH_DEBOUNCE          5 // ms
-#define SWITCH_LOCKOUT         250 // ms
+#define SWITCH_DEBOUNCE          5 // ms, default value
+#define SWITCH_LOCKOUT         250 // ms, default value
+#define SWITCH_MAX_DEBOUNCE   5000 // ms
+#define SWITCH_MAX_LOCKOUT   60000 // ms
 
 
 // Motor ISRs
index 01233bcfc277e27f20e7753010d66b2df270e0d2..f6cb5a9888bed3f7d15eeaca965bec6cf323a455 100644 (file)
 #include <stdio.h>
 
 
+static struct {
+  int16_t debounce;
+  int16_t lockout;
+} sw = {
+  .debounce = SWITCH_DEBOUNCE,
+  .lockout = SWITCH_LOCKOUT,
+};
+
 typedef struct {
   uint8_t pin;
   switch_type_t type;
@@ -87,12 +95,12 @@ void switch_rtc_callback() {
     // Debounce switch
     bool state = IN_PIN(s->pin);
     if (state == s->state && s->initialized) s->debounce = 0;
-    else if ((state && ++s->debounce == SWITCH_DEBOUNCE) ||
-             (!state && --s->debounce == -SWITCH_DEBOUNCE)) {
+    else if ((state && ++s->debounce == sw.debounce) ||
+             (!state && --s->debounce == -sw.debounce)) {
       s->state = state;
       s->debounce = 0;
       s->initialized = true;
-      s->lockout = SWITCH_LOCKOUT;
+      s->lockout = sw.lockout;
       if (s->cb) s->cb((switch_id_t)i, switch_is_active((switch_id_t)i));
     }
   }
@@ -184,3 +192,19 @@ uint8_t get_min_switch(int index) {return _get_state(MIN_SWITCH(index));}
 uint8_t get_max_switch(int index) {return _get_state(MAX_SWITCH(index));}
 uint8_t get_estop_switch() {return _get_state(SW_ESTOP);}
 uint8_t get_probe_switch() {return _get_state(SW_PROBE);}
+
+
+void set_switch_debounce(uint16_t debounce) {
+  sw.debounce = SWITCH_MAX_DEBOUNCE < debounce ? SWITCH_DEBOUNCE : debounce;
+}
+
+
+uint16_t get_switch_debounce() {return sw.debounce;}
+
+
+void set_switch_lockout(uint16_t lockout) {
+  sw.lockout = SWITCH_MAX_LOCKOUT < lockout ? SWITCH_LOCKOUT : lockout;
+}
+
+
+uint16_t get_switch_lockout() {return sw.lockout;}
index 191d6bd227a3d0d44fccac09d48df3f3921cf7e1..08318964c04a95e74bb289a87ae2abaa84d8158d 100644 (file)
@@ -70,6 +70,8 @@ VAR(min_switch,      lw, u8,    MOTORS, 0, 1) // Minimum switch state
 VAR(max_switch,      xw, u8,    MOTORS, 0, 1) // Maximum switch state
 VAR(estop_switch,    ew, u8,    0,      0, 1) // Estop switch state
 VAR(probe_switch,    pw, u8,    0,      0, 1) // Probe switch state
+VAR(switch_debounce, sd, u16,   0,      1, 1) // Switch debounce time in ms
+VAR(switch_lockout,  sc, u16,   0,      1, 1) // Switch lockout time in ms
 
 // Axis
 VAR(axis_position,    p, f32,   AXES,   0, 1) // Axis position
index 6f28a8c995a960864ff72f15396f9a9579c6301e..ffb3800acf160c9bdf85a51810bb90c404916fe7 100644 (file)
@@ -35,7 +35,7 @@ script#io-view-template(type="text/x-template")
         templated-input(v-for="templ in template.switches", :name="$key",
           :model.sync="config.switches[$key]", :template="templ")
 
-          label.extra(slot="extra")
+          label.extra(slot="extra", v-if="templ.pin")
             | Pin {{templ.pin}}
             io-indicator(:name="$key", :state="state")
 
index 3b5c1af8ecb3346662bd505e313ea7c2f908637d..1edcad819f7e07c4ea3b342983788f0806ae23c7 100644 (file)
@@ -171,18 +171,7 @@ def jog(axes): return JOG + encode_axes(axes)
 
 
 def seek(switch, active, error):
-    cmd = SEEK
-
-    if switch == 'probe': cmd += '1'
-    elif switch == 'x-min': cmd += '2'
-    elif switch == 'x-max': cmd += '3'
-    elif switch == 'y-min': cmd += '4'
-    elif switch == 'y-max': cmd += '5'
-    elif switch == 'z-min': cmd += '6'
-    elif switch == 'z-max': cmd += '7'
-    elif switch == 'a-min': cmd += '8'
-    elif switch == 'a-max': cmd += '9'
-    else: raise Exception('Unsupported switch "%s"' % switch)
+    cmd = SEEK + str(switch)
 
     flags = 0
     if active: flags |= SEEK_ACTIVE
index 0cd7082d3eb25223f308cb131cfc1e3aaab6f9e8..48416cc99e6a263c4212725d3193fc44c7b90b1f 100644 (file)
@@ -143,11 +143,11 @@ class Planner():
         m = reLogLine.match(line)
         if not m: return
 
-        level = m.group('level')
-        msg = m.group('msg')
+        level    = m.group('level')
+        msg      = m.group('msg')
         filename = m.group('file')
-        line = m.group('line')
-        column = m.group('column')
+        line     = m.group('line')
+        column   = m.group('column')
 
         where = ':'.join(filter(None.__ne__, [filename, line, column]))
 
@@ -263,7 +263,8 @@ class Planner():
         if type == 'pause': return Cmd.pause(block['pause-type'])
 
         if type == 'seek':
-            return Cmd.seek(block['switch'], block['active'], block['error'])
+            sw = self.ctrl.state.get_switch_id(block['switch'])
+            return Cmd.seek(sw, block['active'], block['error'])
 
         if type == 'end': return '' # Sends id
 
index 12903b8e33c64178cb5d54ef637d628c55a2d473..484ba8296dbef7e01858889a8b95b5098a0384e3 100644 (file)
@@ -392,3 +392,27 @@ class State(object):
 
     def motor_latch_velocity(self, motor):
         return 1000 * self.get(str(motor) + 'lv', 0)
+
+
+    def get_axis_switch(self, axis, side):
+        axis = axis.lower()
+
+        if not axis in 'xyzabc':
+            raise Exception('Unsupported switch "%s-%s"' % (axis, side))
+
+        if not self.is_axis_enabled(axis):
+            raise Exception('Switch "%s-%s" axis not enabled' % (axis, side))
+
+        motor = self.find_motor(axis)
+        # This must match the switch ID enum in avr/src/switch.h
+        return 2 * motor + 2 + (0 if side.lower() == 'min' else 1)
+
+
+    def get_switch_id(self, switch):
+        # TODO Support other input switches in CAMotics gcode/machine/PortType.h
+        # TODO Support stall homing
+        switch = switch.lower()
+        if switch == 'probe': return 1
+        if switch[1:] == '-min': return self.get_axis_switch(switch[0], 'min')
+        if switch[1:] == '-max': return self.get_axis_switch(switch[0], 'max')
+        raise Exception('Unsupported switch "%s"' % switch)
index 766fa46543152a7d97536a596d8a3ee457b447e2..47176bac1cea6ac2933623a77de5b51b5435203d 100644 (file)
       "default": "normally-open",
       "code": "pt",
       "pin": 22
+    },
+    "switch-debounce": {
+      "type": "int",
+      "min": 1,
+      "max": 5000,
+      "unit": "ms",
+      "default": 5,
+      "code": "sd",
+      "help": "Minimum time in ms before a switch change is acknowledged."
+    },
+    "switch-lockout": {
+      "type": "int",
+      "min": 0,
+      "max": 60000,
+      "unit": "ms",
+      "default": 250,
+      "code": "sc",
+      "help": "Time in ms to ignore switch changes after an acknowledge change."
     }
   },