From 042b71c03068933b21ec9f42098a0d2b3fce54b8 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Sat, 14 Jan 2017 21:09:51 -0800 Subject: [PATCH] Fixed motor current & switch variables --- avr/Makefile | 10 +++++-- avr/src/config.h | 7 +---- avr/src/drv8711.c | 40 +++++++++++++++---------- avr/src/switch.c | 47 +++++++++++++++++++++--------- avr/src/vars.c | 7 +++++ avr/src/vars.def | 26 ++++++++++------- avr/src/vars.json.in | 14 +++++++++ src/js/admin-view.js | 1 + src/py/bbctrl/Config.py | 4 +++ src/resources/config-template.json | 47 +++++++++++++++++++----------- 10 files changed, 137 insertions(+), 66 deletions(-) create mode 100644 avr/src/vars.json.in diff --git a/avr/Makefile b/avr/Makefile index ed6910d..89e0930 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -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 $@ $< diff --git a/avr/src/config.h b/avr/src/config.h index a78d232..68e41e2 100644 --- a/avr/src/config.h +++ b/avr/src/config.h @@ -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 diff --git a/avr/src/drv8711.c b/avr/src/drv8711.c index 308afb9..2d2c41f 100644 --- a/avr/src/drv8711.c +++ b/avr/src/drv8711.c @@ -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); } diff --git a/avr/src/switch.c b/avr/src/switch.c index eb9512b..4fb4dce 100644 --- a/avr/src/switch.c +++ b/avr/src/switch.c @@ -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);} diff --git a/avr/src/vars.c b/avr/src/vars.c index 112c40e..8d52aa0 100644 --- a/avr/src/vars.c +++ b/avr/src/vars.c @@ -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)); \ diff --git a/avr/src/vars.def b/avr/src/vars.def index 5a27191..baa1874 100644 --- a/avr/src/vars.def +++ b/avr/src/vars.def @@ -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 index 0000000..2aaf821 --- /dev/null +++ b/avr/src/vars.json.in @@ -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 + "_": {} +} diff --git a/src/js/admin-view.js b/src/js/admin-view.js index 4dca60e..9de8c79 100644 --- a/src/js/admin-view.js +++ b/src/js/admin-view.js @@ -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) { diff --git a/src/py/bbctrl/Config.py b/src/py/bbctrl/Config.py index df3030d..eed411b 100644 --- a/src/py/bbctrl/Config.py +++ b/src/py/bbctrl/Config.py @@ -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 diff --git a/src/resources/config-template.json b/src/resources/config-template.json index 07559ae..ca2f684 100644 --- a/src/resources/config-template.json +++ b/src/resources/config-template.json @@ -16,23 +16,23 @@ "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" } }, @@ -87,15 +87,7 @@ } }, - "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", @@ -108,6 +100,27 @@ "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, -- 2.27.0