forked from nraynaud/webgcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
154 lines (142 loc) · 5.97 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>webgcode test</title>
<link rel="stylesheet" href="webapp/libs/qunit.css">
<style> .chart {
position: relative;
clear: both;
width: 100%;
height: 200px;
}
</style>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<h1>Testing random speed planning</h1>
<h2>Speed/position</h2>
<div class="chart" id="chart1"></div>
<h2>Position/time</h2>
<div class="chart" id="chart2"></div>
<h2>Encoding Error</h2>
<div class="chart" id="chart3"></div>
<script src="webapp/libs/qunit.js"></script>
<script src="webapp/libs/jquery.min.js"></script>
<script src="webapp/libs/jquery.flot.min.js"></script>
<script src="webapp/libs/jsparse.js"></script>
<script src="webapp/cnc/geometry.js"></script>
<script src="webapp/cnc/simulation.js"></script>
<script src="webapp/cnc/parser.js"></script>
<script src="webapp/cnc/elliptic.js"></script>
<script src="tests.js"></script>
<script>
rawData = [];
for (i = 0; i < 20; i++)
rawData.push([Math.random() * 10, Math.random() * 10]);
var acceleration = 4;
data = [];
for (i = 0; i < rawData.length; i++) {
var dataAcceleration = rawData[i].length > 2 ? rawData[i][2] : acceleration;
data.push({length: rawData[i][1], maxAcceleration: dataAcceleration, squaredSpeed: rawData[i][0] * rawData[i][0], originalSpeed: rawData[i][0]});
}
var i;
var speedData = [
{label: 'reference speed(mm->mm/s)', points: {show: true, radius: 1, fill: false, symbol: "circle"}, lines: {show: true}, shadowSize: 0, color: 'red', data: []},
{label: 'planned speed(mm->mm/s)', shadowSize: 0, color: 'blue', data: []},
{points: {show: true, radius: 4, fill: false, symbol: "circle"}, color: 'rgba(0, 255, 0, 0.5)', lines: {show: false}, data: []},
{points: {show: true, radius: 2, fill: false, symbol: "circle"}, color: 'rgba(150, 150, 0, 0.95)', lines: {show: false}, data: []},
{points: {show: true, radius: 5, fill: false, symbol: "circle"}, color: 'rgba(255, 127, 80, 0.5)', lines: {show: false}, data: []},
{label: 'planned max(mm->mm/s)', shadowSize: 0, color: 'rgba(0, 128, 0, 0.5)', data: []}
];
var positionData = [
{label: 'position/time(s->mm)', lines: {show: true}, shadowSize: 0, color: 'red', data: []},
{points: {show: true, radius: 4, fill: false, symbol: "circle"}, color: 'rgba(0, 255, 0, 0.5)', lines: {show: false}, data: []},
{points: {show: true, radius: 2, fill: false, symbol: "circle"}, color: 'rgba(150, 150, 0, 1)', lines: {show: false}, data: []},
{points: {show: true, radius: 5, fill: false, symbol: "circle"}, color: 'rgba(255, 127, 80, 0.5)', lines: {show: false}, data: []},
{points: {show: true, radius: 1, fill: false, symbol: "circle"}, lines: {show: false}, shadowSize: 0, color: 'red', data: []}
];
planSpeed(data);
speedData[1].data.push([0, 0]);
positionData[0].data.push([0, 0]);
positionData[4].data.push([0, 0]);
var steps = 100;
var t = 0;
var st = 0;
var position = 0;
for (i = 0; i < data.length; i++) {
var segment = data[i];
for (var r = 0; r <= steps; r++) {
var point = dataForRatio(segment, r / steps);
var x = (position + segment.length * r / steps);
speedData[1].data.push([x, point.speed]);
positionData[0].data.push([st + point.time, x]);
}
$.each(segment.fragments, function (_, fragment) {
if (fragment.type == 'acceleration' || fragment.type == 'deceleration') {
if (fragment.type == 'acceleration') {
speedData[3].data.push([position + fragment.stopX, Math.sqrt(fragment.toSqSpeed)]);
positionData[2].data.push([t + fragment.duration, position + fragment.stopX]);
}
if (fragment.type == 'deceleration') {
speedData[4].data.push([position + fragment.startX, Math.sqrt(fragment.fromSqSpeed)]);
positionData[3].data.push([t, position + fragment.startX]);
}
}
t += fragment.duration;
});
speedData[0].data.push([position, segment.originalSpeed]);
speedData[5].data.push([position, Math.sqrt(segment.squaredSpeed)]);
position += segment.length;
speedData[0].data.push([position, segment.originalSpeed]);
speedData[5].data.push([position, Math.sqrt(segment.squaredSpeed)]);
st += segment.duration;
positionData[4].data.push([st, position]);
}
var chart1 = $.plot("#chart1", speedData);
var chart2 = $.plot("#chart2", positionData);
var h0 = parseInt('1111000000000000', 2);
var h1 = parseInt('1111111100000000', 2);
var h2 = parseInt('1111111111110000', 2);
var h3 = parseInt('1111111111111111', 2);
var array = [
[h0, 3, 10],
[h1, 2, 6],
[h2, 1, 2],
[h3, 0, 0]
];
function encode(input) {
for (var i = 0; i < array.length; i++)
if ((array[i][0] & input) != 0)
return array[i][1] << 6 | (input >>> array[i][2]);
return input;
}
function decode(input) {
var index = 3 - (input >> 6);
var shift = array[index][2];
var significant = (input & parseInt('00111111', 2));
if (!shift)
return significant;
return ( (significant << 1) | 1) << (shift - 1);
}
var collected = [];
for (i = 0; i < 65535; i++) {
var encoded = encode(i);
var decoded = decode(encode(i));
//console.log(i, encoded, decoded, i.toString(2), encoded.toString(2), decoded.toString(2));
collected.push([i, (i - decoded) / i * 100]);
}
$.plot('#chart3', [
{data: collected}
], {xaxis: {
transform: function (v) {
return Math.log(v + 1);
},
inverseTransform: function (v) {
return Math.exp(v);
}
}});
</script>
</body>
</html>