Go Down

Topic: NEED failsafe. Wireless xbee RC controller (Read 2648 times) previous topic - next topic

sw33n3y

Jun 14, 2013, 02:47 am Last Edit: Jun 14, 2013, 02:50 am by sw33n3y Reason: 1
newbie.

I made a wireless controller with 2 pro minis and 2 xbees that just send 1 pot signal to power a motor Here's the basic code for the Receiver:

#include <Servo.h>

int servopin = 11;
Servo myservo;

void setup(){
  Serial.begin(9600);
  myservo.attach(servopin);
}

void loop(){
if (Serial.available()){
  int servoAng = Serial.read();
  myservo.write(servoAng);
  }
}


Now, i have the Transmitting arduino mapped from 0 to 180. However it is a trigger with a spring so 0 is Full brake and 180 is full throttle and netural is in the 60's i believe. Also should know that I am using "serial.WRITE()" so the values aren't actually numbers just to be clear. I need to add a failsafe in that code so that if the wires disconnect/battery dies/out of range the receiver doesn't continue reading the last value (which 80 percent of the time is  throttle). Everything I've tried seemed to fail and I'm not sure what exactly i need to add. Something like IF wireless connection is available read the pot  ELSE disengage the servopin

JChristensen

#1
Jun 14, 2013, 05:03 am Last Edit: Jun 14, 2013, 05:07 am by Jack Christensen Reason: 1
How often does the transmitting end send data? The receiver could note the time the last data was received, and if it goes for maybe three to five times the usual transmission interval (or whatever constitutes safe), it could shut down.

PS: http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_codetags

sw33n3y

hey! I believe i added a 200ms delay. However, I really don't even know how to go about adding that. Someone was talking about millis() but I don't even know how I'd apply it... any info would help.


thanks

JChristensen

Why don't you give it a shot. Google up some Arduino tutorials.

sw33n3y

#4
Jun 14, 2013, 06:34 am Last Edit: Jun 14, 2013, 06:38 am by sw33n3y Reason: 1
Believe me. I've googled. this was a last resort. It took me about 3 weeks of trial and error to get the code above working. Mainly because, being the first time using XBee i find out after the first week that one of the 2 wasn't even working. I shouldn't of started my programming interest with wireless communication but i wanted to get this thing completed...

It is impossible to find information  on what I'm doing that is easy to understand. Even the arduino tutorial page uses millis for something that isn't going to work for serial.communication.

Id be cool if someone could just add what i need cause nothing works and i've spend 20+ hours on this one thing; which honestly doesn't seem to be that hard.


i just need to add something a long the lines of  IF wireless communication is BROKEN  then dettach MOTORpin. because there is ALWAYS a serial communication. I'm so lost and have tried everything i could,


Code: [Select]
#include <Servo.h>

int servopin = 11;
Servo myservo;

void setup(){
  Serial.begin(9600);
  myservo.attach(servopin);
}

void loop(){
if (Serial.available()){
  int servoAng = Serial.read();
  myservo.write(servoAng);
 
 
}
}


JChristensen

Compiles OK but not tested, testing is left as an exercise for the reader :D

Code: [Select]
#include <Servo.h>

#define FAILSAFE_MS 1000    //stop sketch if no data received in this many milliseconds

int servopin = 11;
Servo myservo;

void setup()
{
   Serial.begin(9600);
   myservo.attach(servopin);
}

void loop()
{
   static unsigned long msLastRX;        //time of most recent reception
   
   if (Serial.available()){
       int servoAng = Serial.read();
       myservo.write(servoAng);
       msLastRX = millis();
   }
   
   if (millis() - msLastRX >= FAILSAFE_MS) {
       //do whatever you need here
       while (1);        //stops the sketch
   }
}

sw33n3y

#6
Jun 15, 2013, 10:45 am Last Edit: Jun 15, 2013, 10:49 am by sw33n3y Reason: 1
Thank you!! having just the one arduino tutorial example of millis() doesn't help but looking at what you did and the example actually got me to understand it! However, I am now experiencing a whole new problem. I'm not sure but I do not think it is related.

So here's the code:


Code: [Select]
#include <Servo.h>


int servopin = 11;
Servo myservo;


long FAILSAFE = 1000;


void setup()
{
    Serial.begin(9600);
    myservo.attach(servopin);
}

void loop()
{
    int now = millis();
    unsigned long lastRX;
   
    if (Serial.available()){
        int servoAng = Serial.read();
        myservo.write(servoAng);
        lastRX = now;
    }
     if (now - lastRX > FAILSAFE) {
        myservo.write(80); // servo.detach resulted in sudden break. 80 is neutral.
       
       
    }
}
   
 



Everything worked great for 15-20 minutes. Then the TX DOUT LED faded out and coms stopped. I disconnected/reconnected battery and it worked for 10 seconds. disconnected, waited 5 minutes and it worked for 60 seconds. Lights seem to work [forever] when RX side is off. Worst part is when LED fades{coms stops] the Motor REVs real fast followed by the failsafe...

JChristensen

millis() returns an unsigned long data type. So all the related variables need to be unsigned long. So,

Code: [Select]

unsigned long FAILSAFE = 1000;
    unsigned long now = millis();


Secondly, when the failsafe condition occurs, we want to stop the sketch. The "while (1)" does this, it puts the processor into a tight loop so that it is basically doing nothing and a reset is required to restart:

Code: [Select]

     if (now - lastRX > FAILSAFE) {
        myservo.write(80); // servo.detach resulted in sudden break. 80 is neutral.
        while (1);         //hang the sketch here, press reset to restart.       
    }

sw33n3y

Is the While(1); really needed? Because it means i have to shut of the ESC and the Controller every time connection is lost. Without it i just reconnected the controller side?


Lastly, Is there any reason why new stuff  is sucking the life out of my 9v batteries. Before they'd last for days and now it seems a 9v won't last for more than 20 minutes...

JChristensen


Is the While(1); really needed? Because it means i have to shut of the ESC and the Controller every time connection is lost. Without it i just reconnected the controller side?


I guess it depends on what you want to happen for the failsafe condition. I assumed everything should just stop, but I can see where you're coming from. If it works to your satisfaction without it, then I guess all is well.

Quote

Lastly, Is there any reason why new stuff  is sucking the life out of my 9v batteries. Before they'd last for days and now it seems a 9v won't last for more than 20 minutes...


Pretty hard to say without seeing a circuit (not sure what "new stuff" is). Those rectangular 9V batteries don't have much capacity. They're usually at the bottom of the list of desirable power sources. If they're used to power an Arduino board, the first thing that happens is that the voltage regulator wastes nearly 45% of their power as heat.

Go Up