aanon4 / FlySkyIBus

FlySky i-bus library for Arduino
https://gitlab.com/timwilkinson/FlySkyIBus
40 stars 16 forks source link

Failsafe/Signal loss #1

Open williamdwarke opened 6 years ago

williamdwarke commented 6 years ago

Any idea how to detect signal loss for implementing a failsafe over this protocol? It seems to spit out valid data even when my transmitter isn't turned on. Really appreciate your library BTW!

Thanks, Willie

pouriap commented 6 years ago

Hi @williamdwarke

I just figured out two things: 1- The receiver keeps sending normal signal unless you have assigned a failsafe for that channel in your transmitter. So if you want failsafe for channel 4, turn it on for channel 4 on your transmitter. Now you'll see the data received by the Arduino is changed when you turn off the controller.

2- When you turn on failsafe a 0xC is added to the received data for each channel. For example if the HEX value of the received data is 0x5DC, after the failsafe is triggered it will become 0xC5DC. So to detect the failsafe we can read the last 4bits of the received data. If it equals 0xC then we have lost signal. This code is working for me:

//turn on failsafe on channel 4 in your transmitter

#include "FlySkyIBus.h"

void setup() 
{
  Serial.begin(115200);
  IBus.begin(Serial);
}

void loop() 
{
  IBus.loop();
  unsigned int value = IBus.readChannel(3);

  Serial.print("Read value in HEX: ");
  Serial.print(value, HEX);
  Serial.print(" - ");

  unsigned int failBits = (value >> 12) & 0xF;  // get the last 4bits of read value
  boolean signalLost = (failBits == 0xC)? true : false;

  if(signalLost){
    // 'value' is now is a meaningless number. We have to get the currect value by getting the first 12bits
    unsigned int failValue = (value) & 0xFFF; //the first 12 bits

    Serial.print("Signal lost!! - ");
    Serial.print("Failsafe value: ");
    Serial.print(failValue);
  }
  else{
    Serial.print("Normal value: ");
    Serial.print(value);
  }

  Serial.println("");

}

This is a quick and dirty fix. The more clean approach would be to learn the protocol and edit the library code to add functionality to handle failsafe. But I don't have the time/intention to do that for the time being.

Note:

Hope this will help you.