Skip to content

Commit e2eef12

Browse files
Hans007athinkyhead
authored andcommitted
G60/G61 Position Save/Restore (MarlinFirmware#16557)
1 parent a0a93e3 commit e2eef12

File tree

10 files changed

+183
-14
lines changed

10 files changed

+183
-14
lines changed

Marlin/Configuration_adv.h

+5
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,11 @@
15761576

15771577
// @section extras
15781578

1579+
//
1580+
// G60/G61 Position Save and Return
1581+
//
1582+
//#define SAVED_POSITIONS 1 // Each saved position slot costs 12 bytes
1583+
15791584
//
15801585
// G2/G3 Arc Support
15811586
//

Marlin/src/HAL/HAL_STM32/timers.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161

6262
#define HAL_TIMER_RATE (F_CPU / 2) // frequency of timer peripherals
6363

64-
// STM32F401 only has timers 1-5 & 9-11 with timers 4 & 5 usually assigned to TIMER_SERVO and TIMER_TONE
65-
6664
#ifndef STEP_TIMER
6765
#define STEP_TIMER 9
6866
#endif
@@ -76,19 +74,19 @@
7674
#define HAL_TIMER_RATE (F_CPU / 2) // frequency of timer peripherals
7775

7876
#ifndef STEP_TIMER
79-
#define STEP_TIMER 6
77+
#define STEP_TIMER 6 // STM32F401 has no TIM6, TIM7, or TIM8
8078
#endif
8179

8280
#ifndef TEMP_TIMER
83-
#define TEMP_TIMER 14
81+
#define TEMP_TIMER 14 // TIM7 is consumed by Software Serial if used.
8482
#endif
8583

8684
#elif defined(STM32F7xx)
8785

88-
#define HAL_TIMER_RATE (F_CPU/2) // frequency of timer peripherals
86+
#define HAL_TIMER_RATE (F_CPU / 2) // frequency of timer peripherals
8987

9088
#ifndef STEP_TIMER
91-
#define STEP_TIMER 6
89+
#define STEP_TIMER 6 // the RIGHT timer!
9290
#endif
9391

9492
#ifndef TEMP_TIMER

Marlin/src/core/language.h

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@
222222
#define MSG_SOFT_MIN " Min: "
223223
#define MSG_SOFT_MAX " Max: "
224224

225+
#define MSG_SAVED_POS "Position saved"
226+
#define MSG_RESTORING_POS "Restoring position"
227+
#define MSG_INVALID_POS_SLOT "Invalid slot. Total: "
228+
225229
#define MSG_SD_CANT_OPEN_SUBDIR "Cannot open subdir "
226230
#define MSG_SD_INIT_FAIL "SD init fail"
227231
#define MSG_SD_VOL_INIT_FAIL "volume.init failed"
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#include "../../../inc/MarlinConfig.h"
24+
25+
#if SAVED_POSITIONS
26+
27+
#include "../../../core/language.h"
28+
#include "../../gcode.h"
29+
#include "../../../module/motion.h"
30+
31+
#define DEBUG_OUT ENABLED(SAVED_POSITIONS_DEBUG)
32+
#include "../../../core/debug_out.h"
33+
34+
/**
35+
* G60: Save current position
36+
*
37+
* S<slot> - Memory slot # (0-based) to save into (default 0)
38+
*/
39+
void GcodeSuite::G60() {
40+
const uint8_t slot = parser.byteval('S');
41+
42+
if (slot >= SAVED_POSITIONS) {
43+
SERIAL_ERROR_MSG(MSG_INVALID_POS_SLOT STRINGIFY(SAVED_POSITIONS));
44+
return;
45+
}
46+
47+
stored_position[slot] = current_position;
48+
SBI(saved_slots, slot);
49+
50+
#if ENABLED(SAVED_POSITIONS_DEBUG)
51+
const xyze_pos_t &pos = stored_position[slot];
52+
DEBUG_ECHOPAIR_F(MSG_SAVED_POS " S", slot);
53+
DEBUG_ECHOPAIR_F(" : X", pos.x);
54+
DEBUG_ECHOPAIR_F_P(SP_Y_STR, pos.y);
55+
DEBUG_ECHOLNPAIR_F_P(SP_Z_STR, pos.z);
56+
#endif
57+
}
58+
59+
#endif // SAVED_POSITIONS
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#include "../../../inc/MarlinConfig.h"
24+
25+
#if SAVED_POSITIONS
26+
27+
#include "../../../core/language.h"
28+
#include "../../module/planner.h"
29+
#include "../../gcode.h"
30+
#include "../../../module/motion.h"
31+
32+
/**
33+
* G61: Return to saved position
34+
*
35+
* F<rate> - Feedrate (optional) for the move back.
36+
* S<slot> - Slot # (0-based) to restore from (default 0).
37+
* X Y Z - Axes to restore. At least one is required.
38+
*/
39+
void GcodeSuite::G61(void) {
40+
41+
const uint8_t slot = parser.byteval('S');
42+
43+
#if SAVED_POSITIONS < 256
44+
if (slot >= SAVED_POSITIONS) {
45+
SERIAL_ERROR_MSG(MSG_INVALID_POS_SLOT STRINGIFY(SAVED_POSITIONS));
46+
return;
47+
}
48+
#endif
49+
50+
// No saved position? No axes being restored?
51+
if (!TEST(saved_slots, slot) || !parser.seen("XYZ")) return;
52+
53+
// Apply any given feedrate over 0.0
54+
const float fr = parser.linearval('F');
55+
if (fr > 0.0) feedrate_mm_s = MMM_TO_MMS(fr);
56+
57+
SERIAL_ECHOPAIR(MSG_RESTORING_POS " S", int(slot));
58+
LOOP_XYZ(i) {
59+
destination[i] = parser.seen(axis_codes[i])
60+
? stored_position[slot][i] + parser.value_axis_units((AxisEnum)i)
61+
: current_position[i];
62+
SERIAL_CHAR(' ', axis_codes[i]);
63+
SERIAL_ECHO_F(destination[i]);
64+
}
65+
SERIAL_EOL();
66+
67+
// Move to the saved position
68+
prepare_move_to_destination();
69+
}
70+
71+
#endif // SAVED_POSITIONS

Marlin/src/gcode/gcode.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,27 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
314314
#endif
315315

316316
#if ENABLED(CNC_COORDINATE_SYSTEMS)
317-
case 53: G53(); break;
318-
case 54: G54(); break;
319-
case 55: G55(); break;
320-
case 56: G56(); break;
321-
case 57: G57(); break;
322-
case 58: G58(); break;
323-
case 59: G59(); break;
317+
case 53: G53(); break; // G53: (prefix) Apply native workspace
318+
case 54: G54(); break; // G54: Switch to Workspace 1
319+
case 55: G55(); break; // G55: Switch to Workspace 2
320+
case 56: G56(); break; // G56: Switch to Workspace 3
321+
case 57: G57(); break; // G57: Switch to Workspace 4
322+
case 58: G58(); break; // G58: Switch to Workspace 5
323+
case 59: G59(); break; // G59.0 - G59.3: Switch to Workspace 6-9
324+
#endif
325+
326+
#if SAVED_POSITIONS
327+
case 60: G60(); break; // G60: save current position
328+
case 61: G61(); break; // G61: Apply/restore saved coordinates.
324329
#endif
325330

326331
#if ENABLED(PROBE_TEMP_COMPENSATION)
327332
case 76: G76(); break; // G76: Calibrate first layer compensation values
328333
#endif
329334

335+
case 60: G60(); break; // G60: save current position
336+
case 61: G61(); break; // G61: Apply/restore saved coordinates.
337+
330338
#if ENABLED(GCODE_MOTION_MODES)
331339
case 80: G80(); break; // G80: Reset the current motion mode
332340
#endif

Marlin/src/gcode/gcode.h

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
* G34 - Z Stepper automatic alignment using probe: I<iterations> T<accuracy> A<amplification> (Requires Z_STEPPER_AUTO_ALIGN)
6868
* G38 - Probe in any direction using the Z_MIN_PROBE (Requires G38_PROBE_TARGET)
6969
* G42 - Coordinated move to a mesh point (Requires MESH_BED_LEVELING, AUTO_BED_LEVELING_BLINEAR, or AUTO_BED_LEVELING_UBL)
70+
* G60 - Save current position. (Requires SAVED_POSITIONS)
71+
* G61 - Apply/restore saved coordinates. (Requires SAVED_POSITIONS)
7072
* G76 - Calibrate first layer temperature offsets. (Requires PROBE_TEMP_COMPENSATION)
7173
* G80 - Cancel current motion mode (Requires GCODE_MOTION_MODES)
7274
* G90 - Use Absolute Coordinates
@@ -471,6 +473,11 @@ class GcodeSuite {
471473
static void G76();
472474
#endif
473475

476+
#if SAVED_POSITIONS
477+
static void G60();
478+
static void G61();
479+
#endif
480+
474481
#if ENABLED(GCODE_MOTION_MODES)
475482
static void G80();
476483
#endif

Marlin/src/inc/Conditionals_adv.h

+5
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,8 @@
142142
#if ENABLED(JOYSTICK)
143143
#define POLL_JOG
144144
#endif
145+
146+
// G60/G61 Position Save
147+
#if SAVED_POSITIONS > 256
148+
#error "SAVED_POSITIONS must be an integer from 0 to 256."
149+
#endif

Marlin/src/module/motion.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ xyze_pos_t current_position = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
109109
*/
110110
xyze_pos_t destination; // {0}
111111

112+
// G60/G61 Position Save and Return
113+
#if SAVED_POSITIONS
114+
uint8_t saved_slots;
115+
xyz_pos_t stored_position[SAVED_POSITIONS];
116+
#endif
117+
112118
// The active extruder (tool). Set with T<extruder> command.
113119
#if EXTRUDERS > 1
114-
uint8_t active_extruder; // = 0
120+
uint8_t active_extruder = 0; // = 0
115121
#endif
116122

117123
#if ENABLED(LCD_SHOW_E_TOTAL)

Marlin/src/module/motion.h

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ extern bool relative_mode;
6565
extern xyze_pos_t current_position, // High-level current tool position
6666
destination; // Destination for a move
6767

68+
// G60/G61 Position Save and Return
69+
#if SAVED_POSITIONS
70+
extern uint8_t saved_slots;
71+
extern xyz_pos_t stored_position[SAVED_POSITIONS];
72+
#endif
73+
6874
// Scratch space for a cartesian result
6975
extern xyz_pos_t cartes;
7076

0 commit comments

Comments
 (0)