Modifying DCC++ for multiple power districts

crusader27529 Aug 19, 2016

  1. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    Hmmm...

    I don't quite see what the issue is with (Arduino) C++, any of the newer higher level functions are all well documented, and designed to make life very easy for your average Arduino tinkering. If you know C as you keep mentioning, then write it in C, the Arduino IDE compiler does't usually have an issue with this. I do most of my stuff nearer to older 'C' style these days, my experience of the language goes back 15 months from zero knowledge.

    Well yes of course, post your coding proposals (in 'C') and I'm sure someone will help......
     
    Scott Eric Catalano likes this.
  2. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    Modifying anything related to c++ and classes and inheritance and the like is beyond what I'm capable of.......

    My C experience is/was specifically related to very low level functions used to implement machine control type functions almost at the assembly language level. I am more of a hardware person than software, but can brute force program when I need to. I've previously attempted to modify DCC++ code related to power monitoring and control, and was far from successful. Although I 'understand' object orientated program in theory, it's actual implementation is another issue entirely. I'm sort of OLD, been retired for some time, and I guess this old dog is having trouble learning new tricks.

    Adding the 'table' of parameters to be used probably wouldn't be the problem, but rather changing what the c++ code does with it is the problem.

    If I could do it myself I wouldn't have created the reply that I wrote. I guess I'm asking for help.
     
    Scott Eric Catalano likes this.
  3. brendanf

    brendanf TrainBoard Member

    62
    54
    8
    When Greg wrote the DCC++ sketch he laid it out very neatly.

    As for how many power districts are supported, this really depends on HOW you go about adding more districts and what Arduino platform you are using. In my case I am stacking more motor shields and jumpering the A0 & A1 from the 2 other shields to other analog inputs and reading those values in and making decisions based on the readings. Adding any extra sensing is pretty straight forward by following his example. You need not really know C++ programming, if you can do basic C you should be able to handle it.

    Adding in counters and auto reset functions is a matter of some simple 'if ();' statements added to the Current:Check() function.

    And that only really matters if that is how a person intends to add more power districts.

    I have no idea. I have yet to get into figuring out how Greg controls the timers via registers.

    I can only comment on what I did. And that was:

    I allow for 3 automatic attempts to restore power.

    1. Kill the district
    2. Delay for a bit
    3. Re-energize the district - Attempt +1
    4. Is it still shorted? Yes - Are we at 3 attempts? - No? Goto 1; Yes - Give up and turn on an LED
    No - All is good, Set attempts to 0 for the next time
    5. Is the manual reset pushed? Yes - Clear the counts and re-energize
    No - do nothing and carry on

    Because of the fact different people are using different Arduinos and different motor shields it isn't quite so simple. For example, an Uno only has 6 analog inputs whereas the Mega has 16. I'm not even sure how many some of the other Arduinos have let alone the knock'offs.
     
    Deltic and Scott Eric Catalano like this.
  4. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    Well, I designed a PCB (the Gerber files needs to manufacture the board will be made available for free to anyone who wants to make some parts) that's based on an Arduino NANO.

    The NANO has 8 analog inputs for sensing, and the same processor as the UNO, so it has enough processing power for DCC++. I chose the NANO because it's more-or-less designed to be 'breadboard friendly', so it's easily plugged into a set of female pin headers, as opposed to the UNO or MEGA, which would require boards be plugged into them.

    The design is simple, and uses one 74LS04 inverter to drive the external motor driver boards. Currently, it's setup for using up to 8 of the BTS7960 motor driver boards that are capable of 43A each, but won't be used to those level....they'd be limited to 5-8 amps each, maximum. These asre the same parts Travis Farmer and Dave Bodner use, and they work very well, and are easy to interface to.

    The design uses the first 2 MD boards for the standard Program and Main function, plus up to 6 other power districts. The design could easily be made to use other MD parts, but my design goal is/was to make this usable on almost any size layout. The cost of the PCB in quantity 10 or so would be about $6, and the only other expenses would be the NANO for about $3, an inverter for about $0.30, and the connectors/cabling for about $1 per MD board. The cost of the MD boards currently is about $12 each.

    Using JMRI, a separate Arduino of whichever flavor you'd like could easily be used to control the rest of the layout functions, as required. Doing it this way give most flexibility, and doesn't have the train control functions limiting the non-train functions anyone might want to add.

    Besides the basic multiple power district current sensing and power re-application logic, the system would need to have more 'slots' for loco control. Since the design is only for the loco control, removing logic structures for turnouts and similar stuff that is extraneous to loco control wouldn't affect anything.

    I'd really like the complete DCC++ environment to be made open source, like JMRI, but the hardware also, so it'd become a model railroad 'standard' for our segment of the hobby. To do that we need a hardware 'standard' to design the software to. That's my ultimate goal.

    Will you help???
     
    Scott Eric Catalano likes this.
  5. UK Steve

    UK Steve TrainBoard Member

    453
    683
    12
    @crusader27529

    I think we can only help once we see how the hardware configuration is to be in your design. Is the code already proposed by Travis of no use in this case?

    If I was doing something like this from scratch, then I would write the logic to run as a standalone method on my MCU of choice. A 'for loop' scan of the 8 analogue pins and associated decision making is hardly rocket science in any programming language. In DCC++ calls are simply done from loop(). You can test your ideas - preconfig parameter tables - whatever, in a sterile situation. And do lots of Serial Debug at the same time. It's how I do things when writing methods of a greater whole. Then later we can join in and help get it integrated into DCC++ with you. No?

    As for 'slots' this is defined as 12 in the original config. And restricted on an UNO because of memory limitations. I don't see why you couldn't increase it you take out some of the accessory control as you suggested, folks have successfully done work in this area already.

    Also, what notification feedback is required from your system, I don't see why a bunch of status LED's and a buzzer wouldn't suffice. Rather than getting district status info back to JMRI (more coding) then the whole thing could stay on a separate Nano job done.
     
    Last edited: Apr 28, 2017
    Scott Eric Catalano and esfeld like this.
  6. brendanf

    brendanf TrainBoard Member

    62
    54
    8
    @crusader27529

    To be honest I think your trying to reinvent the wheel here with designing your own hardware.

    There are a lot of different ways to implement a multi power district and they don't all involve multiple driver boards or modifying software.

    Everything related the DCC++ hardware and software already is completely open source so I am not sure what you think isn't. The only 'standard' here is that a person can easily and freely modify what they need to suit their own needs.

    As for adding more slots, I have an idea on this which would be cheap and easy and that would be to add a small I2C eprom. It would work on any of the platforms.

    I won't be able to help you on this other than occasionally answer questions. I have my own things to do.

    You sir, are retired and have plenty of free time to research and read and teach yourself what you need. Exercise the ol' grey matter.
     
  7. David Jennex

    David Jennex TrainBoard Member

    15
    16
    6
    I'm finally getting around to setting up an HO layout after being out of the hobby for years and years. Final size will be 10 by 20 feet walk in - track length about 100 feet point to point - mountain mining and logging to sea port. With another mountain line HON3, not connected to main tracks about 50 feet.

    I'm not interested in "current sense" but rather Travis' method above. I have a DCC++ System operating with UNO, Pololu Bridge, JMRI and WiThrottle.

    Could either or both of you take a quick look at https://github.com/DccPlusPlus/Documentation/blob/1613b0668d8e0ebb5ce91ad8c4e3747143c2ba5e/Motor Shield Pin Mappings.pdf - UNO/Pololu MC33926 diagram? I'm wondering if a single pole double throw break before make switch changing the normal 10 to 7 and 5 to 8 connections so that 10 would be connected to both 7 and 8 would give me "main" programing on both outputs in one switch direction and normal "main" and "prog" in the other?

    In essence, this would give me two 3 amp main districts most of the time - I really don't need to program locos while involved in operations.

    I'll be changing to MEGA with stacked Pololu shields later, giving me more power district as I complete construction.

    Suggestions are mightily appreciated!:):)

    David
     
    Last edited: Jun 5, 2017
    Scott Eric Catalano likes this.
  8. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    I don't know alot about the actual components on the Pololu boards, but as long as the Arduino outputs can drive whatever the 2 logic 'loads' that the boards presents, it should work....the Arduino pins are current limited to 40ma (I think), but normally should be limited to 20ma. Having said that, the standard TTL logic type load shouls be in the 400Ua range, so the Arduino should work OK.

    I would recommend that when you need to switch between the program track and the second main track that you shut down the power supply that's feeding the motor driver board, just to preclude a voltage spike.

    As always, YMMV......good luck.
     
    Scott Eric Catalano likes this.
  9. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    The code Travis wrote is the basis for my design. I did remove all the extraneous functions not related to train control, and expanded the test addition Travis inserted. If sensor or signaling is required, a second DCC++ system can easily be added through JMRI to do those functions.

    The reson I removed the non-train stuff was completely for performance reasons, and to allow for 32 locomotive slots(currently) for use on larger layouts.

    The system has been designed to allow LONG distances between the primary PCB where the Arduino NANO resides to the motor drivers (IBT-2 boards with BTS7960 chips like Dave Bodner likes). The connections from the Arduino and motor drivers is a standard CAT5 network cable, and can be configured for local (close connection, actual distance is TBD) and remote (driven by RS485 EBAY modules) at a per motor driver basis.

    For example, the program track would very likely be close to the Arduino, so it'd be local, and maybe some of the other motor drivers, but longer distances would use the RS485 hardware. Current prices for the RS485 modules is less than $0.50 each (4 are required for each power district connection). All the configurations for the chouce of local/remote are simple plug-in decisions, adding the RS485 stuff or simple putting a jumper in place of the modules.

    The prototype copper boards are scheduled for delivery to me today or tomorrow, so final debugging will start soon. I'm still waiting for some other parts to arrive. There are LEDs to show activity and status at both ends of each connection, but installation of those components is user optional. The remote connection PCBs connect directly to the IBT-2 motor drivers (using BTS7960 chips) without any cables required beyond the CAT5 to connect to the Arduino.

    The design operation of the current management was a departure from what Travis designed in that the system will temporarily remove power from the motor driver that's in an over-current state for a second, and then try again......the loop count before the system gives up for the motor driver that's the problem will likely be a large number, say 500 or so. Once that limit is reached, the only way to restore power to the problem section(s) is to cycle the entire layout power.

    The status of each motor driver is indicated by a RED & GREEN LED for each, plus the DCC signal for both the MAIN and PROG outputs are also shown on LEDs. Corresponding similar LEDs exist on the PCB for each motor driver, making it relatively simple to determine the status of the system and troubleshoot connection issues.

    That's what the system is currently, but is still in development. Don't forget that all of this, including the files needed to get the PCBs made by a PCB house, will be made public domain. I will offer the PCBs needed for a nominal price for those who don't want to get their own boards made.
     
    Scott Eric Catalano likes this.
  10. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    The boards did arrive, and I started to partially populate them, but I'm missing the required connectors to complete any single channel......there are 8, counting the original MAIN and PROG channels.

    I'll be able to do some simplistic testing for now, and further testing when the parts arrive. Here are some photos of the parts as they go together......don't forget that the CAT5 cable can be essentially as long as is practically required.
     

    Attached Files:

    w8one and Scott Eric Catalano like this.
  11. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    Well, I've got the HW more or less functional, but I can't say that for the SW......the RS-485 stuff seems to work relaibly in my initial tests, but further testing requires that the SW function have my code be at least compile cleanly.

    The code that Travis put up doesn't work in my environment, and I have no idea why......If I issue any of these commands <1> or <0> or <D> for testing the PROG data stream stops. I've looked at the code and don't see anything obvious, but I might as well be a non-programmer as far as C++ is concerned.

    I've decided to rewrite the current monitor code in plain C code(it doesn't appear complex at all), but I need to use some global variables to reference some parameters that the C++ code uses. I'm adding a flag for each power district to specify if an over-current condition has caused a shutdown, so the logic can restore the enable to the appropriate MD boards. These flags need to be accessed by the SerialCommand code (to initially clear the flags when power is turned on or off), and by the replaced C CurrentMonitor code which will perform the logical funtions, but I can't seem to get the global variables to compile clean.

    I've declared the variables as extern int values at the top of the DCCpp code, but the compiler returns that the variable(s) was/were not declared in this scope.

    Just how do you declare a variable as global AND be able to use it in a C++ 'function' call?

    I'm lost, and need some help........
     
    Scott Eric Catalano likes this.
  12. Deltic

    Deltic New Member

    4
    6
    5
    Hi Brendanf, The arrangement you describe sounds just what I am trying to achieve. So far I have had success setting up a mega base station. I am interested in seeing how to take this further with multiple shields with auto reset and eventually adding Adafruit 16 channel PWM board to control servos. Would you mind sharing your modified sketch?

    Thanks in advance
     
    Scott Eric Catalano likes this.
  13. brendanf

    brendanf TrainBoard Member

    62
    54
    8
    I would love to share my sketch but there is one major PITA problem. My laptop went for a giant crap 2 weeks ago and I lost everything I had worked on, and because I am such a giant procrastinator about backing things up all is gone. I haven't had time to buy a new computer yet (currently using my wife's ChromeBook) since my wife is keeping me very busy with the honey-do-list (mainly a fence), but hopefully next week when I am finished I will head out and buy something. After that, I will be more than happy to re-write my work and pass it along to you.

    As for using the Adafruit board, if you look for the thread about setting up turnouts in JMRI I shared a sketch there that I use on an Uno for controlling the servos specifically with the Adafruit board. That is my one saving grace that I don't have to re-write that...
     
    Scott Eric Catalano likes this.
  14. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    I have decided to separate the locomotive functions from DCC++, and if needed, a second DCC++ system can be used concurrently through JMRI.

    I realize that it limits the use of Gregg's fully automated functionality, but I think most won't use that anyway.

    My system is basically HW complete, and the SW should be done in a few weeks. Again, the system is designed to allow remote motor driver boards or just local ones.....that difference is simply based on distance. The system uses EBAY RS485 modules to achieve the distance, and is basically a standard DCC++ setup, but with a total of 8 motor drivers (if you want) that will monitor current individually, and reset the fault automatically.

    The SW & HW will be made available for free, including the files needed to get your ouw PCBs made.

    The attached photo shows the system with 2 motor drivers, showing the DCC++ PROG & MAIN signals (yellow LEDs), and the individual ENABLE signals (green for enable, red for disable). Besides the Arduino NANO module, the other modules shown are the RS485 parts. The cabling is standard CAT5.

    Hope you like it.....I'll eventually create a video showing operation....
     

    Attached Files:

    Scott Eric Catalano likes this.
  15. Deltic

    Deltic New Member

    4
    6
    5
    Damn, that's terrible luck. Hope you get up and running soon. In the meantime, I will check out the turnouts and give that a go.

    Cheers Phil
     
    Scott Eric Catalano likes this.
  16. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    I've finished the SW for the automatic short management, and it works beautifully. I haven't validatre the maximum distance for the current sense cabling yet, but I expect no major issues.

    In any case, here is the initial pass at a document detailing the system......please comment.
     

    Attached Files:

  17. Softec

    Softec TrainBoard Member

    27
    9
    6

    @crusader27529

    I am looking at a modification of your TrainGenie system. I will be using Nano boards to drive up to four IBT-2 motor drivers, each board monitoring the current levels. I plan on 6 of these boards around my layout as power districts, all driven by a common DCC++ signal. Their function is only train control, other Nano’s will control signals, turnouts, etc. II will be using portions of circuit s from your master board and the reverser board to create a new board design. I could not find on the forum if you ever released the Eagle or similar files for this. Are they available and if so could you point me to where they may be?

    Thanks,

    Dan
     
  18. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    I posted all the data a few years ago, I'll have to find where I posted it.

    I'll get back to you.......BTW, I use KiCAD for my schematics & layout......
     
  19. crusader27529

    crusader27529 TrainBoard Member

    247
    167
    11
    Found it.....it's on Google Drive. There aren't any CAD files, but there are schematics and the Gerber files to reproduce the board.

    I can share the file with you, but I need a Google Name or an email address........the compressed file is less then 1M, so it can be attached to an email.

    The way KiCad works, the files are dependent on several other files, and the basic CAD data usually isn't easily recreated.

    The schematics are quite simple, so recreating then in Eagle should be easy......there's also documentation about how it all works and goes together.

    Looks like the file is attached to this replay. Let me know is you need anything else.
     

    Attached Files:

  20. Softec

    Softec TrainBoard Member

    27
    9
    6
    @crusader27529
    Thank you so much. I will keep the board here updated on my progress. As with all your's and Gregg's previous work, this will be open source and all files and source code will be available to the public.

    Thanks Again,
    Dan
     

Share This Page