Skip to content

Commit

Permalink
print_stats: add SET_PRINT_STATS_INFO G-Code for pass slicer variab…
Browse files Browse the repository at this point in the history
…les to Klipper (Klipper3d#5726)

This adds a gcode command that can be used insight the slicer to pass the total layer count and current layer information.

Signed-off-by: Stefan Dej <[email protected]>
  • Loading branch information
meteyou authored and tntclaus committed May 29, 2023
1 parent 571a784 commit 35be94d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
12 changes: 12 additions & 0 deletions docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,18 @@ the paused state is fresh for each print.
#### CANCEL_PRINT
`CANCEL_PRINT`: Cancels the current print.

### [print_stats]

The print_stats module is automatically loaded.

#### SET_PRINT_STATS_INFO
`SET_PRINT_STATS_INFO [TOTAL_LAYER=<total_layer_count>] [CURRENT_LAYER=
<current_layer>]`: Pass slicer info like layer act and total to Klipper.
Add `SET_PRINT_STATS_INFO [TOTAL_LAYER=<total_layer_count>]` to your
slicer start gcode section and `SET_PRINT_STATS_INFO [CURRENT_LAYER=
<current_layer>]` at the layer change gcode section to pass layer
information from your slicer to Klipper.

### [probe]

The following commands are available when a
Expand Down
8 changes: 6 additions & 2 deletions docs/Status_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,12 @@ The following information is available in the `print_stats` object
[virtual_sdcard](Config_Reference.md#virtual_sdcard) config section is
defined):
- `filename`, `total_duration`, `print_duration`, `filament_used`,
`state`, `message`: Estimated information about the current print
when a virtual_sdcard print is active.
`state`, `message`: Estimated information about the current print when a
virtual_sdcard print is active.
- `info.total_layer`: The total layer value of the last `SET_PRINT_STATS_INFO
TOTAL_LAYER=<value>` G-Code command.
- `info.current_layer`: The current layer value of the last
`SET_PRINT_STATS_INFO CURRENT_LAYER=<value>` G-Code command.

## probe

Expand Down
29 changes: 28 additions & 1 deletion klippy/extras/print_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def __init__(self, config):
self.gcode_move = printer.load_object(config, 'gcode_move')
self.reactor = printer.get_reactor()
self.reset()
# Register commands
self.gcode = printer.lookup_object('gcode')
self.gcode.register_command(
"SET_PRINT_STATS_INFO", self.cmd_SET_PRINT_STATS_INFO,
desc=self.cmd_SET_PRINT_STATS_INFO_help)
def _update_filament_usage(self, eventtime):
gc_status = self.gcode_move.get_status(eventtime)
cur_epos = gc_status['position'].e
Expand Down Expand Up @@ -59,13 +64,33 @@ def _note_finish(self, state, error_message = ""):
self.init_duration = self.total_duration - \
self.prev_pause_duration
self.print_start_time = None
cmd_SET_PRINT_STATS_INFO_help = "Pass slicer info like layer act and " \
"total to klipper"
def cmd_SET_PRINT_STATS_INFO(self, gcmd):
total_layer = gcmd.get_int("TOTAL_LAYER", self.info_total_layer, \
minval=0)
current_layer = gcmd.get_int("CURRENT_LAYER", self.info_current_layer, \
minval=0)
if total_layer == 0:
self.info_total_layer = None
self.info_current_layer = None
elif total_layer != self.info_total_layer:
self.info_total_layer = total_layer
self.info_current_layer = 0

if self.info_total_layer is not None and \
current_layer is not None and \
current_layer != self.info_current_layer:
self.info_current_layer = min(current_layer, self.info_total_layer)
def reset(self):
self.filename = self.error_message = ""
self.state = "standby"
self.prev_pause_duration = self.last_epos = 0.
self.filament_used = self.total_duration = 0.
self.print_start_time = self.last_pause_time = None
self.init_duration = 0.
self.info_total_layer = None
self.info_current_layer = None
def get_status(self, eventtime):
time_paused = self.prev_pause_duration
if self.print_start_time is not None:
Expand All @@ -86,7 +111,9 @@ def get_status(self, eventtime):
'print_duration': print_duration,
'filament_used': self.filament_used,
'state': self.state,
'message': self.error_message
'message': self.error_message,
'info': {'total_layer': self.info_total_layer,
'current_layer': self.info_current_layer}
}

def load_config(config):
Expand Down

0 comments on commit 35be94d

Please sign in to comment.