### Install Node.js MavLink dependencies Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Command to install the core node-mavlink library and the serialport package for hardware communication. ```bash npm install --save node-mavlink serialport ``` -------------------------------- ### Run ArduPilot simulation with custom UDP output Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Starts the ArduCopter simulator using sim_vehicle.py with a specific UDP output port configuration to facilitate communication with external MAVLink tools. ```bash $ Tools/autotest/sim_vehicle.py -v ArduCopter -f quad --console --map --out udpin:127.0.0.1:14555 ``` -------------------------------- ### Setup MAVESP8266 UDP Telemetry Stream Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Establishes a UDP telemetry stream using the MAVESP8266 class. It initializes the connection, listens for incoming data packets, and demonstrates sending a ParamRequestList message. ```javascript import { MavEsp8266, common } from 'node-mavlink' async function main() { const port = new MavEsp8266() // start the communication await port.start() // log incoming packets port.on('data', packet => { console.log(packet.debug()) }) // You're now ready to send messages to the controller using the socket // let's request the list of parameters const message = new common.ParamRequestList() message.targetSystem = 1 message.targetComponent = 1 // The `send` method is another utility method, very handy to have it provided // by the library. It takes care of the sequence number and data serialization. await port.send(message) } main() ``` -------------------------------- ### Connect to MavLink via TCP stream Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Example of connecting to a remote telemetry module using a TCP socket, which can then be piped into the MavLink parser. ```javascript import { connect } from 'net' const port = connect({ host: '192.168.4.1', port: 2323 }) port.on('connect', () => { console.log('Connected!') }) ``` -------------------------------- ### Send MavLink Messages via Serial Port (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This example shows how to send MavLink messages using the `send` function, supporting both MavLink v1 and v2 protocols. It demonstrates sending a `ParamRequestList` message and a `RequestProtocolVersionCommand` over a serial connection, logging the number of bytes sent for each. ```typescript import { SerialPort } from 'serialport' import { send, MavLinkProtocolV1, MavLinkProtocolV2 } from 'node-mavlink' import { common } from 'node-mavlink' const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) port.on('open', async () => { // Request parameter list from autopilot const message = new common.ParamRequestList() message.targetSystem = 1 message.targetComponent = 1 // Send using v1 protocol (default) const bytesV1 = await send(port, message) console.log(`Sent ${bytesV1} bytes using v1 protocol`) // Send using v2 protocol const bytesV2 = await send(port, message, new MavLinkProtocolV2()) console.log(`Sent ${bytesV2} bytes using v2 protocol`) // Send command to request protocol version const command = new common.RequestProtocolVersionCommand() command.targetSystem = 1 command.targetComponent = 1 command.confirmation = 1 await send(port, command, new MavLinkProtocolV2()) }) ``` -------------------------------- ### Send MavLink commands Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Shows how to instantiate a command class and transmit it over a stream using the MavLink V2 protocol. ```javascript import { MavLinkProtocolV2, send } from 'node-mavlink' const command = new common.RequestProtocolVersionCommand() command.confirmation = 1 port.on('open', async () => { await send(port, command, new MavLinkProtocolV2()) }) ``` -------------------------------- ### Establish TCP MavLink Communication with SITL/ESP-Link (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This snippet demonstrates establishing TCP-based communication with MavLink devices, commonly used with SITL simulators or ESP-Link modules. It handles connection management, packet parsing using a registry, and sending commands. ```typescript import { MavTCP } from 'node-mavlink' import { minimal, common, ardupilotmega, MavLinkPacketRegistry, MavLinkPacket } from 'node-mavlink' const REGISTRY: MavLinkPacketRegistry = { ...minimal.REGISTRY, ...common.REGISTRY, ...ardupilotmega.REGISTRY, } async function main() { const port = new MavTCP() // Connect to SITL simulator or ESP-Link module // Default: 127.0.0.1:5760 const { ip, port: tcpPort } = await port.start('127.0.0.1', 5760) console.log(`Connected to: ${ip}:${tcpPort}`) // Handle incoming packets port.on('data', (packet: MavLinkPacket) => { const clazz = REGISTRY[packet.header.msgid] if (clazz) { const data = packet.protocol.data(packet.payload, clazz) if (packet.header.msgid === common.CommandAck.MSG_ID) { console.log('Command acknowledged:', data) } } }) // Handle connection close port.on('close', () => console.log('Connection closed')) // Send command const command = new common.RequestProtocolVersionCommand() await port.send(command) // Close after timeout setTimeout(() => port.close(), 5000) } main() ``` -------------------------------- ### Send Signed MAVLink v2 Packages Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Shows how to send signed MAVLink v2 packages. It first generates a secure key from a passphrase and then uses the sendSigned utility function to transmit a message with a valid signature. ```javascript import { common, sendSigned } from 'node-mavlink' async function requestParameterList() { const message = new common.ParamRequestList() message.targetSystem = 1 message.targetComponent = 1 await sendSigned(port, message, key) } ``` -------------------------------- ### Verify MAVLink v2 Signed Packages Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Demonstrates how to verify the signature of incoming MAVLink v2 packets using the MavLinkPacketSignature class. It calculates a secret key from a passphrase and checks if the packet's signature matches. ```javascript import { MavLinkPacketSignature } from 'node-mavlink' // calculate secret key (change 'qwerty' to your secret phrase) const key = MavLinkPacketSignature.key('qwerty') // log incoming messages port.on('data', packet => { console.log(packet.debug()) if (packet.signature) { if (packet.signature.matches(key)) { // signature valid } else { // signature not valid - possible fraud package detected } } else { // packet is not signed } }) ``` -------------------------------- ### Configure custom magic number registry for packet parsing Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Initializes a MavLinkPacketSplitter and MavLinkPacketParser with a custom set of magic numbers, allowing for selective parsing of specific MAVLink messages. ```javascript import { MavLinkSplitter, MavLinkParser } from 'node-mavlink' const MY_MAGIC_NUMBERS = { '0': 50, // Heartbeat // ...other magic number definitions go here } const source = ... // obtain source stream const reader = source .pipe(new MavLinkPacketSplitter({}, { magicNumbers: MY_MAGIC_NUMBERS })) .pipe(new MavLinkPacketParser()) ``` -------------------------------- ### Handle MavLink v2 Packet Signing and Verification (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This snippet demonstrates how to generate a signing key from a passphrase and verify the signature of incoming MavLink v2 packets. It uses the MavLinkPacketSignature class for key generation and signature matching, and integrates with SerialPort for data reception. ```typescript import { MavLinkPacketSignature, MavLinkPacketSplitter, MavLinkPacketParser } from 'node-mavlink' import { SerialPort } from 'serialport' // Generate key from secret passphrase const key = MavLinkPacketSignature.key('qwerty') const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) const reader = port .pipe(new MavLinkPacketSplitter()) .pipe(new MavLinkPacketParser()) reader.on('data', packet => { console.log(packet.debug()) // Check if packet is signed if (packet.signature) { // Verify signature against known key if (packet.signature.matches(key)) { console.log('Signature valid - trusted source') console.log(`Link ID: ${packet.signature.linkId}`) console.log(`Timestamp: ${packet.signature.timestamp}`) } else { console.log('Signature INVALID - possible spoofed packet!') } } else { console.log('Packet is not signed') } }) ``` -------------------------------- ### Serialize and Deserialize MavLink Protocols Source: https://context7.com/ardupilot/node-mavlink/llms.txt Demonstrates the use of MavLinkProtocolV1 and MavLinkProtocolV2 classes for packet serialization and deserialization, including support for message signing in V2. ```typescript import { MavLinkProtocolV1, MavLinkProtocolV2, MavLinkPacketSignature } from 'node-mavlink' import { common } from 'node-mavlink' // Create protocol instances with custom system/component IDs const v1Protocol = new MavLinkProtocolV1(254, 1) // sysid=254, compid=1 const v2Protocol = new MavLinkProtocolV2(254, 1) // Serialize a message const message = new common.Heartbeat() message.type = 6 // MAV_TYPE_GCS message.autopilot = 8 // MAV_AUTOPILOT_INVALID message.systemStatus = 4 // MAV_STATE_ACTIVE const v1Buffer = v1Protocol.serialize(message, 0) console.log('V1 packet size:', v1Buffer.length) const v2Buffer = v2Protocol.serialize(message, 0) console.log('V2 packet size:', v2Buffer.length) // Create signed V2 protocol const signedProtocol = new MavLinkProtocolV2(254, 1, MavLinkProtocolV2.IFLAG_SIGNED) const key = MavLinkPacketSignature.key('secret') const unsignedBuffer = signedProtocol.serialize(message, 0) const signedBuffer = signedProtocol.sign(unsignedBuffer, 1, key) console.log('Signed packet size:', signedBuffer.length) // Deserialize header from buffer const header = v2Protocol.header(v2Buffer) console.log(`Message ID: ${header.msgid}, Sequence: ${header.seq}`) ``` -------------------------------- ### Read and parse MavLink packets from a serial port Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Demonstrates how to pipe a serial port stream through a MavLink packet splitter and parser to receive raw packets. ```javascript import { SerialPort } from 'serialport' import { MavLinkPacketSplitter, MavLinkPacketParser } from 'node-mavlink' const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) const reader = port .pipe(new MavLinkPacketSplitter()) .pipe(new MavLinkPacketParser()) reader.on('data', packet => { console.log(packet) }) ``` -------------------------------- ### Manage UDP MavEsp8266 Communication and Auto-IP Discovery (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This code encapsulates UDP communication with MavEsp8266 firmware, handling socket management, packet parsing, and automatic IP discovery. It allows sending both signed and unsigned MavLink messages and includes a registry for parsing common MavLink dialects. ```typescript import { MavEsp8266, MavLinkPacketSignature } from 'node-mavlink' import { minimal, common, ardupilotmega, MavLinkPacketRegistry, MavLinkPacket } from 'node-mavlink' const REGISTRY: MavLinkPacketRegistry = { ...minimal.REGISTRY, ...common.REGISTRY, ...ardupilotmega.REGISTRY, } async function main() { const port = new MavEsp8266() // Start UDP communication (auto-discovers remote IP from first heartbeat) // Parameters: receivePort (default: 14550), sendPort (default: 14555), ip (optional) const { ip, sendPort, receivePort } = await port.start(14550, 14555) console.log(`Connected to: ${ip}, send: ${sendPort}, receive: ${receivePort}`) // Handle incoming packets port.on('data', (packet: MavLinkPacket) => { const clazz = REGISTRY[packet.header.msgid] if (clazz) { const data = packet.protocol.data(packet.payload, clazz) console.log('Received:', data) } }) // Send unsigned message const paramRequest = new common.ParamRequestList() paramRequest.targetSystem = 1 paramRequest.targetComponent = 1 await port.send(paramRequest) // Send signed message const key = MavLinkPacketSignature.key('secret') await port.sendSigned(paramRequest, key, 1, 254, 1) // Clean up setTimeout(() => port.close(), 10000) } main() ``` -------------------------------- ### Utility Functions for MavLink Operations Source: https://context7.com/ardupilot/node-mavlink/llms.txt Provides helper functions for asynchronous operations, including sleep, conditional waiting with timeouts, and data formatting utilities like hex conversion and buffer dumping. ```typescript import { sleep, waitFor, hex, dump } from 'node-mavlink' async function example() { // Sleep for specified milliseconds await sleep(1000) console.log('Waited 1 second') // Wait for condition with timeout let connected = false setTimeout(() => { connected = true }, 500) try { // Wait up to 10 seconds, checking every 100ms (defaults) await waitFor(() => connected, 10000, 100) console.log('Connection established') } catch (error) { console.log('Timeout waiting for connection') } // Format numbers as hex console.log(hex(255)) // "0xff" console.log(hex(255, 4)) // "0x00ff" console.log(hex(255, 2, '')) // "ff" // Dump buffer in readable format const buffer = Buffer.from([0xFD, 0x09, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00]) dump(buffer, 8) } example() ``` -------------------------------- ### Create MavLink Packet Stream from Readable Source (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This function provides a convenient way to create a MavLink packet stream from any readable source, such as a file. It supports optional CRC error handling and custom magic numbers for parsing telemetry logs or other MavLink data streams. ```typescript import { createReadStream } from 'fs' import { createMavLinkStream, MavLinkPacketRegistry } from 'node-mavlink' import { minimal, common, ardupilotmega } from 'node-mavlink' const REGISTRY: MavLinkPacketRegistry = { ...minimal.REGISTRY, ...common.REGISTRY, ...ardupilotmega.REGISTRY, } // Parse a telemetry log file const fileStream = createReadStream('telemetry.mavlink') const reader = createMavLinkStream(fileStream, { onCrcError: (buffer) => { console.error('CRC error in packet:', buffer.toString('hex')) } }) reader.on('data', packet => { const clazz = REGISTRY[packet.header.msgid] if (clazz) { const data = packet.protocol.data(packet.payload, clazz) console.log(data) } }) ``` -------------------------------- ### Parse MavLink Packets from Serial Stream (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This snippet demonstrates how to use MavLinkPacketSplitter and MavLinkPacketParser to process incoming MavLink packets from a serial port. It sets up a transform stream pipeline to split raw data into packets, parse them, and then log the packet details and decoded data using a message registry. ```typescript import { SerialPort } from 'serialport' import { MavLinkPacketSplitter, MavLinkPacketParser, MavLinkPacket } from 'node-mavlink' import { minimal, common, ardupilotmega, MavLinkPacketRegistry } from 'node-mavlink' // Create message registry for parsing const REGISTRY: MavLinkPacketRegistry = { ...minimal.REGISTRY, ...common.REGISTRY, ...ardupilotmega.REGISTRY, } // Open serial connection to autopilot const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) // Create packet processing pipeline const reader = port .pipe(new MavLinkPacketSplitter()) .pipe(new MavLinkPacketParser()) // Handle incoming packets reader.on('data', (packet: MavLinkPacket) => { console.log(packet.debug()) // Output: Packet (proto: MAV_V2, sysid: 1, compid: 1, msgid: 0, seq: 42, plen: 9, crc: 0x1234) const clazz = REGISTRY[packet.header.msgid] if (clazz) { const data = packet.protocol.data(packet.payload, clazz) console.log('Received:', data) } }) ``` -------------------------------- ### Send Signed MavLink v2 Packets (TypeScript) Source: https://context7.com/ardupilot/node-mavlink/llms.txt This code snippet illustrates how to send secure MavLink v2 packets using the `sendSigned` function. It involves generating a cryptographic key from a passphrase using SHA256 and then sending a signed `ParamRequestList` message over a serial port, logging the number of bytes transmitted. ```typescript import { SerialPort } from 'serialport' import { sendSigned, MavLinkPacketSignature } from 'node-mavlink' import { common } from 'node-mavlink' // Generate signing key from passphrase (SHA256) const key = MavLinkPacketSignature.key('your-secret-passphrase') const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) port.on('open', async () => { const message = new common.ParamRequestList() message.targetSystem = 1 message.targetComponent = 1 // Send signed packet with custom parameters // Parameters: stream, message, key, linkId, sysid, compid const bytes = await sendSigned(port, message, key, 1, 254, 1) console.log(`Sent ${bytes} bytes with signature`) }) ``` -------------------------------- ### Decode MavLink message payloads Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Uses a packet registry to map message IDs to specific data classes, allowing for the structured decoding of packet payloads. ```javascript import { MavLinkPacketRegistry, minimal, common, ardupilotmega } from 'node-mavlink' const REGISTRY: MavLinkPacketRegistry = { ...minimal.REGISTRY, ...common.REGISTRY, ...ardupilotmega.REGISTRY, } reader.on('data', packet => { const clazz = REGISTRY[packet.header.msgid] if (clazz) { const data = packet.protocol.data(packet.payload, clazz) console.log('Received packet:', data) } }) ``` -------------------------------- ### Register custom MAVLink message magic number Source: https://github.com/ardupilot/node-mavlink/blob/master/README.md Registers a custom message magic number with the node-mavlink library to ensure the packet splitter correctly calculates the CRC for non-standard messages. ```javascript import { registerCustomMessageMagicNumber } from 'node-mavlink' registerCustomMessageMagicNumber('999999', 42) ``` -------------------------------- ### Register Custom MavLink Magic Numbers Source: https://context7.com/ardupilot/node-mavlink/llms.txt Registers custom magic numbers for CRC validation of non-standard MavLink messages. This is essential when using custom message definitions outside the standard registry. ```typescript import { registerCustomMessageMagicNumber, MavLinkPacketSplitter, MavLinkPacketParser } from 'node-mavlink' import { SerialPort } from 'serialport' // Register magic number for custom message ID 999999 registerCustomMessageMagicNumber('999999', 42) // Alternative: Replace entire magic numbers registry const MY_MAGIC_NUMBERS = { '0': 50, // Heartbeat '999999': 42, // Custom message } const port = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) const reader = port .pipe(new MavLinkPacketSplitter({}, { magicNumbers: MY_MAGIC_NUMBERS })) .pipe(new MavLinkPacketParser()) reader.on('data', packet => { console.log(packet.debug()) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.