How can I add a mini sonar sensor GL041MT GL042MT to my ArduRover firmware?

I will try to output the data in nmea format. My order will arrive in 10 days.

1 Like

Hi! @geofrancis @McKey
I’m currently unable to concentrate on my work due to my busy schedule.
I’ll test it and report it to you during the week you come back.
But the biggest problem is that I2C communication between pixhawk and arduino is not smooth.
It does not seem that pixhawk is performing I2C request normally.

1 Like

I have made a mavlink adapter, it should work when connected to a mavlink 2 serial port @115200 and set rangefinder 1 type to mavlink (10), place the mavlink folder in the same folder as the ino file.

2 Likes

I was just looking over the code for the JSN-SRT04T, something about the GL041MT example looked very familiar.

this is what i use in the i2c adapter for the JSNSR04T

      CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
         if (data_buffer[3] == CS) {
        distances = 0.1 * ((data_buffer[1] << 8) + data_buffer[2]);

and this is what the example for the GL041MT looks like

 CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        Distance = (buffer_RTT[1] << 8) + buffer_RTT[2];

its the same sonar just running at 115200 instead of 9600.

In theory that means that this lua script could also work to read the sonar directly into the flight controller without arduino if you have a H7 board.

local SCRIPT_NAME          = 'Depthfinder'
local MAV_DEPTH_LABEL      = 'DEPTH'
local BOOT_DELAY_MS        = 2000
local RUN_INTERVAL_MS      =  50
local SERIAL_PORT          =    0
local BAUD_RATE            = 115200
local FRAME_SIZE           =    4
local SPEED_RATIO          =    1
local INVALID_DEPTH        =   -1
local MAV_SEVERITY_WARNING =    4
local MAV_SEVERITY_INFO    =    6
local port = serial:find_serial(SERIAL_PORT)

port:begin(BAUD_RATE)
port:set_flow_control(0)

local function serial_flush(bytes)
    for i = 0, bytes - 1 do
        port:read()
    end
end

function update()
    local bytes_available = port:available():toint()

    if bytes_available ~= FRAME_SIZE then
        serial_flush(bytes_available)
        gcs:send_named_float(MAV_DEPTH_LABEL, INVALID_DEPTH)
        return update, RUN_INTERVAL_MS
    end

    local data = 0
    for i = 0, bytes_available - 1 do
        data = data << 8 | port:read()
    end

    local sum    = data & 0xFF
    local l_data = data >>  8 & 0xFF
    local h_data = data >> 16 & 0xFF

    if (0xFF + h_data + l_data) & 0xFF ~= sum then
        gcs:send_text(MAV_SEVERITY_WARNING,
                      string.format('%s: Bad checksum', SCRIPT_NAME))
        gcs:send_named_float(MAV_DEPTH_LABEL, INVALID_DEPTH)
        return update, RUN_INTERVAL_MS
    end

    local depth_raw = h_data << 8 | l_data
    gcs:send_named_float('DEPTH', depth_raw)
        if terrainSensor ~= nil then
            terrainSensor:handle_script_msg(depth_raw / 100)
            gcs:send_text(6,"rangefinder")
        end
    return update, RUN_INTERVAL_MS
end

gcs:send_text(MAV_SEVERITY_INFO, string.format('%s: Script Active', SCRIPT_NAME))

return update, BOOT_DELAY_MS

How should we test it?

Like this?

Connect the RX and TX of the sonar sensor to Arduino ports 10/11,
Which port should I connect to Pixhawk?

image

1 Like

Yes, it should work like the example for now, so it should send the distance then a mavlink packet, so if you connect with usb to verify that it’s sending data first then if it is connect the tx Rx to a pixhawk serial port.

Unknown special characters are written.
And, Arduino’s RX Led flashes at the same time interval. And, when the Mission planner has set up, a message called Rangefinder 1 No Data is displayed.

the unknown characters are mavlink, there should be a line with the sonar readings on it too.

can you send a screenshot of the data from the adapter?

with it connected to a pixhawk serial port, check on mission planner for data, press CTRL+T then open mavlink inspector from the options, look and see if any rangefinder data is getting sent back .

According to the listing it should be possible to change the sensor serial baud rate, if we can change it to 9600 then its essentially identical to the JSN SR04T and all the issues with i2c go away. I will test this when mine arrives.

I’ll upload the screenshot tomorrow. Please wait a little bit.

1 Like

The information seems to be confirmed in Mavlink Inspector.
But the Arduino serial monitor shows something strange. Is it because of the binary system?
But likewise, No Data

1 Like

on Arduino serial monitor you are seeing the mavlink data followed by the serial print.

is current distance reading changing on mavlink inspector?

can you change line 57 to

  uint8_t id = 1;                              /*< Onboard ID of the sensor*/

can you set

rngfnd1_type: 10 (mavlink)
rngfnd1_orient : 25 (Down)
rngfnd1_addr: 1

Thank you friend.
Thanks to you, everything has been resolved.

Thanks to your hard work, what I wanted was resolved.

Is there a way we can share what we’ve solved together?





1 Like

I have shared the code here, I renamed this thread, so people can find it easier in the future.

I will also revise the name of this topic a little simpler and more accurately for others.
Thank you for your hard work.

I have made another version of the adapter, this should convert it to a type 5 PWM rangefinder that can connect to any servo channel.

1 Like

I would fit the GL041 MT into my sub because its a.) interesting to look whats below and there is b.) still a bit of space left in the free flooding stern section… Unfortunately Ali Express doesnt sell it here in Germany. Is anyone, preferably in europe, willing to assist me with a purchase?

@ geofrancis
AliExpress doesnt ship the GL041MT / GL042MT to my location - but Alibaba offers the L041MTW - which looks identical to me - would it substitute for the GL sonar?

as far as I can see the L041MTW and GL041MT is the same sonar.

there are 4 versions as far as i can tell

GL041MT, 5 ~ 600cm, UART Controlled
GL041M4, 5 ~ 600cm, RS485
GL042MT, 2 ~ 300cm, UART Controlled
GL042M4, 2 ~ 300cm, RS485