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

  • 100KM

    I've posted these files here before in some threads on the forum I believe.  Try searching for ledshow or showleds on the diydrones site.

  • 100KM

    Meanwhile I've converted this to use the usercode.pde file and the userhooks in the code.

    I myself don't use the relay anymore, but I have 6 ledstrips now, all individually controlled by the apm.  With channel 7 I can select multiple blinking patterns in flight.  But the code to use the relay is still in my usercode.pde and can be activated with a  #define relay_leds 1setting in apm_config.h

    I could make a new tutorial/blog but I have other priorities atm (we're expecting twin babies in the next month)

  • Ive tried dropping this into the latest code, it does not work, compile errors

  • So whats the latest on this?, do I still have to add code to the config file and import the running lights?, Im using .55 code

  • 100KM

    No, you don't need to desolder your hardware modifications.  In fact, in my current code (2.0.49) I have relay leds turned off as well, cause I use the AN9-15 analog output ports now to drive even more leds then before and give me a complete lightshow.

    The only thing you have to remember, is that the relay will put 12V on the wires to the leds if it is turned on.  So either insulate the wire ends (if you removed the leds) with some shrinkwrap, or leave the leds connected (and they'll turn on if you switch the relay on).  If you leave the leds connected, you can still flash them with the relay by issuing a relay command for instance in mission planner, for instance when a waypoint is reached. (haven't tested this, but should work)

    I think there's also an option in the software to operate the relay with channel 6 or 7.  In that case you can operate the leds manually from your transmitter.

    The possibilities are endless :-)

  • u4eake,

     

    I have made modifications to the IMU relay area to enable the low battery indicator by following your instruction. However, I'd like to disable the modifications for now and load the most recent code v49 from the v39 I've been using for a while without integrating your additional code.

     

    Questions: Do i need to de-solder all the modifications I did on the IMU board? Any other advice?

     

    Thanks for your great mod on the code.

  • 100KM

    yes, sorry, I meant in running_lights.pde.  Strange that you get error messages with "."  You are using latest libraries, right ?  Cause I have compiled the code with relay.on() and version 2.0.49 without errors, but I have meanwhile new code for leds that I have put in usercode.pde and used the new userhooks in the firmware.

  • you say "it's better though to change in leds.pde...", but there's no relay_on or other in this file, i think u would say running_Lights.pde

    but when i change relay_on with dot, it return error message about "."

    i think my relay is "ko" , in CLI with test, i don't hear this.

  • 100KM

    It's better though to change in leds.pde all relay_on to relay.on and same for relay_off and toggle (always change _ to dot).  That should also work and cost you less memory. 

  • 100KM

    No, you only have to insert the red text, leave everything else alone. 

    If you test in CLI you should hear the relay click when switching on or off.  If it doesn't click, maybe your relay is broken ?

     

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

    This should be inserted in events.pde cause it are functions, not settings.  It will work when inserted in APM_config.h though, but it's really according to conventions.

This reply was deleted.