SMALL - Telemetry SMALL - Radio
Thread Tools
Old May 24, 2015, 05:39 PM
eissug is offline
Find More Posts by eissug
Registered User
Joined Nov 2014
80 Posts
Discussion
Arduino S-BUS Packet Generator Code

Didn't find one quick enough, so I made my own. If it is hard to read, then good. I didn't want to calculate bit shift offset data for 18 channels. It is not as CPU efficient as other methods, but it makes me feel good, so therefore it is OK.

I'll be using this to reformat data being received from my Blade 350QX3's receiver and send it to a X-CAM gimbal. If it helps anyone, let me know. If there is a bug, also let me know!!!

This program should display:
Code:
0x0F5C9126377FA46AAB00108000042000010870000210800C00
all day long. This is the same packet string sent by my OrangeRx with the same inputs.

Code:
unsigned int SBUS_Channel_Data[18];
byte SBUS_Current_Channel = 0;
byte SBUS_Current_Channel_Bit = 0;
byte SBUS_Current_Packet_Bit = 0;
byte SBUS_Packet_Data[25];
byte SBUS_Packet_Position = 0;

byte SBUS_Failsafe_Active = 0;
byte SBUS_Lost_Frame = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("Nae Jagi Saranghae!!");
  SBUS_Channel_Data[0] = 348;
  SBUS_Channel_Data[1] = 1234;
  SBUS_Channel_Data[2] = 1244;
  SBUS_Channel_Data[3] = 575;
  SBUS_Channel_Data[4] = 1706;
  SBUS_Channel_Data[5] = 342;
  SBUS_Channel_Data[6] = 1024;
  SBUS_Channel_Data[7] = 1024;
  SBUS_Channel_Data[8] = 1024;
  SBUS_Channel_Data[9] = 1024;
  SBUS_Channel_Data[10] = 1024;
  SBUS_Channel_Data[11] = 1024;
  SBUS_Channel_Data[12] = 1792;
  SBUS_Channel_Data[13] = 1024;
  SBUS_Channel_Data[14] = 1024;
  SBUS_Channel_Data[15] = 1024;
  SBUS_Channel_Data[16] = 0;
  SBUS_Channel_Data[17] = 0;
  SBUS_Failsafe_Active = 1;
  SBUS_Lost_Frame = 1;
}

void SBUS_Build_Packet(void)
{
  for(SBUS_Packet_Position = 0; SBUS_Packet_Position < 25; SBUS_Packet_Position++) SBUS_Packet_Data[SBUS_Packet_Position] = 0x00;  //Zero out packet data
  
  SBUS_Current_Packet_Bit = 0;
  SBUS_Packet_Position = 0;
  SBUS_Packet_Data[SBUS_Packet_Position] = 0x0F;  //Start Byte
  SBUS_Packet_Position++;
  
  for(SBUS_Current_Channel = 0; SBUS_Current_Channel < 16; SBUS_Current_Channel++)
  {
    for(SBUS_Current_Channel_Bit = 0; SBUS_Current_Channel_Bit < 11; SBUS_Current_Channel_Bit++)
    {
      if(SBUS_Current_Packet_Bit > 7)
      {
        SBUS_Current_Packet_Bit = 0;  //If we just set bit 7 in a previous step, reset the packet bit to 0 and
        SBUS_Packet_Position++;       //Move to the next packet byte
      }
      SBUS_Packet_Data[SBUS_Packet_Position] |= (((SBUS_Channel_Data[SBUS_Current_Channel]>>SBUS_Current_Channel_Bit) & 0x01)<<SBUS_Current_Packet_Bit);  //Downshift the channel data bit, then upshift it to set the packet data byte
      SBUS_Current_Packet_Bit++;
    }
  }
  if(SBUS_Channel_Data[16] > 1023) SBUS_Packet_Data[23] |= (1<<0);  //Any number above 1023 will set the digital servo bit
  if(SBUS_Channel_Data[17] > 1023) SBUS_Packet_Data[23] |= (1<<1);
  if(SBUS_Lost_Frame != 0) SBUS_Packet_Data[23] |= (1<<2);          //Any number above 0 will set the lost frame and failsafe bits
  if(SBUS_Failsafe_Active != 0) SBUS_Packet_Data[23] |= (1<<3);
  SBUS_Packet_Data[24] = 0x00;  //End byte
}

void loop()
{
  SBUS_Build_Packet();
  Serial.write(SBUS_Packet_Data, 25);
}
eissug is offline Find More Posts by eissug
Last edited by eissug; May 24, 2015 at 05:57 PM.
Reply With Quote
Sign up now
to remove ads between posts
Old May 24, 2015, 05:55 PM
eissug is offline
Find More Posts by eissug
Registered User
Joined Nov 2014
80 Posts
Btw, if you want to send to an SBUS device you will probably need to change the baud rate to 100000.
eissug is offline Find More Posts by eissug
Reply With Quote
Old May 26, 2015, 09:39 AM
Ernst38 is online now
Find More Posts by Ernst38
Registered User
Joined Jan 2007
8 Posts
Thanks for sharing,
This is very useful if you are experimenting with SBUS/pwm converters, or for a SBUS servotester .
Ernst38 is online now Find More Posts by Ernst38
Reply With Quote
Old Yesterday, 05:52 AM
dollop is offline
Find More Posts by dollop
Constantine Safronov
Israel, Netanya
Joined Apr 2007
43 Posts
2eissug
Thank you for sharing.
Is it true, that I need to invert the signal before I connect it to a sbus device?
dollop is offline Find More Posts by dollop
Last edited by dollop; Yesterday at 06:38 AM.
Reply With Quote
Old Yesterday, 06:41 AM
eissug is offline
Find More Posts by eissug
Registered User
Joined Nov 2014
80 Posts
Yes. Use an HC04, transistor, or something like it. I wish the AVR had a polarity setting for the serial port but it doesn't.
eissug is offline Find More Posts by eissug
Reply With Quote


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Discussion DJI NAZA/Phantom/A2 CAN bus communication protocol - NazaCanDecoder Arduino library pawelsky Multirotor Electronics 613 May 25, 2015 02:33 AM
Question Hex fell from the sky with "Internal bus error code 2" RIGeek Eagle Tree Systems 2 May 05, 2015 10:14 AM
Discussion Error in KVOSD Arduino Code thomas_rs FPV Equipment 0 Apr 10, 2015 10:18 PM
Discussion Clean starter code for Arduino? moparren Multirotor Talk 3 Dec 24, 2014 07:27 PM
Sold Futaba S.Bus Servos and S.Bus Accessories ir3 Aircraft - General - Radio Equipment (FS/W) 0 Dec 25, 2013 12:04 PM