-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (94 loc) · 2.66 KB
/
index.js
File metadata and controls
116 lines (94 loc) · 2.66 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
var THREE = require('three');
var Controller = require('velocitymodel').VelocityModel;
var K_P = 5;
var K_D = 5;
var DEFAULT_DT = 50;
var DEFAULT_ID = 'model';
var scene;
var camera;
var device;
var renderer;
var alphaController;
var betaController;
var gammaController;
function buildDevice() {
var geometry = new THREE.BoxGeometry(10, 20, 1.5);
var material = new THREE.MeshBasicMaterial({
vertexColors: THREE.FaceColors,
overdraw: 0.5
});
for (var i = 0; i < geometry.faces.length; i += 2) {
var color = Math.random() * 0xffffff;
geometry.faces[i].color.setHex(color);
geometry.faces[i + 1].color.setHex(color);
}
return new THREE.Mesh(geometry, material);
}
function build3DModel(id, w, h) {
var container = document.getElementById(id);
var width = w || container.offsetWidth;
var height = h || width;
camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
camera.position.y = -25;
device = buildDevice();
scene = new THREE.Scene();
scene.add(device);
camera.lookAt(scene.position);
camera.updateMatrixWorld();
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xffffff);
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
}
function toRad(degrees) {
return degrees * Math.PI / 180;
}
function render() {
device.rotation.x = 0;
device.rotation.y = 0;
device.rotation.z = 0;
device.rotateZ(alphaController.update());
device.rotateX(betaController.update());
device.rotateY(gammaController.update());
renderer.render(scene, camera);
}
DeviceModel.prototype.getAxisAngles = function() {
return {
x: device.rotation.x,
y: device.rotation.y,
z: device.rotation.z
};
};
DeviceModel.prototype.setEulerAngles = function(alpha, beta, gamma) {
alphaController.setTarget(toRad(alpha));
betaController.setTarget(toRad(beta));
gammaController.setTarget(toRad(gamma));
if (!gammaController.inside(gammaController.PID.target)) {
alphaController.pos = alphaController.PID.target;
alphaController.v = 0;
betaController.pos = betaController.PID.target;
betaController.v = 0;
}
};
function DeviceModel(id, width, height, dt) {
var ID = id || DEFAULT_ID;
var DT = dt || DEFAULT_DT;
alphaController = new Controller(K_P, K_D, DT / 1000, {
min: 0,
max: 2 * Math.PI,
circular: true
});
betaController = new Controller(K_P, K_D, DT / 1000, {
min: -Math.PI,
max: Math.PI,
circular: true
});
gammaController = new Controller(K_P, K_D, DT / 1000, {
min: -Math.PI / 2,
max: Math.PI / 2,
circular: true
});
build3DModel(ID, width, height);
setInterval(render, DT);
}
module.exports = DeviceModel;