-
Notifications
You must be signed in to change notification settings - Fork 0
/
threejs_vr_vl53l1x_handheld_plot.html
200 lines (135 loc) · 5.11 KB
/
threejs_vr_vl53l1x_handheld_plot.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
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
<!--
threejs_vr_vl53l1x_handheld_plot.html
https://github.com/Physicslibrary/Threejs-VR-Sensors
MIT License
Copyright (c) 2020 Hartwell Fong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
June 2020.
A project to explore interfacing sensors into virtual reality with a Raspberry Pi.
This involves programming (C, python, javascript) I2C, SPI, and GPIO pins on a
Raspberry Pi. Data read from sensors are sent to wifi-connected devices
(eg. Oculus Quest) using websocket.
System requirements
Raspberry Pi with wireless LAN (tested Raspberry Pi 3 Model B+, Pi 3 Model A+, Pi Zero W).
Oculus Quest recommended (6DoF headset/controllers).
Oculus Browser (tested Quest Update >16.0 and three.js r115).
Line "var ws = new WebSocket" is commented out. This websocket client is hosted on a Raspberry Pi.
References
https://www.raspberrypi.org/
Derek Molloy, Exploring Raspberry Pi, John Wiley & Sons (2016).
https://threejs.org/
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VL53L1X Time-of-Flight Sensor</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script type="module">
import * as THREE from '../build/three.module.js';
import { VRButton } from './jsm/webxr/VRButton.js';
var camera, scene, renderer;
var distance = [];
var i;
var N = 64;
var controller;
var controller2;
var controller_box;
var controller_box2;
var object;
init();
animate();
function init() {
// Uncomment one
// var ws = new WebSocket('wss://127.0.0.1:8000'); // Raspberry Pi testing
// var ws = new WebSocket('wss://192.168.4.1:8000'); // Oculus Quest testing
ws.onopen = function() {
console.log("websocket onopen");
};
ws.onclose = function() {
console.log("websocket onclose");
};
// websocketd sends distance reading of VL53L1X from Raspberry Pi 3
ws.onmessage = function(event) {
if(i>N-1){i=0;}
distance[i].position.y = parseFloat(event.data)/2-0.5;
// console.log(distance[i].position.y); // console output to Raspberry Pi Chromium
i++;
}
scene = new THREE.Scene();
scene.background = new THREE.Color(0x505050);
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.body.appendChild(VRButton.createButton(renderer));
renderer.xr.enabled = true;
controller = renderer.xr.getController(0); // right hand controller
scene.add(controller);
controller2 = renderer.xr.getController(1); // left hand controller
scene.add(controller2);
var box = new THREE.BoxBufferGeometry(0.05,0.05,0.05);
controller_box = new THREE.Mesh(box, new THREE.MeshBasicMaterial({color: 0xffffff}));
controller_box.material.wireframe = true;
controller_box2 = new THREE.Mesh(box, new THREE.MeshBasicMaterial({color: 0xffffff}));
controller_box2.material.wireframe = true;
controller.add(controller_box);
controller2.add(controller_box2);
object = new THREE.Object3D();
// Setup 64 boxes for plotting temperature along x-axis
box = new THREE.BoxBufferGeometry(0.01,0.01,0.01);
for(i=0;i<N;i++){
distance[i] = new THREE.Mesh(box, new THREE.MeshBasicMaterial({color: 0x00ff00}));
distance[i].material.wireframe = true;
distance[i].position.x = 2*i/N-1;
distance[i].position.y = 0.0;
distance[i].position.z = 0.0;
object.add(distance[i]);
}
var geometry = new THREE.PlaneGeometry(2,1,4);
var material = new THREE.MeshBasicMaterial( {color: 0x0e0e0e, side: THREE.DoubleSide} );
var frame = new THREE.Mesh(geometry,material);
frame.position.x = -0.01;
frame.position.y = 0.0;
frame.position.z = 0,0;
object.add(frame);
object.scale.x = 0.3;
object.scale.y = 0.3;
object.scale.z = 0.3;
object.rotation.x = -Math.PI/4;
controller.add(object);
i = 0;
// add a floor
var color = new THREE.Color(0x8ec683);
var floor = new THREE.GridHelper(8,8,color,color);
scene.add(floor);
}
function animate() {
renderer.setAnimationLoop(render);
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>