Skip to content

Commit

Permalink
Fix angles for disabled EDITABLE_SERVO_ANGLES (#12559)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Nov 30, 2018
1 parent c986239 commit aaf862a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 83 deletions.
4 changes: 2 additions & 2 deletions Marlin/src/gcode/config/M281.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#include "../../inc/MarlinConfig.h"

#if HAS_SERVOS && ENABLED(EDITABLE_SERVO_ANGLES)
#if ENABLED(EDITABLE_SERVO_ANGLES)

#include "../gcode.h"
#include "../../module/servo.h"
Expand Down Expand Up @@ -53,4 +53,4 @@ void GcodeSuite::M281() {
}
}

#endif // HAS_SERVOS && EDITABLE_SERVO_ANGLES
#endif // EDITABLE_SERVO_ANGLES
6 changes: 6 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,12 @@
#define Z_PROBE_SERVO_NR -1
#endif

#define HAS_SERVO_ANGLES (ENABLED(SWITCHING_EXTRUDER) || ENABLED(SWITCHING_NOZZLE) || (HAS_Z_SERVO_PROBE && defined(Z_PROBE_SERVO_NR)))

#if !HAS_SERVO_ANGLES
#undef EDITABLE_SERVO_ANGLES
#endif

// Sensors
#define HAS_FILAMENT_WIDTH_SENSOR (PIN_EXISTS(FILWIDTH))

Expand Down
156 changes: 83 additions & 73 deletions Marlin/src/module/configuration_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@

#if HAS_SERVOS
#include "servo.h"
#define EEPROM_NUM_SERVOS NUM_SERVOS
#else
#undef NUM_SERVOS
#define NUM_SERVOS NUM_SERVO_PLUGS
#define EEPROM_NUM_SERVOS NUM_SERVO_PLUGS
#endif

