Skip to content

Commit c1d1d60

Browse files
colinrgodseyEmmanuel Viala
authored and
Emmanuel Viala
committed
G6 Direct Stepping (MarlinFirmware#17853)
1 parent b7797d8 commit c1d1d60

17 files changed

+859
-64
lines changed

Marlin/Configuration_adv.h

+18-6
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,16 @@
16631663
// Support for G5 with XYZE destination and IJPQ offsets. Requires ~2666 bytes.
16641664
//#define BEZIER_CURVE_SUPPORT
16651665

1666+
/**
1667+
* Direct Stepping
1668+
*
1669+
* Comparable to the method used by Klipper, G6 direct stepping significantly
1670+
* reduces motion calculations, increases top printing speeds, and results in
1671+
* less step aliasing by calculating all motions in advance.
1672+
* Preparing your G-code: https://github.com/colinrgodsey/step-daemon
1673+
*/
1674+
//#define DIRECT_STEPPING
1675+
16661676
/**
16671677
* G38 Probe Target
16681678
*
@@ -1731,14 +1741,16 @@
17311741
//================================= Buffers =================================
17321742
//===========================================================================
17331743

1734-
// @section hidden
1744+
// @section motion
17351745

1736-
// The number of linear motions that can be in the plan at any give time.
1737-
// THE BLOCK_BUFFER_SIZE NEEDS TO BE A POWER OF 2 (e.g. 8, 16, 32) because shifts and ors are used to do the ring-buffering.
1738-
#if ENABLED(SDSUPPORT)
1739-
#define BLOCK_BUFFER_SIZE 16 // SD,LCD,Buttons take more memory, block buffer needs to be smaller
1746+
// The number of lineear moves that can be in the planner at once.
1747+
// The value of BLOCK_BUFFER_SIZE must be a power of 2 (e.g. 8, 16, 32)
1748+
#if BOTH(SDSUPPORT, DIRECT_STEPPING)
1749+
#define BLOCK_BUFFER_SIZE 8
1750+
#elif ENABLED(SDSUPPORT)
1751+
#define BLOCK_BUFFER_SIZE 16
17401752
#else
1741-
#define BLOCK_BUFFER_SIZE 16 // maximize block buffer
1753+
#define BLOCK_BUFFER_SIZE 16
17421754
#endif
17431755

17441756
// @section serial

Marlin/src/HAL/AVR/MarlinSerial.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include "MarlinSerial.h"
4444
#include "../../MarlinCore.h"
4545

46+
#if ENABLED(DIRECT_STEPPING)
47+
#include "../../feature/direct_stepping.h"
48+
#endif
49+
4650
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0, 0, { 0 } };
4751
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
4852
template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
@@ -131,6 +135,18 @@
131135

132136
static EmergencyParser::State emergency_state; // = EP_RESET
133137

138+
// This must read the R_UCSRA register before reading the received byte to detect error causes
139+
if (Cfg::DROPPED_RX && B_DOR && !++rx_dropped_bytes) --rx_dropped_bytes;
140+
if (Cfg::RX_OVERRUNS && B_DOR && !++rx_buffer_overruns) --rx_buffer_overruns;
141+
if (Cfg::RX_FRAMING_ERRORS && B_FE && !++rx_framing_errors) --rx_framing_errors;
142+
143+
// Read the character from the USART
144+
uint8_t c = R_UDR;
145+
146+
#if ENABLED(DIRECT_STEPPING)
147+
if (page_manager.maybe_store_rxd_char(c)) return;
148+
#endif
149+
134150
// Get the tail - Nothing can alter its value while this ISR is executing, but there's
135151
// a chance that this ISR interrupted the main process while it was updating the index.
136152
// The backup mechanism ensures the correct value is always returned.
@@ -142,14 +158,6 @@
142158
// Get the next element
143159
ring_buffer_pos_t i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
144160

145-
// This must read the R_UCSRA register before reading the received byte to detect error causes
146-
if (Cfg::DROPPED_RX && B_DOR && !++rx_dropped_bytes) --rx_dropped_bytes;
147-
if (Cfg::RX_OVERRUNS && B_DOR && !++rx_buffer_overruns) --rx_buffer_overruns;
148-
if (Cfg::RX_FRAMING_ERRORS && B_FE && !++rx_framing_errors) --rx_framing_errors;
149-
150-
// Read the character from the USART
151-
uint8_t c = R_UDR;
152-
153161
if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
154162

155163
// If the character is to be stored at the index just before the tail

Marlin/src/MarlinCore.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
#include "gcode/parser.h"
6060
#include "gcode/queue.h"
6161

62+
#if ENABLED(DIRECT_STEPPING)
63+
#include "feature/direct_stepping.h"
64+
#endif
65+
6266
#if ENABLED(TOUCH_BUTTONS)
6367
#include "feature/touch/xpt2046.h"
6468
#endif
@@ -713,6 +717,9 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
713717

714718
// Handle Joystick jogging
715719
TERN_(POLL_JOG, joystick.inject_jog_moves());
720+
721+
// Direct Stepping
722+
TERN_(DIRECT_STEPPING, page_manager.write_responses());
716723
}
717724

718725
/**
@@ -1124,6 +1131,10 @@ void setup() {
11241131
SETUP_RUN(max7219.init());
11251132
#endif
11261133

1134+
#if ENABLED(DIRECT_STEPPING)
1135+
SETUP_RUN(page_manager.init());
1136+
#endif
1137+
11271138
marlin_state = MF_RUNNING;
11281139

11291140
SETUP_LOG("setup() completed.");
+273
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2020 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+
#include "../inc/MarlinConfigPre.h"
23+
24+
#if ENABLED(DIRECT_STEPPING)
25+
26+
#include "direct_stepping.h"
27+
28+
#include "../MarlinCore.h"
29+
30+
#define CHECK_PAGE(I, R) do{ \
31+
if (I >= sizeof(page_states) / sizeof(page_states[0])) { \
32+
fatal_error = true; \
33+
return R; \
34+
} \
35+
}while(0)
36+
37+
#define CHECK_PAGE_STATE(I, R, S) do { \
38+
CHECK_PAGE(I, R); \
39+
if (page_states[I] != S) { \
40+
fatal_error = true; \
41+
return R; \
42+
} \
43+
}while(0)
44+
45+
namespace DirectStepping {
46+
47+
template<typename Cfg>
48+
State SerialPageManager<Cfg>::state;
49+
50+
template<typename Cfg>
51+
volatile bool SerialPageManager<Cfg>::fatal_error;
52+
53+
template<typename Cfg>
54+
volatile PageState SerialPageManager<Cfg>::page_states[Cfg::NUM_PAGES];
55+
56+
template<typename Cfg>
57+
volatile bool SerialPageManager<Cfg>::page_states_dirty;
58+
59+
template<typename Cfg>
60+
millis_t SerialPageManager<Cfg>::next_response;
61+
62+
template<typename Cfg>
63+
uint8_t SerialPageManager<Cfg>::pages[Cfg::NUM_PAGES][Cfg::PAGE_SIZE];
64+
65+
template<typename Cfg>
66+
uint8_t SerialPageManager<Cfg>::checksum;
67+
68+
template<typename Cfg>
69+
typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_byte_idx;
70+
71+
template<typename Cfg>
72+
typename Cfg::page_idx_t SerialPageManager<Cfg>::write_page_idx;
73+
74+
template<typename Cfg>
75+
typename Cfg::write_byte_idx_t SerialPageManager<Cfg>::write_page_size;
76+
77+
template <typename Cfg>
78+
void SerialPageManager<Cfg>::init() {
79+
for (int i = 0 ; i < Cfg::NUM_PAGES ; i++)
80+
page_states[i] = PageState::FREE;
81+
82+
fatal_error = false;
83+
next_response = 0;
84+
state = State::NEWLINE;
85+
86+
page_states_dirty = false;
87+
88+
SERIAL_ECHOLNPGM("pages_ready");
89+
}
90+
91+
template<typename Cfg>
92+
FORCE_INLINE bool SerialPageManager<Cfg>::maybe_store_rxd_char(uint8_t c) {
93+
switch (state) {
94+
default:
95+
case State::MONITOR:
96+
switch (c) {
97+
case '\n':
98+
case '\r':
99+
state = State::NEWLINE;
100+
default:
101+
return false;
102+
}
103+
case State::NEWLINE:
104+
switch (c) {
105+
case Cfg::CONTROL_CHAR:
106+
state = State::ADDRESS;
107+
return true;
108+
case '\n':
109+
case '\r':
110+
state = State::NEWLINE;
111+
return false;
112+
default:
113+
state = State::MONITOR;
114+
return false;
115+
}
116+
case State::ADDRESS:
117+
//TODO: 16 bit address, State::ADDRESS2
118+
write_page_idx = c;
119+
write_byte_idx = 0;
120+
checksum = 0;
121+
122+
CHECK_PAGE(write_page_idx, true);
123+
124+
if (page_states[write_page_idx] == PageState::FAIL) {
125+
// Special case for fail
126+
state = State::UNFAIL;
127+
return true;
128+
}
129+
130+
set_page_state(write_page_idx, PageState::WRITING);
131+
132+
state = Cfg::DIRECTIONAL ? State::COLLECT : State::SIZE;
133+
134+
return true;
135+
case State::SIZE:
136+
// Zero means full page size
137+
write_page_size = c;
138+
state = State::COLLECT;
139+
return true;
140+
case State::COLLECT:
141+
pages[write_page_idx][write_byte_idx++] = c;
142+
checksum ^= c;
143+
144+
// check if still collecting
145+
if (Cfg::PAGE_SIZE == 256) {
146+
// special case for 8-bit, check if rolled back to 0
147+
if (Cfg::DIRECTIONAL || !write_page_size) { // full 256 bytes
148+
if (write_byte_idx) return true;
149+
} else {
150+
if (write_byte_idx < write_page_size) return true;
151+
}
152+
} else if (Cfg::DIRECTIONAL) {
153+
if (write_byte_idx != Cfg::PAGE_SIZE) return true;
154+
} else {
155+
if (write_byte_idx < write_page_size) return true;
156+
}
157+
158+
state = State::CHECKSUM;
159+
return true;
160+
case State::CHECKSUM: {
161+
const PageState page_state = (checksum == c) ? PageState::OK : PageState::FAIL;
162+
set_page_state(write_page_idx, page_state);
163+
state = State::MONITOR;
164+
return true;
165+
}
166+
case State::UNFAIL:
167+
if (c == 0) {
168+
set_page_state(write_page_idx, PageState::FREE);
169+
} else {
170+
fatal_error = true;
171+
}
172+
state = State::MONITOR;
173+
return true;
174+
}
175+
}
176+
177+
template <typename Cfg>
178+
void SerialPageManager<Cfg>::write_responses() {
179+
if (fatal_error) {
180+
kill(GET_TEXT(MSG_BAD_PAGE));
181+
return;
182+
}
183+
184+
// Runs on a set interval also, as responses may get lost.
185+
if (next_response && next_response < millis()) {
186+
page_states_dirty = true;
187+
}
188+
189+
if (!page_states_dirty) return;
190+
191+
page_states_dirty = false;
192+
next_response = millis() + Cfg::RESPONSE_INTERVAL_MS;
193+
194+
SERIAL_ECHO(Cfg::CONTROL_CHAR);
195+
constexpr int state_bits = 2;
196+
constexpr int n_bytes = Cfg::NUM_PAGES >> state_bits;
197+
volatile uint8_t bits_b[n_bytes] = { 0 };
198+
199+
for (page_idx_t i = 0 ; i < Cfg::NUM_PAGES ; i++) {
200+
bits_b[i >> state_bits] |= page_states[i] << ((i * state_bits) & 0x7);
201+
}
202+
203+
uint8_t crc = 0;
204+
for (uint8_t i = 0 ; i < n_bytes ; i++) {
205+
crc ^= bits_b[i];
206+
SERIAL_ECHO(bits_b[i]);
207+
}
208+
209+
SERIAL_ECHO(crc);
210+
SERIAL_EOL();
211+
}
212+
213+
template <typename Cfg>
214+
FORCE_INLINE void SerialPageManager<Cfg>::set_page_state(const page_idx_t page_idx, const PageState page_state) {
215+
CHECK_PAGE(page_idx,);
216+
217+
page_states[page_idx] = page_state;
218+
page_states_dirty = true;
219+
}
220+
221+
template <>
222+
FORCE_INLINE uint8_t *PageManager::get_page(const page_idx_t page_idx) {
223+
CHECK_PAGE(page_idx, nullptr);
224+
225+
return pages[page_idx];
226+
}
227+
228+
template <>
229+
FORCE_INLINE void PageManager::free_page(const page_idx_t page_idx) {
230+
set_page_state(page_idx, PageState::FREE);
231+
}
232+
233+
};
234+
235+
DirectStepping::PageManager page_manager;
236+
237+
const uint8_t segment_table[DirectStepping::Config::NUM_SEGMENTS][DirectStepping::Config::SEGMENT_STEPS] PROGMEM = {
238+
239+
#if STEPPER_PAGE_FORMAT == SP_4x4D_128
240+
241+
{ 1, 1, 1, 1, 1, 1, 1, 0 }, // 0 = -7
242+
{ 1, 1, 1, 0, 1, 1, 1, 0 }, // 1 = -6
243+
{ 0, 1, 1, 0, 1, 0, 1, 1 }, // 2 = -5
244+
{ 0, 1, 0, 1, 0, 1, 0, 1 }, // 3 = -4
245+
{ 0, 1, 0, 0, 1, 0, 0, 1 }, // 4 = -3
246+
{ 0, 0, 1, 0, 0, 0, 1, 0 }, // 5 = -2
247+
{ 0, 0, 0, 0, 1, 0, 0, 0 }, // 6 = -1
248+
{ 0, 0, 0, 0, 0, 0, 0, 0 }, // 7 = 0
249+
{ 0, 0, 0, 0, 1, 0, 0, 0 }, // 8 = 1
250+
{ 0, 0, 1, 0, 0, 0, 1, 0 }, // 9 = 2
251+
{ 0, 1, 0, 0, 1, 0, 0, 1 }, // 10 = 3
252+
{ 0, 1, 0, 1, 0, 1, 0, 1 }, // 11 = 4
253+
{ 0, 1, 1, 0, 1, 0, 1, 1 }, // 12 = 5
254+
{ 1, 1, 1, 0, 1, 1, 1, 0 }, // 13 = 6
255+
{ 1, 1, 1, 1, 1, 1, 1, 0 }, // 14 = 7
256+
{ 0 }
257+
258+
#elif STEPPER_PAGE_FORMAT == SP_4x2_256
259+
260+
{ 0, 0, 0, 0 }, // 0
261+
{ 0, 1, 0, 0 }, // 1
262+
{ 1, 0, 1, 0 }, // 2
263+
{ 1, 1, 1, 0 }, // 3
264+
265+
#elif STEPPER_PAGE_FORMAT == SP_4x1_512
266+
267+
{0} // Uncompressed format, table not used
268+
269+
#endif
270+
271+
};
272+
273+
#endif // DIRECT_STEPPING

0 commit comments

Comments
 (0)