This repository was archived by the owner on Mar 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpolation.js
More file actions
149 lines (138 loc) · 3.68 KB
/
interpolation.js
File metadata and controls
149 lines (138 loc) · 3.68 KB
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
var lazy = require("lazy")
, filesystem = require("fs")
, lines = []
, j = 0
, x = 1
, z = 0
, y = 1
, i = 0
, v
, line_collection = ''
, started = false
, data = []
, intersections = 10;
console.log('reading file');
new lazy(filesystem.createReadStream('test/model_potentionmeter_example_2.gcode'))
.lines
.forEach(function(line){
if (started == true) {
if(line) {
if (line != '0'){
line = line.toString();
z = line.indexOf("Z");
// make a array containing one circle
// build a create collection of lines with only x, y and z
if (z > 0) {
splited = line.split(' ');
if (splited) {
line_collection = line_collection + splited[1].substring(1)+ '|';
}
} else {
splited = line.split(' ');
if (splited) {
line_collection = line_collection + splited[1].substring(1) + ' ' + splited[2].substring(1) + ',';
}
}
}
}
}
if (line == ";start") {
started = true;
console.log('processing...');
}
});
function write_to_file(blob) {
console.log('writing to file...')
filesystem.appendFile('test/data_.gcode', blob, function (err) {
console.log('writing done.')
});
}
function find_diff(line_collection) {
var new_line_collection = '';
var new_circle = '';
var previous_z = '';
var current_z = '';
var new_line_circle = '';
var previous_x_y_z = '';
var current_x_y_z = '';
// split on z plane
var z_plane = line_collection.split('|')
for (j=0; j < z_plane.length; j++) {
if (j != 0) {
// get this and prevoius collection
previous_collection_z = z_plane[j-1];
current_collection_z = z_plane[j];
// split on point
previous_layer = previous_collection_z.split(',')
current_layer = current_collection_z.split(',')
for (l=0; l < current_layer.length; l++) {
current_x_y_z = current_layer[l].split(' ');
previous_x_y_z = previous_layer[l].split(' ');
diff_x = (current_x_y_z[0] - previous_x_y_z[0])/intersections;
diff_y = (current_x_y_z[1] - previous_x_y_z[1])/intersections;
if (l == current_layer.length-1)
previous_layer[l] = previous_layer[l] + ' ' + diff_x + ' Z';
else {
previous_layer[l] = previous_layer[l] + ' ' + diff_x + ' ' + diff_y;
}
new_line_circle = new_line_circle + previous_layer[l] + ',';
}
}
new_line_collection = new_line_collection + new_line_circle + '|';
new_line_circle = '';
}
return new_line_collection;
}
var r = 1;
function interpolation(diff_collection) {
var lines = '';
var form = '';
var new_collection = '';
collection = diff_collection.split('|');
for (n=0; n < collection.length; n++) {
points = collection[n].split(',')
if (points.length > 1) {
for (q=0; q < intersections; q++) {
r = r + q;
level = r;
lines = make_lines(points, q);
new_collection = new_collection + lines;
}
}
}
write_to_file(new_collection);
}
var add_z = 1;
var values = [];
function make_lines(points, q) {
var line = ''
for (p=0; p < points.length; p++){
x_y_d = points[p].split(' ')
tran_x = x_y_d[2];
tran_y = x_y_d[3];
if (parseFloat(x_y_d[1])) {
if (x_y_d[2] == 'Z') {
current = parseFloat(x_y_d[0]);
add = parseFloat(x_y_d[1]);
z = current + (add * q);
point = 'G1' + ' Z'+z+'\n';
} else {
x = parseFloat(x_y_d[0]) + parseFloat((tran_x * q));
y = parseFloat(x_y_d[1]) + parseFloat((tran_y * q));
point = 'G1' + ' X'+x + ' Y' + y+'\n';
}
// if is a number
line = line + point;
}
}
values[0] = line;
values[1] = z;
return values;
}
setTimeout(function(){
diff_collection = find_diff(line_collection);
setTimeout(function(){
interpolation(diff_collection);
}
, 2000);
}, 2000);