Clean up digital pots and microsteps

This commit is contained in:
Scott Lahteine 2016-09-25 06:32:58 -05:00
parent cb48292338
commit 5fff8d148b
4 changed files with 178 additions and 142 deletions

View File

@ -462,10 +462,13 @@
#define HAS_SOLENOID_1 (PIN_EXISTS(SOL1)) #define HAS_SOLENOID_1 (PIN_EXISTS(SOL1))
#define HAS_SOLENOID_2 (PIN_EXISTS(SOL2)) #define HAS_SOLENOID_2 (PIN_EXISTS(SOL2))
#define HAS_SOLENOID_3 (PIN_EXISTS(SOL3)) #define HAS_SOLENOID_3 (PIN_EXISTS(SOL3))
#define HAS_MICROSTEPS (PIN_EXISTS(X_MS1)) #define HAS_MICROSTEPS_X (PIN_EXISTS(X_MS1))
#define HAS_MICROSTEPS_Y (PIN_EXISTS(Y_MS1))
#define HAS_MICROSTEPS_Z (PIN_EXISTS(Z_MS1))
#define HAS_MICROSTEPS_E0 (PIN_EXISTS(E0_MS1)) #define HAS_MICROSTEPS_E0 (PIN_EXISTS(E0_MS1))
#define HAS_MICROSTEPS_E1 (PIN_EXISTS(E1_MS1)) #define HAS_MICROSTEPS_E1 (PIN_EXISTS(E1_MS1))
#define HAS_MICROSTEPS_E2 (PIN_EXISTS(E2_MS1)) #define HAS_MICROSTEPS_E2 (PIN_EXISTS(E2_MS1))
#define HAS_MICROSTEPS (HAS_MICROSTEPS_X || HAS_MICROSTEPS_Y || HAS_MICROSTEPS_Z || HAS_MICROSTEPS_E0 || HAS_MICROSTEPS_E1 || HAS_MICROSTEPS_E2)
#define HAS_STEPPER_RESET (PIN_EXISTS(STEPPER_RESET)) #define HAS_STEPPER_RESET (PIN_EXISTS(STEPPER_RESET))
#define HAS_X_ENABLE (PIN_EXISTS(X_ENABLE)) #define HAS_X_ENABLE (PIN_EXISTS(X_ENABLE))
#define HAS_X2_ENABLE (PIN_EXISTS(X2_ENABLE)) #define HAS_X2_ENABLE (PIN_EXISTS(X2_ENABLE))

View File

