Futaba S-BUS协议解析

来源:互联网 发布:php的面向对象编程 编辑:程序博客网 时间:2024/06/04 17:52

一、协议说明

  1. S-BUS协议实际上通过串口进行数据发送。但是需要注意的是,S-BUS的逻辑电平是反的,需用如下电路对电平反相,再接到串口接收的管脚。
    SUBS反相器

  2. 串口配置为波特率100kbps( 100000 ),8位数据,偶校验(even),2位停止位,无流控。

  3. 每帧25个字节,帧格式如下【参考https://mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】:
    [Start Byte] [Data0] [Data1] …. [Data21] [Flags][End Byte]
    两帧之间的时间间隔4ms(高速模式),约7ms一帧。
    其中:
    Start Byte = 0x0F。中间22个字节为16个通道的数据,每个通道用 11 bit表示,范围是0-2047。
    End Byte根据S-BUS协议版本不同而不同。
    Flags的定义:
    bit7 = ch17 = digital channel (0x80)
    bit6 = ch18 = digital channel (0x40)
    bit5 = Frame lost, equivalent red LED on receiver (0x20)
    bit4 = failsafe activated (0x10)
    bit3 = n/a
    bit2 = n/a
    bit1 = n/a
    bit0 = n/a
    数据部分:
    如果把22个字节看作一个数的话,则数据部分采用小端模式。即若把数据按照[Data21][Data20]…[Data1][Data0]的顺序排列,则[Data21]的 bit 7 为MSB,[Data0]的 bit 0 为LSB,每 11 bit 为一个通道的数据。Data[21]和[Data20]的高3位为通道16的值,以此类推。
    借用一张图,改了改:
    各通道数据的排列方式
    下面是一组数据的例子(来自 R7008SB):
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 04
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 14
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 24
    0F 01 04 20 A8 01 08 16 50 83 1A 80 00 04 20 00 01 08 07 38 00 10 80 00 34

二、S-BUS解析程序

void Process(uint8_t* raw,uint16_t* result){  uint8_t bitsToRead=3; // bitsToRead表示需要从下一个字节中读取多少bit。规律:bitsToRead 每次总是增加 3  uint8_t bitsToShift;  uint8_t startByte=21;  uint8_t channelId=15;  do  {    result[channelId]=raw[startByte];    if(bitsToRead<=8)    {      result[channelId]<<=bitsToRead;      bitsToShift=8-bitsToRead;      result[channlId]+=(raw[startByte-1]>>bitsToShift);    }    else    {      result[channelId]<<=8;      result[channelId]+=raw[startByte-1];      startByte--;      bitsToRead-=8;      bitsToShift=8-bitsToRead;      result[channelId]+=(raw[startByte-1]>>bitsToShift);    }    result[channelId]&=0x7FF;    channelId++;    startByte--;    bitsToRead+=3;  }while(startByte>0);  }

感谢来自
taogashi【http://www.eefocus.com/taogashi/blog/14-05/303577_f140d.html】
以及
Uwe Gartmann【https://developer.mbed.org/users/Digixx/notebook/futaba-s-bus-controlled-by-mbed/】
的有关S-BUS协议的介绍。

0 0
原创粉丝点击