__init__.py 6.52 KB
Newer Older
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
from __future__ import absolute_import

### (Don't forget to remove me)
# This is a basic skeleton for your plugin's __init__.py. You probably want to adjust the class name of your plugin
# as well as the plugin mixins it's subclassing from. This is really just a basic skeleton to get you started,
# defining your plugin as a template plugin, settings and asset plugin. Feel free to add or remove mixins
# as necessary.
#
# Take a look at the documentation on what other plugin mixins are available.

import octoprint.plugin

import sys
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
15
import os
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
16

Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
17
18
19
#sys.path.insert(0, '/home/ngchan/devel/gcode-parser/gcode-parser')

#import gcode_parser as gcp
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import flask

class AutofdpPlugin(octoprint.plugin.SettingsPlugin,
                    octoprint.plugin.AssetPlugin,
                    octoprint.plugin.TemplatePlugin,
		    octoprint.plugin.StartupPlugin,
		    octoprint.plugin.EventHandlerPlugin,
		    octoprint.plugin.SimpleApiPlugin):

	def __init__(self):
		self.model = []
		self.curr_layer = 0
		self.num_layers = 0

34
35
		self.offsets = dict([('x', 0), ('y', 0), ('z', 0)])

Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	##~~ SimpleApiPlugin mixin
	def get_api_commands(self):
		return dict(
			change_layer=["inc"],
			test_command=[]
		)

	def on_api_command(self, command, data):
		if command == "change_layer":
			if data["inc"] == "+1" and self.curr_layer < self.num_layers - 1:
				self.curr_layer += 1
				self.send_layer(self.curr_layer)
			if data["inc"] == "-1" and self.curr_layer > 0:
				self.curr_layer -= 1
				self.send_layer(self.curr_layer)
		if command == "test_command":
			self._logger.info(data)
53
			self.probe_point(10,10,10)
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
54
55
56
57
58
59
60
61
62
63

	##~~ EventHandlerPlugin mixin

	def send_layer(self, layer):
		data = dict(layer_svg=self.model.layers[layer].to_svg_inline(500,self.model.max_x, self.model.max_y),
			    num_layers=len(self.model.layers),
			    curr_layer=self.curr_layer+1)
		self._plugin_manager.send_plugin_message(self._identifier, data)

	def process_gcode(self, payload):
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
64
65
		full_path = os.path.expanduser("~/.octoprint/uploads/{}".format(payload['name']))
		self.model = gcp.parse_gcode(full_path)
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		self.num_layers = len(self.model.layers)
		self.curr_layer = 0;

	def on_event(self, event, payload):
		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 == 'Dwelling':		#G4 command received
			self._logger.info('Dwell, sending a test command...')
			self._printer.commands('G0 X10 Y10')
		if event == 'Waiting':	#M0, M1, M226 received
			self._logger.info('Waiting')
80
81
82
		if event == 'ZChange':
			data = self._printer.get_current_data()
			self._logger.info('Layer changed. currentZ = {}'.format(data))
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
83
84
85
86
87
88
89
90

	##~~ TemplatePlugin mixin

	def get_template_configs(self):
		return [
			dict(type="tab", custom_bindings=True),
			dict(type="settings", custom_bindings=False)
		]
91

Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
92
93
94
95
96
97
98
#	def get_template_vars(self):
#		return dict(x_offset=self._settings.get(["x_offset"]),
#			    y_offset=self._settings.get(["y_offset"]),
#			    z_offset=self._settings.get(["z_offset"]))	
	##~~ StartupPlugin mixin

	def on_after_startup(self):
99
100
101
102
		self._logger.info("autoFDP starting, retrieving saved settings...")
		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']))
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

	##~~ SettingsPlugin mixin

	def get_settings_defaults(self):
		return dict(
			# put your plugin's default settings here
			x_offset="0",
			y_offset="0",
			z_offset="0"
		)

	def on_settings_save(self, data):
		octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
		self._logger.info("SETTINGS HAVE BEEN CHANGED")
		#TODO: PROBE UPDATE CODE HERE
118
119
120
		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']))
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
121
122
123
124
125
126
127
128
129
130

	##~~ AssetPlugin mixin
	def get_assets(self):
		# Define your plugin's asset files to automatically include in the
		# core UI here.
		return dict(
			js=["js/autoFDP.js"],
			#css=["css/autoFDP.css"],
			#less=["less/autoFDP.less"]
		)
131
132
133
134

	##~~ CUSTOM FUNCTIONS FOR FAULT DETECTION PROBE

	def sample_layer(self):
Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
135
		return True
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

	def probe_point(self, x, y, z):
		"""
		Probe at selected x,y,z position. Move probe to point, extend probe, measure output, and report result
		"""
		self._logger.info('STARTING PROBE SEQUENCE')

		probe_x = x - self.offsets['x']
		probe_y = y - self.offsets['y']
		probe_z = z + self.offsets['z']

		# Go to 2mm above the sample position, accounting for tool offset
		self._printer.commands('G0 X{} Y{} Z{}'.format(x+self.offsets['x'],
							       y+self.offsets['y'],
							       z+self.offsets['z']))
		#TODO: EXTEND PROBE HERE

		#TODO: READ PROBE VALUE HERE

		#TODO: RETURN PROBE READING HERE
		return True


Nicholas Gar Hei Chan's avatar
Nicholas Gar Hei Chan committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	##~~ Softwareupdate hook

	def get_update_information(self):
		# Define the configuration for your plugin to use with the Software Update
		# Plugin here. See https://docs.octoprint.org/en/master/bundledplugins/softwareupdate.html
		# for details.
		return dict(
			autoFDP=dict(
				displayName="Autofdp Plugin",
				displayVersion=self._plugin_version,

				# version check: github repository
				type="github_release",
				user="you",
				repo="OctoPrint-Autofdp",
				current=self._plugin_version,

				# update method: pip
				pip="https://github.com/you/OctoPrint-Autofdp/archive/{target_version}.zip"
			)
		)


# If you want your plugin to be registered within OctoPrint under a different name than what you defined in setup.py
# ("OctoPrint-PluginSkeleton"), you may define that here. Same goes for the other metadata derived from setup.py that
# can be overwritten via __plugin_xyz__ control properties. See the documentation for that.
__plugin_name__ = "AutoFDP Plugin"

# Starting with OctoPrint 1.4.0 OctoPrint will also support to run under Python 3 in addition to the deprecated
# Python 2. New plugins should make sure to run under both versions for now. Uncomment one of the following
# compatibility flags according to what Python versions your plugin supports!
#__plugin_pythoncompat__ = ">=2.7,<3" # only python 2
#__plugin_pythoncompat__ = ">=3,<4" # only python 3
__plugin_pythoncompat__ = ">=2.7,<4" # python 2 and 3

def __plugin_load__():
	global __plugin_implementation__
	__plugin_implementation__ = AutofdpPlugin()

	global __plugin_hooks__
	__plugin_hooks__ = {
		"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
	}