Skip to content

Commit 307dfb1

Browse files
committed
♻️ Planner flags refactor
1 parent 884f7c7 commit 307dfb1

File tree

7 files changed

+153
-142
lines changed

7 files changed

+153
-142
lines changed

Marlin/src/gcode/motion/G2_G3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void plan_arc(
315315
// Compute exact location by applying transformation matrix from initial radius vector(=-offset).
316316
// To reduce stuttering, the sin and cos could be computed at different times.
317317
// For now, compute both at the same time.
318-
const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment);
318+
const float Ti = i * theta_per_segment, cos_Ti = cos(Ti), sin_Ti = sin(Ti);
319319
rvec.a = -offset[0] * cos_Ti + offset[1] * sin_Ti;
320320
rvec.b = -offset[0] * sin_Ti - offset[1] * cos_Ti;
321321
}

Marlin/src/gcode/motion/G6.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void GcodeSuite::G6() {
5050
// No speed is set, can't schedule the move
5151
if (!planner.last_page_step_rate) return;
5252

53-
const page_idx_t page_idx = (page_idx_t) parser.value_ulong();
53+
const page_idx_t page_idx = (page_idx_t)parser.value_ulong();
5454

5555
uint16_t num_steps = DirectStepping::Config::TOTAL_STEPS;
5656
if (parser.seen('S')) num_steps = parser.value_ushort();

Marlin/src/gcode/temp/M106_M107.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void GcodeSuite::M106() {
9090
// Set speed, with constraint
9191
thermalManager.set_fan_speed(pfan, speed);
9292

93-
TERN_(LASER_SYNCHRONOUS_M106_M107, planner.buffer_sync_block(BLOCK_FLAG_SYNC_FANS));
93+
TERN_(LASER_SYNCHRONOUS_M106_M107, planner.buffer_sync_block(BLOCK_BIT_SYNC_FANS));
9494

9595
if (TERN0(DUAL_X_CARRIAGE, idex_is_duplicating())) // pfan == 0 when duplicating
9696
thermalManager.set_fan_speed(1 - pfan, speed);
@@ -111,7 +111,7 @@ void GcodeSuite::M107() {
111111
if (TERN0(DUAL_X_CARRIAGE, idex_is_duplicating())) // pfan == 0 when duplicating
112112
thermalManager.set_fan_speed(1 - pfan, 0);
113113

114-
TERN_(LASER_SYNCHRONOUS_M106_M107, planner.buffer_sync_block(BLOCK_FLAG_SYNC_FANS));
114+
TERN_(LASER_SYNCHRONOUS_M106_M107, planner.buffer_sync_block(BLOCK_BIT_SYNC_FANS));
115115
}
116116

117117
#endif // HAS_FAN

Marlin/src/module/planner.cpp

+74-81
Large diffs are not rendered by default.

Marlin/src/module/planner.h

+65-43
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@
7070

7171
#if ENABLED(DIRECT_STEPPING)
7272
#include "../feature/direct_stepping.h"
73-
#define IS_PAGE(B) TEST(B->flag, BLOCK_BIT_IS_PAGE)
74-
#else
75-
#define IS_PAGE(B) false
7673
#endif
7774

7875
#if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
@@ -92,7 +89,34 @@
9289
#define HAS_DIST_MM_ARG 1
9390
#endif
9491

95-
enum BlockFlagBit : char {
92+
#if ENABLED(LASER_POWER_INLINE)
93+
94+
typedef struct {
95+
bool isPlanned:1;
96+
bool isEnabled:1;
97+
bool dir:1;
98+
bool Reserved:6;
99+
} power_status_t;
100+
101+
typedef struct {
102+
power_status_t status; // See planner settings for meaning
103+
uint8_t power; // Ditto; When in trapezoid mode this is nominal power
104+
#if ENABLED(LASER_POWER_INLINE_TRAPEZOID)
105+
uint8_t power_entry; // Entry power for the laser
106+
#if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
107+
uint8_t power_exit; // Exit power for the laser
108+
uint32_t entry_per, // Steps per power increment (to avoid floats in stepper calcs)
109+
exit_per; // Steps per power decrement
110+
#endif
111+
#endif
112+
} block_laser_t;
113+
114+
#endif
115+
116+
/**
117+
* Planner block flags as boolean bit fields
118+
*/
119+
enum BlockFlagBit {
96120
// Recalculate trapezoids on entry junction. For optimization.
97121
BLOCK_BIT_RECALCULATE,
98122

@@ -109,7 +133,7 @@ enum BlockFlagBit : char {
109133

110134
// Direct stepping page
111135
#if ENABLED(DIRECT_STEPPING)
112-
, BLOCK_BIT_IS_PAGE
136+
, BLOCK_BIT_PAGE
113137
#endif
114138

115139
// Sync the fan speeds from the block
@@ -118,57 +142,55 @@ enum BlockFlagBit : char {
118142
#endif
119143
};
120144

121-
enum BlockFlag : char {
122-
BLOCK_FLAG_RECALCULATE = _BV(BLOCK_BIT_RECALCULATE)
123-
, BLOCK_FLAG_NOMINAL_LENGTH = _BV(BLOCK_BIT_NOMINAL_LENGTH)
124-
, BLOCK_FLAG_CONTINUED = _BV(BLOCK_BIT_CONTINUED)
125-
, BLOCK_FLAG_SYNC_POSITION = _BV(BLOCK_BIT_SYNC_POSITION)
126-
#if ENABLED(DIRECT_STEPPING)
127-
, BLOCK_FLAG_IS_PAGE = _BV(BLOCK_BIT_IS_PAGE)
128-
#endif
129-
#if ENABLED(LASER_SYNCHRONOUS_M106_M107)
130-
, BLOCK_FLAG_SYNC_FANS = _BV(BLOCK_BIT_SYNC_FANS)
131-
#endif
132-
};
145+
/**
146+
* Planner block flags as boolean bit fields
147+
*/
148+
typedef struct {
149+
union {
150+
uint8_t bits;
133151

134-
#define BLOCK_MASK_SYNC ( BLOCK_FLAG_SYNC_POSITION | TERN0(LASER_SYNCHRONOUS_M106_M107, BLOCK_FLAG_SYNC_FANS) )
152+
struct {
153+
bool recalculate:1;
135154

136-
#if ENABLED(LASER_POWER_INLINE)
155+
bool nominal_length:1;
137156

138-
typedef struct {
139-
bool isPlanned:1;
140-
bool isEnabled:1;
141-
bool dir:1;
142-
bool Reserved:6;
143-
} power_status_t;
157+
bool continued:1;
144158

145-
typedef struct {
146-
power_status_t status; // See planner settings for meaning
147-
uint8_t power; // Ditto; When in trapezoid mode this is nominal power
148-
#if ENABLED(LASER_POWER_INLINE_TRAPEZOID)
149-
uint8_t power_entry; // Entry power for the laser
150-
#if DISABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
151-
uint8_t power_exit; // Exit power for the laser
152-
uint32_t entry_per, // Steps per power increment (to avoid floats in stepper calcs)
153-
exit_per; // Steps per power decrement
159+
bool sync_position:1;
160+
161+
#if ENABLED(DIRECT_STEPPING)
162+
bool page:1;
154163
#endif
155-
#endif
156-
} block_laser_t;
157164

158-
#endif
165+
#if ENABLED(LASER_SYNCHRONOUS_M106_M107)
166+
bool sync_fans:1;
167+
#endif
168+
};
169+
};
170+
171+
void clear() volatile { bits = 0; }
172+
void apply(const uint8_t f) volatile { bits |= f; }
173+
void apply(const BlockFlagBit b) volatile { SBI(bits, b); }
174+
void reset(const BlockFlagBit b) volatile { bits = _BV(b); }
175+
void set_nominal(const bool n) volatile { recalculate = true; if (n) nominal_length = true; }
176+
177+
} block_flags_t;
159178

160179
/**
161-
* struct block_t
162-
*
163-
* A single entry in the planner buffer.
164-
* Tracks linear movement over multiple axes.
180+
* A single entry in the planner buffer, used to set up and
181+
* track a coordinated linear motion for one or more axes.
165182
*
166183
* The "nominal" values are as-specified by G-code, and
167184
* may never actually be reached due to acceleration limits.
168185
*/
169186
typedef struct block_t {
170187

171-
volatile uint8_t flag; // Block flags (See BlockFlag enum above) - Modified by ISR and main thread!
188+
volatile block_flags_t flag; // Block flags
189+
190+
volatile bool is_fan_sync() { return TERN0(LASER_SYNCHRONOUS_M106_M107, flag.sync_fans); }
191+
volatile bool is_sync() { return flag.sync_position || is_fan_sync(); }
192+
volatile bool is_page() { return TERN0(DIRECT_STEPPING, flag.page); }
193+
volatile bool is_move() { return !(is_sync() || is_page()); }
172194

173195
// Fields used by the motion planner to manage acceleration
174196
float nominal_speed_sqr, // The nominal speed for this block in (mm/sec)^2
@@ -759,7 +781,7 @@ class Planner {
759781
* case of LASER_SYNCHRONOUS_M106_M107 the fan pwm
760782
*/
761783
static void buffer_sync_block(
762-
TERN_(LASER_SYNCHRONOUS_M106_M107, uint8_t sync_flag=BLOCK_FLAG_SYNC_POSITION)
784+
TERN_(LASER_SYNCHRONOUS_M106_M107, const BlockFlagBit flag=BLOCK_BIT_SYNC_POSITION)
763785
);
764786

765787
#if IS_KINEMATIC

Marlin/src/module/stepper.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ void Stepper::pulse_phase_isr() {
16991699
}while(0)
17001700

17011701
// Direct Stepping page?
1702-
const bool is_page = IS_PAGE(current_block);
1702+
const bool is_page = current_block->is_page();
17031703

17041704
#if ENABLED(DIRECT_STEPPING)
17051705
// Direct stepping is currently not ready for HAS_I_AXIS
@@ -1977,7 +1977,7 @@ uint32_t Stepper::block_phase_isr() {
19771977
count_position[_AXIS(AXIS)] += page_step_state.bd[_AXIS(AXIS)] * count_direction[_AXIS(AXIS)];
19781978
#endif
19791979

1980-
if (IS_PAGE(current_block)) {
1980+
if (current_block->is_page()) {
19811981
PAGE_SEGMENT_UPDATE_POS(X);
19821982
PAGE_SEGMENT_UPDATE_POS(Y);
19831983
PAGE_SEGMENT_UPDATE_POS(Z);
@@ -2167,16 +2167,13 @@ uint32_t Stepper::block_phase_isr() {
21672167
if ((current_block = planner.get_current_block())) {
21682168

21692169
// Sync block? Sync the stepper counts or fan speeds and return
2170-
while (current_block->flag & BLOCK_MASK_SYNC) {
2170+
while (current_block->is_sync()) {
21712171

2172-
#if ENABLED(LASER_SYNCHRONOUS_M106_M107)
2173-
const bool is_sync_fans = TEST(current_block->flag, BLOCK_BIT_SYNC_FANS);
2174-
if (is_sync_fans) planner.sync_fan_speeds(current_block->fan_speed);
2175-
#else
2176-
constexpr bool is_sync_fans = false;
2177-
#endif
2178-
2179-
if (!is_sync_fans) _set_position(current_block->position);
2172+
if (current_block->is_fan_sync()) {
2173+
TERN_(LASER_SYNCHRONOUS_M106_M107, planner.sync_fan_speeds(current_block->fan_speed));
2174+
}
2175+
else
2176+
_set_position(current_block->position);
21802177

21812178
discard_current_block();
21822179

@@ -2196,7 +2193,7 @@ uint32_t Stepper::block_phase_isr() {
21962193
#endif
21972194

21982195
#if ENABLED(DIRECT_STEPPING)
2199-
if (IS_PAGE(current_block)) {
2196+
if (current_block->is_page()) {
22002197
page_step_state.segment_steps = 0;
22012198
page_step_state.segment_idx = 0;
22022199
page_step_state.page = page_manager.get_page(current_block->page_idx);

Marlin/src/module/stepper.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ class Stepper {
524524
// Discard current block and free any resources
525525
FORCE_INLINE static void discard_current_block() {
526526
#if ENABLED(DIRECT_STEPPING)
527-
if (IS_PAGE(current_block))
528-
page_manager.free_page(current_block->page_idx);
527+
if (current_block->is_page()) page_manager.free_page(current_block->page_idx);
529528
#endif
530529
current_block = nullptr;
531530
axis_did_move = 0;

0 commit comments

Comments
 (0)