100KM

LED low battery warning on arducopter

Hi all, On popular request, here's a short tutorial on how to make a low battery warning on my hexa with blinking leds, as you can see at the end of the following video :

 

 

EDIT : The software changes in this blog are now outdated and not necessary anymore.  For the new software see my new blog with much more possibilities (without using the relay) : U4eake's showleds, arming and low battery warning leds

Furthermore I'll use this as a reminder on what to change in my code when new versions come out :-)

 

First I'll give proper credit where credit is due, this code was originally written by Bill Sanford and posted on rcgroups here : Bill Sanfords running lights.  They are originally intended to be run with a brushed esc, so they can pulsate.

 

I never found an explanation on the 'attraction mode' leds on standard arducopters, so I modified Bill's code it a little and used the onboard relay to drive my hobbyking 12V leds :  http://www.hobbyking.com/hobbyking/store/__8941__Turnigy_High_Density_R_C_LED_Flexible_Strip_Red_1mtr_.html

LED-S-RE.jpg?width=510

I have a lot of them, so they draw much more current then the outputs of the APM can provide, but the relay has no problem with it.

 

General principle :

I have the leds setup as follows : on arming motors, the leds go on, indicating the copter is armed.  On disarming they go out.

When battery voltage reaches 10.6V the leds start blinking slowly (2 Hz).  From experimenting I know that I now have about 1 min of flighttime left with my battery.  You may need to experiment yourself to find out how much you have left.

When voltage goes down to 9.9V, it's really time to land (about 15s left) and the leds start blinking rapidly (5hz).  This is also the time when the copter will go into RTL, unless you modify the code (will get to that later on, see software, point 3. event.pde)

 

Hardware :

First the hardware.  I soldered a 3S balancing connector to the relay ports of the IMU as shown in the pictures. Yellow wire goes to pin AN0 on the imu.  Red wire goed to middle pin of relay.

 

PAY ATTENTION : on the 3S connector, swap the black and yellow wires if you use standard 3S balancing connectors !!!  Standard 3S balancing connectors have the black wire on the rightside end in the picture below.  On the imu however, the third pin is ground and the rightmost pin is AN0 which should be connected to +12V.  So swap the wires if you're using a standard 3S balancing connector or you'll invert polarity and damage your board !


3689416707?profile=original3689416753?profile=original

 

Then on the other side of the imu, solder the ground pin together with relay pin 0 with a solder bridge.  Also solder a short wire, connecting the pin AN0 (yellow wire) to the right side relay pin (pin 1).  

 

3689416732?profile=original

Now connect Yellow to battery +12V and black to battery minus.  Connect red to leds+ en blue to leds-.  All leds should be connected in parallel to the red and blue wire.  Don't overdo it though, the relay must be able to handle the amps.  60 hobbyking leds (1m) is still fine.  If you want more, check ampdraw en relay specifications.

With relay in pos 0, the red wire (led+) is connected to ground, so leds are off.

With relay in pos 1, the red wire (led+) gets fed +12V comming from Yellow wire at AN0.

Furthermore AN0 is constantly connected to +12V from battery so it can monitor battery voltage.

 

Your hardware changes are now done.

DISCLAIMER : I assume if you do this that you have sufficient knowledge about soldering and electronics.  Even though I tried to explain as best as I could, I take NO responsability if you destroy your board doing this.  Do this at your own risk !

 

 

Software : These changes are no longer valid, see my new blog mentioned above for recent code !

You'll need to make the following modifications the the firmware and then compile and upload with arduino.

1. In ArduCopterMega.pde

Find the medium_loop() function and in it find case 4.  There is a call to the read_battery() function there. 

Insert a call to handle_relay_lights() right after the read_battery() call.  See red text below.

// This case controls the slow loop

//---------------------------------

case 4:

  loop_step = 5;

  medium_loopCounter = 0;


  delta_ms_medium_loop = millis() - medium_loopTimer;

  medium_loopTimer     = millis();


  if (g.battery_monitoring != 0)

{

  read_battery();                               

handle_relay_lights();

  }

 

  ArduCopterMega.pde is now ready.  Don't forget to save it !

 

2. APM_config.h

Add the following code :

 

#ifndef BATTERY_EVENT

# define BATTERY_EVENT ENABLED

#endif
#ifndef LOW_VOLTAGE

# define LOW_VOLTAGE 9.9

#endif
#ifndef MID_VOLTAGE

# define MID_VOLTAGE 10.6

#endif
#ifndef VOLT_DIV_RATIO

# define VOLT_DIV_RATIO 3.33

#endif
#ifndef INPUT_VOLTAGE

# define INPUT_VOLTAGE 5.26

#endif

 

// global variables for motor leds and battery monitoring through onboard relay 

int rl_state   = 0 ;     // this global variable is used to save the on/off state of the relay

