Commit 57fe39aa authored by Nicholas Gar Hei Chan's avatar Nicholas Gar Hei Chan
Browse files

Debugged probing procedure

parent 7fe18002
......@@ -196,7 +196,7 @@ class Layer():
def gen_sample_points(self, method, num_samples):
points = self.get_points()
if method == "random":
if method == "Random sampling":
sample_pts = list(np.random.randint(0, points['num_pts'], num_samples))
x_pts = [points['x'][i] for i in sample_pts]
y_pts = [points['y'][i] for i in sample_pts]
......@@ -204,7 +204,7 @@ class Layer():
if method == "none":
self.sample_points = dict([('x', []), ('y', [])])
def parse_gcode(filename, sample_spacing, samples_per_layer):
def parse_gcode(filename, sample_spacing, samples_per_layer, method):
layers = []
num_layers = 0
layer_heights = []
......@@ -283,7 +283,7 @@ def parse_gcode(filename, sample_spacing, samples_per_layer):
for i in range(len(layers)):
if (i % sample_spacing) == 0 and (i != 0):
layers[i].gen_sample_points("random", samples_per_layer)
layers[i].gen_sample_points(method, samples_per_layer)
else:
layers[i].gen_sample_points("none", 0)
......
......@@ -10,6 +10,8 @@ from __future__ import absolute_import
# Take a look at the documentation on what other plugin mixins are available.
import subprocess
import time
import threading
import octoprint.plugin
import sys
......@@ -19,6 +21,8 @@ import gcodeparser as gcp
import flask
sampling = 0
class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
......@@ -52,8 +56,13 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
payload: dict
File selected payload from the on-event handler.
"""
num_pts = int(self._settings.get(['sampling_points']))
spacing = int(self._settings.get(['sampling_freq']))
method = self._settings.get(['sampling_algo'])
self._logger.info('Processing GCODE with {} layer spacing, {} samples per layer and {}'.format(spacing, num_pts, method))
full_path = os.path.expanduser("~/.octoprint/uploads/{}".format(payload['name']))
self.model = gcp.parse_gcode(full_path,5,10)
self.model = gcp.parse_gcode(full_path,spacing, num_pts, method)
self.num_layers = len(self.model.layers)
self.curr_layer = 0;
......@@ -61,12 +70,17 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
""" Sample a layer
"""
layer = self.model.layers[current_layer-1]
if len(layer.sample_points['x']):
self._printer.pause_print()
self._logger.info("Starting sampling run...")
result = True
layer = []
next_height = []
if current_layer != 0:
layer = self.model.layers[current_layer-1]
next_height = self.model.layers[current_layer].z_height
else:
return True
def probe_process(self, layer, next_height):
self._logger.info("Starting sampling run at Z={}".format(layer.z_height))
result = True
for point in zip(layer.sample_points['x'], layer.sample_points['y']):
if self.probe_point(point[0], point[1], layer.z_height):
self._logger.info("Good reading at X={}, Y={}".format(point[0], point[1]))
......@@ -75,9 +89,19 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
result = False
break
if result:
self._printer.resume_print()
self._printer.extrude(float(self._settings.get(['retraction'])), None, None)
pass
else:
self._printer.cancel_print()
self._printer.pause_print()
# self._printer.commands("G0 Z{}".format(next_height))
self._printer.set_job_on_hold(False)
if len(layer.sample_points['x']):
if self._printer.set_job_on_hold(True):
thread = threading.Thread(target=probe_process, args=[self,layer,next_height])
thread.daemon = True
thread.start()
return None,
else:
return True
......@@ -95,10 +119,15 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
self._logger.info('Sample point outside of printer bounds. Assuming good')
return True
# Go to 2mm above the sample position, accounting for tool offset
self._printer.commands('G0 X{} Y{} Z{}'.format(probe_x,
probe_y,
probe_z))
# Go to the sample position, accounting for tool offset
command = 'G0 F4329 X{} Y{}'.format(probe_x, probe_y)
self._logger.info(command)
self._printer.commands(command)
self._printer.commands('G4 P0')
self._printer.commands('M114')
self.movement_lock = True
while(self.movement_lock):
pass
reading = subprocess.run("./sample.sh", capture_output=True).stdout.decode("utf-8").strip() == '1'
......@@ -114,13 +143,18 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
self.curr_layer = 0
self.num_layers = 0
self.sample_lock = threading.Lock()
self.movement_lock = False
self.offsets = dict([('x', 0), ('y', 0), ('z', 0)])
##~~ SimpleApiPlugin mixin
def get_api_commands(self):
return dict(
change_layer=["inc"],
test_command=[]
test_command=[],
reload_model=[]
)
def on_api_command(self, command, data):
......@@ -131,28 +165,35 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
if data["inc"] == "-1" and self.curr_layer > 0:
self.curr_layer -= 1
self.send_layer(self.curr_layer)
if command == "test_command":
elif command == "reload_model":
self._logger.info('reloading model')
elif command == "test_command":
self._logger.info(data)
self.probe_point(10,10,10)
##~~ EventHandlerPlugin mixin
def on_event(self, event, payload):
global sampling
if event == 'FileSelected':
self._logger.info('File selected: "{}" at path "{}"'.format(payload['name'], payload['path']))
self.process_gcode(payload)
self._logger.info('GCODE Processed: {} layers'.format(self.num_layers))
self.send_layer(0)
if event == 'ZChange':
if event == 'ZChange' and self._settings.get(['enable'])==True:
data = self._printer.get_current_data()
self._logger.info(data)
currentZ = data['currentZ']
try:
current_layer = self.model.layer_heights.index(currentZ)
self._logger.info('Current layer: {}'.format(current_layer))
self._printer.extrude(float(self._settings.get(['retraction'])), None, None)
self.sample_layer(current_layer)
self._logger.info('Finished sampling run')
except:
self._logger.info('Not a valid layer at z = {}'.format(currentZ))
return
self.sample_layer(current_layer)
#self._logger.info('Layer changed. currentZ = {}'.format(data))
if event == 'PositionUpdate' and self._settings.get(['enable'])==True:
if self.movement_lock == True:
self.movement_lock = False
##~~ TemplatePlugin mixin
......@@ -174,19 +215,28 @@ class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
def get_settings_defaults(self):
return dict(
# put your plugin's default settings here
enable="False",
x_offset="0",
y_offset="0",
z_offset="0"
z_offset="0",
retraction="0",
sampling_algo="Random sampling",
sampling_freq="10",
sampling_points="5"
)
def on_settings_save(self, data):
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self._logger.info("SETTINGS HAVE BEEN CHANGED")
self._logger.info(self._settings.get(['enable']))
self._logger.info(self._settings.get(['sampling_algo']))
#TODO: PROBE UPDATE CODE HERE
self.offsets['x'] = float(self._settings.get(['x_offset']))
self.offsets['y'] = float(self._settings.get(['y_offset']))
self.offsets['z'] = float(self._settings.get(['z_offset']))
self._logger.info(self._settings.get(['enable']) == "True")
self._logger.info(self._settings.get(['enable']) == True)
##~~ AssetPlugin mixin
def get_assets(self):
# Define your plugin's asset files to automatically include in the
......
......@@ -47,6 +47,9 @@ $(function() {
OctoPrint.simpleApiCommand("autoFDP", "change_layer", {"inc": "-1"});
};
self.reload_model = function(data) {
OctoPrint.simpleApiCommand("autoFDP", "reload_model", {"cmd": "reload"});
};
self.test_command = function(data) {
OctoPrint.simpleApiCommand("autoFDP", "test_command", {"key": "hello"});
};
......
<!-- Settings page for autoFDP Octoprint Plugin
Contains form elements for printer XYZ offset, retraction length, default sampling algorithm and
default sampling parameters.
-->
<h3>Probe Settings</h3>
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">X Offset</label>
<label class="control-label">Enable AutoFDP</label>
<div class="controls">
<div class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.autoFDP.enable">
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">X offset</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.x_offset">
......@@ -12,7 +26,7 @@
</div>
<div class="control-group">
<label class="control-label">Y Offset</label>
<label class="control-label">Y offset</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.y_offset">
......@@ -22,7 +36,7 @@
</div>
<div class="control-group">
<label class="control-label">Z Offset</label>
<label class="control-label">Z offset</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.z_offset">
......@@ -32,4 +46,46 @@
</div>
<span class="help-block">X,Y,Z offsets are measured from the nozzle to the probe tip in the standard coordinates of your specific printer. </span>
<div class="control-group">
<label class="control-label">Retraction distance</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.retraction">
<span class="add-on">mm</span>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Default sampling algorithm</label>
<div class="controls">
<select data-bind="value: settings.plugins.autoFDP.sampling_algo">
<option>Random sampling</option>
<option>Perimeter sampling</option>
<option>Uniform sampling</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Default layers per sample</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.sampling_freq">
<span class="add-on">Layers</span>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Default number of samples</label>
<div class="controls">
<div class="input-append">
<input type="text" data-bind="value: settings.plugins.autoFDP.sampling_points">
<span class="add-on"Points</span>
</div>
</div>
</div>
</form>
......@@ -44,6 +44,5 @@
</div>
<div class="form-actions">
<button type="button" class="btn btn-primary" data-bind="click: test_command">Process GCODE</button>
<button type="button" class="btn">Reset to defaults</button>
<button type="button" class="btn btn-primary" data-bind="click: reload_model">Process GCODE</button>
</div>
......@@ -2,7 +2,7 @@
if grep -q "Raspberry Pi" /proc/cpuinfo; then
gpio mode 0 in
gpio mode 1 out
gpio mode 7 out
gpio write 7 1
sleep 0.2
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment