-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstgc_wheel.jscad
216 lines (204 loc) · 7.36 KB
/
stgc_wheel.jscad
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
// title : STGC Encoder Disk
// author : James Newton
// license : MIT License
// revision : 0.003
// tags : encoder, grey code, single track, STGC
// file : stgc_wheel.js
function getParameterDefinitions() {
return [
{ name: 'slots', type: 'choice', caption: "Code:",
values: [
"0000001|\
5,4,3,2,1,2,5,0,1,6,2,3,1,0,1,5,4,2,\
4,3,2,1,0,1,4,6,0,5,1,2,0,6,0,4,3,1,\
3,2,1,0,6,0,3,5,6,4,0,1,6,5,6,3,2,0,\
2,1,0,6,5,6,2,4,5,3,6,0,5,4,5,2,1,6,\
1,0,6,5,4,5,1,3,4,2,5,6,4,3,4,1,0,5,\
0,6,5,4,3,4,0,2,3,1,4,5,3,2,3,0,6,4,\
6,5,4,3,2,3,6,1,2,0,3,4,2,1,2,6,5|18|126",
"01010001|\
7,6,7,1,5,4,5,2,1,4,1,7,0,3,7,\
6,5,6,0,4,3,4,1,0,3,0,6,7,2,6,\
5,4,5,7,3,2,3,0,7,2,7,5,6,1,5,\
4,3,4,6,2,1,2,7,6,1,6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6,9,6,8,6,0,6,8,6,9,6,8,\
6,1,6,8,6,9,6,8,6,0,6,8,6,9,6,8,6,4,\
1,2,1,5,2,7,5,8,5,7,5,9,5,7,5,8,5,7,\
5,0,5,7,5,8,5,7,5,9,5,7,5,8,5,7,5,3|36|360",
"000000|\
4,2,4,5,2,3,1,3,4,1,2,0,2,3,0,1,5,1,2,5,\
0,4,0,1,4,5,3,5,0,3,4,2,4,5,2,3,1,3,4,1,\
2,0,2,3,0,1,5,1,2,5,0,4,0,1,4,5,3,5,0|5|60",
"00001|3,0,1,4,1,0,2,4,0,3,0,4,1,3,4,2,4,3,0,2,3,1,3,2,4,1,2,0,2,1|6|31",
"00001|3,1,0,1,4,0,2,0,4,0,3,4,1,4,3,4,2,3,0,3,2,3,1,2,4,2,1,2,0|6|30",
"110100|1,2,3,5,0,1,2,4,5,0,1,3,4,5,0,2,3,4,5,1,2,3,4,0|4|25",
"11110|3,4,2,3,1,2,0,1,4,0|2|11",
"1110|2,3,1,2,0,1,3,0|2|9"
],
captions:[
"7,126 IEEE 42 #5 app",
"8,240 IEEE 42 #5 app",
"10,360 IEEE 42 #5 app",
"6,60 IEEE 42 #5 app",
"5,31 IEEE 42 #5 III",
"5,30 yoctopuce.com", //yoctopuce.com/EN/article/how-to-measure-wind-part-2"
"6,25 winzurf.co.nz", //http://www.winzurf.co.nz/Single_Track_Grey_Code_Patent/Single_track_Grey_code_encoder_patent.pdf
"5,11 winzurf.co.nz", //http://www.winzurf.co.nz/Single_Track_Grey_Code_Patent/Single_track_Grey_code_encoder_patent.pdf
"4,9 winzurf.co.nz", //http://www.winzurf.co.nz/Single_Track_Grey_Code_Patent/Single_track_Grey_code_encoder_patent.pdf
]},
{ name: 'disk', type: 'float', initial: 50, caption: "disk diameter:" },
{ name: 'hub', type: 'float', initial: 3, caption: "hub diameter:" },
{ name: 'slotd', type: 'float', initial: 2, caption: "slots inset:" },
{ name: 'slotlength', type: 'float', initial: 5, caption: "slot length:" },
{ name: 'slotfudge', type: 'float', initial: .05, caption: "slot break:" },
{ name: 'mask', type: 'float', initial: 0.1, caption: "slot mask adj:" },
{ name: 'cutfudge', type: 'float', initial: 0.4, caption: "laser cut width or<br> (-) filament spread:" },
{ name: 'thick', type: 'float', initial: 3, caption: "material thickness:" },
{ name: 'othick', type: 'float', initial: 0.4, caption: "opaque thickness:" },
{ name: 'output', type: 'choice', caption: 'Output:', values: [0, 1], captions: ["Assembly", "Parts"], initial: 0 } ];
}
//p is starting keyword as a string of binary digits. e.g. "1110"
//s is the sequence as an arraw
//d distance between sensors
function stgc(s, p, d) {
let l=p.length; //number of sensors
if (isNaN(d)) {
dh=Math.ceil(s.length/w.value.length);
d=1;
}
else {
dh=d;
}
let fail=1;
let v,b=0;
let m=[];
for(;d<=dh && (fail>0);d++) {
console.log('length:'+l+' reading:position '+p+':0')
for(let i=0;i<=s.length*l;i++) {
m[i]='_';
}
fail=0;
for(var i=0;i<s.length;i++) {
for (var j=0;j<l;j++){
v=j*(d)+i;
b=p.substr(j,1);
if (m[v]!='_' && m[v]!=b) {
m[v]='x';
if (v<s.length*l) {
fail=v+1;
i=s.length;
console.log('length:'+d+' fails at:'+fail);
}
}
else {
m[v]=b;
}
}
v=parseInt(s[i]);
b=p.substr(v,1);
p=p.substring(0,v)+(b=='0'?'1':'0')+p.substr(v+1,9);
}
}
console.log('fail is:'+fail);
if (fail>0) {
alert('Fails at '+(fail-1));
}
console.log(m)
return m
}
function cutarc(r, a, i) {
let curvedpath = CSG.Path2D.arc({
center: [0,0,0],
radius: r,
startangle: a * i - (a/2) + params.slotfudge,
endangle: a * i + (a/2),
resolution: resolution,
});
let curvedpathin = CSG.Path2D.arc({
center: [0,0,0],
radius: r-params.slotlength,
endangle: a * i - (a/2),
startangle: a * i + (a/2) - params.slotfudge,
resolution: resolution,
});
let curvedpath2 = curvedpath.concat(curvedpathin)
let curvedpathclosed = curvedpath2.close()
let curvedarea = curvedpathclosed.innerToCAG()
//let curvedvolumn = curvedarea No, need depth
// let curvedvolumn = curvedpath.rectangularExtrude(
// params.slotlength, params.thick, resolution, false
// ); No, because it extends the arc.
// expandToCAG(pathradius, resolution) No, same issue
// let curvedvolumn = curvedarea.linear_extrude({depth:10})
let curvedvolumn = curvedarea.extrude({offset: [0, 0, params.thick]})
return curvedvolumn
}
function cutarcs(positions, r) {
let a = 360/positions //angle length of each arc
console.log(positions+" slots of "+a+" degrees")
curves = []
for (let i = 0; i<positions; i+=2){
curves.push(cutarc(r,a,i))
}
return curves
}
function cutstgcarcs(l, m,r) {
let a = 360/l //angle length of each arc
console.log(l+" segments of "+a+" degrees")
curves = []
for (let i = 0; i<l; i++){
if ("0"==m[i]) continue
curves.push(cutarc(r,a,i))
}
return curves
}
var resolution = 64
function main () {
console.log(params.slots)
let code=params.slots.split("|")
let start_key = code[0]
let seq = code[1].split(",")
let distance = parseInt(code[2])
let positions = parseInt(code[3])
let m = stgc(seq, start_key, distance)
//return union(cutarcs(seq.length, m, params.disk-params.slotd*2))
return difference(
cylinder({r:params.disk, h:params.thick})
,union(cutstgcarcs(seq.length, m, params.disk-params.slotd*2-params.slotlength))
,union(cutarcs(positions, params.disk-params.slotd))
,cylinder({r:params.hub, h:params.thick})
)
// difference(
// cylinder({r: 10, h:10}),
// sphere({r: 2, center: true})
// ).translate([0, 0, 1.5]).scale(10);
}