RC receiver breaks time functions?

Hi, I'm currently working on a project that I am going to be controlling with a FlySky FS-I6 controller. I currently have the receiver hooked up to a small tank drive bot for testing purposes. The data transfer works fine once connected. However, it does take the transmitter a few seconds to connect and in that time the Arduino seems to be reading junk data and runs the motors at random speeds most commonly seeming like max speed. This is not an issue for the small-scale bot but would be when I attempt to use this on my full-sized project so I attempted to use delay() and then millis() to create a wait in my code to give the transmitter time to connect. These functions however just seem to break the code if they are active. Does using IBUS for communication break these functions somehow? If so, are there any alternative solutions to this problem? Thanks.

#include <IBusBM.h>
#include <AFMotor.h>

IBusBM IBus;
double ly = 0;
double lx = 0;
double ry = 0;
double rx = 0;
double ld = 0;
double rd = 0;

double leftMotorDmd;
double rightMotorDmd;
double startTime;

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup() {
  delay(3000);
  IBus.begin(Serial);
  //startTime = millis();
  
}

void loop() {
  ly = (double)map(IBus.readChannel(2), 1000, 2000, -1024, 1024) / (double)1024;
  lx = (double)map(IBus.readChannel(3), 1000, 2000, -1024, 1024) / (double)1024;
  ry = (double)map(IBus.readChannel(1), 1000, 2000, -1024, 1024) / (double)1024;
  rx = (double)map(IBus.readChannel(0), 1000, 2000, -1024, 1024) / (double)1024;
  ld = (double)map(IBus.readChannel(4), 1000, 2000, 0, 1024) / (double)1024;
  rd = (double)map(IBus.readChannel(5), 1000, 2000, 0, 1024) / (double)1024;
  
 
  leftMotorDmd = ld * 255 * (ry + lx);
  rightMotorDmd = ld * 255 * (ry - lx);
  //if(millis() > startTime + 3000){
    runMotorDmd();
  //}


  
}


void runMotorDmd(){
  int d;
  if(leftMotorDmd < 0){
    double dmd;
     d = 2;
     dmd = leftMotorDmd * -1.00;
     motor2.run(d);
     motor2.setSpeed(dmd);
  }else{
    double dmd;
    d = 1;
    dmd = leftMotorDmd;
    motor2.run(d);
    motor2.setSpeed(dmd);
  }
  if(rightMotorDmd < 0){
    double dmd;
     d = 2;
     dmd = rightMotorDmd * -1.00;
     motor1.run(d);
     motor1.setSpeed(dmd);
  }else{
    double dmd;
    d = 1;
    dmd = rightMotorDmd;
    motor1.run(d);
    motor1.setSpeed(dmd);
  }
}

type or paste code here

type or paste code here

You might have setup halt until a particular "arming" code (perhaps the value of the left stick while down and right) allows it to break out of a while() loop.

That could work, although I'd like to avoid doing that if possible as if the Arduino is truly getting random values then it's a strong possibility it could randomly get the arming parameters to activate and I absolutely can't allow that to happen on the full project without risking major damage.

Ok, what about pre assigning

these safe values for some time in setup() while things connect? Or until it gets your arming value?

If I could do literally anything for a set amount of time I wouldn't have an issue in the first place. But the main issue of this post is that for some reason none of the time-based functions work in the code I'm using for my receiver.

Ok then, good luck!

You neglected to specify what hardware you are using for your receiver.

In that case, why not just throw away the first thousand or so readings as garbage before you start to act on the readings? Something like

int throwAway = 1000;

and then, in loop(),

  if(throwAway == 0){
    runMotorDmd();
  }
  else throwAway--;
1 Like

millis() is an unsigned long, not a double, although that should not cause the millis timing to fail completely.

1 Like

You didn't mention what board you are using, but since you're using the AFMotor library I'm going to assume that it's an Uno R3, as that library only works with an ATmega processor. In which case, double is exactly the same as float. Meaning you only get 6-7 digits of precision. If your code is depending on there being more precision than that, that could be a problem.

But possibly more relevantly (if that's a word), you do know that the IBusBM library hooks into the same timer as used by delay() and millis(), right? So if the library isn't playing nice, millis() and delay() could go right out the window.

And as @david_2018, millis() is an unsigned long, not a double. Not sure what the implications of declaring startTime as effectively a float, but it ought to be changed.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.