Stepper current refactor

This commit is contained in:
Scott Lahteine
2020-08-12 18:46:55 -05:00
committed by InsanityAutomation
parent 0d367432e6
commit 79ff336b2b
4 changed files with 52 additions and 55 deletions
+14 -14
View File
@@ -541,22 +541,22 @@ void GcodeSuite::M422() {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current");
// Store current motor settings, then apply reduced value
#if HAS_DIGIPOTSS
#if HAS_MOTOR_CURRENT_SPI
const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT);
const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS];
stepper.digipot_current(Z_AXIS, target_current);
stepper.set_digipot_current(Z_AXIS, target_current);
#elif HAS_MOTOR_CURRENT_PWM
const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT);
const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS];
stepper.digipot_current(1, target_current);
#elif DAC_STEPPER_CURRENT
stepper.set_digipot_current(1, target_current);
#elif HAS_MOTOR_CURRENT_DAC
const float target_current = parser.floatval('S', GANTRY_CALIBRATION_CURRENT);
const float previous_current = dac_amps(Z_AXIS, target_current);
dac_current_raw(Z_AXIS, target_current);
#elif ENABLED(HAS_I2C_DIGIPOT)
stepper_dac.set_current_value(Z_AXIS, target_current);
#elif ENABLED(HAS_MOTOR_CURRENT_I2C)
const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT);
previous_current = dac_amps(Z_AXIS);
digipot_i2c_set_current(Z_AXIS, target_current)
digipot_i2c.set_current(Z_AXIS, target_current)
#elif HAS_TRINAMIC_CONFIG
const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT);
static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS];
@@ -604,14 +604,14 @@ void GcodeSuite::M422() {
if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current");
// Reset current to original values
#if HAS_DIGIPOTSS
stepper.digipot_current(Z_AXIS, previous_current);
#if HAS_MOTOR_CURRENT_SPI
stepper.set_digipot_current(Z_AXIS, previous_current);
#elif HAS_MOTOR_CURRENT_PWM
stepper.digipot_current(1, previous_current);
#elif DAC_STEPPER_CURRENT
dac_current_raw(Z_AXIS, previous_current);
#elif ENABLED(HAS_I2C_DIGIPOT)
digipot_i2c_set_current(Z_AXIS, previous_current)
stepper.set_digipot_current(1, previous_current);
#elif HAS_MOTOR_CURRENT_DAC
stepper_dac.set_current_value(Z_AXIS, previous_current);
#elif ENABLED(HAS_MOTOR_CURRENT_I2C)
digipot_i2c.set_current(Z_AXIS, previous_current)
#elif HAS_TRINAMIC_CONFIG
#if AXIS_IS_TMC(Z)
stepperZ.rms_current(previous_current_arr[0]);
+2 -2
View File
@@ -2612,7 +2612,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
/**
* Digipot requirement
*/
#if HAS_I2C_DIGIPOT
#if HAS_MOTOR_CURRENT_I2C
#if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
#error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451."
#elif !MB(MKS_SBASE) \
@@ -2734,7 +2734,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
#endif
#if ENABLED(MECHANICAL_GANTRY_CALIBRATION)
#if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM)
#if NONE(HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM)
#error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway."
#else
#ifndef GANTRY_CALIBRATION_CURRENT
+22 -27
View File
@@ -83,10 +83,6 @@ Stepper stepper; // Singleton
#define BABYSTEPPING_EXTRA_DIR_WAIT
#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS
bool Stepper::initialized; // = false
#endif
#ifdef __AVR__
#include "speed_lookuptable.h"
#endif
@@ -110,7 +106,7 @@ Stepper stepper; // Singleton
#include "../feature/dac/dac_dac084s085.h"
#endif
#if HAS_DIGIPOTSS
#if HAS_MOTOR_CURRENT_SPI
#include <SPI.h>
#endif
@@ -142,11 +138,12 @@ Stepper stepper; // Singleton
bool Stepper::separate_multi_axis = false;
#endif
#if HAS_MOTOR_CURRENT_PWM
uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load()
#elif HAS_DIGIPOTSS
constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
uint32_t Stepper::motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load()
#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI
bool Stepper::initialized; // = false
uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
#if HAS_MOTOR_CURRENT_SPI
constexpr uint32_t Stepper::digipot_count[];
#endif
#endif
// private:
@@ -2593,7 +2590,7 @@ void Stepper::init() {
set_directions();
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
initialized = true;
digipot_init();
#endif
@@ -2933,10 +2930,10 @@ void Stepper::report_positions() {
* Software-controlled Stepper Motor Current
*/
#if HAS_DIGIPOTSS
#if HAS_MOTOR_CURRENT_SPI
// From Arduino DigitalPotControl example
void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) {
WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
SPI.transfer(address); // Send the address and value via SPI
SPI.transfer(value);
@@ -2944,7 +2941,7 @@ void Stepper::report_positions() {
//delay(10);
}
#endif // HAS_DIGIPOTSS
#endif // HAS_MOTOR_CURRENT_SPI
#if HAS_MOTOR_CURRENT_PWM
@@ -2961,7 +2958,7 @@ void Stepper::report_positions() {
#if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1)
case 2:
#endif
digipot_current(i, motor_current_setting[i]);
set_digipot_current(i, motor_current_setting[i]);
default: break;
}
}
@@ -2971,23 +2968,23 @@ void Stepper::report_positions() {
#if !MB(PRINTRBOARD_G2)
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
void Stepper::digipot_current(const uint8_t driver, const int16_t current) {
void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
motor_current_setting[driver] = current; // update motor_current_setting
if (!initialized) return;
#if HAS_DIGIPOTSS
SERIAL_ECHOLNPAIR("Digipotss current ", current);
#if HAS_MOTOR_CURRENT_SPI
//SERIAL_ECHOLNPAIR("Digipotss current ", current);
const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
digitalPotWrite(digipot_ch[driver], current);
set_digipot_value_spi(digipot_ch[driver], current);
#elif HAS_MOTOR_CURRENT_PWM
#define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
switch (driver) {
case 0:
@@ -3023,15 +3020,13 @@ void Stepper::report_positions() {
void Stepper::digipot_init() {
#if HAS_DIGIPOTSS
#if HAS_MOTOR_CURRENT_SPI
SPI.begin();
SET_OUTPUT(DIGIPOTSS_PIN);
LOOP_L_N(i, COUNT(motor_current_setting)) {
//digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
digipot_current(i, motor_current_setting[i]);
}
LOOP_L_N(i, COUNT(motor_current_setting))
set_digipot_current(i, motor_current_setting[i]);
#elif HAS_MOTOR_CURRENT_PWM
+14 -12
View File
@@ -245,16 +245,18 @@ class Stepper {
static bool separate_multi_axis;
#endif
#if HAS_MOTOR_CURRENT_PWM
#ifndef PWM_MOTOR_CURRENT
#define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI
#if HAS_MOTOR_CURRENT_PWM
#ifndef PWM_MOTOR_CURRENT
#define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
#endif
#define MOTOR_CURRENT_COUNT 3
#elif HAS_MOTOR_CURRENT_SPI
static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
#define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count)
#endif
static bool initialized;
static uint32_t motor_current_setting[3];
#elif HAS_DIGIPOTSS
static bool initialized;
static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
static uint32_t motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load()
static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
#endif
private:
@@ -466,9 +468,9 @@ class Stepper {
// Triggered position of an axis in steps
static int32_t triggered_position(const AxisEnum axis);
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
static void digitalPotWrite(const int16_t address, const int16_t value);
static void digipot_current(const uint8_t driver, const int16_t current);
#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
static void set_digipot_value_spi(const int16_t address, const int16_t value);
static void set_digipot_current(const uint8_t driver, const int16_t current);
#endif
#if HAS_MICROSTEPS
@@ -591,7 +593,7 @@ class Stepper {
static int32_t _eval_bezier_curve(const uint32_t curr_step);
#endif
#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS
#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI
static void digipot_init();
#endif