Allows the parsing and generation of mavlink v1 and v2 packets within a browser environment.
🏠 Homepage
npm i mavlink-browser
npm run test
//initial
import { mavlink20, MAVLink20Processor, Buffer } from 'mavlink-browser';
const mavlinkParser = new MAVLink20Processor(null, 42, 150);
//Establish a connection medium. Ex - WebSerial:
const port = await navigator.serial.requestPort();
await port.open({ baudRate: 57600 });
//Create a message handler to handle incoming mavlink messages
mavlinkParser.on('message',(msg) => {
//Can implement any custom logic to handle incoming messages. Below just logs the message to the console.
console.log(msg)
})
//Can also create a handler for every mavlink message ex:
mavlinkParser.on('HEARTBEAT', (heartbeat) => {
console.log(heartbeat)
})
mavlinkParser.on('MISSION_ACK', (mack) => {
console.log(mack)
})
//Intialize serial port reader
const reader = port.readable.getReader();
while (reader) {
const { value, done } = await reader.read();
if (done) {
reader.releaseLock();
break;
}
// the below line parses the uint8 array and emits a message which is sent to the handler created above
mavlinkParser.parseBuffer(value)
}
//Sending messages:
const writer = port.writable.getWriter();
//Create a command ex arm command:
const armCommand = new mavlink20.messages.command_long(1, 0, mavlink20.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0);
//Format it accordingly and send it through the webserial port
if (writer) {
writer.write(new Buffer(armCommand.pack(mavlinkParser)))
}
👤 Neel Sani
- Website: neels.dev/
- Github: @neelsani
- Email: neel@neels.dev
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide. Or just shoot me an email at neel@neels.dev
Give a ⭐️ if this project helped you!