TypeScript Node MavLink library

416 views
Skip to first unread message

Matthias Hryniszak

unread,
May 3, 2021, 2:48:34 PM5/3/21
to MAVLink
Hello,

I'm new here, so I'll maybe start with a short introduction. My name is Matthias. I am a software developer with a little over 30 years under my belt. Recently I became fascinated with extending the UAV ecosystem (for Ardupilot and INAV) trying to put some of my experience back into the projects that gave me a lot of joy over the last decade or more.

And so I've tried to use the generated sources for JavaScript, but it has proven to be quite difficult. And so I have decided to give it a shot and write my own library that allows to read and write MavLink messages.


From what I can tell this is the first package for node.js that doesn't require generating sources from the original package and at the same time gives a bit of documentation available right in the code editor such as VSCode using JSDoc

Would it be possible to include it in the list of available options?

Best regards,
Matthias

Bart Hodlik

unread,
May 4, 2021, 12:22:22 PM5/4/21
to MAVLink
Hey Matthias,

This is very interesting. I've been working with Node Red to split mavlink messages that have been converted to JSON through mavlink2rest. 
Have you implemented this node in Node Red at all?

Matthias Hryniszak

unread,
May 4, 2021, 12:37:23 PM5/4/21
to MAVLink
Hi,

no - this is a brand new, no-external-dependencies approach. Can you shoot me a link to the Node Red?

As for converting the messages to JSON that'd be dead simple: just call JSON.stringify(data) and you'd be done. The only exception are BigInt (uint64_t, int64_t) fields which are not directly serializable to JSON. You'd need to do a bit of preprocessing of the objects to figure out how to serialize them properly.

If you'd like to do that preprocessing you can access the definition of list of fields using the packet.protocol.constructor.FIELDS collection. This will tell you the field name and its type (sort of reflection metadata) and you'll have it done in a few lines of code:

function convertToJson(data) {
  const FILTERED_FIELD_TYPES = [
    'int64_t', 'uint64_t', 'int64_t[]', 'int64_t[]'
  ]

  const result = data.constructor.FIELDS
    .filter(field => !FILTERED_FIELD_TYPES.includes(field.type))
    .reduce((acc, field) => ({ ...acc, [field.name]: data[field.name] }), {})

  return JSON.stringify(result)
}

Of course instead of filtering you could figure some other way of managing those fields, maybe truncating the value or converting it to string - kinda depends on what you're after.

Hope that helps.

M.

Bart Hodlik

unread,
May 5, 2021, 1:05:59 PM5/5/21
to MAVLink
Here's node-red: https://nodered.org/. Its built on Node.js
Here's the project that is converting MAVLINK to JSON: https://github.com/patrickelectric/mavlink2rest

Hamish Willee

unread,
May 5, 2021, 7:29:27 PM5/5/21
to MAVLink
Hi Matthias,


> Would it be possible to include it in the list of available options?

Yes. Please create a PR against the docs here: https://github.com/mavlink/mavlink-devguide/blob/master/en/README.md#external-generatorslanguages - and add a link to your howto guide in https://mavlink.io/en/getting_started/use_libraries.html

We don't do a lot of validation for the external libraries, so to help users, in the table please provide links to docs, tests etc as done by the other cases. 

Last of all, if there is any way to incorporate the benefits of your changes into the mavgen generator then I urge you to do so. That gets the benefit of being tested against every change made to the source XML and also potentially against lots of "generic" tests.

Cheerio
Hamish

Matthias Hryniszak

unread,
May 6, 2021, 7:10:39 PM5/6/21
to MAVLink
About that Node Red thing and splitting messages. It looks like what you're trying to do is to read MavLink messages via a websocket. That can easily be accomplished with node-mavlink and a few lines of JavaScript using the ws package.

If you're still interested in it I can create a project that will do just that what the mavlink2rest library does (just using Node.js rather than Python). You could then judge if it suits you better.

Just let me know if you're interested and what you actual use case is so that I can tailor the solution more towards your needs.


Cheers,
M.

Reply all
Reply to author
Forward
0 new messages