-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrolpanel.js
277 lines (231 loc) · 7.78 KB
/
controlpanel.js
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
// TODO: handle clear log menu
// TODO: handle clear queue menu
// TODO: handle clear ack menu
/**
* The controller for the control panel.
*/
app.controller('controlPanelCtrl', function($scope, $timeout,
hotkeys, settingsService, machineService) {
$scope.emergencyStop = machineService.emergencyStop;
$scope.settings = settingsService.settings;
$scope.machineService = machineService;
$scope.logs = machineService.logs.slice();
$scope.stepSize = 1;
$scope.movement = 1;
$scope.abmovement = 0;
$scope.realposition = 0;
// Rendering takes a lot of time, so only update the logs from the source of
// truth every once in a while.
var debounce = null;
$scope.$watch(
function() {return machineService.logs;},
function(newValue, oldValue) {
if (!debounce) {
debounce = $timeout(function() {
// Set the logs that we render to a _copy_ of the current logs.
$scope.logs = newValue.slice();
debounce = null;
}, 200);
}
}, true);
hotkeys.bindTo($scope)
.add({
combo: ['up', 'i'],
description: 'move the Y axis in the + direction',
callback: function() {$scope.relativeMove("Y")}
})
.add({
combo: ['down' ,'k'],
description: 'move the Y axis in the - direction',
callback: function() {$scope.relativeMove("Y-")}
})
.add({
combo: ['left', 'j'],
description: 'move the X axis in the - direction',
callback: function() {$scope.relativeMove("X-")}
})
.add({
combo: ['right', 'l'],
description: 'move the X axis in the + direction',
callback: function() {$scope.relativeMove("X")}
})
.add({
combo: ['a'],
description: 'move the Z axis in the + direction',
callback: function() {$scope.relativeMove("Z")}
})
.add({
combo: ['z'],
description: 'move the Z axis in the - direction',
callback: function() {$scope.relativeMove("Z-")}
})
.add({
combo: '-',
description: 'decrement the step size',
callback: function() {$scope.incrementStepSize(-1)}
})
.add({
combo: '=',
description: 'increment the step size',
callback: function() {$scope.incrementStepSize(1)}
})
.add({
combo: '/',
description: 'focus the manual command entry',
callback: function() {
// Let the event finish propagating.
$timeout(function() {
$("#input-control-cmd").focus();
}, 1);
}
});
// The manual input field has to be configured manually.
// The history of manual commands that the user has entered.
var manualInputHistory = [];
var manualInputPosition = 0;
$scope.manualCommand = "";
$scope.sendManualCommand = function(command) {
manualInputHistory.push(command);
manualInputPosition = manualInputHistory.length;
machineService.enqueueCommands([command + '\n']);
$scope.manualCommand = "";
}
$("#input-control-cmd").keydown(function(e) {
e.stopPropagation();
if (e.keyCode == 27) { // escape; blur the manual command input.
// the delay is to allow the current event propagation to finish.
$timeout(function() {
$("#input-control-cmd").blur();
}, 1);
} else if (e.keyCode == 38) { // up arrow; show previous history position.
manualInputPosition = Math.max(manualInputPosition - 1, 0);
var prevCommand = ((manualInputPosition < manualInputHistory.length) ?
manualInputHistory[manualInputPosition] : "");
$scope.manualCommand = prevCommand;
$timeout(function() {
$("#input-control-cmd")[0].setSelectionRange(prevCommand.length, prevCommand.length);
}, 1);
} else if (e.keyCode == 40) { // down arrow; show next history position.
manualInputPosition = Math.min(manualInputPosition + 1, manualInputHistory.length);
var nextCommand = ((manualInputPosition < manualInputHistory.length) ?
manualInputHistory[manualInputPosition] : "");
$scope.manualCommand = nextCommand;
$timeout(function() {
$("#input-control-cmd")[0].setSelectionRange(nextCommand.length, nextCommand.length);
}, 1);
}
});
var shouldSendCommands = function() {
return machineService.isConnected && machineService.commandQueue.length == 0;
}
$scope.getStepSize = function() {
return Math.pow(10, $scope.stepSize);
}
$scope.incrementStepSize = function(amt) {
$scope.stepSize = Math.max(-1, Math.min(3, $scope.stepSize + amt));
}
$scope.incrementmovement = function(qt) {
$scope.movement = $scope.movement + qt ;
/* Aggiungere controllo che non vada sotto lo 0 assoluto e sopra il massimo impostato nei settings */
}
$scope.incrementmovementab = function(abqt) {
$scope.abmovement = $scope.abmovement + abqt ;
if ($scope.abmovement < 0) {$scope.abmovement = 0}
if ($scope.abmovement > settingsService.settings.workspace_width_mm) {
$scope.abmovement = settingsService.settings.workspace_width_mm;
$scope.realposition = settingsService.settings.workspace_width_mm;
}
}
$scope.setrealposition = function(addsub) {
$scope.realposition = $scope.realposition + addsub ;
}
$scope.abmovequalrealpos = function() {
$scope.abmovement = $scope.realposition;
}
$scope.realposequalabmov = function() {
$scope.realposition = $scope.abmovement;
}
/**
* Enqueue a command to perform a relative move. The global step size
* will be used.
*
* @param {string} axis The axis to move about (eg. 'X-')
*/
$scope.relativeMove = function(axis) {
if (!shouldSendCommands()) {
return;
}
var commands = [];
if (!machineService.isRelativeMode) {
commands.push("G91\n");
}
if (!machineService.isMm) {
commands.push("G21\n");
}
var feedrate = settingsService.settings.workspace_jog_feedrate;
var mv = "G1";
if (settingsService.settings.workspace_jog_rapid) {
mv = "G0"
} else if (feedrate != NaN && feedrate > 0) {
mv += " F" + feedrate;
}
mv += " " + axis + $scope.getStepSize();
commands.push(mv + '\n');
machineService.enqueueCommands(commands);
};
$scope.absoluteMove = function(axis) {
if (!shouldSendCommands()) {
return;
}
var commands = [];
commands.push("G90\n");
if (!machineService.isMm) {
commands.push("G21\n");
}
var feedrate = settingsService.settings.workspace_jog_feedrate;
var mv = "G1";
if (settingsService.settings.workspace_jog_rapid) {
mv = "G0"
} else if (feedrate != NaN && feedrate > 0) {
mv += " F" + feedrate;
}
mv += " " + axis + $scope.abmovement;
commands.push(mv + '\n');
machineService.enqueueCommands(commands);
};
$scope.relativeMove2 = function(axis) {
if (!shouldSendCommands()) {
return;
}
var commands = [];
commands.push("G91\n");
if (!machineService.isMm) {
commands.push("G21\n");
}
var feedrate = settingsService.settings.workspace_jog_feedrate;
var mv = "G1";
if (settingsService.settings.workspace_jog_rapid) {
mv = "G0"
} else if (feedrate != NaN && feedrate > 0) {
mv += " F" + feedrate;
}
mv += " " + axis + $scope.movement;
commands.push(mv + '\n');
machineService.enqueueCommands(commands);
};
$scope.sendCommands = function(cmds) {
if (!shouldSendCommands()) {
return;
}
machineService.enqueueCommands(cmds);
}
// Update the size of various elements to fill the screen.
var resize = function() {
var anchor = document.getElementById("bottom-tracker-logs");
var elem = document.getElementById("console-log");
elem.style.setProperty("height", (anchor.getBoundingClientRect().top -
elem.getBoundingClientRect().top) + "px");
};
$scope.$on('resize', resize);
resize();
});