diff --git a/Makefile b/Makefile index 74f3369..0a7dbd4 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ INCLUDES = \ # Hack: .c files are compiled as if they were c++. SRC_DIRS = \ grbl \ + smoother \ # Compile all .c files in these directories, except ones on the exclude list. # These files get extra -Wno-* flags to reduce spam. @@ -145,7 +146,7 @@ build/src/%.o : %.cpp default: build/$(PROJECT).hex build/$(PROJECT).bin build/$(PROJECT).elf: $(SRC_OBJECTS) $(CMSIS_OBJECTS) $(LINKER_SCRIPT) - $(LD) -mcpu=cortex-m3 -mthumb -T$(filter %.ld, $^) -o $@ $(filter %.o, $^) $(LIBS) + $(LD) -mcpu=cortex-m3 -mthumb -specs=nosys.specs -T$(filter %.ld, $^) -o $@ $(filter %.o, $^) $(LIBS) build/$(PROJECT).bin : build/$(PROJECT).elf $(OBJCOPY) -O binary $^ $@ @@ -161,4 +162,4 @@ flash: build/$(PROJECT).hex clean: $(call RM,build) --include $(wildcard build/*.d build/cmsis/*.d) +-include $(wildcard build/src/*.d build/cmsis/*.d) diff --git a/grbl/config.h b/grbl/config.h index 8d2cf83..0510b36 100644 --- a/grbl/config.h +++ b/grbl/config.h @@ -696,22 +696,31 @@ #define SPINDLE_PWM_PORT PORTB #define SPINDLE_PWM_BIT 3 // Uno Digital Pin 11 +#define CURRENT_I2C Driver_I2C1 // I2C driver for current control. Comment out to disable. +#define CURRENT_MCP44XX_ADDR 0b0101100 // Address of MCP44XX +#define CURRENT_WIPERS {0, 1, 6, 7}; // Wiper registers (X, Y, Z, A) +#define CURRENT_FACTOR 113.33 // Convert amps to digipot value + // Paste default settings definitions here. -#define DEFAULT_X_STEPS_PER_MM 250.0 -#define DEFAULT_Y_STEPS_PER_MM 250.0 -#define DEFAULT_Z_STEPS_PER_MM 250.0 -#define DEFAULT_X_MAX_RATE 500.0 // mm/min -#define DEFAULT_Y_MAX_RATE 500.0 // mm/min +#define DEFAULT_X_STEPS_PER_MM 158.0 +#define DEFAULT_Y_STEPS_PER_MM 158.0 +#define DEFAULT_Z_STEPS_PER_MM 158.0 +#define DEFAULT_X_MAX_RATE 30000 // mm/min +#define DEFAULT_Y_MAX_RATE 30000 // mm/min #define DEFAULT_Z_MAX_RATE 500.0 // mm/min -#define DEFAULT_X_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 -#define DEFAULT_Y_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 -#define DEFAULT_Z_ACCELERATION (10.0*60*60) // 10*60*60 mm/min^2 = 10 mm/sec^2 +#define DEFAULT_X_ACCELERATION (5000.0*60*60) // 5000*60*60 mm/min^2 = 5000 mm/sec^2 +#define DEFAULT_Y_ACCELERATION (5000.0*60*60) // 5000*60*60 mm/min^2 = 5000 mm/sec^2 +#define DEFAULT_Z_ACCELERATION (5000.0*60*60) // 5000*60*60 mm/min^2 = 5000 mm/sec^2 +#define DEFAULT_X_CURRENT 0.4 // amps +#define DEFAULT_Y_CURRENT 1.5 // amps +#define DEFAULT_Z_CURRENT 0.0 // amps +#define DEFAULT_A_CURRENT 0.0 // amps #define DEFAULT_X_MAX_TRAVEL 200.0 // mm #define DEFAULT_Y_MAX_TRAVEL 200.0 // mm #define DEFAULT_Z_MAX_TRAVEL 200.0 // mm #define DEFAULT_SPINDLE_RPM_MAX 1000.0 // rpm #define DEFAULT_SPINDLE_RPM_MIN 0.0 // rpm -#define DEFAULT_STEP_PULSE_MICROSECONDS 10 +#define DEFAULT_STEP_PULSE_MICROSECONDS 1 #define DEFAULT_STEPPING_INVERT_MASK 0 #define DEFAULT_DIRECTION_INVERT_MASK 0 #define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled) diff --git a/grbl/main.c b/grbl/main.c index e703440..26d5c50 100644 --- a/grbl/main.c +++ b/grbl/main.c @@ -20,6 +20,7 @@ */ #include "grbl.h" +#include "current_control.h" /*Holding space for dummy registers*/ DummyReg DDRA; @@ -89,6 +90,7 @@ int main(void) // Initialize system upon power-up. serial_init(); // Setup serial baud rate and interrupts settings_init(); // Load Grbl settings from EEPROM + current_init(); // Configure stepper driver current stepper_init(); // Configure stepper pins and interrupt timers system_init(); // Configure pinout pins and pin-change interrupt diff --git a/lpc17xx/RTE_Device.h b/lpc17xx/RTE_Device.h index f3591a1..c1fa81d 100644 --- a/lpc17xx/RTE_Device.h +++ b/lpc17xx/RTE_Device.h @@ -249,7 +249,7 @@ // I2C1 (Inter-integrated Circuit Interface 1) [Driver_I2C1] // Configuration settings for Driver_I2C1 in component ::Drivers:I2C -#define RTE_I2C1 0 +#define RTE_I2C1 1 // I2C1_SCL Pin <0=>P0_1 <1=>P0_20 #define RTE_I2C1_SCL_PORT_ID 0 diff --git a/smoother/current_control.cpp b/smoother/current_control.cpp new file mode 100644 index 0000000..ac4926c --- /dev/null +++ b/smoother/current_control.cpp @@ -0,0 +1,63 @@ +// Current control +// +// Copyright 2017 Todd Fleming +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "current_control.h" +#include "grbl.h" +#include "Driver_I2C.h" + +#ifdef CURRENT_I2C +extern ARM_DRIVER_I2C CURRENT_I2C; +static const uint8_t wiperRegs[] = CURRENT_WIPERS; +#endif + +void current_init() +{ +#ifdef CURRENT_I2C + CURRENT_I2C.Initialize(nullptr); + CURRENT_I2C.PowerControl(ARM_POWER_FULL); + CURRENT_I2C.Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_STANDARD); + CURRENT_I2C.Control(ARM_I2C_BUS_CLEAR, 0); + + uint8_t init1[] = {0x40, 0xff}; + uint8_t init2[] = {0xA0, 0xff}; + CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init1, 2, false); + CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, init2, 2, false); + + set_current(0, DEFAULT_X_CURRENT); + set_current(1, DEFAULT_Y_CURRENT); + set_current(2, DEFAULT_Z_CURRENT); + set_current(3, DEFAULT_A_CURRENT); +#endif +} + +void set_current(uint8_t motor, float amps) +{ +#if defined(CURRENT_I2C) && defined(CURRENT_MCP44XX_ADDR) + uint8_t command[] = { + uint8_t(wiperRegs[motor] << 4), + uint8_t(std::round(amps * CURRENT_FACTOR)), + }; + CURRENT_I2C.MasterTransmit(CURRENT_MCP44XX_ADDR, command, sizeof(command), false); + while (CURRENT_I2C.GetStatus().busy) + ; +#endif +} diff --git a/smoother/current_control.h b/smoother/current_control.h new file mode 100644 index 0000000..15f5115 --- /dev/null +++ b/smoother/current_control.h @@ -0,0 +1,26 @@ +// Current control +// +// Copyright 2017 Todd Fleming +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include + +void current_init(); +void set_current(uint8_t motor, float amps);