#if HAS_BED_PROBE
Expand Down Expand Up @@ -187,7 +187,7 @@ typedef struct SettingsDataStruct {
//
// SERVO_ANGLES
//
uint16_t servo_angles[NUM_SERVOS][2]; // M281 P L U
uint16_t servo_angles[EEPROM_NUM_SERVOS][2]; // M281 P L U

//
// DELTA / [XYZ]_DUAL_ENDSTOPS
Expand Down Expand Up @@ -632,36 +632,11 @@ void MarlinSettings::postprocess() {
// Servo Angles
//
{
#if !(HAS_SERVOS && ENABLED(EDITABLE_SERVO_ANGLES))

uint16_t servo_angles[NUM_SERVOS][2] = { { 0, 0 } };

#if ENABLED(SWITCHING_EXTRUDER)

constexpr uint16_t sesa[][2] = SWITCHING_EXTRUDER_SERVO_ANGLES;
servo_angles[SWITCHING_EXTRUDER_SERVO_NR][0] = sesa[0][0];
servo_angles[SWITCHING_EXTRUDER_SERVO_NR][1] = sesa[0][1];
#if EXTRUDERS > 3
servo_angles[SWITCHING_EXTRUDER_E23_SERVO_NR][0] = sesa[1][0];
servo_angles[SWITCHING_EXTRUDER_E23_SERVO_NR][1] = sesa[1][1];
#endif

#elif ENABLED(SWITCHING_NOZZLE)

constexpr uint16_t snsa[] = SWITCHING_NOZZLE_SERVO_ANGLES;
servo_angles[SWITCHING_NOZZLE_SERVO_NR][0] = snsa[0];
servo_angles[SWITCHING_NOZZLE_SERVO_NR][1] = snsa[1];

#elif defined(Z_SERVO_ANGLES) && defined(Z_PROBE_SERVO_NR)

constexpr uint16_t zsa[] = Z_SERVO_ANGLES;
servo_angles[Z_PROBE_SERVO_NR][0] = zsa[0];
servo_angles[Z_PROBE_SERVO_NR][1] = zsa[1];

#endif

#endif // !HAS_SERVOS || !EDITABLE_SERVO_ANGLES
_FIELD_TEST(servo_angles);

#if !HAS_SERVOS
uint16_t servo_angles[NUM_SERVO_PLUGS][2] = { { 0, 0 } };
#endif
EEPROM_WRITE(servo_angles);
}

Expand Down Expand Up @@ -1311,10 +1286,14 @@ void MarlinSettings::postprocess() {
// SERVO_ANGLES
//
{
#if !(HAS_SERVOS && ENABLED(EDITABLE_SERVO_ANGLES))
uint16_t servo_angles[NUM_SERVOS][2];
_FIELD_TEST(servo_angles);

#if ENABLED(EDITABLE_SERVO_ANGLES)
uint16_t (&servo_angles_arr)[EEPROM_NUM_SERVOS][2] = servo_angles;
#else
uint16_t servo_angles_arr[EEPROM_NUM_SERVOS][2];
#endif
EEPROM_READ(servo_angles);
EEPROM_READ(servo_angles_arr);
}

//
Expand Down Expand Up @@ -1992,39 +1971,13 @@ void MarlinSettings::reset(PORTARG_SOLO) {
// Servo Angles
//

#if HAS_SERVOS && ENABLED(EDITABLE_SERVO_ANGLES)

#if ENABLED(SWITCHING_EXTRUDER)

#if EXTRUDERS > 3
#define REQ_ANGLES 4
#else
#define REQ_ANGLES 2
#endif
constexpr uint16_t sesa[] = SWITCHING_EXTRUDER_SERVO_ANGLES;
static_assert(COUNT(sesa) == REQ_ANGLES, "SWITCHING_EXTRUDER_SERVO_ANGLES needs " STRINGIFY(REQ_ANGLES) " angles.");
servo_angles[SWITCHING_EXTRUDER_SERVO_NR][0] = sesa[0];
servo_angles[SWITCHING_EXTRUDER_SERVO_NR][1] = sesa[1];
#if EXTRUDERS > 3
servo_angles[SWITCHING_EXTRUDER_E23_SERVO_NR][0] = sesa[2];
servo_angles[SWITCHING_EXTRUDER_E23_SERVO_NR][1] = sesa[3];
#endif

#elif ENABLED(SWITCHING_NOZZLE)

constexpr uint16_t snsa[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
servo_angles[SWITCHING_NOZZLE_SERVO_NR][0] = snsa[0];
servo_angles[SWITCHING_NOZZLE_SERVO_NR][1] = snsa[1];

#elif defined(Z_SERVO_ANGLES) && defined(Z_PROBE_SERVO_NR)

constexpr uint16_t zsa[2] = Z_SERVO_ANGLES;
servo_angles[Z_PROBE_SERVO_NR][0] = zsa[0];
servo_angles[Z_PROBE_SERVO_NR][1] = zsa[1];

#endif
#if ENABLED(EDITABLE_SERVO_ANGLES)
COPY(servo_angles, base_servo_angles);
#endif

#endif // HAS_SERVOS && EDITABLE_SERVO_ANGLES
//
// Endstop Adjustments
//

#if ENABLED(DELTA)
const float adj[ABC] = DELTA_ENDSTOP_ADJ, dta[ABC] = DELTA_TOWER_ANGLE_TRIM;
Expand Down Expand Up @@ -2083,6 +2036,10 @@ void MarlinSettings::reset(PORTARG_SOLO) {

#endif

//
// Preheat parameters
//

#if HAS_LCD_MENU
ui.preheat_hotend_temp[0] = PREHEAT_1_TEMP_HOTEND;
ui.preheat_hotend_temp[1] = PREHEAT_2_TEMP_HOTEND;
Expand All @@ -2092,6 +2049,10 @@ void MarlinSettings::reset(PORTARG_SOLO) {
ui.preheat_fan_speed[1] = PREHEAT_2_FAN_SPEED;
#endif

//
// Hotend PID
//

#if ENABLED(PIDTEMP)
HOTEND_LOOP() {
PID_PARAM(Kp, e) = float(DEFAULT_Kp);
Expand All @@ -2101,29 +2062,54 @@ void MarlinSettings::reset(PORTARG_SOLO) {
PID_PARAM(Kc, e) = DEFAULT_Kc;
#endif
}
#if ENABLED(PID_EXTRUSION_SCALING)
thermalManager.lpq_len = 20; // default last-position-queue size
#endif
#endif // PIDTEMP
#endif

//
// PID Extrusion Scaling
//

#if ENABLED(PID_EXTRUSION_SCALING)
thermalManager.lpq_len = 20; // Default last-position-queue size
#endif

//
// Heated Bed PID
//

#if ENABLED(PIDTEMPBED)
thermalManager.bed_pid.Kp = DEFAULT_bedKp;
thermalManager.bed_pid.Ki = scalePID_i(DEFAULT_bedKi);
thermalManager.bed_pid.Kd = scalePID_d(DEFAULT_bedKd);
#endif

//
// LCD Contrast
//

#if HAS_LCD_CONTRAST
ui.set_contrast(DEFAULT_LCD_CONTRAST);
#endif

//
// Power-Loss Recovery
//

#if ENABLED(POWER_LOSS_RECOVERY)
recovery.enable(true);
#endif

//
// Firmware Retraction
//

#if ENABLED(FWRETRACT)
fwretract.reset();
#endif

//
// Volumetric & Filament Size
//

#if DISABLED(NO_VOLUMETRICS)

parser.volumetric_enabled =
Expand All @@ -2148,16 +2134,36 @@ void MarlinSettings::reset(PORTARG_SOLO) {

reset_stepper_drivers();

//
// Linear Advance
//

#if ENABLED(LIN_ADVANCE)
LOOP_L_N(i, EXTRUDERS) planner.extruder_advance_K[i] = LIN_ADVANCE_K;
#endif

//
// Motor Current PWM
//

#if HAS_MOTOR_CURRENT_PWM
uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT;
for (uint8_t q = 3; q--;)
stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q]));
#endif

//
// CNC Coordinate System
//

#if ENABLED(CNC_COORDINATE_SYSTEMS)
(void)gcode.select_coordinate_system(-1); // Go back to machine space
#endif

//
// Skew Correction
//

#if ENABLED(SKEW_CORRECTION_GCODE)
planner.skew_factor.xy = XY_SKEW_FACTOR;
#if ENABLED(SKEW_CORRECTION_FOR_Z)
Expand All @@ -2166,6 +2172,10 @@ void MarlinSettings::reset(PORTARG_SOLO) {
#endif
#endif

//
// Advanced Pause filament load & unload lengths
//

#if ENABLED(ADVANCED_PAUSE_FEATURE)
for (uint8_t e = 0; e < EXTRUDERS; e++) {
fc_settings[e].unload_length = FILAMENT_CHANGE_UNLOAD_LENGTH;
Expand Down Expand Up @@ -2491,7 +2501,7 @@ void MarlinSettings::reset(PORTARG_SOLO) {

#endif // HAS_LEVELING

#if HAS_SERVOS && ENABLED(EDITABLE_SERVO_ANGLES)
#if ENABLED(EDITABLE_SERVO_ANGLES)

CONFIG_ECHO_HEADING("Servo Angles:");
for (uint8_t i = 0; i < NUM_SERVOS; i++) {
Expand All @@ -2515,7 +2525,7 @@ void MarlinSettings::reset(PORTARG_SOLO) {
}
}

#endif // HAS_SERVOS && EDITABLE_SERVO_ANGLES
#endif // EDITABLE_SERVO_ANGLES

#if HAS_SCARA_OFFSET

Expand Down
5 changes: 4 additions & 1 deletion Marlin/src/module/servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
#include "servo.h"

HAL_SERVO_LIB servo[NUM_SERVOS];
uint16_t servo_angles[NUM_SERVOS][2];

#if ENABLED(EDITABLE_SERVO_ANGLES)
uint16_t servo_angles[NUM_SERVOS][2];
#endif

void servo_init() {
#if NUM_SERVOS >= 1 && HAS_SERVO_0
Expand Down
61 changes: 54 additions & 7 deletions Marlin/src/module/servo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,60 @@
#include "../inc/MarlinConfig.h"
#include "../HAL/shared/servo.h"

extern HAL_SERVO_LIB servo[NUM_SERVOS];
extern uint16_t servo_angles[NUM_SERVOS][2];
extern void servo_init();
#if HAS_SERVO_ANGLES

#if ENABLED(SWITCHING_EXTRUDER)
#ifndef SWITCHING_EXTRUDER_E23_SERVO_NR
#define SWITCHING_EXTRUDER_E23_SERVO_NR -1
#endif
#if EXTRUDERS > 3
#define REQ_ANGLES 4
#else
#define REQ_ANGLES 2
#endif
#define SADATA SWITCHING_EXTRUDER_SERVO_ANGLES
#define ASRC(N,E) (SWITCHING_EXTRUDER_SERVO_NR == N ? asrc[E] : SWITCHING_EXTRUDER_E23_SERVO_NR == N ? asrc[E+2] : 0)
#elif ENABLED(SWITCHING_NOZZLE)
#define SADATA SWITCHING_NOZZLE_SERVO_ANGLES
#define ASRC(N,E) (SWITCHING_NOZZLE_SERVO_NR == N ? asrc[E] : 0)
#elif defined(Z_SERVO_ANGLES) && defined(Z_PROBE_SERVO_NR)
#define SADATA Z_SERVO_ANGLES
#define ASRC(N,E) (Z_PROBE_SERVO_NR == N ? asrc[E] : 0)
#endif

#if ENABLED(EDITABLE_SERVO_ANGLES)
extern uint16_t servo_angles[NUM_SERVOS][2];
#define BASE_SERVO_ANGLES base_servo_angles
#else
#define BASE_SERVO_ANGLES servo_angles
#endif

constexpr uint16_t asrc[] = SADATA;
#if REQ_ANGLES
static_assert(COUNT(asrc) == REQ_ANGLES, "SWITCHING_EXTRUDER_SERVO_ANGLES needs " STRINGIFY(REQ_ANGLES) " angles.");
#endif

constexpr uint16_t BASE_SERVO_ANGLES [NUM_SERVOS][2] = {
{ ASRC(0,0), ASRC(0,1) }
#if NUM_SERVOS > 1
, { ASRC(1,0), ASRC(1,1) }
#if NUM_SERVOS > 2
, { ASRC(2,0), ASRC(2,1) }
#if NUM_SERVOS > 3
, { ASRC(3,0), ASRC(3,1) }
#endif
#endif
#endif
};

#if HAS_Z_SERVO_PROBE
#define DEPLOY_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][0])
#define STOW_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][1])
#endif

#endif // HAS_SERVO_ANGLES

#define MOVE_SERVO(I, P) servo[I].move(P)

#if HAS_Z_SERVO_PROBE
#define DEPLOY_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][0])
#define STOW_Z_SERVO() MOVE_SERVO(Z_PROBE_SERVO_NR, servo_angles[Z_PROBE_SERVO_NR][1])
#endif
extern HAL_SERVO_LIB servo[NUM_SERVOS];
extern void servo_init();

0 comments on commit aaf862a

Please sign in to comment.