Skip to content

Commit

Permalink
0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
OCSYT committed Jan 24, 2024
1 parent e8a1d39 commit a101ce9
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 32 deletions.
53 changes: 46 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const fs = require('fs');
const path = require('path');

const mainPath = app.isPackaged
? path.dirname(app.getPath('exe'))
: __dirname;
? path.dirname(app.getPath('exe'))
: __dirname;

const jsonFilePath = path.join(mainPath, 'store.json');

Expand Down Expand Up @@ -203,9 +203,14 @@ ipcMain.on('sendData', (event, postData) => {
PACKET_COUNTER += 1;
buildRotationAndSend(postData["rotation"], deviceid);
PACKET_COUNTER += 1;
//buildBatteryAndSend(postData["battery"], deviceid);
//PACKET_COUNTER += 1;

if (postData["yawReset"] == true) {
sendYawReset();
PACKET_COUNTER += 1;
}
if (deviceid == 0) {
sendBatteryLevel(postData["battery"]);
PACKET_COUNTER += 1;
}
const currentTimestamp = Date.now();
const timeDifference = currentTimestamp - lastTimestamp;
if (deviceid == 0) {
Expand All @@ -220,6 +225,40 @@ ipcMain.on('sendData', (event, postData) => {
}
});

function sendYawReset() {
var buffer = new ArrayBuffer(128);
var view = new DataView(buffer);
view.setInt32(0, 21);
view.setBigInt64(4, BigInt(PACKET_COUNTER));
view.setInt8(12, 3);
sendBuffer = new Uint8Array(buffer);
sock.send(sendBuffer, 0, sendBuffer.length, SLIME_PORT, SLIME_IP, (err) => {
if (err) {
console.error(`Error sending packet for sensor ${trackerId}:`, err);
} else {

}
});
}

function sendBatteryLevel(batteryLevel) {
var buffer = new ArrayBuffer(128);
var view = new DataView(buffer);
view.setInt32(0, 12);
view.setBigInt64(4, BigInt(PACKET_COUNTER));
view.setFloat32(12, 0);
view.setFloat32(16, batteryLevel);
sendBuffer = new Uint8Array(buffer);
sock.send(sendBuffer, 0, sendBuffer.length, SLIME_PORT, SLIME_IP, (err) => {
if (err) {
console.error(`Error sending packet for sensor ${trackerId}:`, err);
} else {

}
});
}


function buildAccelPacket(ax, ay, az, trackerID) {
let buffer = new Uint8Array(128);
let view = new DataView(buffer.buffer);
Expand All @@ -245,7 +284,7 @@ function buildAccelAndSend(acceleration, trackerId) {

sock.send(buffer, 0, buffer.length, SLIME_PORT, SLIME_IP, (err) => {
if (err) {
console.error(`Error sending acceleration packet for sensor ${trackerId}:`, err);
console.error(`Error sending packet for sensor ${trackerId}:`, err);
} else {

}
Expand Down Expand Up @@ -282,7 +321,7 @@ function buildRotationAndSend(rotation, trackerId) {

sock.send(buffer, 0, buffer.length, SLIME_PORT, SLIME_IP, (err) => {
if (err) {
console.error(`Error sending rotation packet for sensor ${trackerId}:`, err);
console.error(`Error sending packet for sensor ${trackerId}:`, err);
} else {

}
Expand Down
12 changes: 11 additions & 1 deletion tracker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ <h3>Settings</h3>
</div>
<div class="content">
<br>
<h3>SlimeTora 0.0.3</h3>
<h3>SlimeTora 0.0.4</h3>
<p>Developed by BracketProto</p>
<p>https://bracketproto.com/</p>
<p>https://github.com/OCSYT/SlimeTora</p>
<br>
<br>
<div class="credits">
<p>Other credits:</p>
<p>Haritora SlimeVR Bridge: https://github.com/sim1222/haritorax-slimevr-bridge</p>
<P>Quaternion.js: https://github.com/rawify/Quaternion.js</P>
<p>SlimeVR Sender Example: https://github.com/SlimeVR/SlimeVR-Sender-Example/</p>
<p>Moslime: https://github.com/moslime/moslime/</p>
<p>SlimeVR Discord server: https://discord.gg/SlimeVR</p>
</div>
<br>
</div>
<script src="./quaternion.js"></script>
<script src="./script.js">
Expand Down
83 changes: 64 additions & 19 deletions tracker/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ function lerp(start, end, amt) {

function interpolateIMU(currentData, newData, t) {

if(t == 1){
if (t == 1) {
return newData;
}

const currentrot = new Quaternion(currentData["rotation"]);
const newrot = new Quaternion(newData["rotation"]);
const interpolatedQuaternion = currentrot.slerp(newrot)(t);
console.log(interpolatedQuaternion);
const interpolatedData = {
deviceName: newData.deviceName,
deviceId: newData.deviceId,
Expand All @@ -124,7 +123,8 @@ function interpolateIMU(currentData, newData, t) {
y: lerp(currentData.acceleration.y, newData.acceleration.y, t),
z: lerp(currentData.acceleration.z, newData.acceleration.z, t),
},
battery: newData.battery
battery: newData.battery,
yawReset: newData.yawReset
};

return interpolatedData;
Expand All @@ -147,10 +147,12 @@ async function connectToDevice() {

const battery_service = await server.getPrimaryService(batteryId);
const sensor_service = await server.getPrimaryService(sensorServiceId);
const setting_service = await server.getPrimaryService(settingId);
const device_service = await server.getPrimaryService(deviceInfoId);
console.log('Connected to device:', device.name);

const devicelist = document.getElementById("devicelist");
const deviceelement = document.createElement("p");
const deviceelement = document.createElement("div");
const iframe = document.createElement('iframe');

// Set attributes for the iframe
Expand All @@ -166,23 +168,48 @@ async function connectToDevice() {
deviceelement.id = device.name;
devicelist.appendChild(deviceelement);
trackerdevices[device.id] = device;
battery[device.id] = 0;
trackercount.innerHTML = "Connected Trackers: " + Object.values(trackerdevices).length;
const sensor_characteristic = await sensor_service.getCharacteristic('00dbf1c6-90aa-11ed-a1eb-0242ac120002');
const battery_characteristic = await battery_service.getCharacteristic('00002a19-0000-1000-8000-00805f9b34fb');
const button_characteristic = await sensor_service.getCharacteristic('00dbf450-90aa-11ed-a1eb-0242ac120002');
var sensor_value = await sensor_characteristic.readValue();
var battery_value = await battery_characteristic.readValue();
var button_value = (await button_characteristic.readValue()).getInt8(0);
var new_button_value = null;
var button_enabled = false;
var postDataCurrent = null;
var postData = null;
var allowyawreset = false;
const updateValues = async () => {
sensor_value = await sensor_characteristic.readValue();
battery_value = await battery_characteristic.readValue();
new_button_value = (await button_characteristic.readValue()).getInt8(0);
if (button_value != new_button_value) {
button_enabled = true;
}
else {
button_enabled = false;
}
button_value = new_button_value;
setTimeout(updateValues, 16.7);
}
updateValues();
const trackercheck = setInterval(async () => {

const battery = decodeBatteryPacket(device, battery_value)[1];
var yawreset = false;
if (button_enabled == true && allowyawreset == true) {
allowyawreset = false;
yawreset = true;
}
else {
if (button_enabled == false) {
allowyawreset = true;
}
}

battery[device.id] = decodeBatteryPacket(device, battery_value)[1];;
const lowestbattery = Math.min(...Object.values(battery));

const IMUData = decodeIMUPacket(device, sensor_value);
const iframe = document.getElementById(device.id + "threejs");
Expand All @@ -201,7 +228,8 @@ async function connectToDevice() {
y: IMUData[2].y,
z: IMUData[2].z
},
battery: battery
battery: lowestbattery,
yawReset: yawreset
};


Expand All @@ -220,35 +248,52 @@ async function connectToDevice() {
rotationW: postDataCurrent["rotation"].w
}, '*');

const rotation_Euler = quaternionToEulerAngles(postDataCurrent["rotation"]);
deviceelement.innerHTML =
"Device name: " + postDataCurrent["deviceName"] + "<br>" +
"Device ID: " + postDataCurrent["deviceId"] + "<br>" +
"Rotation: " + rotation_Euler.x.toFixed(0) + ", " + rotation_Euler.y.toFixed(0) + ", " + rotation_Euler.z.toFixed(0) + "<br>"
+ "Accel: " + postDataCurrent["acceleration"].x.toFixed(0) + ", " + postDataCurrent["acceleration"].y.toFixed(0) + ", " + postDataCurrent["acceleration"].z.toFixed(0) + ", " + "<br>"
+ "Battery: " + battery * 100 + "% <br><br>";
// rotation is given in radians
const rotation = new Quaternion([postDataCurrent["rotation"].w, postDataCurrent["rotation"].x, postDataCurrent["rotation"].y, postDataCurrent["rotation"].z]);
const rotation_Euler_raw = rotation.toEuler("XYZ");

// Convert radians to degrees
const rotation_Euler = {
x: rotation_Euler_raw[0] * (180 / Math.PI),
y: rotation_Euler_raw[1] * (180 / Math.PI),
z: rotation_Euler_raw[2] * (180 / Math.PI)
};

const deviceName = postDataCurrent["deviceName"];
const deviceId = postDataCurrent["deviceId"];
const { x: rotX, y: rotY, z: rotZ } = rotation_Euler;
const { x: accelX, y: accelY, z: accelZ } = postDataCurrent["acceleration"];
const batteryPercentage = (battery[device.id] * 100);

// Build the HTML content
const content =
"<strong>Device name:</strong> " + deviceName + "<br>" +
"<strong>Device ID:</strong> " + deviceId + "<br>" +
"<strong>Rotation:</strong> X: " + rotX.toFixed(0) + ", Y: " + rotY.toFixed(0) + ", Z: " + rotZ.toFixed(0) + "<br>" +
"<strong>Acceleration:</strong> X: " + accelX.toFixed(0) + ", Y: " + accelY.toFixed(0) + ", Z: " + accelZ.toFixed(0) + "<br>" +
"<strong>Battery:</strong> " + batteryPercentage + "% <br><br>";
deviceelement.innerHTML = content;

}, 16.7);

device.addEventListener('gattserverdisconnected', (event) => {
clearInterval(trackercheck);
deviceelement.remove();
delete trackerdevices[device.id];
delete battery[device.id];
iframe.remove();
ipc.send("disconnect", device.name);
trackercount.innerHTML = "Connected Trackers: " + Object.values(trackerdevices).length;
trackercount.innerHTML = "Connected Trackers: " + Object.values(trackerdevices).length;
});

} catch (error) {
console.log(error);
//console.log(error);
}
}

battery = {};
trackerdevices = {};

function quaternionToEulerAngles(q) {
return { x: q.x * 100, y: q.y * 100, z: q.z * 100 }; //just removing w axis for now and multiplying by 100
}

function decodeBatteryPacket(device, data) {
const dataView = new DataView(data.buffer);
const batteryLevel = dataView.getInt8(0, true) / 100.0;
Expand Down
3 changes: 3 additions & 0 deletions tracker/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ body {
justify-content: center;
text-align: center;
}
.credits{
font-size: 15px;
}

#devicelist {
background-color: #081e30;
Expand Down
5 changes: 0 additions & 5 deletions tracker/visualization.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@

renderer.render(scene, camera);
};
function rotateObject(object, degreeX = 0, degreeY = 0, degreeZ = 0) {
object.rotateX(THREE.MathUtils.degToRad(degreeX));
object.rotateY(THREE.MathUtils.degToRad(degreeY));
object.rotateZ(THREE.MathUtils.degToRad(degreeZ));
}

window.addEventListener('message', (event) => {
if (event.data.type === 'rotate') {
Expand Down

0 comments on commit a101ce9

Please sign in to comment.