Geoff Bunza geoffb

Model animation—bringing models to life—adds enormously to the interest and enjoyment of any layout or individual model! This entry describes how to build what I call a “DCC Function Generator,” a combination of DCC base station and throttle for about $8 that can be used for mixed DCC animation control, DCC testing, Absolute/Emergency Stop blocking for lift-outs and bridges even for Keep-Alive equipped DCC locos, trolley multi-stop control, and all around DCC havoc!

CC_Gen-s.JPG 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Geoff Bunza geoffb

@ProfK re: Alternate skecth and explanation

Hi again Prof,

Simple timed runs will work but inevitably will have "drift" with the train position over time. The problem comes from friction, motor and drive train non-linearities, start and stopping friction not being repeatable, and foward vs. reverse motion not being equivalent. In other words, if you drive a loco/train forward at a specific speed for XX seconds, stop it, reverse it and drive it for XX seconds in reverse, you are likely not to arrive exactly at the same point at which you started!! ...and the difference between original start and subsequent stopping point will grow. There are a number of factors you can try to adjust, but virtually every mechanical design we can attempt will likely, eventually, yield the same observable behavior.

So.... it would be much better to place end detectors to catch the end travel of the shuttle train and use that to reverse the direction. The following sketch does a better job than the original sketch I posted with additional mods along the lines you were looking for: broadcast 0 address, 2 end sensors, optional stopping sensor(s) along the way, estop capability, switch (DPDT relay) for alternate DCC control, ability to set F0-F4, direction LEDs. and speed control knob. You can place the loco on the track in any direction as long as it does not cover an end sensor at the start of the run. I have not tried this sketch out. It does compile and download. Let me now how it goes.

I think this will make for a much more robust control mechanism for any display layout.

'Hope this helps. Have fun! 
Best regards,
Geoff
 

/********************
DCC_FTN_Gen_Univ_Shuttle.ino  Univeral Shuttle  GBunza 2019
This version is used to operate a trolley or shuttle on its own independent track with stops and reversals
Each of the following inputs are typically connected to a position sensor or short block sensor which goes low
  When detecting the presence of the loco/trolley/shuttle (like an optical sensor):
The end_sensor1 and end_sensor2 detect end of runs
stop_sensor1  intermediate short stop switch / sensor
If you don�t need the intermediate stops, then don�t connect stop_sensor1 to anything
You can also connect multiple sensors with "open collector outputs" or "opne drain outputs
together (in parallel) for multiple stops all to the stop_sensor1
A switch or pushbutton is connected to D17 to act as an emergency stop ESTOP and may also be omitted
locoAdr= 0 which is a broadcast address to all locos on the track !!
pin 17 can have a switch for relay control to release auto shuttle 0 == NO auto shuttle
An emergency stop switch can be attached to pin15
* The DCC waveform is output on Pin 9, and 10 (inverted)
********************/
// Pro Mini pin map:
// 2 DCC_EN  DCC hardware enable to H Bridge
// 3 fwdLEDout   LED LOW = ON
// 4 bkwdLEDout  LED LOW = ON
// 5 master_DPDT_relay  DPDT relay to switch between auto shittle and manual DCC control
// 6 end_sensor1                // sense  motion, stop, &  reverse
// 7 end_sensor2               // sense  motion, stop, & reverse
// 8 p0 / F0
// 9 DCC Library driver
// 10 DCC Library driver inverted
// 11 p1 / F1
// 12 p2 / F2
// 13 p3 / F3
// 14 - stop_sensor1             // intermediate short stop switch / sensor
// 15 - estop_pin     15   // ESTOP Pushbutton
// 16 - A2 analog speed_pin
// 17 - special relay control SWITCH to release auto shuttle 0 == NO auto shuttle

#include < DCCPacket.h>
#include < DCCPacketQueue.h>
#include < DCCPacketScheduler.h>

// for 128 speed steps uncomment the following line; for 28 speed steps comment the following line with "//"
#define speedsteps128

#define DCC_EN 2
#define end_sensor1   6    // sense  motion, stop, &  reverse
#define end_sensor2   7    // sense  motion, stop, & reverse
#define stop_sensor1  14   // intermediate short stop switch / sensor
#define estop_pin     15   // ESTOP Pushbutton
#define speed_pin     A2   // Analog pin 2 == D16

