Skip to content

Commit 316e897

Browse files
committed
added option to save unmodified original #3
1 parent e74ea56 commit 316e897

File tree

5 files changed

+92
-55
lines changed

5 files changed

+92
-55
lines changed

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This plugin creates a model of the work surface (using the least squares method on user provided points), allowing for leveling of machines through gcode that otherwise cannot be leveled (e.g. for a grbl machine). A user just needs to measure some z values at a variety of x and y values (e.g. with the paper test), then configure a couple of settings, and the plugin will handle the leveling on file upload.
44

5+
This plugin really only makes sense it you have no other way of leveling out stuff (i.e. your firmware doesn't offer that feature). Also note, that the plugin will stop leveling upon a `G91` command.
6+
57
## Setup
68

79
Install via the bundled [Plugin Manager](https://docs.octoprint.org/en/master/bundledplugins/pluginmanager.html)
@@ -14,19 +16,26 @@ or manually using this URL:
1416

1517
## Configuration
1618

17-
The Polynomial Degree determines how detailed and curvy the model of the surface will be.
18-
The degree should be kept as low as possible to avoid issues between test points.
19+
+ The polynomial degree settings determine how many curves you can have in each direction.
20+
- For example:
21+
* A degree of 0 will just be a flat constant height.
22+
* A degree of 1 will be a sloped line (in each direction so a setting of x:1 y:1 could handle a slightly tilted surface).
23+
* A degree of 2 will curve down and up.
24+
- If you are just worried about a bit of sag on an axis then something like a degree of 2 would make sense. If you use too large of a degree, like 10 in each direction with only 5 points, then the model of the surface will really closely match at your entered points, but it will be so curvy that it will be useless between the points.
25+
* Start with a smaller degree (e.g. 1 or 2)
26+
* Adjust existing points if you are having issues at the places you measured
27+
* or consider increasing the degree by 1
28+
* Add points between existing values if you are having issues in that area
29+
30+
+ The minimum and maximum z values are safeguards against bad combinations of gcode and configuration that would spit out positions outside of machines range.
31+
- If the plugin detects that a movement would fall outside this range, then the file upload will display an error and you should consider changing the configuration.
1932

20-
The Minimum and Maximum z values are used to check for positions that could damage your machine.
21-
These values should be safe to move to with a <code>G0</code> command.
22-
If the plugin detects that a movement would fall outside this range, then the file upload will display an error and you should consider changing the configuration.
33+
+ The invert setting is not needed by most normal configurations, and should be left disabled.
34+
- In my specific setup, I have a G0 Z0 sent to the printer place my toolhead as high as it can go, and a G0 Z71 takes the toolhead right to the surface. With this setting enabled, I can have the gcode files I upload to OctoPrint say that a Z of 0 is right on the surface and a z of 10 is 10 mm above it.
2335

24-
The invert option determines how the original positions and the gcode positions should be combined.
25-
If you want positive gcode values to move the toolhead up and the toolhead is at the work surface at a negative, then do not enable the invert option.
26-
If positive movement moves the toolhead down, but you want a positive value in the gcode to move up then enable the invert option.
2736

28-
The Line break up option breaks up long moves into shorter ones that follow the height model.
29-
Set the distance to 0.0 to disable this feature; otherwise, all moves longer than the specified length will be broken into smaller moves.
37+
+ The segment length option breaks up long moves into shorter ones that follow the height model at each of the endpoints.
38+
- Set the distance to 0.0 to disable this feature; otherwise, all moves longer than the specified length will be broken into smaller moves.
3039

31-
The calibration points are used to create a model of the surface.
32-
Enter the x and y coordinate, then the observed z coordinate.
40+
+ The calibration points are used to create a model of the surface.
41+
- Enter the x and y coordinate, then the measured z coordinate.

octoprint_gcodeleveling/__init__.py

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import octoprint.filemanager.util
77
import octoprint_gcodeleveling.twoDimFit
88

9+
from octoprint.filemanager import FileDestinations
10+
911
import re
1012
import sys
1113

@@ -89,7 +91,7 @@ def construct_line(self, basePos, dirVector, seg):
8991
outLine += " " + self.spareParts
9092
self.spareParts = ""
9193

92-
outLine += "\n"
94+
outLine += "\r\n"
9395

9496
return outLine
9597

@@ -106,7 +108,7 @@ def reconstruct_line(self):
106108
outLine += " " + self.spareParts
107109
self.spareParts = ""
108110

109-
outLine += "\n"
111+
outLine += "\r\n"
110112

111113
return outLine
112114

@@ -244,26 +246,68 @@ class GcodeLevelingPlugin(octoprint.plugin.StartupPlugin,
244246

245247

246248
def createFilePreProcessor(self, path, file_object, blinks=None, printer_profile=None, allow_overwrite=True, *args, **kwargs):
249+
if self.pointsEntered:
250+
fileName = file_object.filename
251+
if not octoprint.filemanager.valid_file_type(fileName, type="gcode"):
252+
return file_object
253+
254+
if fileName.endswith("_NO-GCL.gcode"):
255+
return file_object
256+
else:
257+
if self.unmodifiedCopy:
258+
import os
259+
260+
gclFileName = re.sub(".gcode", "_NO-GCL.gcode", fileName)
261+
gclShortPath = re.sub(".gcode", "_NO-GCL.gcode", path)
262+
263+
gclPath = self._file_manager.join_path(FileDestinations.LOCAL, gclShortPath)
264+
265+
266+
267+
268+
269+
# Unprocessed file stream is directed to a different file
270+
unprocessed = octoprint.filemanager.util.StreamWrapper(gclFileName, file_object.stream())
271+
unprocessed.save(gclPath)
272+
273+
cleanFO = octoprint.filemanager.util.DiskFileWrapper(gclFileName, gclPath)
274+
self._file_manager.add_file(FileDestinations.LOCAL, gclPath, cleanFO, allow_overwrite=True, display=gclFileName)
275+
276+
fileStream = file_object.stream()
277+
self._logger.info("Gcode PreProcessing started.")
278+
self.gcode_preprocessor = GcodePreProcessor(fileStream, self.python_version, self._logger, self.coeffs, self.zMin, self.zMax, self.lineBreakDist, self.invertPosition)
279+
return octoprint.filemanager.util.StreamWrapper(fileName, self.gcode_preprocessor)
280+
else:
281+
self._logger.info("Points have not been entered (or they are all zero). Enter points or disable this plugin if you do not need it.")
247282

248-
fileName = file_object.filename
249-
if not octoprint.filemanager.valid_file_type(fileName, type="gcode"):
250283
return file_object
251-
fileStream = file_object.stream()
252-
self._logger.info("Gcode PreProcessing started.")
253-
self.gcode_preprocessor = GcodePreProcessor(fileStream, self.python_version, self._logger, self.coeffs, self.zMin, self.zMax, self.lineBreakDist, self.invertPosition)
254-
return octoprint.filemanager.util.StreamWrapper(fileName, self.gcode_preprocessor)
255284

256285

257286
def update_from_settings(self):
258287
points = self._settings.get(['points'])
259-
self.zMin = float(self._settings.get(['zMin']))
260-
self.zMax = float(self._settings.get(['zMax']))
261-
self.lineBreakDist = float(self._settings.get(['lineBreakDist']))
262-
self.modelDegree = self._settings.get(['modelDegree'])
263-
self.invertPosition = bool(self._settings.get(['invertPosition']))
264288

265-
self.coeffs = twoDimFit.twoDpolyFit(points, int(self.modelDegree['x']), int(self.modelDegree['y']))
266-
self._logger.info("Leveling Model Computed")
289+
allZeros = True
290+
for point in points:
291+
for coor in point:
292+
if coor != 0.0:
293+
allZeros = False
294+
295+
self.pointsEntered = not allZeros
296+
297+
if self.pointsEntered:
298+
self.zMin = float(self._settings.get(['zMin']))
299+
self.zMax = float(self._settings.get(['zMax']))
300+
self.lineBreakDist = float(self._settings.get(['lineBreakDist']))
301+
self.modelDegree = self._settings.get(['modelDegree'])
302+
self.invertPosition = bool(self._settings.get(['invertPosition']))
303+
self.unmodifiedCopy = bool(self._settings.get(['unmodifiedCopy']))
304+
305+
self.coeffs = twoDimFit.twoDpolyFit(points, int(self.modelDegree['x']), int(self.modelDegree['y']))
306+
self._logger.info("Leveling Model Computed")
307+
else:
308+
self._logger.info("Points have not been entered (or they are all zero). Enter points or disable this plugin if you do not need it.")
309+
310+
267311

268312
# ~~ StartupPlugin mixin
269313
def on_after_startup(self):
@@ -287,7 +331,8 @@ def get_settings_defaults(self):
287331
"zMin": 0.0,
288332
"zMax": 100.0,
289333
"lineBreakDist": 10.0,
290-
"invertPosition": False
334+
"invertPosition": False,
335+
"unmodifiedCopy": True
291336
}
292337

293338
def on_settings_save(self, data):
@@ -301,12 +346,8 @@ def get_settings_version(self):
301346
##~~ AssetPlugin mixin
302347

303348
def get_assets(self):
304-
# Define your plugin's asset files to automatically include in the
305-
# core UI here.
306349
return dict(
307350
js=["js/GcodeLeveling.js"]
308-
# css=["css/GcodeLeveling.css"],
309-
# less=["less/GcodeLeveling.less"]
310351
)
311352

312353
def get_template_configs(self):
@@ -321,9 +362,6 @@ def get_template_configs(self):
321362
##~~ Softwareupdate hook
322363

323364
def get_update_information(self):
324-
# Define the configuration for your plugin to use with the Software Update
325-
# Plugin here. See https://docs.octoprint.org/en/master/bundledplugins/softwareupdate.html
326-
# for details.
327365
return dict(
328366
gcodeleveling=dict(
329367
displayName="GcodeLeveling Plugin",

octoprint_gcodeleveling/templates/README.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

octoprint_gcodeleveling/templates/gcodeleveling_settings.jinja2

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,7 @@
33
<h3>Gcode Leveling Settings</h3>
44
<div>
55
<span class="help-inline"><span class="label label-info">Info:</span>
6-
The Polynomial Degree determines how detailed and curvy the model of the surface will be.
7-
The degree should be kept as low as possible to avoid issues between test points.
8-
</span>
9-
<span class="help-inline"><span class="label label-warning">Warning:</span>
10-
The Minimum and Maximum z values are used to check for positions that could damage your machine.
11-
These values should be safe to move to with a <code>G0</code> command.
12-
If the plugin detects that a movement would fall outside this range, then the file upload will display an error and you should consider changing the configuration.
13-
</span>
14-
<span class="help-inline"><span class="label label-info">Info:</span>
15-
The invert option determines how the original positions and the gcode positions should be combined.
16-
If you want positive gcode values to move the toolhead up and the toolhead is at the work surface at a negative, then do not enable the invert option.
17-
If positive movement moves the toolhead down, but you want a positive value in the gcode to move up then enable the invert option.
18-
</span>
19-
<span class="help-inline"><span class="label label-info">Info:</span>
20-
The Line break up option breaks up long moves into shorter ones that follow the height model.
21-
Set the distance to 0.0 to disable this feature; otherwise, all moves longer than the specified length will be broken into smaller moves.
6+
See <a href="https://github.com/Willmac16/OctoPrint-GcodeLeveling#Configuration">The Configuration section</a> for more info about configuring this plugin.
227
</span>
238
</div>
249
<div>
@@ -42,13 +27,19 @@
4227
</div>
4328
</div>
4429
<div>
45-
<span class="help-inline">{{ _('Invert addition of original position')}}</span>
30+
<span class="help-inline">{{ _('Invert GCODE File position addition')}}</span>
4631
<div class="input-append">
4732
<input type="checkbox" data-bind="checked: settingsViewModel.settings.plugins.gcodeleveling.invertPosition">
4833
</div>
4934
</div>
5035
<div>
51-
<span class="help-inline">{{ _('Length of move to break up')}}</span>
36+
<span class="help-inline">{{ _('Create Unmodified Copy on Upload')}}</span>
37+
<div class="input-append">
38+
<input type="checkbox" data-bind="checked: settingsViewModel.settings.plugins.gcodeleveling.unmodifiedCopy">
39+
</div>
40+
</div>
41+
<div>
42+
<span class="help-inline">{{ _('Length of Segments')}}</span>
5243
<div class="input-append">
5344
<input type="number" step="0.001" class="input-mini text-right" data-bind="value: settingsViewModel.settings.plugins.gcodeleveling.lineBreakDist">
5445
</div>

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "OctoPrint-GcodeLeveling"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "0.1.1"
17+
plugin_version = "0.2.0"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)