Skip to content

Commit 6adf24f

Browse files
committed
Auto sleep stepper
1 parent bfb67f0 commit 6adf24f

File tree

7 files changed

+64
-5
lines changed

7 files changed

+64
-5
lines changed

grbl/defaults.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#define DEFAULT_STEPPING_INVERT_MASK 0
4848
#define DEFAULT_DIRECTION_INVERT_MASK 0
4949
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
50+
#define DEFAULT_AUTOSLEEP 10 // minutes
5051
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
5152
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
5253
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -89,6 +90,7 @@
8990
#define DEFAULT_STEPPING_INVERT_MASK 0
9091
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<Y_AXIS)|(1<<Z_AXIS))
9192
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
93+
#define DEFAULT_AUTOSLEEP 10 // minutes
9294
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
9395
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
9496
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -127,6 +129,7 @@
127129
#define DEFAULT_STEPPING_INVERT_MASK 0
128130
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<Y_AXIS)|(1<<Z_AXIS))
129131
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 250 // msec (0-254, 255 keeps steppers enabled)
132+
#define DEFAULT_AUTOSLEEP 10 // minutes
130133
#define DEFAULT_STATUS_REPORT_MASK 3 // WPos enabled
131134
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
132135
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -172,6 +175,7 @@
172175
#define DEFAULT_STEPPING_INVERT_MASK 0
173176
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<Y_AXIS)|(1<<Z_AXIS))
174177
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 255 // msec (0-254, 255 keeps steppers enabled)
178+
#define DEFAULT_AUTOSLEEP 10 // minutes
175179
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
176180
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
177181
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -217,6 +221,7 @@
217221
#define DEFAULT_STEPPING_INVERT_MASK 0
218222
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<X_AXIS)|(1<<Z_AXIS))
219223
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 255 // msec (0-254, 255 keeps steppers enabled)
224+
#define DEFAULT_AUTOSLEEP 10 // minutes
220225
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
221226
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
222227
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -261,6 +266,7 @@
261266
#define DEFAULT_STEPPING_INVERT_MASK 0
262267
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<X_AXIS)|(1<<Z_AXIS))
263268
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 255 // msec (0-254, 255 keeps steppers enabled)
269+
#define DEFAULT_AUTOSLEEP 10 // minutes
264270
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
265271
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
266272
#define DEFAULT_ARC_TOLERANCE 0.01 // mm
@@ -306,6 +312,7 @@
306312
#define DEFAULT_STEPPING_INVERT_MASK 0
307313
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<X_AXIS)|(1<<Y_AXIS))
308314
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 255 // msec (0-254, 255 keeps steppers enabled)
315+
#define DEFAULT_AUTOSLEEP 10 // minutes
309316
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
310317
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
311318
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -351,6 +358,7 @@
351358
#define DEFAULT_STEPPING_INVERT_MASK 0
352359
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<X_AXIS)|(1<<Y_AXIS))
353360
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 255 // msec (0-254, 255 keeps steppers enabled)
361+
#define DEFAULT_AUTOSLEEP 10 // minutes
354362
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
355363
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
356364
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -390,6 +398,7 @@
390398
#define DEFAULT_STEPPING_INVERT_MASK 0
391399
#define DEFAULT_DIRECTION_INVERT_MASK 0
392400
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
401+
#define DEFAULT_AUTOSLEEP 10 // minutes
393402
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
394403
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
395404
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -429,6 +438,7 @@
429438
#define DEFAULT_STEPPING_INVERT_MASK 0
430439
#define DEFAULT_DIRECTION_INVERT_MASK 0
431440
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
441+
#define DEFAULT_AUTOSLEEP 10 // minutes
432442
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
433443
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
434444
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -472,6 +482,7 @@
472482
#define DEFAULT_STEPPING_INVERT_MASK 0
473483
#define DEFAULT_DIRECTION_INVERT_MASK ((1<<Y_AXIS))
474484
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
485+
#define DEFAULT_AUTOSLEEP 10 // minutes
475486
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
476487
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
477488
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -511,6 +522,7 @@
511522
#define DEFAULT_STEPPING_INVERT_MASK 0
512523
#define DEFAULT_DIRECTION_INVERT_MASK 0
513524
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
525+
#define DEFAULT_AUTOSLEEP 10 // minutes
514526
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
515527
#define DEFAULT_JUNCTION_DEVIATION 0.02 // mm
516528
#define DEFAULT_ARC_TOLERANCE 0.002 // mm
@@ -550,6 +562,7 @@
550562
#define DEFAULT_STEPPING_INVERT_MASK 0
551563
#define DEFAULT_DIRECTION_INVERT_MASK 0
552564
#define DEFAULT_STEPPER_IDLE_LOCK_TIME 25 // msec (0-254, 255 keeps steppers enabled)
565+
#define DEFAULT_AUTOSLEEP 10 // minutes
553566
#define DEFAULT_STATUS_REPORT_MASK 1 // MPos enabled
554567
#define DEFAULT_JUNCTION_DEVIATION 0.01 // mm
555568
#define DEFAULT_ARC_TOLERANCE 0.002 // mm

