Skip to content

Commit

Permalink
Improve controller code
Browse files Browse the repository at this point in the history
  • Loading branch information
Finchiedev committed Nov 5, 2018
1 parent b3ae915 commit f222c29
Showing 1 changed file with 48 additions and 35 deletions.
83 changes: 48 additions & 35 deletions App/JS/client.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@

var net = require('net')
var client = new net.Socket()
var net = require('net');
var client = new net.Socket();

function terminateConnection(connection) {
connection.write("01-0000")
connection.write("02-0000")
connection.write("03-0000")
connection.write("04-0000")

connection.write("010000");
connection.write("020000");
connection.write("030000");
connection.write("040000");
}

function axisValue(dynamixel, axis) {
var gamepad = navigator.getGamepads()[0]
var val = Math.round((gamepad.axes[axis]+1) * 512)
//if (dynamixel == 1 || dynamixel == 3) {
// val -= 1024
//}
var out = ""
var gamepad = navigator.getGamepads()[0];
var val = Math.round((gamepad.axes[axis]+1) * 512);

if (val < 0 || val > 1024) {
console.error("Controller value is out of bounds! (" + val + ")");
}

//Logic to make controller value proper
if (val <= 512) {
val = (val - 512) * -2;
} else if (val < 512) {
val <<= 1;
}

var out = "";
if (val < 10) {
out += "000"
out += val
out += "000";
out += val;
} else if (val < 100) {
out += "00"
out += val
out += "00";
out += val;
} else if (val < 1000) {
out += "0"
out += val
out += "0";
out += val;
} else {
out += val
out += val;
}
return out

return out;
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
}

async function pollGamepad(interval) {
while (true) {
client.write('01-' + axisValue(1, 3))
await sleep(interval)
client.write('02-' + axisValue(2, 1))
await sleep(interval)
client.write('03-' + axisValue(3, 3))
await sleep(interval)
client.write('04-' + axisValue(4, 1))
while (true) {
console.log(axisValue(2, 1));
client.write('01-' + axisValue(1, 3));
await sleep(interval);
client.write('02' + axisValue(2, 1));
await sleep(interval);
client.write('03-' + axisValue(3, 3));
await sleep(interval);
client.write('04' + axisValue(4, 1));
}
}

client.connect(9999, '192.168.100.1', function() {
window.addEventListener("gamepadconnected", function(event) {
pollGamepad(100)
})
})
window.addEventListener("gamepadconnected", function(event) {
pollGamepad(100);
});
});

// client.on('close', function(){
// console.warn("Client terminated connection!")
Expand Down

0 comments on commit f222c29

Please sign in to comment.