Multiprotocol Radio

RC radio to controle all your drone ( Wifi, Bluetooth, Frsky, DSMX, Flysky, Bayang, Hubsan, ... )

Processors:

Features:

Wireless connectivity:

Menu Video

Images

Bayang Protocol in Lua using the nrf24l01+ driver

require "struct"

local BAYANG_BIND_COUNT     = 100
local BAYANG_PACKET_PERIOD  = 1000
local BAYANG_INITIAL_WAIT   = 500
local BAYANG_PACKET_SIZE    = 15
local BAYANG_RF_NUM_CHANNELS= 4
local BAYANG_RF_BIND_CHANNEL= 0
local BAYANG_ADDRESS_LENGTH = 5

IS_BIND = false
bind_counter = 0


BAYANG_FLAG_RTH         = 0x01
BAYANG_FLAG_HEADLESS    = 0x02
BAYANG_FLAG_FLIP        = 0x08
BAYANG_FLAG_VIDEO       = 0x10
BAYANG_FLAG_PICTURE     = 0x20
-- flags going to packet[3]
BAYANG_FLAG_INVERTED    = 0x80 -- inverted flight on Floureon H101

rx_tx_addr = {0x42, 0x42, 0x42, 0x42, 0x42}
hopping_frequency = {0,0,0,0}

function BAYANG_send_packet(bind,adc)

    local buff = ""
    if bind then -- bind
        buff = struct.pack('BBBBBBBBBBBBBB',
            0xA4, --0xA4,
            rx_tx_addr[1], -- txid[0]
            rx_tx_addr[2], -- txid[1]
            rx_tx_addr[3], -- txid[2]
            rx_tx_addr[4], -- txid[3]
            rx_tx_addr[5], -- txid[4]
            hopping_frequency[1],
            hopping_frequency[2],
            hopping_frequency[3],
            hopping_frequency[4],
            rx_tx_addr[1], -- txid[0]
            rx_tx_addr[2], -- txid[1]
            rx_tx_addr[3], -- txid[2]
            0x0A -- sub_protocol == H8S3D ? 0x34 : 0x0A;
        )
    else
        buff = struct.pack('BBBB>h>h>h>hBB',
            0xA5, -- PCMD
            0xFA, -- normal mode is 0xf7, expert 0xfa
            0x00, -- cmd
            0x00, -- inverted,
            math.floor(adc[4]/4),  -- adc3
            math.floor(adc[3]/4),  -- adc2
            math.floor(adc[2]/4),  -- adc1
            math.floor(adc[1]/4),  -- adc0
            rx_tx_addr[3], -- txid[2]
            0x0A -- sub_protocol == H8S3D ? 0x34 : 0x0A;
        )
    end
    local crc = 0
    for i=0,BAYANG_PACKET_SIZE-2 do
        crc = (crc + buff:byte(i+1))
    end

    buff = buff .. string.char(crc&255)

    nrf:WriteReg(nrf24l01.RF_CH, bind and BAYANG_RF_BIND_CHANNEL or hopping_frequency[hopping_frequency_no+1])
    hopping_frequency_no = (hopping_frequency_no + 1) % BAYANG_RF_NUM_CHANNELS

    -- clear packet status bits and TX FIFO
    nrf:WriteReg(nrf24l01.STATUS, 0x70)
    nrf:FlushTx()

    nrf:XN297_WritePayload(buff, BAYANG_PACKET_SIZE)
    -- nrf:WritePayload(buff, BAYANG_PACKET_SIZE)

    nrf:SetTxRxMode(nrf24l01.TXRX_OFF)
    nrf:SetTxRxMode(nrf24l01.TX_EN)

    -- Power on, TX mode, 2byte CRC
    -- Why CRC0? xn297 does not interpret it - either 16-bit CRC or nothing
    nrf:XN297_Configure((1 << nrf24l01.EN_CRC) | (1 << nrf24l01.CRCO) | (1 << nrf24l01.PWR_UP))
    --nrf:WriteReg(nrf24l01.CONFIG, ((1 << nrf24l01.EN_CRC) | (1 << nrf24l01.CRCO) | (1 << nrf24l01.PWR_UP)) & 0xFF)
    --print("send",bind)

    nrf:SetPower(3) -- Set tx_power
end


function BAYANG_init()

    nrf = nrf24l01.attach()

    nrf:SetTxRxMode(nrf24l01.TX_EN);

    nrf:XN297_SetTXAddr("\x00\x00\x00\x00\x00", BAYANG_ADDRESS_LENGTH);

    nrf:FlushTx();
    nrf:FlushRx();

    nrf:WriteReg(nrf24l01.STATUS, 0x70);     -- Clear data ready, data sent, and retransmit
    nrf:WriteReg(nrf24l01.EN_AA, 0x00);      -- No Auto Acknowldgement on all data pipes
    nrf:WriteReg(nrf24l01.EN_RXADDR, 0x01);  -- Enable data pipe 0 only
    nrf:WriteReg(nrf24l01.RX_PW_P0, BAYANG_PACKET_SIZE);

    nrf:SetBitrate(nrf24l01.BR_1M);             -- 1Mbps
    nrf:SetPower(3);
    nrf:Activate(0x73);                      -- Activate feature register
    nrf:WriteReg(nrf24l01.DYNPD, 0x00);      -- Disable dynamic payload length on all pipes
    nrf:WriteReg(nrf24l01.FEATURE, 0x01);
    nrf:Activate(0x73);

    nrf:PrintInfo()

end

function BAYANG_callback(adc)
    if IS_BIND then
        BAYANG_send_packet(false,adc)
    else
        if bind_counter == 0 then
            nrf:XN297_SetTXAddr(
                struct.pack(
                    'BBBBB',
                    rx_tx_addr[1],
                    rx_tx_addr[2],
                    rx_tx_addr[3],
                    rx_tx_addr[4],
                    rx_tx_addr[5]
                ),
                BAYANG_ADDRESS_LENGTH
            )
            IS_BIND = true
        else
            BAYANG_send_packet(true);
            bind_counter = bind_counter - 1;
        end
    end
end

function BAYANG_initialize_txid()
    hopping_frequency[1] = 1
    hopping_frequency[2] = (rx_tx_addr[1]&0x1F)+0x10;
    hopping_frequency[3] = hopping_frequency[2]+0x20;
    hopping_frequency[4] = hopping_frequency[3]+0x20;
    hopping_frequency_no=0;
end

function initBAYANG()
    bind_counter = BAYANG_BIND_COUNT
    BAYANG_initialize_txid()
    BAYANG_init()
    packet_count=0
end