APM2 LED Show - UserCode

 

I would like to introduce my APM2quadLEDshow, a frugal alternative to using an IO board for LED's with Arducopter3.X. In a nutshell, I took U4eake's famous showleds UserCode, eliminated the 50hz loop to keep AC3.X happy, ported it for APM2.x hardware, added GPS status, and formatted the patterns for quadcopters using ports AN3-7. Please refer to U4eake's well written blog for basic information on wiring, changing light patterns, etc:

http://diydrones.com/profiles/blog/show?id=705844%3ABlogPost%3A750391&xg_source=activity&page=8#comments

The only required hardware is a ULN2803 chip, and of course whatever LED's, limiting resistors, and harnesses you need to wire your LEDs to it. I have flight tested this code with Arducopter3.1dev, and there were no ill effects on looptime, even flying in auto modes. This code is licensed under GPLv3; so feel free to use, copy, and redistribute this code, as long as it remains available free to the public. I owe credit to:  U4eake, Rob_Lefebvre, Bill Sanford, and Max Levine... without their help I would still only be crashing, not sharing this code with the community.

Files:

apm2ledShow.zip

Software Setup :

For your convenience, I have provided an AC3.1dev hex file in the above zip, which was compiled with APM2ledShow, and configured quad-X. You may flash this hex using "custom code" MP. Nothing on a copter is truly "plug-n-play". So if you go this route, be sure to compare your configuration and make sure everything works for your copter before flight! ...and you may skip to "Hardware Setup" below.

For those more comfortable compiling their own hex, I have included all of the necessary files in "APM2ledShow.zip". Like U4eake's blog says, you must overwrite your UserCode.h and UserVariables.h files with the one's in my zipfile. Then you must make these changes to APM_Config.h

Add these lines:
#define SHOW_LEDS             1
#define MID_VOLTAGE      10.7
#define COPTER_LEDS           0                       //The rest of these items can be set in the MissionPlanner if you wish.
#define battery_EVENT      ENABLED             //My values are included just as an example, but all aircraft are unique.
#define LOW_VOLTAGE      10.5           //So your numbers are likely different. NEVER blindly copy someone else's parameters!
#define VOLT_DIV_RATIO      3.46
#define INPUT_VOLTAGE      5.05

Note the first 2 defines are required for compiling, the rest are accessible through the Planner. These voltages and dividers work for my quad... make sure your values match your copter. Also, you must uncomment these lines in APM_Config.h (remove the leading "//"):

#define USERHOOK_VARIABLES "UserVariables.h"
#define USERHOOK_INIT userhook_init();                      // for code to be run once at startup
#define USERHOOK_MEDIUMLOOP userhook_MediumLoop();        // for code to be run at 10hz

Hardware Setup:

U4eake's blog is already a well written resource; no need to reinvent the wheel. Just follow the diagrams posted in U4eake's blog, but instead of wiring your ULN2803 in to APM1, you wire it to APM2 ports AN3-7... arranging your LED sets like this:
     AN7   AN6   AN5   AN4   AN3    AN0-2       -APM2 Plug ID
     Fr/L   Fr/R   Rr/R   Rr/L   GPS    A,V,RSSI  -LED Arm Location (does NOT follow Arducopter esc numbering convention!!!)

Note that this code uses bitwise and/or to mask the sensor ports from bitshift operations. If you wire your rig different than shown above, you will have to make sure "legledmask" in UserVariables.h matches (read below).

Editing Flash Patterns:

I am not much of a programmer. So I feel a newb's pain when it comes to understanding/editing code. Therefore, I put lots of effort in to writing/commenting the code so almost anyone can read it get right to making successful modifications. Editing APM2ledShow patterns is a great way to get acquainted with Arduino, and learn about bitwise operations too. I hope this leads to users making/sharing their own creative patterns! ;)

Again we will refer to U4eake's blog for the basics. He points you to the Arduino bitmath playground... highly recommended reading if you plan on editing your ledshow. You should right away notice some differences in the byte format, and the port address. The latest APM Arduino and AC3 code requires us to use the format "0bXXXXXXXX" instead of "BXXXXXXXX". Also, we are using APM2 hardware, so we are working with DDRF/PORTF, rather than DDRK/PORTK. The code writes directly to port F with a bitmask. This means you can have fun with bitshift operations on the leg LEDs, without fear of switching your sensor ports high. The default reserves AN0-2 for sensors (A, V, and RSSI on my quad). If you wish to use a wiring configuration other than default, the bitmask is declared as "legledmask", in UserVariables.h. The GPS bit does not use masking because it only operates on the 4th bit "0b0000x000". So, do not bitshift "gpsbit" unless you know what you are doing. Same goes for the 2 low voltage warning patterns; use conditional "0bXXXX0000" operations there.

