forked from MaslowCNC/GroundControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
396 lines (325 loc) · 13.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'''
Kivy Imports
'''
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,disable_multitouch')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.uix.popup import Popup
import math
'''
Internal Module Imports
'''
from UIElements.frontPage import FrontPage
from UIElements.screenControls import ScreenControls
from UIElements.gcodeCanvas import GcodeCanvas
from UIElements.otherFeatures import OtherFeatures
from UIElements.softwareSettings import SoftwareSettings
from UIElements.viewMenu import ViewMenu
from UIElements.runMenu import RunMenu
from UIElements.connectMenu import ConnectMenu
from UIElements.diagnosticsMenu import Diagnostics
from UIElements.manualControls import ManualControl
from DataStructures.data import Data
from Connection.nonVisibleWidgets import NonVisibleWidgets
from UIElements.notificationPopup import NotificationPopup
'''
Main UI Program
'''
class GroundControlApp(App):
json = '''
[
{
"type": "string",
"title": "Serial Connection",
"desc": "Select the COM port to connect to machine",
"section": "Maslow Settings",
"key": "COMport"
},
{
"type": "string",
"title": "Distance Between Motors",
"desc": "The horizontal distance between the center of the motor shafts in MM.",
"section": "Maslow Settings",
"key": "motorSpacingX"
},
{
"type": "string",
"title": "Work Area Width in MM",
"desc": "The width of the machine working area (normally 8 feet).",
"section": "Maslow Settings",
"key": "bedWidth"
},
{
"type": "string",
"title": "Work Area Height in MM",
"desc": "The Height of the machine working area (normally 4 feet).",
"section": "Maslow Settings",
"key": "bedHeight"
},
{
"type": "string",
"title": "Motor Offset Height in MM",
"desc": "The vertical distance from the edge of the work area to the level of the motors.",
"section": "Maslow Settings",
"key": "motorOffsetY"
},
{
"type": "string",
"title": "Distance Between Sled Mounting Points",
"desc": "The horizontal distance between the points where the chains mount to the sled.",
"section": "Maslow Settings",
"key": "sledWidth"
},
{
"type": "string",
"title": "Vertical Distance Sled Mounts to Cutter",
"desc": "The vertical distance between where the chains mount on the sled to the cutting tool.",
"section": "Maslow Settings",
"key": "sledHeight"
},
{
"type": "string",
"title": "Center Of Gravity",
"desc": "How far below the cutting bit is the center of gravity. This can be found by resting the sled on a round object and observing where it balances.",
"section": "Maslow Settings",
"key": "sledCG"
},
{
"type": "bool",
"title": "z-axis installed",
"desc": "Does the machine have an automatic z-axis?",
"section": "Maslow Settings",
"key": "zAxis"
},
{
"type": "string",
"title": "Z-Axis Pitch",
"desc": "The number of mm moved per rotation of the z-axis",
"section": "Maslow Settings",
"key": "zDistPerRot"
},
{
"type": "string",
"title": "Open File",
"desc": "The path to the open file",
"section": "Maslow Settings",
"key": "openFile"
}
]
'''
advanced = '''
[
{
"type": "string",
"title": "Encoder Steps per Revolution",
"desc": "The number of encoder steps per revolution of the left or right motor",
"section": "Advanced Settings",
"key": "encoderSteps"
},
{
"type": "string",
"title": "Gear Teeth",
"desc": "The number of teeth on the gear of the left or right motor",
"section": "Advanced Settings",
"key": "gearTeeth"
},
{
"type": "string",
"title": "Chain Pitch",
"desc": "The distance between chain roller centers",
"section": "Advanced Settings",
"key": "chainPitch"
},
{
"type": "string",
"title": "Z-Axis Encoder Steps per Revolution",
"desc": "The number of encoder steps per revolution of the z-axis",
"section": "Advanced Settings",
"key": "zEncoderSteps"
}
]
'''
def build(self):
Window.maximize()
interface = FloatLayout()
self.data = Data()
self.frontpage = FrontPage(self.data, name='FrontPage')
interface.add_widget(self.frontpage)
self.nonVisibleWidgets = NonVisibleWidgets()
'''
Load User Settings
'''
self.data.comport = self.config.get('Maslow Settings', 'COMport')
self.data.gcodeFile = self.config.get('Maslow Settings', 'openFile')
self.data.config = self.config
'''
Initializations
'''
self.frontpage.setUpData(self.data)
self.nonVisibleWidgets.setUpData(self.data)
self.frontpage.gcodecanvas.initialize()
'''
Scheduling
'''
Clock.schedule_interval(self.runPeriodically, .01)
'''
Push settings to machine
'''
self.push_settings_to_machine()
return interface
def build_config(self, config):
"""
Set the default values for the configs sections.
"""
config.setdefaults('Maslow Settings', {'COMport': '',
'zAxis': False,
'zDistPerRot':3.17,
'bedWidth':2438.4,
'bedHeight':1219.2,
'motorOffsetY':463,
'motorSpacingX':3035,
'sledWidth':310,
'sledHeight':139,
'sledCG':79,
'openFile': " "})
config.setdefaults('Advanced Settings', {'encoderSteps': 8148.0,
'gearTeeth': 10,
'chainPitch':6.35,
'zEncoderSteps':7550.0})
def build_settings(self, settings):
"""
Add custom section to the default configuration object.
"""
settings.add_json_panel('Maslow Settings', self.config, data=self.json)
settings.add_json_panel('Advanced Settings', self.config, data=self.advanced)
def on_config_change(self, config, section, key, value):
"""
Respond to changes in the configuration.
"""
if section == "Maslow Settings":
if key == "COMport":
self.data.comport = value
self.push_settings_to_machine()
if (key == "bedHeight" or key == "bedWidth"):
self.frontpage.gcodecanvas.drawWorkspace()
def close_settings(self, settings):
"""
Close settings panel
"""
super(GroundControlApp, self).close_settings(settings)
def push_settings_to_machine(self):
cmdString = ("B03"
+" A" + str(self.data.config.get('Maslow Settings', 'bedWidth'))
+" C" + str(self.data.config.get('Maslow Settings', 'bedHeight'))
+" Q" + str(self.data.config.get('Maslow Settings', 'motorSpacingX'))
+" E" + str(self.data.config.get('Maslow Settings', 'motorOffsetY'))
+" F" + str(self.data.config.get('Maslow Settings', 'sledWidth'))
+" G" + str(self.data.config.get('Maslow Settings', 'sledHeight'))
+" H" + str(self.data.config.get('Maslow Settings', 'sledCG'))
+" I" + str(self.data.config.get('Maslow Settings', 'zAxis'))
+" J" + str(self.data.config.get('Advanced Settings', 'encoderSteps'))
+" K" + str(self.data.config.get('Advanced Settings', 'gearTeeth'))
+" M" + str(self.data.config.get('Advanced Settings', 'chainPitch'))
+" N" + str(self.data.config.get('Maslow Settings' , 'zDistPerRot'))
+" P" + str(self.data.config.get('Advanced Settings', 'zEncoderSteps'))
+ " "
)
self.data.gcode_queue.put(cmdString)
'''
Update Functions
'''
def writeToTextConsole(self, message):
try:
newText = self.frontpage.consoleText[-3000:] + message
self.frontpage.consoleText = newText
self.frontpage.textconsole.gotToBottom()
except:
self.frontpage.consoleText = "text not displayed correctly"
def runPeriodically(self, *args):
'''
this block should be handled within the appropriate widget
'''
while not self.data.message_queue.empty(): #if there is new data to be read
message = self.data.message_queue.get()
if message[0:2] == "pz":
self.setPosOnScreen(message)
elif message[0:2] == "pt":
self.setTargetOnScreen(message)
elif message[0:8] == "Message:":
self.previousUploadStatus = self.data.uploadFlag
self.data.uploadFlag = 0
content = NotificationPopup(continueOn = self.dismiss_popup_continue, hold=self.dismiss_popup_hold , text = message[9:])
self._popup = Popup(title="Notification: ", content=content,
auto_dismiss=False, size_hint=(0.25, 0.25))
self._popup.open()
else:
self.writeToTextConsole(message)
def dismiss_popup_continue(self):
'''
Close The Pop-up and continue cut
'''
self._popup.dismiss()
self.data.uploadFlag = self.previousUploadStatus #resume cutting if the machine was cutting before
def dismiss_popup_hold(self):
'''
Close The Pop-up and continue cut
'''
self._popup.dismiss()
self.data.uploadFlag = 0 #stop cutting
def setPosOnScreen(self, message):
'''
This should be moved into the appropriate widget
'''
try:
startpt = message.find('(')
startpt = startpt + 1
endpt = message.find(')')
numz = message[startpt:endpt]
units = message[endpt+1:endpt+3]
valz = numz.split(",")
xval = float(valz[0])
yval = float(valz[1])
zval = float(valz[2])
error = float(valz[3])
if math.isnan(xval):
self.writeToTextConsole("Unable to resolve x Kinematics.")
xval = 0
if math.isnan(yval):
self.writeToTextConsole("Unable to resolve y Kinematics.")
yval = 0
if math.isnan(zval):
self.writeToTextConsole("Unable to resolve z Kinematics.")
zval = 0
if math.isnan(error):
self.writeToTextConsole("Unable to resolve position error.")
error = 0
except:
print "bad data"
return
self.frontpage.setPosReadout(xval,yval,zval,units)
self.frontpage.gcodecanvas.positionIndicator.setPos(xval,yval,self.data.units, error)
def setTargetOnScreen(self, message):
'''
This should be moved into the appropriate widget
'''
try:
startpt = message.find('(')
startpt = startpt + 1
endpt = message.find(')')
numz = message[startpt:endpt]
units = message[endpt+1:endpt+3]
valz = numz.split(",")
xval = float(valz[0])
yval = float(valz[1])
zval = float(valz[2])
#self.frontpage.gcodecanvas.targetIndicator.setPos(xval,yval,self.data.units)
except:
print "unable to convert to number"
if __name__ == '__main__':
GroundControlApp().run()