@ -6517,7 +6517,7 @@ inline void gcode_M907() {
if (code_seen(axis_codes[i])) stepper.digipot_current(i, code_value_int()); if (code_seen(axis_codes[i])) stepper.digipot_current(i, code_value_int());
if (code_seen('B')) stepper.digipot_current(4, code_value_int()); if (code_seen('B')) stepper.digipot_current(4, code_value_int());
if (code_seen('S')) for (int i = 0; i <= 4; i++) stepper.digipot_current(i, code_value_int()); if (code_seen('S')) for (int i = 0; i <= 4; i++) stepper.digipot_current(i, code_value_int());
#endif #elif HAS_MOTOR_CURRENT_PWM
#if PIN_EXISTS(MOTOR_CURRENT_PWM_XY) #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
if (code_seen('X')) stepper.digipot_current(0, code_value_int()); if (code_seen('X')) stepper.digipot_current(0, code_value_int());
#endif #endif
@ -6527,6 +6527,7 @@ inline void gcode_M907() {
#if PIN_EXISTS(MOTOR_CURRENT_PWM_E) #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
if (code_seen('E')) stepper.digipot_current(2, code_value_int()); if (code_seen('E')) stepper.digipot_current(2, code_value_int());
#endif #endif
#endif
#if ENABLED(DIGIPOT_I2C) #if ENABLED(DIGIPOT_I2C)
// this one uses actual amps in floating point // this one uses actual amps in floating point
LOOP_XYZE(i) if (code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value_float()); LOOP_XYZE(i) if (code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value_float());

View File

@ -733,19 +733,27 @@ void Stepper::isr() {
void Stepper::init() { void Stepper::init() {
digipot_init(); //Initialize Digipot Motor Current // Init Digipot Motor Current
microstep_init(); //Initialize Microstepping Pins #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
digipot_init();
#endif
// initialise TMC Steppers // Init Microstepping Pins
#if HAS_MICROSTEPS
microstep_init();
#endif
// Init TMC Steppers
#if ENABLED(HAVE_TMCDRIVER) #if ENABLED(HAVE_TMCDRIVER)
tmc_init(); tmc_init();
#endif #endif
// initialise L6470 Steppers
// Init L6470 Steppers
#if ENABLED(HAVE_L6470DRIVER) #if ENABLED(HAVE_L6470DRIVER)
L6470_init(); L6470_init();
#endif #endif
// Initialize Dir Pins // Init Dir Pins
#if HAS_X_DIR #if HAS_X_DIR
X_DIR_INIT; X_DIR_INIT;
#endif #endif
@ -777,8 +785,7 @@ void Stepper::init() {
E3_DIR_INIT; E3_DIR_INIT;
#endif #endif
//Initialize Enable Pins - steppers default to disabled. // Init Enable Pins - steppers default to disabled.
#if HAS_X_ENABLE #if HAS_X_ENABLE
X_ENABLE_INIT; X_ENABLE_INIT;
if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH); if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
@ -787,7 +794,6 @@ void Stepper::init() {
if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH); if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
#endif #endif
#endif #endif
#if HAS_Y_ENABLE #if HAS_Y_ENABLE
Y_ENABLE_INIT; Y_ENABLE_INIT;
if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH); if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
@ -796,7 +802,6 @@ void Stepper::init() {
if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH); if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
#endif #endif
#endif #endif
#if HAS_Z_ENABLE #if HAS_Z_ENABLE
Z_ENABLE_INIT; Z_ENABLE_INIT;
if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH); if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
@ -805,7 +810,6 @@ void Stepper::init() {
if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH); if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
#endif #endif
#endif #endif
#if HAS_E0_ENABLE #if HAS_E0_ENABLE
E0_ENABLE_INIT; E0_ENABLE_INIT;
if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH); if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
@ -823,9 +827,7 @@ void Stepper::init() {
if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH); if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
#endif #endif
// // Init endstops and pullups
// Init endstops and pullups here
//
endstops.init(); endstops.init();
#define _STEP_INIT(AXIS) AXIS ##_STEP_INIT #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
@ -839,7 +841,7 @@ void Stepper::init() {
#define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E) #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
// Initialize Step Pins // Init Step Pins
#if HAS_X_STEP #if HAS_X_STEP
#if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE) #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
X2_STEP_INIT; X2_STEP_INIT;
@ -1164,28 +1166,28 @@ void Stepper::report_positions() {
#endif //HAS_DIGIPOTSS #endif //HAS_DIGIPOTSS
#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
void Stepper::digipot_init() { void Stepper::digipot_init() {
#if HAS_DIGIPOTSS #if HAS_DIGIPOTSS
const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT; static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
SPI.begin(); SPI.begin();
pinMode(DIGIPOTSS_PIN, OUTPUT); SET_OUTPUT(DIGIPOTSS_PIN);
for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) { for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
//digitalPotWrite(digipot_ch[i], digipot_motor_current[i]); //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
digipot_current(i, digipot_motor_current[i]); digipot_current(i, digipot_motor_current[i]);
} }
#endif #elif HAS_MOTOR_CURRENT_PWM
#if HAS_MOTOR_CURRENT_PWM
#if PIN_EXISTS(MOTOR_CURRENT_PWM_XY) #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT); SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
digipot_current(0, motor_current_setting[0]); digipot_current(0, motor_current_setting[0]);
#endif #endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_Z) #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT); SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
digipot_current(1, motor_current_setting[1]); digipot_current(1, motor_current_setting[1]);
#endif #endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_E) #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT); SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
digipot_current(2, motor_current_setting[2]); digipot_current(2, motor_current_setting[2]);
#endif #endif
//Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise) //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
@ -1210,53 +1212,69 @@ void Stepper::digipot_current(uint8_t driver, int current) {
case 2: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_E_PIN); break; case 2: _WRITE_CURRENT_PWM(MOTOR_CURRENT_PWM_E_PIN); break;
#endif #endif
} }
#else
UNUSED(driver);
UNUSED(current);
#endif #endif
} }
void Stepper::microstep_init() {
#if HAS_MICROSTEPS_E1
pinMode(E1_MS1_PIN, OUTPUT);
pinMode(E1_MS2_PIN, OUTPUT);
#endif #endif
#if HAS_MICROSTEPS #if HAS_MICROSTEPS
pinMode(X_MS1_PIN, OUTPUT);
pinMode(X_MS2_PIN, OUTPUT);
pinMode(Y_MS1_PIN, OUTPUT);
pinMode(Y_MS2_PIN, OUTPUT);
pinMode(Z_MS1_PIN, OUTPUT);
pinMode(Z_MS2_PIN, OUTPUT);
pinMode(E0_MS1_PIN, OUTPUT);
pinMode(E0_MS2_PIN, OUTPUT);
const uint8_t microstep_modes[] = MICROSTEP_MODES;
for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
microstep_mode(i, microstep_modes[i]);
#endif
}
/** /**
* Software-controlled Microstepping * Software-controlled Microstepping
*/ */
void Stepper::microstep_init() {
SET_OUTPUT(X_MS1_PIN);
SET_OUTPUT(X_MS2_PIN);
#if HAS_MICROSTEPS_Y
SET_OUTPUT(Y_MS1_PIN);
SET_OUTPUT(Y_MS2_PIN);
#endif
#if HAS_MICROSTEPS_Z
SET_OUTPUT(Z_MS1_PIN);
SET_OUTPUT(Z_MS2_PIN);
#endif
#if HAS_MICROSTEPS_E0
SET_OUTPUT(E0_MS1_PIN);
SET_OUTPUT(E0_MS2_PIN);
#endif
#if HAS_MICROSTEPS_E1
SET_OUTPUT(E1_MS1_PIN);
SET_OUTPUT(E1_MS2_PIN);
#endif
static const uint8_t microstep_modes[] = MICROSTEP_MODES;
for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
microstep_mode(i, microstep_modes[i]);
}
void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) { void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
if (ms1 >= 0) switch (driver) { if (ms1 >= 0) switch (driver) {
case 0: digitalWrite(X_MS1_PIN, ms1); break; case 0: digitalWrite(X_MS1_PIN, ms1); break;
#if HAS_MICROSTEPS_Y
case 1: digitalWrite(Y_MS1_PIN, ms1); break; case 1: digitalWrite(Y_MS1_PIN, ms1); break;
#endif
#if HAS_MICROSTEPS_Z
case 2: digitalWrite(Z_MS1_PIN, ms1); break; case 2: digitalWrite(Z_MS1_PIN, ms1); break;
#endif
#if HAS_MICROSTEPS_E0
case 3: digitalWrite(E0_MS1_PIN, ms1); break; case 3: digitalWrite(E0_MS1_PIN, ms1); break;
#endif
#if HAS_MICROSTEPS_E1 #if HAS_MICROSTEPS_E1
case 4: digitalWrite(E1_MS1_PIN, ms1); break; case 4: digitalWrite(E1_MS1_PIN, ms1); break;
#endif #endif
} }
if (ms2 >= 0) switch (driver) { if (ms2 >= 0) switch (driver) {
case 0: digitalWrite(X_MS2_PIN, ms2); break; case 0: digitalWrite(X_MS2_PIN, ms2); break;
#if HAS_MICROSTEPS_Y
case 1: digitalWrite(Y_MS2_PIN, ms2); break; case 1: digitalWrite(Y_MS2_PIN, ms2); break;
#endif
#if HAS_MICROSTEPS_Z
case 2: digitalWrite(Z_MS2_PIN, ms2); break; case 2: digitalWrite(Z_MS2_PIN, ms2); break;
#endif
#if HAS_MICROSTEPS_E0
case 3: digitalWrite(E0_MS2_PIN, ms2); break; case 3: digitalWrite(E0_MS2_PIN, ms2); break;
#if PIN_EXISTS(E1_MS2) #endif
#if HAS_MICROSTEPS_E1
case 4: digitalWrite(E1_MS2_PIN, ms2); break; case 4: digitalWrite(E1_MS2_PIN, ms2); break;
#endif #endif
} }
@ -1277,15 +1295,21 @@ void Stepper::microstep_readings() {
SERIAL_PROTOCOLPGM("X: "); SERIAL_PROTOCOLPGM("X: ");
SERIAL_PROTOCOL(READ(X_MS1_PIN)); SERIAL_PROTOCOL(READ(X_MS1_PIN));
SERIAL_PROTOCOLLN(READ(X_MS2_PIN)); SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
#if HAS_MICROSTEPS_Y
SERIAL_PROTOCOLPGM("Y: "); SERIAL_PROTOCOLPGM("Y: ");
SERIAL_PROTOCOL(READ(Y_MS1_PIN)); SERIAL_PROTOCOL(READ(Y_MS1_PIN));
SERIAL_PROTOCOLLN(READ(Y_MS2_PIN)); SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
#endif
#if HAS_MICROSTEPS_Z
SERIAL_PROTOCOLPGM("Z: "); SERIAL_PROTOCOLPGM("Z: ");
SERIAL_PROTOCOL(READ(Z_MS1_PIN)); SERIAL_PROTOCOL(READ(Z_MS1_PIN));
SERIAL_PROTOCOLLN(READ(Z_MS2_PIN)); SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
#endif
#if HAS_MICROSTEPS_E0
SERIAL_PROTOCOLPGM("E0: "); SERIAL_PROTOCOLPGM("E0: ");
SERIAL_PROTOCOL(READ(E0_MS1_PIN)); SERIAL_PROTOCOL(READ(E0_MS1_PIN));
SERIAL_PROTOCOLLN(READ(E0_MS2_PIN)); SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
#endif
#if HAS_MICROSTEPS_E1 #if HAS_MICROSTEPS_E1
SERIAL_PROTOCOLPGM("E1: "); SERIAL_PROTOCOLPGM("E1: ");
SERIAL_PROTOCOL(READ(E1_MS1_PIN)); SERIAL_PROTOCOL(READ(E1_MS1_PIN));
@ -1293,6 +1317,8 @@ void Stepper::microstep_readings() {
#endif #endif
} }
#endif // HAS_MICROSTEPS
#if ENABLED(LIN_ADVANCE) #if ENABLED(LIN_ADVANCE)
void Stepper::advance_M905(const float &k) { void Stepper::advance_M905(const float &k) {

View File

@ -239,13 +239,16 @@ class Stepper {
// //
static FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); } static FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
#if HAS_DIGIPOTSS #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
static void digitalPotWrite(int address, int value); static void digitalPotWrite(int address, int value);
#endif
static void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
static void digipot_current(uint8_t driver, int current); static void digipot_current(uint8_t driver, int current);
#endif
#if HAS_MICROSTEPS
static void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
static void microstep_mode(uint8_t driver, uint8_t stepping); static void microstep_mode(uint8_t driver, uint8_t stepping);
static void microstep_readings(); static void microstep_readings();
#endif
#if ENABLED(Z_DUAL_ENDSTOPS) #if ENABLED(Z_DUAL_ENDSTOPS)
static FORCE_INLINE void set_homing_flag(bool state) { performing_homing = state; } static FORCE_INLINE void set_homing_flag(bool state) { performing_homing = state; }
@ -380,7 +383,10 @@ class Stepper {
} }
static void digipot_init(); static void digipot_init();
#if HAS_MICROSTEPS
static void microstep_init(); static void microstep_init();
#endif
}; };