grbl/protocol.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ void protocol_main_loop()
7171
uint8_t line_flags = 0;
7272
uint8_t char_counter = 0;
7373
uint8_t c;
74+
75+
const uint_fast32_t stepper_sleep_counts = 60000 * settings.auto_sleep;
76+
uint_fast32_t stepper_sleep = stepper_sleep_counts;
77+
7478
for (;;) {
7579

7680
// Process one line of incoming serial data, as the data becomes available. Performs an
@@ -100,6 +104,13 @@ void protocol_main_loop()
100104
// Everything else is gcode. Block if in alarm or jog mode.
101105
report_status_message(STATUS_SYSTEM_GC_LOCK);
102106
} else {
107+
// Wakeup stepper if IU have disabled before
108+
if (stepper_sleep == 0) {
109+
st_wake_up();
110+
}
111+
// Reset loop sleep
112+
stepper_sleep = stepper_sleep_counts;
113+
103114
// Parse and execute g-code block.
104115
report_status_message(gc_execute_line(line));
105116
}
@@ -158,6 +169,25 @@ void protocol_main_loop()
158169

159170
protocol_execute_realtime(); // Runtime command check point.
160171
if (sys.abort) { return; } // Bail to main() program loop to reset system.
172+
173+
// idle lock is off?
174+
if (settings.stepper_idle_lock_time == 255) {
175+
// Will I do a block in next loop?
176+
if (plan_get_current_block() == NULL) {
177+
// Last waiting loop. so disable stepper
178+
if (stepper_sleep == 1) {
179+
st_disable_set(true);
180+
}
181+
// I will sleep ...
182+
if (stepper_sleep > 0) {
183+
stepper_sleep--;
184+
_delay_ms(1);
185+
}
186+
} else {
187+
// I will do something, so don't sleep
188+
stepper_sleep = stepper_sleep_counts;
189+
}
190+
}
161191
}
162192

163193
return; /* Never reached */

grbl/report.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ void report_grbl_settings() {
208208
#else
209209
report_util_uint8_setting(32,0);
210210
#endif
211+
report_util_uint8_setting(33,settings.auto_sleep);
211212
// Print axis settings
212213
uint8_t idx, set_idx;
213214
uint8_t val = AXIS_SETTINGS_START_VAL;

grbl/settings.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const __flash settings_t defaults = {\
5757
.acceleration[Z_AXIS] = DEFAULT_Z_ACCELERATION,
5858
.max_travel[X_AXIS] = (-DEFAULT_X_MAX_TRAVEL),
5959
.max_travel[Y_AXIS] = (-DEFAULT_Y_MAX_TRAVEL),
60-
.max_travel[Z_AXIS] = (-DEFAULT_Z_MAX_TRAVEL)};
60+
.max_travel[Z_AXIS] = (-DEFAULT_Z_MAX_TRAVEL),
61+
.auto_sleep = DEFAULT_AUTOSLEEP };
6162

6263

6364
// Method to store startup lines into EEPROM
@@ -286,6 +287,7 @@ uint8_t settings_store_global_setting(uint8_t parameter, float value) {
286287
case 27: settings.homing_pulloff = value; break;
287288
case 30: settings.rpm_max = value; spindle_init(); break; // Re-initialize spindle rpm calibration
288289
case 31: settings.rpm_min = value; spindle_init(); break; // Re-initialize spindle rpm calibration
290+
case 33: settings.auto_sleep = int_value; break; // autosleep sec
289291
case 32:
290292
#ifdef VARIABLE_SPINDLE
291293
if (int_value) { settings.flags |= BITFLAG_LASER_MODE; }

grbl/settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
// Version of the EEPROM data. Will be used to migrate existing data from older versions of Grbl
2929
// when firmware is upgraded. Always stored in byte 0 of eeprom
30-
#define SETTINGS_VERSION 10 // NOTE: Check settings_reset() when moving to next version.
30+
#define SETTINGS_VERSION 11 // NOTE: Check settings_reset() when moving to next version.
3131

3232
// Define bit flag masks for the boolean settings in settings.flag.
3333
#define BIT_REPORT_INCHES 0
@@ -110,6 +110,7 @@ typedef struct {
110110
float homing_seek_rate;
111111
uint16_t homing_debounce_delay;
112112
float homing_pulloff;
113+
uint8_t auto_sleep;
113114
} settings_t;
114115
extern settings_t settings;
115116

grbl/stepper.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,20 @@ void st_go_idle()
262262
delay_ms(settings.stepper_idle_lock_time);
263263
pin_state = true; // Override. Disable steppers.
264264
}
265-
if (bit_istrue(settings.flags,BITFLAG_INVERT_ST_ENABLE)) { pin_state = !pin_state; } // Apply pin invert.
266-
if (pin_state) { STEPPERS_DISABLE_PORT |= (1<<STEPPERS_DISABLE_BIT); }
267-
else { STEPPERS_DISABLE_PORT &= ~(1<<STEPPERS_DISABLE_BIT); }
265+
st_disable_set(pin_state);
268266
}
269267

268+
void st_disable_set(bool a_state) {
269+
bool pin_state = a_state; // Keep enabled.
270+
if (bit_istrue(settings.flags, BITFLAG_INVERT_ST_ENABLE)) {
271+
pin_state = !pin_state;
272+
} // Apply pin invert.
273+
if (pin_state) {
274+
STEPPERS_DISABLE_PORT |= (1 << STEPPERS_DISABLE_BIT);
275+
} else {
276+
STEPPERS_DISABLE_PORT &= ~(1 << STEPPERS_DISABLE_BIT);
277+
}
278+
}
270279

271280
/* "The Stepper Driver Interrupt" - This timer interrupt is the workhorse of Grbl. Grbl employs
272281
the venerable Bresenham line algorithm to manage and exactly synchronize multi-axis moves.

grbl/stepper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ void st_update_plan_block_parameters();
5656
// Called by realtime status reporting if realtime rate reporting is enabled in config.h.
5757
float st_get_realtime_rate();
5858

59+
// Disable stepper
60+
void st_disable_set(bool a_state) ;
61+
5962
#endif

0 commit comments

Comments
 (0)