How to use lua script output sensor value to get ground station

I am a beginner in computer programming, I have a sonar device, not in NMEA format, I have a device connected to SERIAL2, already TTL signal, baud rate 9600, A frame of the value that the sonar sends when it’s working is 31 31 2E 32 37 01 31 34 2E 37 30 08 41 6B 3E C0 41 34 59 20 94 and the preceding 31 31 2E 32 37 is a byte of ASC code 11.27 m, The ones in the middle are meaningless, and the 94 at the end is an accumulated sum. Can I use lua script to output some value to my ground station tag?

1 Like

This example should get you started:


You’ll need a parsing function that returns the range as a number to pass to the gcs:send_named_float() binding. I can possibly help with that, but it may take a few days to get to it.

it’s so kind of you !thank you very much!

Can you provide a datasheet for the sonar?

If not, can you elaborate on the expected serial traffic? Is the range value always 5 characters? Or is the range value always followed by 0x01?

yes it’s always 5 characters ,

It looks like it’s always suffixed by 0x01, which seems to be a better way to break it out of the frame than to count on it being 5 characters.

Also, what is the frame output rate? 1Hz (once per second)?

it’s 2HZ 2times per second

Ok, then this Arduino program should approximate sonar output for the purposes of my own testing without having the actual sonar hardware on hand.

#include <Arduino.h>

uint8_t get_checksum(char *c, size_t size) {
    uint8_t checksum = 0;
    for (size_t i = 0; i < size; i++) {
        checksum += c[i];
    return checksum;

char buffer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x31,
                 0x34, 0x2E, 0x37, 0x30, 0x08, 0x41, 0x6B,
                 0x3E, 0xC0, 0x41, 0x34, 0x59, 0x20, 0x00};

void setup() { Serial.begin(9600); }

void loop() {
    float range_val = (float)random(10000) / 100.0;
    sprintf(buffer, "%05.02f", range_val);
    buffer[5] = 0x01;
    buffer[sizeof(buffer) - 1] = get_checksum(buffer, sizeof(buffer) - 1);

Set the following parameters:

Reboot your autopilot.

Upload the attached script (below) to your autopilot via MAVFTP. Place it in the scripts folder, like so:


Reboot again.

On Mission Planner’s “Quick” tab, you should now be able to select the following values for display. The one labeled “MAV_RNG” is the reported sonar range, per your description. The one labeled “MAV_BAD_F” is debug data, showing how many bad frames have been discarded.


uart_sonar.lua (2.0 KB)

Thank you so much for helping me, I will test the feedback as soon as possible, thank you again for being such a warm-hearted person!

i’m sorry for late, something important things for these days , i have make a test now ,but ,i can’t find the "MAV_RNG"and “MAV_BAD_F” from the "quick"TAB ,IS THERE SOMETHING DIffrifent between the firm or MP ? i’M using MP 1.3.8375.24878 with ardurover V4.3.0-BARA6(A89354A4), can you help me more ? thank you very much!@Yuri_Rage

and i think the script worked,but i cant find mav_rng and mav_bad_f

Check your wiring and signal from the sonar. If no data is received, the values will not populate.

I asked the manufacturer for the manual yesterday and looked at it carefully. It turns out that this device does not always send data. When you send a Binary 01 to the device, the device will only return data once. Dear friends, can you help me to modify the code, thank you very much!! @Yuri_Rage

I can’t really do any development this week, as I am extremely busy. The basics are all there for you in the script I wrote. Just need a minor modification to send 0x01 before polling the UART read buffer.

If you have no success in your own attempts, I can probably help more directly next week.

uart.lua (2.0 KB)
i have changed it but it become…

You don’t want to send text. You want port:write(0x01)

I had a few minutes. This is entirely untested. Try it and see what happens.

uart_sonar_v2.lua (2.2 KB)

Make sure you remove other versions of the script from the SD card, as they will cause conflicts on the serial port. You should have only one script at a time in the /scripts directory for this project.

it works well!! but no mav_bad_f i think all data is good ,no bad data …