webgcode

Форк
0
417 строк · 13.2 Кб
1
"use strict";
2
require(['libs/jsparse', 'cnc/gcode/parser', 'cnc/gcode/simulation', 'cnc/util'], function (jp, parser, simulation, u) {
3
    function p(obj) {
4
        return new u.Point(obj.x, obj.y, obj.z);
5
    }
6

7
    test("G0 evaluation", function () {
8
        var code = 'G0 X10';
9
        var result = parser.evaluate(code);
10
        equal(result.length, 1, '"' + code + '" code length');
11
        deepEqual(result[0], {
12
            feedRate: 3000,
13
            lineNo: 0,
14
            speedTag: "rapid",
15
            from: p({x: 0, y: 0, z: 0}),
16
            to: p({x: 10, y: 0, z: 0}),
17
            type: "line"
18
        }, '"' + code + '" second component check');
19
    });
20
    test("G0 evaluation with expression", function () {
21
        var code = 'G0 X[5+5]';
22
        var result = parser.evaluate(code);
23
        equal(result.length, 1, '"' + code + '" code length');
24
        deepEqual(result[0], {
25
            feedRate: 3000,
26
            lineNo: 0,
27
            speedTag: "rapid",
28
            from: p({x: 0, y: 0, z: 0}),
29
            to: p({x: 10, y: 0, z: 0}),
30
            type: "line"
31
        }, '"' + code + '" second component check');
32
    });
33
    test("G1 evaluation", function () {
34
        var code = 'G1 X10';
35
        var result = parser.evaluate(code);
36
        equal(result.length, 1, '"' + code + '" code length');
37
        deepEqual(result[0], {
38
            feedRate: 200,
39
            lineNo: 0,
40
            speedTag: "normal",
41
            from: p({x: 0, y: 0, z: 0}),
42
            to: p({x: 10, y: 0, z: 0}),
43
            type: "line"
44
        }, '"' + code + '" second component check');
45
    });
46
    test("G2 evaluation", function () {
47
        var code = 'G2 X10 Y0 Z0 I5 J0 F200';
48
        var result = parser.evaluate(code);
49
        equal(result.length, 1, '"' + code + '" code length');
50
        deepEqual(result[0], {
51
            angularDistance: -Math.PI,
52
            center: {
53
                first: 5,
54
                second: 0
55
            },
56
            feedRate: 200,
57
            lineNo: 0,
58
            speedTag: "normal",
59
            from: p({x: 0, y: 0, z: 0}),
60
            fromAngle: -Math.PI,
61
            plane: {
62
                firstCenterCoord: "i",
63
                firstCoord: "x",
64
                lastCoord: "z",
65
                secondCenterCoord: "j",
66
                secondCoord: "y"
67
            },
68
            radius: 5,
69
            to: p({x: 10, y: 0, z: 0}),
70
            type: "arc"
71
        }, '"' + code + '" second component check');
72
    });
73
    test("G3 evaluation", function () {
74
        var code = 'G3 X5 Y5 Z0 I5 J0 F200';
75
        var result = parser.evaluate(code);
76
        equal(result.length, 1, '"' + code + '" code length');
77
        deepEqual(result[0], {
78
            angularDistance: 3 * Math.PI / 2,
79
            center: {
80
                first: 5,
81
                second: 0
82
            },
83
            feedRate: 200,
84
            lineNo: 0,
85
            speedTag: "normal",
86
            from: p({x: 0, y: 0, z: 0}),
87
            fromAngle: -Math.PI,
88
            plane: {
89
                firstCenterCoord: "i",
90
                firstCoord: "x",
91
                lastCoord: "z",
92
                secondCenterCoord: "j",
93
                secondCoord: "y"
94
            },
95
            radius: 5,
96
            to: p({x: 5, y: 5, z: 0}),
97
            type: "arc"
98
        }, '"' + code + '" second component check');
99
    });
100
    test("jsparse number evaluation", function () {
101

102
        function testValue(str, expected, specialParser) {
103
            if (specialParser == undefined)
104
                specialParser = parser.createParser();
105
            var parsed = jp.wsequence(specialParser.expression, jp.expect(jp.end))(jp.ps(str));
106
            deepEqual(parsed.ast[0], expected, str + ' = ' + expected);
107
        }
108

109
        function testValues(values, specialParser) {
110
            $.each(values, function (_, input) {
111
                testValue(input[0], input[1], specialParser);
112
            });
113
        }
114

115
        $.each(["-10", "+1.5", "-1.5", "1", "1.5", ".5", "-.5", "+.55"], function (_, str) {
116
            testValue(str, parseFloat(str));
117
        });
118
        testValues([
119
            ['1+2', 3],
120
            ['-1+2', 1],
121
            ['-1+-2', -3],
122
            ['1+2+-1', 2],
123
            ['1+2++1', 4]
124
        ]);
125
        testValues([
126
            ['1-2', -1],
127
            ['-1-+2', -3],
128
            ['-1-2', -3],
129
            ['1-2-1', -2],
130
            ['1+2--1', 4]
131
        ]);
132
        testValues([
133
            ['1*2', 2],
134
            ['-1*+2', -2],
135
            ['-1*2', -2],
136
            ['1*2-1', 1],
137
            ['1+2*-1', -1],
138
            ['1/2', 0.5],
139
            ['-1/+2', -0.5],
140
            ['-1/2', -0.5],
141
            ['1/2-1', -0.5],
142
            ['1+2/-1', -1],
143
            ['2/-1*3+1', -5],
144
            ['[1 + 2] * -1', -3]
145
        ]);
146
        testValues([
147
            ['3**2', 9],
148
            ['3**2**3', Math.pow(Math.pow(3, 2), 3)], //yeah, it's left in g-code
149
            ['3**2+3', 12]
150
        ]);
151
        testValues([
152
            ['3**2LT100+100', 1],
153
            ['1EQ0', 0],
154
            ['1EQ1', 1],
155
            ['0EQ1', 0],
156
            ['1NE0', 1],
157
            ['1NE1', 0],
158
            ['0NE1', 1],
159
            ['1GT0', 1],
160
            ['1GT1', 0],
161
            ['0GT1', 0],
162
            ['1GE0', 1],
163
            ['1GE1', 1],
164
            ['0GE1', 0],
165
            ['1LT0', 0],
166
            ['1LT1', 0],
167
            ['0LT1', 1],
168
            ['1LE0', 0],
169
            ['1LE1', 1],
170
            ['0LE1', 1],
171
            ['1AND1', 1],
172
            ['1AND0', 0],
173
            ['0AND0', 0],
174
            ['1XOR1', 0],
175
            ['1XOR0', 1],
176
            ['0XOR0', 0],
177
            ['1OR1', 1],
178
            ['1OR0', 1],
179
            ['0OR0', 0]
180
        ]);
181
        testValues([
182
            ['EXP[2-1]', Math.E],
183
            ['ABS[-1]', 1],
184
            ['ACOS[1]', 0],
185
            ['ASIN[1]', Math.PI / 2],
186
            ['COS[0]', 1],
187
            ['EXP[1]', Math.E],
188
            ['FIX[2.8]', 2],
189
            ['FIX[-2.8]', -3],
190
            ['FUP[2.8]', 3],
191
            ['FUP[ -2.8 ]', -2],
192
            ['ATAN[-1] / [1]', -Math.PI / 4],
193
            ['ATAN[-1] / [1] / [-0.5]', Math.PI / 2]
194
        ]);
195
        var p1 = parser.createParser();
196
        p1.memory['var1'] = 3;
197
        testValues([
198
            ['#3 + 5', 5],
199
            ['#<_3_aa_b2z>+ 5', 5],
200
            ['#<var1>', 3],
201
            ['#<VAR1>', 3]
202
        ], p1);
203

204
        var p2 = parser.createParser();
205
        p2.line(jp.ps('#54 = [COS[0]]'));
206
        p2.line(jp.ps('#<lol>=12\n'));
207
        //check that affectation are done after reading on the same line
208
        p2.line(jp.ps('#<v1>=10 #<v2>=[#<v1>+1]\n'));
209
        testValues([
210
            ['#54', 1],
211
            ['#<lol>', 12],
212
            ['#<undefined>', 0],
213
            ['#<v2>', 1],
214
            ['#<v1>', 10]
215
        ], p2);
216
        deepEqual(parser.createParser().parseLine("#4 = 4.000000 #5 = 5.000000 G1 X10 F[3000] X [12+#4]"), {
217
            f: [3000],
218
            g: [1],
219
            x: [10, 12]
220
        });
221
        deepEqual(parser.createParser().parseLine('G02X10Y30R10 '), {
222
            g: [2],
223
            r: [10],
224
            x: [10],
225
            y: [30]
226
        });
227
        deepEqual(parser.createParser().parseLine('G01Z[-1.000000*#7+#10]F#4 '), {
228
            g: [1],
229
            f: [0],
230
            z: [0]
231
        });
232
    });
233
    test("simple speed planning", function () {
234
        var data = [
235
            {length: 3, maxAcceleration: 8, squaredSpeed: 16, originalSpeed: 4}
236
        ];
237
        simulation.planSpeed(data);
238
        var segment = {
239
            acceleration: {length: 0},
240
            deceleration: {length: 0},
241
            duration: 1.25,
242
            length: 3,
243
            maxAcceleration: 8,
244
            originalSpeed: 4,
245
            squaredSpeed: 16,
246
            fragments: [
247
                {
248
                    duration: 0.5,
249
                    fromSqSpeed: 0,
250
                    length: 1,
251
                    segment: null,
252
                    startX: 0,
253
                    stopX: 1,
254
                    toSqSpeed: 16,
255
                    type: "acceleration"
256
                },
257
                {
258
                    duration: 0.25,
259
                    length: 1,
260
                    segment: null,
261
                    startX: 1,
262
                    stopX: 2,
263
                    squaredSpeed: 16,
264
                    type: "constant"
265
                },
266
                {
267
                    duration: 0.5,
268
                    fromSqSpeed: 16,
269
                    length: 1,
270
                    segment: null,
271
                    startX: 2,
272
                    stopX: 3,
273
                    toSqSpeed: 0,
274
                    type: "deceleration"
275
                }
276
            ]
277
        };
278
        $.each(segment.fragments, function (_, fragment) {
279
            fragment.segment = segment;
280
        });
281
        deepEqual(data, [segment], 'speed planning check');
282
    });
283
    test("short distance speed planning", function () {
284
        var data = [
285
            {length: 2, maxAcceleration: 8, squaredSpeed: 25, originalSpeed: 5}
286
        ];
287
        simulation.planSpeed(data);
288
        var segment = {
289
            acceleration: {length: 0},
290
            deceleration: {length: 0},
291
            duration: 1,
292
            length: 2,
293
            maxAcceleration: 8,
294
            originalSpeed: 5,
295
            squaredSpeed: 16,
296
            fragments: [
297
                {
298
                    duration: 0.5,
299
                    fromSqSpeed: 0,
300
                    length: 1,
301
                    segment: null,
302
                    startX: 0,
303
                    stopX: 1,
304
                    toSqSpeed: 16,
305
                    type: "acceleration"
306
                },
307
                {
308
                    duration: 0.5,
309
                    fromSqSpeed: 16,
310
                    length: 1,
311
                    segment: null,
312
                    startX: 1,
313
                    stopX: 2,
314
                    toSqSpeed: 0,
315
                    type: "deceleration"
316
                }
317
            ]
318
        };
319
        $.each(segment.fragments, function (_, fragment) {
320
            fragment.segment = segment;
321
        });
322
        deepEqual(data, [segment], 'should not go full speed');
323
    });
324
    test("two segments speed planning", function () {
325
        var data = [
326
            {length: 3, maxAcceleration: 8, squaredSpeed: 16, originalSpeed: 4},
327
            {length: 3, maxAcceleration: 8, squaredSpeed: 16, originalSpeed: 4}
328
        ];
329
        simulation.planSpeed(data);
330
        var segment1 = {
331
            acceleration: {length: 0},
332
            deceleration: {length: 1},
333
            duration: 1,
334
            length: 3,
335
            maxAcceleration: 8,
336
            originalSpeed: 4,
337
            squaredSpeed: 16,
338
            fragments: [
339
                {
340
                    duration: 0.5,
341
                    fromSqSpeed: 0,
342
                    length: 1,
343
                    segment: null,
344
                    startX: 0,
345
                    stopX: 1,
346
                    toSqSpeed: 16,
347
                    type: "acceleration"
348
                },
349
                {
350
                    duration: 0.5,
351
                    squaredSpeed: 16,
352
                    length: 2,
353
                    segment: null,
354
                    startX: 1,
355
                    stopX: 3,
356
                    type: "constant"
357
                }
358
            ]
359
        };
360
        $.each(segment1.fragments, function (_, fragment) {
361
            fragment.segment = segment1;
362
        });
363
        var segment2 = {
364
            acceleration: {length: 1},
365
            deceleration: {length: 0},
366
            duration: 1,
367
            length: 3,
368
            maxAcceleration: 8,
369
            originalSpeed: 4,
370
            squaredSpeed: 16,
371
            fragments: [
372
                {
373
                    duration: 0.5,
374
                    length: 2,
375
                    segment: null,
376
                    startX: 0,
377
                    stopX: 2,
378
                    squaredSpeed: 16,
379
                    type: "constant"
380
                },
381
                {
382
                    duration: 0.5,
383
                    fromSqSpeed: 16,
384
                    toSqSpeed: 0,
385
                    length: 1,
386
                    segment: null,
387
                    startX: 2,
388
                    stopX: 3,
389
                    type: "deceleration"
390
                }
391
            ]
392
        };
393
        $.each(segment2.fragments, function (_, fragment) {
394
            fragment.segment = segment2;
395
        });
396
        deepEqual(data, [segment1, segment2], 'should transition smoothly between segments');
397
    });
398
    test("rd(x, y, z)", function () {
399
        equal(rd(0, 2, 1), 1.7972103521033886);
400
        equal(rd(2, 3, 4), 0.16510527294261057);
401
    });
402
    test("rf(x, y, z)", function () {
403
        equal(rf(1, 2, 4), 0.6850858166334359);
404
        equal(rf(1, 2, 0), 1.31102877714606);
405
        equal(rf(2, 3, 4), 0.5840828416771515);
406
    });
407
    test("E(m)", function () {
408
        equal(completeEllipticIntegralSecondKind(0.5), 1.350643881047675);
409
    });
410
    test("E(phi,m)", function () {
411
        equal(incompleteEllipticIntegralSecondKind(Math.PI / 4, 0.5), 0.748186504177661);
412
        equal(incompleteEllipticIntegralSecondKind(Math.PI / 4, 0.7), 0.7323015038648828);
413
        equal(incompleteEllipticIntegralSecondKind(Math.PI / 2, 0.7), 1.2416705679458233);
414
        equal(incompleteEllipticIntegralSecondKind(-Math.PI / 2, 0.7), -1.2416705679458233);
415
        equal(incompleteEllipticIntegralSecondKind(-3 * Math.PI + 0.5, 0.5), -7.613952326493532);
416
    });
417
});

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.