Before DCC came along the usual way to operate model trains was by dividing the track into sections and using switches to associate some sections with controller A and some other sections with controller B. The associations could be changed as required for different train movements. As might be imagined this all requires a lot of wire and a lot of switches.
It has occurred to me that the same effect could be achieved by giving each track section its own H-Bridge and sending appropriate PWM signals to them. So the PWM appropriate to controller A might be directed to H-Bridges 1, 2 and 4, for example.
AFAIK there is no attempt within an Arduino to synchronize the pulses for PWM from different Timers and I suspect a problem might arise if a locomotive straddles two sections as in the diagram. If the pulses are no synchronized it would seem likely to get 2 pulses per interval rather than one so it would suddenly speed up - or the H-Bridges would not like it and would shut-down.
However I assume if the pulses could be synchronized then there would be no difficulty driving a loco across the gap between sections.
(And just to be clear, I am not concerned about the obvious problem if the loco straddles two sections that are being controlled by different people. I just want a loco to be able to drive across the gap between two sections which are under the control of one person)
First, is my assessment broadly correct?
And second, has anyone any clever ideas for a simple means to synchronize the PWM pulses?
AFAIK there is no attempt within an Arduino to synchronize the pulses for PWM from different Timers and I suspect a problem might arise if a locomotive straddles two sections as in the diagram. If the pulses are no synchronized it would seem likely to get 2 pulses per interval rather than one so it would suddenly speed up - or the H-Bridges would not like it and would shut-down.
I wouldn't expect a problem with speed because the train has inertia and it can't instantly change speed. (Current only flows through the engine, right?)
But, the train could short one section of the track to the other so if the polarity of one track sections is reversed, you'd have a short and burn-out the H-bridges.
If it is moving slowly it could take a few seconds for the loco to cross the joint and I suspect there would be a noticeable change in speed - in effect the input power could be doubled. (I think). Also current may be picked up from the wheels of a loco and its tender, or from all wheels on a long diesel loco. And, in fact, the loco must be able to stop (and restart) while straddling the joint.
There won't be a polarity problem in normal operation but, in any case, the H-Bridge must be able to withstand a short circuit - they happen frequently for different reasons.
Thought about this a lot in years past. I never tried it, but my plan was to put a battery onboard, maybe the coal car? And just use the tracks for feeding the battery charging current. This way you just run dead gaps between different sections and take care of the polarity issues onboard the engine. Tracks run at full power and the train just uses what it needs.
But my question is about a system for another layout that is 20 years old or so and could do with re-wiring. Any solution must be capable of using non-modified locomotives.
I am just brainstorming a method of vastly simplifying the wiring with all the signalling controlled from a PC screen. Then I began to wonder how to simplify the way in which the PC Screen can associate different controllers with different track sections without the need for the existing mechanical (human activated) switches or relays.
I just did a project that needed synchronized Arduinos.
I had to keep 6 Arduinos running in synk for days at a time. My method was two slightly different versions of the same code. One master & five slaves. Each string gets its own processor. The master sends out a pulse to start the slaves. Each has its own line so timing can be staggered.
For what you are doing I'd think about doing something like a recursive node where it feeds the next node a timing pulse. I don't know how you'd control what nodes are on or off.. But its a thought.
If the H-Bridge driver has sense outputs, perhaps these could be used to also detect which section of track the train is on. If shorting both, then one driver could have higher priority or you could sequence control to get it across the gap.
AFAIK there is no attempt within an Arduino to synchronize the pulses for PWM from different Timers
Thanks @dlloyd, that looks like a useful link and I have bookmarked it as I don't have time to study it now.
I have also been experimenting with bit-banged PWM just using micros() for timing and I think that should work adequately. Bit-banging may also allow me to have more PWM outputs than you can have on a Mega.
If this idea works it would be perfectly sensible to dedicate a complete Uno or Mega to it when you consider the time saving compared to a traditional wire/switch solution.
wg0z:
the trick is to have ONE PWM signal and control the individual h-bridges wrt usage of same.
How do you suggest I do that without some form of switching? I am trying to avoid the complexity of switching.
Some of the time I want the PWM from controller A to go to tracks 1, 2 and 4. On another occasion I want the PWM from controller A to go to tracks 1 and 2 and the PWM from controller B to go to tracks 4 and 6. Think of the controllers as different human train drivers.
Robin2:
How do you suggest I do that without some form of switching? I am trying to avoid the complexity of switching.
Some of the time I want the PWM from controller A to go to tracks 1, 2 and 4. On another occasion I want the PWM from controller A to go to tracks 1 and 2 and the PWM from controller B to go to tracks 4 and 6. Think of the controllers as different human train drivers.
...R
Real life train drivers control one PWM, that of the engine they're driving
I'm not sure what your objection against switching is. Additional logic hardware? Design of the logic? Note that the switching does not need relays as in the old days It can be done before the H-bridges using logic gates controlled by an Arduino or possibly by a dedicated Arduino.
Not being a model train person myself, is there a reason you need to split up the tracks into different segments? Is there more than one train on the track that needs to be controlled independently?
There is a way of synchronizing AVR timers. The General Timer/Counter Control Register (GTCCR) has some Prescaler Reset bits that can be used to stop the timers and restart them at the exact same time. Write the TSM, PSRASY, and PSRSYNC bits to hold all the timer prescalers in reset, write all the timer values to 0, then write the TSM bit to 0. All the timers should start counting in lockstep now and the PWMs should all be synced.
That doesn't really solve the bridging problem though. If the PWM signals have different duty cycles, then even if they are synchronized there will be times when one is high and the other is low. Bridging the two segments at that time will cause a short.
How is the locomotive wired internally? Which wheels are used as contacts for the motor power?
sterretje:
Real life train drivers control one PWM, that of the engine they're driving
That's exactly what I intend but I guess I haven't explained it sufficiently.
With traditional wiring to drive a model train along a route requires connecting 2 or 3 (or more) separate track sections to the driver's controller. The one controller (and its one h-bridge) provides power for all of those sections and the train can drive from one end to the other. On another occasion (maybe 10 minutes later) a different driver wants to drive a different train along a route that uses perhaps 2 of those track sections plus some other sections. That requires pressing some switches which disconnect the tracks from the first controller and connect them to the second controller. Then the second driver can send his train along the new route.
When the switching is built to allow any of 4 controllers to be connected to a single section, and when there are perhaps 10 sections altogether the wiring becomes very complicated.
My idea is to attach a h-bridge to each section and connect each one separately back to an Arduino. Then if I want to send a train along 3 track sections all that is needed is to output the appropriate PWM signal on the relevant 3 pins. There is no need for any power switching and each track section just needs a simple connection back to the Arduino.
The original post (and its diagram) was intended to illustrate the locomotive straddling the joint between two sections which are both being fed with (say) analogWrite(86) from two separate h-bridges each getting its PWM signal from two different PWM pins on the Arduino.
And, of course, the idea is that there could be up to 4 trains moving at the same time on different sections of track - each controlled by a different person.
Maybe think of a simple scenario like this. Normally there are trains running in opposite directions on parallel tracks. But sometimes a train needs to cross from one side to the other and then cross further into a goods yard. The train will then continue to work in the goods yard while the mainline tracks are returned to regular use.
I hope that makes the concept clearer.
I think this will also make it clear to @Boardburner that the tracks are neither parallel nor staggered but end to end - so the train can be driven smoothly across several sections.
I do understand I haven't built a layout in 40 years though.
I would give @Jiggy-Ninja's solution a shot. You however will run into problems when more than one Arduino is involved in generating the PWM signals; possibly solvable by syncing them.
An easy way out (for up to 16 PWMs) might be a solution based on PCA9685; single clock source (by the looks of it) for 16 PWMs so they should be in sync.
sterretje:
I would give @Jiggy-Ninja's solution a shot.
Yes. That looks like a solution.
I infer from his suggestion and your support of it that you don't see an obstacle to my idea provided the PWM's are in sync but problems would probably arise if they are not in sync. Am I correct, as it is the principal point of this Thread? (And, of course, all the relevant H-Bridges will be working at the same duty cycle.)
I don't have access to the actual track diagram at the moment but my guess is that there are about 10 electrical sections. Neither do I have the equipment to carry out any tests at the moment - hence this Thread. I wanted to confirm that my idea makes sense before making a short presentation to the layout owners.
The H-Bridges that I am thinking of using (Toshiba TA8428K) require separate PWM inputs for Forward and Reverse so now I am wondering if it may be simpler just to Bit-Bang the PWM as that would allow me to have 20 PWMs if I want to. The alternative would seem to be some external logic circuits to "steer" the PWM to the correct pin of the TA8428K. And it should be simple to synchronize the Bit-Bang PWM.
I infer from his suggestion and your support of it that you don't see an obstacle to my idea provided the PWM's are in sync but problems would probably arise if they are not in sync.Am I correct, as it is the principal point of this Thread? (And, of course, all the relevant H-Bridges will be working at the same duty cycle.)
I infer from his suggestion and your support of it that you don't see an obstacle to my idea provided the PWM's are in sync but problems would probably arise if they are not in sync. Am I correct, as it is the principal point of this Thread? (And, of course, all the relevant H-Bridges will be working at the same duty cycle.)
That is correct. If two conductors are at the same voltage potential, it is safe to short them together because there is no potential difference between them.
I'm not sure bit-banging 20 PWM channels would work well though. Back-of-the envelope estimation suggests that if you dedicate the controller to just bit-bang the PWM, you might get a passable frequency with 6 or 7 bits of resolution without huge amounts of jitter. Trying to push for 8 bit resoution or higher and either your output frequency needs to drop or your jitter increases. I'm not sure how insensitive the motor will be to the jitter, but the locomotive is going to have a significant amount of inertia should it should be able to handle some amount of jitter, as long as the average duty cycle is kept steady.
The other downside to bitbanging is that adjacent tracks will not be switching at the same moment. Even if you service the pins in the same order that they are hooked to the track, there will still be a few microseconds where the adjacent tracks will be in different states. That difference won't exist if you use digital switching circuits or a hardware PWM generator. That PCA chip sterretje mentioned is a decent one to start looking at, or some other LED driver.
How many independent controllers are you planning to have? How is it selected which controller will connect to which track? I think you mentioned 4 controllers in a previous post. If that's so, there are chips with dual 4-to-1 multiplexers that you can use to select the input for each track. One of those per H-bridge will be plenty. Each needs 3 bits for channel selection (2 select + 1 inhibit), so 4 SIPO shift registers can be daisy chained to cover all 10 track sections.
Or you can do Option 3, also known as "the fun one": CPLD
Jiggy-Ninja:
I'm not sure bit-banging 20 PWM channels would work well though. Back-of-the envelope estimation suggests that if you dedicate the controller to just bit-bang the PWM, you might get a passable frequency with 6 or 7 bits of resolution without huge amounts of jitter.
What sort of frequency have you in mind?
The other downside to bitbanging is that adjacent tracks will not be switching at the same moment. Even if you service the pins in the same order that they are hooked to the track, there will still be a few microseconds where the adjacent tracks will be in different states.
What would you consider the maximum acceptable timing error where two outputs are shorted together by (say) a loco wheel?
If bit-banging is not an option then I could use one or two Megas to get the required number of PWM outputs.
How is it selected which controller will connect to which track?
What I have in mind is an array that identifies which controller should power each track section (one element per section with a number 0-3). With the click of a mouse a controller can be linked to a section by writing the appropriate number into the array element for that section. There will be another array that identifies which PWM pin is associated with each section.
The selection process will be required whether or not I use bit-banging.
If there is an option that does not require external chips - apart from the H-Bridge - it would significantly reduce the construction effort.
At this stage this is just an idea that looks like it would save a great deal of complex wiring of traditional switches and relays. And the "customer" may have absolutely zero interest in it even if it is technically sound and the only cost is for the parts.
Not a model trainer, so I couldn't say. You would have to manually experiment with your locomotives to see how low the frequency can be and still have acceptable performance. I don't know how fast those motors can react. I probably wouldn't want to go lower than 100 Hz.
The downside to frequencies that low is that magnetics make can make noise. If you ever stood near a large transformer and heard the 120 Hz (100 Hz for you) buzz, that noise is made by the vibrations of the core's laminations due to the magnetic field repeatedly strengthening and weakening. Lower PWM frequencies will cause there to be larger ripple on the motor current, which might cause the core to whine louder. If you've ever heard of "coil whine" in computer components (like a graphics card), it's the same concept.
What would you consider the maximum acceptable timing error where two outputs are shorted together by (say) a loco wheel?
If bit-banging is not an option then I could use one or two Megas to get the required number of PWM outputs.
The bridge you're looking at will actually probably be OK, now that I read more into the datasheet. When both INxs are low, the outputs are High-Z (table on page 7). If you hold one IN low and PWM the other one, you should be safe as long as you aren't driving adjacent tracks in opposite directions (one CW and the other CCW). Though even in that case, the bridge has overcurrent protection (page 4), so they should just shut down rather than breaking.
I can't say for certain if bit banging is feasible or not. My estimate put it as just barely plausible, so it could go either way. A Mega is probably way too much overkill though. An external chip that's designed to generate PWM signals is all you need.