PostScript:

No more comments for now... I already mentioned this has been tested with auto modes on my rig... still no guarantees, YMMV...

Enjoy!

[update 10/11/2013

For AC3.1rc3 or higher, "battery_voltage1" was changed to "battery.voltage()". Updated compatible UserCode and Hex attached.

apm2ledShow-AC3_1rc3.zip

]

 

E-mail me when people leave their comments –

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

Join diydrones

Comments

  • Well another year has almost gone past and because I haven't worked on my ArduCopter quad for some time I thought I'd try the latest RC firmware but once again I'm getting various errors when trying to compile.  Kevin do you perhaps know of any other changes in the code since we last spoke that would cause the same errors as last?  I've only been playing around with it for a few minutes now but I thought I'd ask just in case... Thanks, 

  • No worries Kevin.  I just found it ironic that after 6 hours of sniffing through the pages and trying to compare files I found the problem and posted my findings to Randy. Then right after that I decided to have a quick look at u4eake's and your topics again and that's when I saw your comment... (dumb me)

  • Sorry about the inconvenience Andre. Nice to hear you found the rest of the necessary changes for your code. Luckily, "battery_voltage_1 undefined in this scope" popped up on top of the Arduino error dump, and I knew something was up. I searched "battery" in the ac3rc3 source and found the culprit... darn devs randomly changing names. I can imagine battery.voltage() may be the result of a change in data structure, but volt_div_ratio to batt_volt_mult is just a name change. That one better have a good reason behind it, other than annoying script kiddies like myself. ;)

    It would be nice if there was a bulletin board for said name changes, or better yet an IDE that flags variables changes for any variables called in custom code. Until then I hope I have better luck catching these things as they scroll down the warp speed AC change log.

  • I also think that "VOLT_DIV_RATIO" has been changed to "BATT_VOLT_MULT" and that "INPUT_VOLTAGE" has been completely removed from the code...

  • Hi Kevin,

    Not to sound rude but it would have been nice if you had posted the update regarding 3.1-rc3 as a new comment - that way I would have received an email notification.  I just spent over 6 hours trying to figure out why my file wouldnt compile. ;-)

    That being said, I am happy to report that the code still works on the APM1 boards as well.

    Thanks again...

  • Thanks Bill and u4eake!

    I wasn't acquainted with all the details of the GPL. I just wanted to add some words to discourage for profit use, but I understand it now. I like the idea of giving the code to folks with capital interests, knowing they have to share whatever their big pockets come up with. ;)

    I included that all off position without much thought; figured it could be used as a "stealth mode". It wasn't easy coming up with 19 somewhat unique patterns for a quad; most of your hexcopter formatted patterns become redundant on a quad. I can replace all off position with something else... any suggestions? Of course it is easy to copy/paste to replace with something else, but I'd like the default code to work for most ppl... I can maintain my own mods locally. ;)

    After doing some night time formation flying with my buddy, I noticed the side to side pattern is very visible from angles found in formation flight. I think I'm going to put that side/side stuff in the normal flight mode, and use the all arms flash for battery warning.

    CodeWikipedia: A code is a rule for converting a piece of information (for example, a letter, word, phrase, or gesture) into another - usually shortened or covert - form or representation (one sign into another sign), not necessarily of the same type.

  • 100KM

    Hi Kevin,  Thanks a lot for the port to apm2.5 and AC3.0 and for posting it!  Great work!

    I'm glad to see the ledshow isn't dead :-)

    I would maybe eliminate the "all off" position, as it might make you think the quad is unarmed, when in fact it is armed.  I especially like the strobe effect (2 fast flashes).  I never thought of that myself.

  • Developer
    Its quite light show, to my untrained eye it seems more like disco ;-) great job.

    You may want to revise the part of your post that states "redistribute as you wish for non commercial purposes" as thus contradicts the licensing terms of GPLv3 which maintains the "the freedom to use the software for any purpose", and that's even commercial enterprise. Only requirements for open source is that if you or said commercial enterprise redistribute the software, you also need to make that code available to all (and truglodites ;-) ) http://www.gnu.org/licenses/quick-guide-gplv3.html
    gnu.org
This reply was deleted.