int rl_counter = 0 ;    // this global variable is used for the flash rate timer

 

I know the variables should probably be defined in parameters.h but upto now I've been too lazy to do so...  If you want to, pls do so, but bear in mind that it's an extra file to modify each time a new firmware comes out.

  That concludes the APM_config.h modifications. MID_VOLTAGE is when the lights start flashing slowly, LOW_VOLTAGE is when they flash rapidly and when copter goes into RTL.

You may have to experiment a little to find the right values for you.  See also the wiki about battery monitoring.

 

3. Events.pde As of version 2.0.38 this step is already implemented in Events.pde, no need to change it.

Since I now have leds to warn me when battery gets low, I don't want the copter to unexpectedly go into RTL cause my leds have been blinking and I didn't land in time.  One time when I was testing this, hoovering low with blinking lights, my copter suddenly took of and flew into the hedge.  Luckily I wasn't in between.

So I only want the copter to go into RTL when he's flying on an  automatic mode (auto, loiter), not when I'm in control.

Find the line void low_battery_event(void) in the event.pde file.

Change the whole function to look like this (red text changed) :

void low_battery_event(void)

{

gcs.send_text_P(SEVERITY_HIGH,PSTR("Low Battery!"));

if (control_mode > ALT_HOLD) { set_mode(RTL); }

}

I am thinking hard if it wouldn't be better (if possible) to change it into set_mode(Land); to have the copter land on an empty battery instead of attempting to get home with no juice left (which will probably end in disaster if the copter is more then 15sec away from home)

 

4. RunningLights.pde

Add the file RunningLights.pde to the directory where ArduCopterMega.pde is located.  This is a new file to add.   You can download it below.

RunningLights.pde

 

That's it !!!  Compile and upload with arduino, enable battery monitoring to battery volts in setup (type setup in CLI and then battery 3) or choose battery volts in mission planner -> hardware setup -> monitor.

 

3689416774?profile=original

 

Enjoy and feel free to ask questions ! 


E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones

Comments

  • with new code 2.0.49, must i insert this lines code :

    loop_step = 5;

      medium_loopCounter = 0;

      delta_ms_medium_loop = millis() - medium_loopTimer;

      medium_loopTimer     = millis();

     

    because it caused a error message in arduino when i compile it. 

    no lights, my relay seems not work, it seems always off. when i test it in CLI, it indicate :

    "relay on

    relay off "

    but no change, no lights, and i don't know if it really work. 

     

  • i'm confused, this code must be placed in the bottom of APM Config.h or events.pde ?

    static void relay_on(){ PORTL |= B00000100;}
    static void relay_off(){ PORTL &= ~B00000100;}
    static void relay_toggle(){ PORTL ^= B00000100;}

     

    Whoops you're right events.pde

    I am glad it works again.

    It assures it does not flip when the battery becomes low.

  • i'm confused, this code must be placed in the bottom of APM Config.h or events.pde ?

    static void relay_on(){ PORTL |= B00000100;}
    static void relay_off(){ PORTL &= ~B00000100;}
    static void relay_toggle(){ PORTL ^= B00000100;}

     

  • I tested and verified today that the code below.

    static void relay_on(){ PORTL |= B00000100;}
    static void relay_off(){ PORTL &= ~B00000100;}
    static void relay_toggle(){ PORTL ^= B00000100;}

    placed in the bottom of APM config h with previous sketches works well.

    The arming then battery blink slow then fast to tell me to land soon works again.

     

  • I did what described in the title, but no LED lights. I tried several things with the code 2048 and 2.0.49, and try with not changing the line
    / / This box controls the slow loop
    //---------------------------------
    case 4:
    medium_loopCounter = 0;

    if (g.battery_monitoring! = 0) {
    read_battery (); handle_relay_lights ();

    I have  the battery battery monitoring when changing and upload code APM_config.h values​​, the values ​​are found in apm planner.

    i don't know why it doesn't work, help please
  • Wow the extra code you gave at the bottom did the trick, cant wait to test!, Thankyou very much!

     

  • 100KM

    What are the compile errors ?  Something with relay_on() missing ?

    If so, try either to change the relay_on() relay_off() and relay_toggle() functions to relay.on() relay.off() and relay.toggle().

    It looks like these functions have been moved to a library.

    Alternatively you could add the following lines to the bottom of events.pde :

    static void relay_on(){ PORTL |= B00000100;}
    static void relay_off(){ PORTL &= ~B00000100;}
    static void relay_toggle(){ PORTL ^= B00000100;}

  • Ive done as instructed but Im getting compile errors, using the latest code 2.0.47

  • So what parts do I amend in the code?, Im wanting to get this up and running on each latest update so Im wanting to know what I need to amend and where, Cheers!

  • 100KM

    Best is to download the latest version from the download section or GIT, then make the modifications.

This reply was deleted.