Add data from a serial device to mavlink telemetry

I have a device that is taking measurements and communicating over a serial UART. My goal is to send the measurement data down to the ground as part of the standard telemetry stream, where I can parse it and use it in a separate application. I would like to grab that data from the device, parse it at the rate it is coming in, and then put an averaged value (at a lower rate) into the standard telemetry stream that comes down to the GCS.

I have successfully set up my device and connected it to a UART port (telem2), and hacked a library (e.g. AP_RangeFinder_LightWareSerial) example to grab the data, parse it, and spit it out to the USB console. My next step is to put this into copter. My questions are:

  1. Where is the best place in the code to add the call to this non-flight critical measurement
  2. How do I add that to the standard telemetry message to the GCS

After this, I’ll be working on the GCS side of things, and will need to parse the data from the telemetry stream. Is it possible to do this while QGC is running, or will it be blocking the port?


use lua scripting to do that. Most times there is no need to change the C++ code at all.

While I was able to get data through telem2 by hacking the example library sketch (it was actually GPS_UBLOX_passthrough), I cannot get it to work within copter. Right now, I am trying a simple step where I am trying to read from within GCS_Mavlink and dump it in to an existing message. I understand that I’ll want the read loop to be somewhere else, but I’m just trying baby steps. When I try something like hal.uartD->read() from here, I get back -1, or hal.uartD->available, I get back 0. I also tried hal.uartD->is_initialized(), and get back 1. I’m debugging this by adding those results to a 3Hz float message field and reading in QGC.

My guess is that something else is reading this buffer before I get to it in this code. I have Serial2 (which should be uartD) set up to the right baud rate and ‘mavlink’. It is actually spitting out ascii, but I have to set the protocol to something.

Any help? Thanks!

If it’s a device that’s likely to have broader demand than just your use case, feel free to make a PR for it, and against Mavlink. If it’s a narrow/bespoke use case, as Amilcar suggests it’s better to use lua so you don’t have to maintain a fork.