DCCPacketScheduler dps;
uint8_t functs0to4 =  0x00;
uint8_t functs5to8 =  0x00;
uint8_t functs9to12 = 0x00;

int8_t last_locoSpeed=0;
uint16_t locoAdr= 0 ;   //PK - Addr set to "0" (Zero) for broadcast
unsigned long shuttletime=   150000 ;   //PK - default shuttle time 2min30sec in each direction
#define DCC_EN 2
#define fwdLEDout 3
#define bkwdLEDout 4
#define master_DPDT_relay 5
#define p0      8    // F0
#define p1     11    // F1
#define p2     12    // F2
#define p3     13    // F3
#define p7     17    //   special relay control SWITCH to release auto shuttle 0 == NO auto shuttle

unsigned long timing_start,timing_end;
int8_t dcc_speed = 1; // loco speed
byte t_cnt;
byte loco_dir = 1;    // loco direction - start forward =1

void setup() {
  pinMode(end_sensor1, INPUT_PULLUP);      // sense forward motion, stop, & go in reverse
  pinMode(end_sensor2, INPUT_PULLUP);      // sense reverse motion, stop, & go forward
  pinMode(stop_sensor1, INPUT_PULLUP);      // intermediate stop switch / sensor
  pinMode(estop_pin, INPUT_PULLUP);  // Optional Pushbutton for ESTOP
  pinMode(fwdLEDout, OUTPUT);
  pinMode(bkwdLEDout, OUTPUT);
  pinMode(master_DPDT_relay, OUTPUT);
  pinMode(p0, INPUT_PULLUP);  
  pinMode(p1, INPUT_PULLUP);    
  pinMode(p2, INPUT_PULLUP);  
  pinMode(p3, INPUT_PULLUP);  
  pinMode(p7, INPUT_PULLUP); //special relay control SWITCH
                             // to release auto shuttle 0 == NO auto shuttle
  loco_dir = 1;            // loco direction - start forward =1
  dcc_speed = 1;           // Speed 0 forward
  pinMode(DCC_EN,OUTPUT);  // will be kept high, DCC_EN pin
  digitalWrite(DCC_EN, 1);
  delay(3000);
  dps.setup();
  delay (5);
#ifdef speedsteps128
  dps.setSpeed128(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Speed 0 forward
#endif
#ifndef speedsteps128
  dps.setSpeed28(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Speed 0 forward
#endif
  dcc_cmd_rpt (2000 );
  digitalWrite(fwdLEDout, 1);
  digitalWrite(bkwdLEDout, 1);
}
void loop() {  
      // Start by checking if we can go
      while (digitalRead(estop_pin)== LOW) {
        dps.eStop();          // ESTOP Power Cut Off
                              // Global Broadcast
        dcc_cmd_rpt(4000);    // wait for 4 seconds
      }
      while ((digitalRead (p7) & 0x01) == 0)  { //special relay control to release auto shuttle
        digitalWrite (master_DPDT_relay, 0);  //turn on relay to release control
        digitalWrite(fwdLEDout, 0);     // turn on BOTH direction leds for indication
        digitalWrite(bkwdLEDout, 0);
      }
      // OK to proceed
      set_function (0, digitalRead (p0) & 0x01) ;
      set_function (1, digitalRead (p1) & 0x01) ;
      set_function (2, digitalRead (p2) & 0x01) ;
      set_function (3, digitalRead (p3) & 0x01) ;
      
      dcc_speed =  (analogRead (speed_pin)/8);   // Speed range from 0-127
            if (dcc_speed < 127) dcc_speed++;   // Positive speed is forward
      if (loco_dir == 0) dcc_speed =  - dcc_speed;
      digitalWrite(fwdLEDout, loco_dir^1);
      digitalWrite(bkwdLEDout, loco_dir);
      if (((digitalRead(end_sensor1) & 0x01)==0 )

||((digitalRead(end_sensor2) & 0x01)==0 )) { // check if we came to the end, & go in reverse
        dcc_speed = 1;         // Stop
        dcc_cmd_rpt (5000 );   // wait 5 seconds before reversing
        loco_dir =  loco_dir ^1;         // reverse the direction  XOR 1
        dcc_speed = ( (analogRead (speed_pin)/8)+1 );
        if (loco_dir == 0) dcc_speed =  - dcc_speed;
        dcc_cmd_rpt (500 );
      }
      if ((digitalRead(stop_sensor1) & 0x01)==0 ) {    // intermediate short stop switch / sensor
        last_locoSpeed = dcc_speed;
        dcc_speed = 1;                        // stop
        dcc_cmd_rpt (6000 );                  // wait 6 seconds
        dcc_speed = last_locoSpeed ;          // resume speed
        dcc_cmd_rpt (5000 );                  // for at least 8 seconds
      }  
      dcc_cmd_rpt ( 10 );
}

void set_function (byte f_number, byte f_value) {
  switch ( f_number )  {
    case 0:  // Function 0
      functs0to4 = (functs0to4 & 0b00011110)

| f_value;
      break;
    case 1:  // Function 1
      functs0to4 = (functs0to4 & 0b00011101) | (f_value< < 1);
      break;
    case 2:  // Function 2
      functs0to4 = (functs0to4 & 0b00011011) | (f_value< < 2);
      break;
    case 3:  // Function 3
      functs0to4 = (functs0to4 & 0b00010111) | (f_value< < 3);
      break;
    case 4:  // Function 4
      functs0to4 = (functs0to4 & 0b00001111) | (f_value< < 4);
      break;
    case 5:  // Function 5
      functs5to8 = (functs5to8 & 0b00011110) | (f_value);
      break;
    case 6:  // Function 6
      functs5to8 = (functs5to8 & 0b00011101) | (f_value< < 1);
      break;
    case 7:  // Function 7
      functs5to8 = (functs5to8 & 0b00011011) | (f_value< < 2);
      break;
    case 8:  // Function 8
      functs5to8 = (functs5to8 & 0b00010111) | (f_value< < 3);
      break;
    case 9:  // Function 9
      functs9to12 = (functs9to12 & 0b00011110) | (f_value);
      break;
    case 10:  // Function 10
      functs9to12 = (functs9to12 & 0b00011101) | (f_value< < 1);
      break;
    case 11:  // Function 11
      functs9to12 = (functs9to12 & 0b00011011) | (f_value< < 2);
      break;
    case 12:  // Function 12
      functs9to12 = (functs9to12 & 0b00010111) | (f_value< < 3);
      break;
  }
}
void dcc_cmd_rpt (unsigned long loop_delay) {
#ifdef speedsteps128
      dps.setSpeed128(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Set Speed
#endif
#ifndef speedsteps128
      dps.setSpeed28(locoAdr,DCC_SHORT_ADDRESS,dcc_speed); // Set Speed
#endif
      dps.setFunctions(locoAdr, DCC_SHORT_ADDRESS, functs0to4,functs5to8, functs9to12);
      wait_dcc(loop_delay);
}
void wait_dcc (unsigned long wait_time) {
      unsigned long tempul;
      tempul = wait_time;
      timing_start = millis();
      timing_end = timing_start+ tempul;
      while (timing_start< timing_end) {
      dps.update();
      timing_start = millis();
      }
}

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Geoff Bunza geoffb

@!Prof K re: Headligh and Whistle/Horn

Hi Prof,
 
I forgot about the headlight and horn "test."
At the very end of setup() you can add something like:
  set_function (0, 1) ;   // Turn on Headlight
  set_function (1, 1) ;   // Turn on Bell
  set_function (2, 1) ;   // Turn on Whistle/Horn
  dcc_cmd_rpt (4000 );  // 4 second ON time
  set_function (1, 0) ;   // Turn off Bell
  set_function (2, 0) ;   // Turn off  Whistle/Horn
  dcc_cmd_rpt (100 );   // short delay
}                                      // end of setup()
 
I think you can get the idea. Ask more questions if needed.
Have fun! 
Best regards,
Geoff

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Prof_Klyzlr

"Train won't make it" issues

Dear Dr Geoff, I appreciate the concern re "positional drift over time" and matching "will it stop at the same places at each end consistently", But the system as-envisaged is "self-correcting" without need for active end-detection or switching, Because the "run time" in each direction is sooooooo long that it is impossible (barring major calamity like derailment), for the shuttle train to _not_ arrive at the end-point on each pass over the "modelled scene"... ...indeed, the use of simple "diode stopping" means that a loco/train which has "arrived" at an end point, If manually pulled-back from the stop-position onto the active shuttle Will continue to "Drive towards the end" until the Shuttle "flips over", And allows it to "come back"... (call it "stubborn / persistent dead reckoning" ) If anything, the "time variable" is not "how long is the train instructed to move in either direction", But rather "how long will the train dwell at either end stopping-location" (the train should/will always "start on-the-1" in sync with the shuttle's "change of state", from the known predictable diode-stop position, and if it runs excessively fast "accross the scene" as to arrive at the far-end diode-stopping-section surprisingly early, it will just patiently wait there "a bit longer than predicted" until the Shuttle times-out and "flips over"). I'm not relying on the shuttle-circuit timing to "stop the train where it needs to stop", I just need the shuttle to "keep pushing the train towards the (diode braked) end-position (until it's time to flip direction and come back)" Brute force consistency and reliability, that's the aim... Happy modelling, Aim to Improve, Prof Klyzlr PS the self-same stopping sections, being independent of the shuttle circuit, should work equally well to ensure a train can't "swandive off the end-of-track" when under manual control too...
Reply 0
Geoff Bunza geoffb

@ProfK re: DCC Stopping Blocks !!

Hi Prof,

I completely forgot you were going to use DCC stopping blocks. Obviously then, you only need the timer to run the train into the end block.  'Nuff said.
Have fun! 
Best regards,
Geoff

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Prof_Klyzlr

Aww shucks...

Dear Dr Geoff, But, but, but, I even noted the Asym Braking part in the header comments of the sketch, Don't I get points for full and complete documentation within the code??? Happy Modelling, Aiming to code better, Prof Klyzlr
Reply 0
dark2star

Wild thought

Hi,

Geoff, thanks for your input.

While reading the discussion regarding stopping points, timing and braking, I had a wild thought... If there are partially or fully isolated end sections anyway, one could put them on a normally-off relay and just power the relay for a second to start the train in the opposite direction... Of course, that won't work with keep-alive, sound and light.

Anyway, I think with positive stopping (diode sections should be the best solution), and with long-enough dwell times at the ends, the system should self-correct, even if the train fails to reach one end for whatever reason.

With my kids playing with wood-track toy trains I think we are blessed by the reliability of our trains

Have fun!

Reply 0
Geoff Bunza geoffb

@dark2star

Hi dark2star (name?),

I think ProfK has already made the same point (re  stopping blocks)... and has from the beginning of his request.

Have fun! 
Best regards,
Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Prof_Klyzlr

Another practical application

Dear Dr Geoff, Just a quick stair-step application thought for the dumb-timer unit. Consider: - you have DCC locos with dirty wheels - You've disabled "run on Analog" to avoid "spontaneous Runaway" issues. What to do RE rapid simple wheel cleaning? - The common "one truck on rails, other on cleaning cloth, 5sec wheelspin, swap trucks, 5sec wheelspin, done" technique won't work using a battery or analog pack, (run on Analog = disabled), And having to Setup and address-select/run each loco on a "full DCC rig" can be time-consuming and cumbersome (how many simultaneous hands required?). Sooooooo, a couple of options: - A set of 4X BCD number selectors and a "go" button = "select the loco address, press the Go button, loco does a 5sec SS10 run" (or maybe a "5sec Fwd, 5sec Bkwd" shuttle) - A hard-coded "broadcast Address 0" + Go-button (same basic operation, just assumes that "all locos respect Broadcast commands", so does not need manual address setting) Just thinking out loud, after a session working on an exhibition-layput-on-build which is dealing with extreme levels of wheel/rail dirt issues.... Happy modelling Aim to improve, Prof Klyzlr
Reply 0
coxsteve49

Odd DCC_FTN_gen_back_Forth_Stops behaviour

Hi Geoff

I wonder if you might be able to help me?  I used your PCB to build a couple of the DCC_FTN_gen_back_Forth_Stops variant and they each seem to have reverted to odd loco movement behaviour.

The loco moves in the correct direction, but slows and stops every few cm, then starts & stops again, etc.

Interestingly, I have just built a unit using an UNO for testing purposes.  This unit behaved normally for a while, but now exhibits the same behaviour as the earlier units.

Any ideas?

Cheers

Steve

Reply 0
Geoff Bunza geoffb

@Steve re:Starts and Stops

Hi Steve,

Quote:
The loco moves in the correct direction, but slows and stops every few cm, then starts & stops again, etc
The only time I have seen this behavior is when the SN754410 driver that I use overheats due to excessive current draw. The chip is rated for at least an amp, but I did not provide for any heat sinking (which would be appropriate if you were going to drive it to its limit-- and this could be added. My locos are operating with this typically at about 350ma.
 
The chip has a thermal protective cutoff built into it. High current draw will heat the chip, and if it gets too hot it will shut down automatically. If you are operating near its thermal cutoff it would stop the loco, cool off a bit and then restart -- the same behavior you report. A mechanical bind could do the same. I assume the track is clean.
 
'Hope this helps. Have fun! 
Best regards,
Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
coxsteve49

Thanks Geoff for your prompt reply

Hi Geoff

Many thanks for your prompt reply.  I'll check that out.

Cheers

Steve

Reply 0
taholmes160

Multiple packet generators on the track bus

Dr. Geoff:

Thanks for the tip to this project, it looks great -- I am wondering -- does this device function as a "throttle" (telling the command station to generate xyz packet abc times, or does it act as a "Command Station" generating packets in response to inputs. 

Here is the concern, its my understanding that there can only be one command station on the bus, along with boosters.

Heres the likely scenario -- I'd like to have my layout wired so that I can throw switches either from the throttle (using the SMA20 accessory decoder) or from front panel push buttons - The SMA 20 handles the throw from throttle.  My thought was to use this project to generate the DCC to trigger the SMA 20(s) in response to the pushbutton on the fascia.

I would appreciate your suggestions on how to resolve this -- I specifically do NOT want to have to involve JMRI -- I might use it, but do not want the layout being dependant on it

Thanks
TIM

Reply 0
Geoff Bunza geoffb

@Tim re:Questions

Hi Tim,

does this device function as a "throttle" (telling the command station to generate xyz packet abc times, or does it act as a "Command Station" generating packets in response to inputs. 

It is both... precisely. It generates the DCC packets and pits them out on a DCC bus, using the pre-programmed set of commands as it you were punching buttons on a DCC throttle. In fact you could create a button which the Arduino read to output a specific DCC command (like turn on headlight) just like you could with a throttle connected to a DCC base station -- so it combines all elements of both a base station and throttle.

its my understanding that there can only be one command station on the bus, along with boosters.

Correct. The base station needs to generate all the DCC bus timing and packets. There is no timing arbitration for multiple base stations in the DCC specified protocol.

The SMA 20 handles the throw from throttle.  My thought was to use this project to generate the DCC to trigger the SMA 20(s) in response to the pushbutton on the fascia.

No, there can only be one DCC packet/command generator on a single DCC bus -- the DCC base station. You could have multiple throttle connected, each of which could generate accessory switch commands, but that would require connecting via the base station throttle connect like Digitrax's loconet, which this project does not do.

Other modelers have suggested adding switches to the SMA20 decoders (akin to your panel switches). This generates an inherent ambiguity, however, because the base station always thinks it knows the state of all the decoders and switches. A panel switch activating a turnout independent of the base station would leave the turnout potentially in a state inconsistent with the base station, particularly since there is no to little communication back to the base station from a decoder.

It should be possible to build a decoder that communicated the turnout status directly back to JMRI or to the base station if it also had a separate comm channel like a USB sensor channel or via loconet, which with additional work could update the state of the turnout.

One could consider a local switch as an "override" but no matter what you call it, the base station will still not really know what is the state of the decoder switch or function.

You could also conceivably have a separate DCC bus just for turnout or signalling, using a DIY base station, like this of DCC++, where you add special panel switch inputs to the base station code as a separate control path.

Have fun! 
Best regards,
Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
taholmes160

Solution?

HI Dr. Geoff:

Thanks for the reply -- I was kinda afraid that would be the answer -- I think the best answer, at least for controlling switches and signals is to use an NCE MiniPanel (I run NCE DCC) to handle inputs from panel switches and other sensors (Occupancy detectors etc) signaling the SMA20s to throw switches, turn on lights etc.  

Do you see an other / better alternative?

TIM

Reply 0
Reply