Fixed motor current & switch variables
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 15 Jan 2017 05:09:51 +0000 (21:09 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Sun, 15 Jan 2017 05:09:51 +0000 (21:09 -0800)
avr/Makefile
avr/src/config.h
avr/src/drv8711.c
avr/src/switch.c
avr/src/vars.c
avr/src/vars.def
avr/src/vars.json.in [new file with mode: 0644]
src/js/admin-view.js
src/py/bbctrl/Config.py
src/resources/config-template.json

index ed6910da6746147976d02c6146b04de42857475d..89e093032b20494634e1cf055f08e17c88964816 100644 (file)
@@ -55,10 +55,16 @@ BOOT_OBJ := $(patsubst src/%.S,build/%.o,$(BOOT_OBJ))
 BOOT_LDFLAGS = $(LDFLAGS) -Wl,--section-start=.text=0x030000
 
 # Build
-all: $(TARGET) $(PROJECT).hex $(PROJECT).eep $(PROJECT).lss xboot.hex \
-  boot-size size
+all: $(PROJECT).hex build/vars.json boot size
+
+boot: xboot.hex boot-size
+
+build/vars.json: src/vars.def
 
 # Compile
+build/%.json: src/%.json.in
+       cpp -Isrc $< | sed '/^#.*$$/d' > $@
+
 build/%.o: src/%.c
        @mkdir -p $(shell dirname $@)
        $(CC) $(INCLUDES) $(CFLAGS) -c -o $@ $<
index a78d2325307721859bacc0c569d1c59becc6a76d..68e41e284bef52fae166fe55f893f4013041929d 100644 (file)
@@ -92,15 +92,10 @@ enum {
 #define SPI_SS_PIN SERIAL_CTS_PIN // Needed for SPI configuration
 
 
-// Compile-time settings
-#define __CLOCK_EXTERNAL_16MHZ   // uses PLL to provide 32 MHz system clock
-//#define __CLOCK_INTERNAL_32MHZ
-
-
 #define AXES                     6 // number of axes
 #define MOTORS                   4 // number of motors on the board
 #define COORDS                   6 // number of supported coordinate systems
-#define SWITCHES                 9 // number of supported limit switches
+#define SWITCHES                10 // number of supported switches
 #define PWMS                     2 // number of supported PWM channels
 
 #define DISABLE_SOFT_LIMIT       -1000000
index 308afb963b81d920369fd6d46a17cb24d676fa36..2d2c41f31a08caacc2667c9d61731e4c247c60a0 100644 (file)
@@ -53,7 +53,8 @@ typedef struct {
 
   bool active;
   float idle_current;
-  float drive_current;
+  float max_current;
+  float min_current;
   float stall_threshold;
   float power;
 
@@ -110,14 +111,8 @@ static void _driver_check_status(int driver) {
 static float _driver_get_current(int driver) {
   drv8711_driver_t *drv = &drivers[driver];
 
-#if 1
   if (!drv->active) return drv->idle_current;
-  return
-    MOTOR_MIN_CURRENT + (drv->drive_current - MOTOR_MIN_CURRENT) * drv->power;
-
-#else
-  return drv->active ? drv->drive_current : drv->idle_current;
-#endif
+  return drv->min_current + (drv->max_current - drv->min_current) * drv->power;
 }
 
 
@@ -286,7 +281,8 @@ void drv8711_init() {
   // Configure drivers
   for (int i = 0; i < DRIVERS; i++) {
     drivers[i].idle_current = MOTOR_IDLE_CURRENT;
-    drivers[i].drive_current = MOTOR_MAX_CURRENT;
+    drivers[i].max_current = MOTOR_MAX_CURRENT;
+    drivers[i].min_current = MOTOR_MIN_CURRENT;
     drivers[i].stall_threshold = MOTOR_STALL_THRESHOLD;
 
     drv8711_disable(i);
@@ -368,31 +364,43 @@ void drv8711_set_stall_callback(int driver, stall_callback_t cb) {
 }
 
 
-float get_drive_power(int driver) {
+float get_max_current(int driver) {
+  if (driver < 0 || DRIVERS <= driver) return 0;
+  return drivers[driver].max_current;
+}
+
+
+void set_max_current(int driver, float value) {
+  if (driver < 0 || DRIVERS <= driver || value < 0 || 1 < value) return;
+  drivers[driver].max_current = value;
+}
+
+
+float get_min_current(int driver) {
   if (driver < 0 || DRIVERS <= driver) return 0;
-  return drivers[driver].drive_current;
+  return drivers[driver].min_current;
 }
 
 
-void set_drive_power(int driver, float value) {
+void set_min_current(int driver, float value) {
   if (driver < 0 || DRIVERS <= driver || value < 0 || 1 < value) return;
-  drivers[driver].drive_current = value;
+  drivers[driver].min_current = value;
 }
 
 
-float get_idle_power(int driver) {
+float get_idle_current(int driver) {
   if (driver < 0 || DRIVERS <= driver) return 0;
   return drivers[driver].idle_current;
 }
 
 
-void set_idle_power(int driver, float value) {
+void set_idle_current(int driver, float value) {
   if (driver < 0 || DRIVERS <= driver || value < 0 || 1 < value) return;
   drivers[driver].idle_current = value;
 }
 
 
-float get_current_power(int driver) {
+float get_active_current(int driver) {
   if (driver < 0 || DRIVERS <= driver) return 0;
   return _driver_get_current(driver);
 }
index eb9512b64fc6a962c8d09f7452ff6901dbc2379c..4fb4dcef7a4f138707c2bbd7fbdf8d88f1f45596 100644 (file)
@@ -69,18 +69,18 @@ typedef struct {
 } switch_t;
 
 
-
+// Order must match indices in var functions below
 static switch_t switches[SWITCHES] = {
-  {.pin = MIN_X_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MAX_X_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MIN_Y_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MAX_X_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MIN_Z_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MAX_Z_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MIN_A_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = MAX_A_PIN, .type = SW_NORMALLY_OPEN},
-  {.pin = ESTOP_PIN, .type = SW_NORMALLY_OPEN},
-  //  {.pin = PROBE_PIN, .type = SW_NORMALLY_OPEN},
+  {.pin = MIN_X_PIN, .type = SW_DISABLED},
+  {.pin = MAX_X_PIN, .type = SW_DISABLED},
+  {.pin = MIN_Y_PIN, .type = SW_DISABLED},
+  {.pin = MAX_X_PIN, .type = SW_DISABLED},
+  {.pin = MIN_Z_PIN, .type = SW_DISABLED},
+  {.pin = MAX_Z_PIN, .type = SW_DISABLED},
+  {.pin = MIN_A_PIN, .type = SW_DISABLED},
+  {.pin = MAX_A_PIN, .type = SW_DISABLED},
+  {.pin = ESTOP_PIN, .type = SW_DISABLED},
+  {.pin = PROBE_PIN, .type = SW_DISABLED},
 };
 
 
@@ -180,11 +180,12 @@ bool switch_is_enabled(int index) {
 
 
 switch_type_t switch_get_type(int index) {
-  return switches[index].type;
+  return (index < 0 || SWITCHES <= index) ? SW_DISABLED : switches[index].type;
 }
 
 
 void switch_set_type(int index, switch_type_t type) {
+  if (index < 0 || SWITCHES <= index) return;
   switch_t *s = &switches[index];
 
   if (s->type != type) {
@@ -200,5 +201,23 @@ void switch_set_callback(int index, switch_callback_t cb) {
 
 
 // Var callbacks
-uint8_t get_switch_type(int index) {return switch_get_type(index);}
-void set_switch_type(int index, uint8_t value) {switch_set_type(index, value);}
+uint8_t get_min_switch(int index) {return switch_get_type(MIN_SWITCH(index));}
+
+
+void set_min_switch(int index, uint8_t value) {
+  switch_set_type(MIN_SWITCH(index), value);
+}
+
+
+uint8_t get_max_switch(int index) {return switch_get_type(MAX_SWITCH(index));}
+
+
+void set_max_switch(int index, uint8_t value) {
+  switch_set_type(MAX_SWITCH(index), value);
+}
+
+
+uint8_t get_estop_switch() {return switch_get_type(8);}
+void set_estop_switch(uint8_t value) {switch_set_type(8, value);}
+uint8_t get_probe_switch() {return switch_get_type(9);}
+void set_probe_switch(uint8_t value) {switch_set_type(9, value);}
index 112c40e00f462933eaeaf81426e0242568386b66..8d52aa01c1fd0c567191db4f4bf9138fd40b3c14 100644 (file)
@@ -212,6 +212,13 @@ static void var_print_int32_t(uint32_t x) {
 }
 
 
+// Ensure no code is used more than once
+enum {
+#define VAR(NAME, CODE, ...) var_code_##CODE##_reuse,
+#include "vars.def"
+#undef VAR
+};
+
 // Var forward declarations
 #define VAR(NAME, CODE, TYPE, INDEX, SET, ...)          \
   TYPE get_##NAME(IF(INDEX)(int index));                \
index 5a2719136bf68c8d555eb5106ad95a6339bd632a..baa18742ee5e91dbd6811972f927d6308a5a0c84 100644 (file)
@@ -39,13 +39,14 @@ VAR(microstep,      mi, uint16_t, MOTORS, 1, 1, "Microsteps per full step")
 VAR(reverse,        rv, uint8_t,  MOTORS, 1, 1, "Reverse motor polarity")
 
 VAR(power_mode,     pm, uint8_t,  MOTORS, 1, 1, "Motor power mode")
-VAR(drive_power,    dp, float,    MOTORS, 1, 1, "Motor drive power")
-VAR(idle_power,     ip, float,    MOTORS, 1, 1, "Motor idle power")
-VAR(current_power,  cp, float,    MOTORS, 0, 0, "Motor power now")
+VAR(max_current,    xc, float,    MOTORS, 1, 1, "Max motor drive current")
+VAR(min_current,    lc, float,    MOTORS, 1, 1, "Min motor drive current")
+VAR(idle_current,   ic, float,    MOTORS, 1, 1, "Motor idle current")
+VAR(active_current, ac, float,    MOTORS, 0, 0, "Motor current now")
 VAR(status_flags,   mf, uint8_t,  MOTORS, 0, 0, "Motor status flags")
 VAR(status_strings, ms, flags_t,  MOTORS, 0, 0, "Motor status strings")
 
-VAR(motor_fault,    mf, bool,     0,      0, 0, "Motor fault status")
+VAR(motor_fault,    fa, bool,     0,      0, 0, "Motor fault status")
 
 VAR(velocity_max,   vm, float,    MOTORS, 1, 1, "Maxium velocity in mm/min")
 VAR(feedrate_max,   fr, float,    MOTORS, 1, 1, "Maxium feedrate in mm/min")
@@ -53,14 +54,20 @@ VAR(jerk_max,       jm, float,    MOTORS, 1, 1, "Maxium jerk in mm/min^3")
 VAR(junction_dev,   jd, float,    MOTORS, 1, 1, "Junction deviation")
 VAR(radius,         ra, float,    MOTORS, 1, 1, "Axis radius or zero")
 
-// Homing
-VAR(homing_mode,    hm, uint8_t,  MOTORS, 1, 1, "Homing type")
+// Switches
 VAR(travel_min,     tn, float,    MOTORS, 1, 1, "Minimum soft limit")
 VAR(travel_max,     tm, float,    MOTORS, 1, 1, "Maximum soft limit")
+VAR(min_switch,     ls, uint8_t,  MOTORS, 1, 1, "Minimum switch mode")
+VAR(max_switch,     xs, uint8_t,  MOTORS, 1, 1, "Maximum switch mode")
+VAR(estop_switch,   et, uint8_t,  0,      1, 1, "Estop switch mode")
+VAR(probe_switch,   pt, uint8_t,  0,      1, 1, "Probe switch mode")
+
+// Homing
+VAR(homing_mode,    ho, uint8_t,  MOTORS, 1, 1, "Homing type")
 VAR(search_velocity,sv, float,    MOTORS, 1, 1, "Homing search velocity")
 VAR(latch_velocity, lv, float,    MOTORS, 1, 1, "Homing latch velocity")
-VAR(latch_backoff,  lb, float,    MOTORS, 1, 1, "Homing latch backof")
-VAR(zero_backoff,   zb, float,    MOTORS, 1, 1, "Homing zero backof")
+VAR(latch_backoff,  lb, float,    MOTORS, 1, 1, "Homing latch backoff")
+VAR(zero_backoff,   zb, float,    MOTORS, 1, 1, "Homing zero backoff")
 
 // Axis
 VAR(position,        p, float,    AXES,   0, 0, "Current axis position")
@@ -90,9 +97,6 @@ VAR(huanyang_status,    hs, uint8_t,  0,  0, 0, "Huanyang status flags")
 VAR(huanyang_debug,     hb, bool,     0,  1, 0, "Huanyang debugging")
 VAR(huanyang_connected, he, bool,     0,  0, 0, "Huanyang connected")
 
-// Switches
-VAR(switch_type,    sw, uint8_t,  SWITCHES, 1, 1, "Normally open or closed")
-
 // GCode
 VAR(line,           ln, int32_t,  0,      0, 0, "Last GCode line executed")
 VAR(unit,            u, pstring,  0,      0, 0, "Current unit of measure")
diff --git a/avr/src/vars.json.in b/avr/src/vars.json.in
new file mode 100644 (file)
index 0000000..2aaf821
--- /dev/null
@@ -0,0 +1,14 @@
+#include "cpp_magic.h"
+{
+#define VAR(NAME, CODE, TYPE, INDEX, SET, SAVE, HELP) \
+  #CODE: {                                            \
+    "name": #NAME,                                    \
+    "type": #TYPE,                                    \
+    "index": IF_ELSE(INDEX)(true, false),             \
+    "setable": IF_ELSE(SET)(true, false),             \
+    "help": HELP                                      \
+  },
+#include "vars.def"
+#undef VAR
+  "_": {}
+}
index 4dca60efe985c45d4a14291978158fa1693ff29e..9de8c795ced35e8e19604001a77e5813e58c2f97 100644 (file)
@@ -67,6 +67,7 @@ module.exports = {
     reset: function () {
       this.confirmReset = false;
       api.put('config/reset').done(function () {
+        this.$dispatch('update');
         this.configReset = true;
 
       }.bind(this)).fail(function (error) {
index df3030dde3bdabe237b2237237aa2ad520f2b13d..eed411b36ce45d044103c01027f9e2e86a9df80a 100644 (file)
@@ -1,3 +1,4 @@
+import os
 import json
 import logging
 import pkg_resources
@@ -57,6 +58,9 @@ class Config(object):
         log.info('Saved')
 
 
+    def reset(self): os.unlink('config.json')
+
+
     def encode_cmd(self, index, value, spec):
         if spec['type'] == 'enum': value = spec['values'].index(value)
         elif spec['type'] == 'bool': value = 1 if value else 0
index 07559aefd98cf5268128bfc2a401789ba916a63e..ca2f6842bd333274b77d86a40afa0fa197b1a35d 100644 (file)
         "default": "in-cycle",
         "code": "pm"
       },
-      "min-power": {
+      "max-current": {
         "type": "percent",
         "unit": "%",
-        "default": 30,
-        "code": "pl"
+        "default": 80,
+        "code": "xc"
       },
-      "max-power": {
+      "min-current": {
         "type": "percent",
         "unit": "%",
-        "default": 80,
-        "code": "th"
+        "default": 30,
+        "code": "lc"
       },
-      "idle-power": {
+      "idle-current": {
         "type": "percent",
         "unit": "%",
         "default": 10,
-        "code": "ip"
+        "code": "ic"
       }
     },
 
       }
     },
 
-    "homing": {
-      "homing-mode": {
-        "type": "enum",
-        "values": [
-          "disabled", "stall", "min-normally-open", "min-normally-closed",
-          "max-normally-open", "max-normally-closed"],
-        "default": "disabled",
-        "code": "hm"
-      },
+    "limits": {
       "min-soft-limit": {
         "type": "float",
         "unit": "mm",
         "default": 150,
         "code": "tm"
       },
+      "min-switch": {
+        "type": "enum",
+        "values": ["disabled", "normally-open", "normally-closed"],
+        "default": "disabled",
+        "code": "ls"
+      },
+      "max-switch": {
+        "type": "enum",
+        "values": ["disabled", "normally-open", "normally-closed"],
+        "default": "disabled",
+        "code": "xs"
+      }
+    },
+
+    "homing": {
+      "homing-mode": {
+        "type": "enum",
+        "values": ["disabled", "stall", "min-switch", "max-switch"],
+        "default": "disabled",
+        "code": "ho"
+      },
       "search-velocity": {
         "type": "float",
         "min": 0,