This commit is contained in:
micuat 2022-03-07 19:05:34 +01:00
parent 19c56dcd4a
commit d41117f6fe

View file

@ -22,7 +22,7 @@ udpPort.open();
const { fft, util } = require("fft-js"); const { fft, util } = require("fft-js");
const port = new SerialPort({ const port = new SerialPort({
path: '/dev/ttyACM0', path: '/dev/ttyUSB1',
baudRate: 115200, baudRate: 115200,
autoOpen: false, autoOpen: false,
}) })
@ -41,59 +41,50 @@ port.on('open', function () {
// open logic // open logic
}) })
// Read data that is available but keep the stream in "paused mode" let signals = [];
//port.on('readable', function () { let numDevices = 2;
// console.log('Data:', port.read().toString())
//})
let signal = []; for (let i = 0; i < numDevices; i++) {
signals[i] = [];
}
const parser = port.pipe(new DelimiterParser({ delimiter: "\n" })) const parser = port.pipe(new DelimiterParser({ delimiter: "\n" }))
parser.on("data", (data) => { parser.on("data", (data) => {
let xyz = data.toString().replace(/\s/g, "").split(","); let xyzs = data.toString().replace(/\s/g, "").split(",");
if (isNaN(xyz[0]) === false && xyz[0] !== "" let results = [];
&& isNaN(xyz[1]) === false && xyz[1] !== "" if (xyzs.length == 3 * numDevices) {
&& isNaN(xyz[2]) === false && xyz[2] !== "") { for (let i = 0; i < numDevices; i++) {
// console.log(xyz) let signal = signals[i];
let x = parseInt(xyz[0]); let x = parseFloat(xyzs[3 * i + 0]);
let y = parseInt(xyz[0]); let y = parseFloat(xyzs[3 * i + 1]);
let z = parseInt(xyz[0]); let z = parseFloat(xyzs[3 * i + 2]);
signal.push(Math.sqrt(x * x + y * y + z * z)); signal.push(Math.sqrt(x * x + y * y + z * z));
}
else {
// console.log("nan", xyz)
}
if (signal.length > 16) { if (signal.length > 16) {
signal.shift(); signal.shift();
let p = fft(signal); let p = fft(signal);
let mag = util.fftMag(p); let mag = util.fftMag(p);
// console.log(mag.slice(2,3)); let value = Math.min(1, mag[7] * 2);
console.log(Math.min(1, mag[7] / 1000)); results.push(value);
udpPort.send({ udpPort.send({
address: "/ctrl", address: "/ctrl",
args: [ args: [
{ {
type: "s", type: "s",
value: "hello" value: "s" + i
}, },
{ {
type: "f", type: "f",
value: Math.min(1, mag[7] / 1000) value
}, },
] ]
}, "127.0.0.1", 6010); }, "127.0.0.1", 6010);
}
// udpPort.send({ }
// address: "/processing", }
// args: mag.map(m => { if (results.length > 0) {
// return { console.log(results);
// type: "f",
// value: m
// }
// })
// }, "127.0.0.1", 6060);
} }
}); });