function arrayBufferToHex(buffer) {
const hexArray = [];
for (let i = 0; i < buffer.length; i++) {
hexArray.push(buffer[i].toString(16).padStart(2, '0').toUpperCase());
}
return "0x" + hexArray.join(", 0x");
}
const uint8Array = new Uint8Array([0xfd, 0x1c, 0x00, 0x00, 0x7d, 0x01, 0x01, 0x1e, 0x00, 0x00, 0xef, 0xbc, 0x01, 0x00, 0x36, 0x1a, 0xb2, 0x3b, 0x96, 0x0a, 0x11, 0x3e, 0xfc, 0x31, 0x14, 0x3b, 0x90, 0x96, 0xa1, 0xba, 0x69, 0x60, 0x01, 0xbb, 0x88, 0x6e, 0xfa, 0xb9, 0x84, 0xe4]);
const uint8Array1 = new Uint8Array([0xfd, 0x1b, 0x00, 0x00, 0x7c, 0x01, 0x01, 0x21, 0x00, 0x00, 0xee, 0xbc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xec, 0xff, 0xff, 0xff, 0x3c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1e, 0x39]);
const uint8Array2 = new Uint8Array([0xFD, 0x1F, 0x00, 0x00, 0xB6, 0x01, 0x01, 0x01, 0x00, 0x00, 0x2F, 0xFC, 0x70, 0x53, 0x2F, 0x9C, 0x60, 0x52, 0x2B, 0x9C, 0x70, 0x53, 0x52, 0x01, 0x0E, 0x2E, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x22, 0x9D]);
// const msgid = (uint8Array[9]<< 16) | (uint8Array[8]<< 8) | (uint8Array[7])
// console.log("msgid: ", msgid)
// const hexString = arrayBufferToHex(uint8Array);
// console.log(hexString);
var cache_buffer;
var cache_buffer_len = 0;
function parseMavlink(data) {
const data_view = new DataView(data.buffer);
const msgid = (data[9] << 16) | (data[8] << 8) | data[7];
console.log("************************************************");
console.log(arrayBufferToHex(data));
// console.log("msgid: ", msgid);
switch (msgid) {
case 0:
console.log("heartBeat");
break;
case 1:
console.log("system status");
console.log("voltage_battery: " + data_view.getUint16(22, true));
break;
case 0x1E:
console.log("attitude");
console.log("time_boot_ms: " + data_view.getUint32(10, true));
console.log("roll: " + data_view.getFloat32(14, true));
console.log("pitch: " + data_view.getFloat32(18, true));
console.log("yaw: " + data_view.getFloat32(22, true));
console.log("rollspeed: " + data_view.getFloat32(26, true));
console.log("pitchspeed: " + data_view.getFloat32(30, true));
console.log("yawspeed: " + data_view.getFloat32(34, true));
break;
case 0x21:
console.log("global position");
console.log("time_boot_ms: " + data_view.getUint32(10, true));
console.log("lat: " + data_view.getInt32(14, true));
console.log("lon: " + data_view.getInt32(18, true));
console.log("alt: " + data_view.getInt32(22, true));
console.log("relative_alt: " + data_view.getInt32(26, true));
console.log("vx: " + data_view.getInt16(30, true));
console.log("vy: " + data_view.getInt16(32, true));
console.log("vz: " + data_view.getInt16(34, true));
console.log("hdg: " + data_view.getUint16(36, true));
break;
case 0xB2:
console.log("AHRS2");
break;
}
console.log("************************************************\n");
}
function parseBuffer(buffer) {
for (let i = 0; i < buffer.length; i++) {
if (buffer[i] == 0xfd) {
mavlink_data_len = buffer[i + 1] + 12;
if (i + mavlink_data_len <= buffer.length) {
mavlink_data = buffer.slice(i, i + mavlink_data_len)
parseMavlink(mavlink_data);
i += mavlink_data_len - 1;
}
else {
cache_buffer = new Uint8Array(buffer[i + 1] + 12);
cache_buffer[0] = 0xfd;
cache_buffer_len = 1;
}
}
else if (cache_buffer_len > 0) {
cache_buffer[cache_buffer_len] = buffer[i];
cache_buffer_len++;
if (cache_buffer_len == cache_buffer[1] + 12) {
parseMavlink(cache_buffer);
cache_buffer_len = 0;
}
}
}
}
parseBuffer(uint8Array);
parseBuffer(uint8Array1);
parseBuffer(uint8Array2);