Skip to content

Conversation

stephendade
Copy link
Contributor

This PR adds support for the Rockblock 9704 satellite modem (https://www.groundcontrol.com/product/rockblock-9704/), using the MAVLink High Latency protocol. It sends a HIGH_LATENCY2 packet once per 30 seconds.

The software required at the GCS to process messages is at stephendade/rockblock2mav#5

I also:

  • Added Lua bindings for CRC16 functions
  • Disabled stream rate setting for MAVLink High Latency protocol

Tested in hardware with a CubeOrange+.

(Thanks to GroundControl for supplying the hardware and airtime)

-- compute CRC-16/XMODEM checksum of text
---@param text string
---@return integer -- checksum
function crc_xmodem(text) end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be interesting to do some bench-marking vs a native lua implementation. I'm a little reluctant to start a president crc bindings just because there are so many people may want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A crc implementation in lua for example:

--- Apply a byte to the crc and return the result
---@param crc integer
---@param byte integer
---@return integer
local function applyCRCByte(crc, byte)
crc = crc ~ byte
for _ = 1, 8 do
if (crc & 0x80) ~= 0 then
crc = ((crc << 1) ~ 0xE7) & 0xFF
else
crc = (crc << 1) & 0xFF
end
end
return crc
end
--- Apply a table of bytes to the crc and return the result
---@param crc integer
---@param data integer[]
---@return integer
local function applyCRC(crc, data)
for i = 1, #data do
crc = applyCRCByte(crc, data[i])
end
return crc
end

@Hwurzburg Hwurzburg added the WikiNeeded needs wiki update label Oct 16, 2025
@stephendade
Copy link
Contributor Author

It would be interesting to do some bench-marking vs a native lua implementation

@IamPete1 Here's one I put together:

-- Show various CRC calculations

--CRC16 Xmodem
local inString = "Hello, world! This is a test string. 50 bytes long"

--CRC16 via pure Lua
local function crc_xmodem_update(crc, data)
    crc = crc ~ (data << 8)
    for i = 0, 7 do
        if (crc & 0x8000) ~= 0 then
            crc = (crc << 1) ~ 0x1021
        else
            crc = crc << 1
        end
    end
    return crc & 0xFFFF
end

local function crc_xmodem_lua(data)
    local crc = 0
    for i = 1, #data do
        crc = crc_xmodem_update(crc, string.byte(data, i))
    end
    return crc
end

local function update()
    local start = micros()
    -- so 3 crc and take the average time
    for i = 1, 3 do
        crc = crc_xmodem(inString)
    end
    local stop = micros()
    local delta = (stop - start)/3

    gcs:send_text(3, "CRC16 (bindings) is: " .. string.format("0x%04X", crc))
    gcs:send_text(3, "CRC16 (bindings) calculated in " .. tostring(delta) .. " us")

    start = micros()
    for i = 1, 3 do
        crc = crc_xmodem_lua(inString)
    end
    stop = micros()
    delta = (stop - start)/3

    gcs:send_text(3, "CRC16 (pure Lua) is: " .. string.format("0x%04X", crc))
    gcs:send_text(3, "CRC16 (pure Lua) calculated in " .. tostring(delta) .. " us")

end

-- wrapper around update()
function protected_wrapper()
    local success, err = pcall(update)
    if not success then
        gcs:send_text(MAV_SEVERITY.ERROR, "CRC: Internal Error: " .. err)
        -- when we fault we run the update function again after 1s, slowing it
        -- down a bit so we don't flood the console with errors
        return protected_wrapper, 5000
    end
    return protected_wrapper, 5000
end

-- start running update loop
return protected_wrapper()

Calculating the CRC16 of a 50 byte string has the following execution time on a CubeOrangePlus:

  • 15-20usec using the Lua bindings
  • 900-1400usec using the pure Lua implementation.

So the Lua bindings are ~50x faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WikiNeeded needs wiki update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants