greg ciurpita gregc
i'd like to describe an ABS system developed for a club layout.   It began when troubleshooting older PSX circuit breakers.     The club has 2 multi-track mainlines, broken into eight blocks each and separate blocks in each direction.   The thought of implementing an ABS occurred when we realized the PSX and PSX-ARs have block occupancy detectors.
 
the club has several open houses during the year.  Aisles become crowded and since members just run 2-3 trains around each mainline, members don't follow their trains and it was hard to know how far ahead the next train was.   The photo shows the length of the layout with the track disappearing into a tunnel.   There's now a pair of signals, red on the left and  green on the right signal, just visible in the photo.
 
[ardpkgI]

greg - LaVale, MD -- my layout   --  threads of interest

Reply 1
greg ciurpita gregc
"Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowchart; it'll be obvious." -- Fred Brooks, The Mythical Man Month (1975)
 
The system first started as some simple hardware  that lit  an LED on a build to let members know they were getting too close.   The next iteration put an esp32 and MCP 23017   on a solderless bread board to control 2 signals on either side of the tunnel in the image above.     
 
I believe this first iteration started using a table to describe the block occupancy input and signal LED output pins.    Because the signals could be common anode or cathode the table captured that parameter.    The table "morphed" over time to capture what was needed which include
  • block ID
  • next block ID
  • block occupancy input pin
  • the index in another table for the signal LED pins
  • that the signal LEDs were active High/Low common cathode/anode
  • tower (node) ID
  • description
all the code is available on https://github.com/gciurpita/Node2
 
the description table is in its own file.   It includes comments describing the MCP 23017 pins, the tower/nodes, the signal LED pins and the block description table.   ASCII track diagrams help relate block #s to trackage.
 
the tower/node IDs became needed when there were multiple nodes all using the same code and description table.   Each node would only monitor the inputs in the table entries for that nodes.
 
It also had information about other nodes which is needed if it controls a signal that is affected by a block detector on a different node  ...  or by the next block. 
#include "signals.h"

//   MCP23017
//   8 - GPB0  1  28 GPA7 - 7
//   9 - GPB1  2  27 GPA6 - 6
//  10 - GPB2  3  26 GPA5 - 5
//  11 - GPB3  4  25 GPA4 - 4
//  12 - GPB4  5  24 GPA3 - 3
//  13 - GPB5  6  23 GPA2 - 2
//  14 - GPB6  7  22 GPA1 - 1
//  15 - GPB7  8  21 GPA0 - 0

//        Vdd  9  20 INTA
//        Vss 10  19 INTB
//         NC 11  18 Nreset
//        SCL 12  17 A2
//        SDA 13  16 A1
//         NC 14  15 A0

#define P(chp, port, pin)    (chp<<4 | port<<3 | pin)

// -------------------------------------
Twr twrs [] = {
    { TwrNul, 0, 0           },
    { CB, "CB", "Cumberland" },
    { HY, "HY", "Hyndman"    },
    { GA, "GA", "Garrett"    },
    { TN, "TN", "Test-Node"  },
    { DP, "DP", "Display"    },
};
const int Ntwr = sizeof(twrs)/sizeof(Twr);

// -------------------------------------
SigPin sigPin [] = {
 //      Red   Yel   Grn   Whi
    { {    7,    6,    5,    4 }, },
    { {    7,    6,    5,   __ }, },
    { {    3,    2,    1,    0 }, },
    { {   14,   __,   15,   __ }, },
    { {    4,   __,   __,   __ }, },    // brewery
};
const int NsigPin = sizeof(sigPin) / sizeof (SigPin);

// -------------------------------------
SigMap sigMap [] = {
//       __,_____._______________,_______________________,________      east
// 100 /     110       120                   130
//      ___,_____,_______________,_______________________,________/ 141 west
//           111       121                   131
//      cumerberland     hyndman  sand patch   myersdale   garrett

    { 141, 140,   33,     0,  HI,   TN, "Exp GA" },

 //   blk  nxt   pin  idx+1  on,  twr  desc
    { 100, 111,    8,     2,  LO,   CB, "B&O CB Loop" },
    { 111, 121,    9,     0,  HI,   CB, "B&O CB West" },

    { 121, 131,   12,     0,  HI,   HY, "B&O HY West" },
    { 131, 141,    8,     1,  HI,   HY, "B&O HY Sand" },

    { 141, 130,   10,     2,  LO,   GA, "B&O GA Loop" },
    { 130, 120,    9,     0,  LO,   GA, "B&O GA West" },

    { 120, 110,    9,     3,  HI,   HY, "B&O Sand Patch East" },

    { 110, 100,   10,     5,  HI,   CB, "B&O CB East" },

    { 100, 111,   __,   100,  HI,   DP, "B&O CB Loop" },
    { 111, 121,   __,   101,  HI,   DP, "B&O CB West" },
    { 121, 131,   __,   103,  HI,   DP, "B&O HY West" },
    { 131, 141,   __,   113,  HI,   DP, "B&O HY Sand" },

    { 141, 130,   __,   112,  HI,   DP, "B&O DP Loop" },
    { 130, 120,   __,   114,  HI,   DP, "B&O DP West" },
    { 120, 110,   __,   115,  HI,   DP, "B&O Sand Patch East" },
    { 110, 100,   __,   102,  HI,   DP, "B&O CB East" },

//       __,_____________________,__________,____________,_____      east
// 200 /            210              220         230
//      ___,_____________________,__________,____________,_____/ 240 west
//                  211              221         231
//      cumerberland                  deal       myersdale   connelsville

    { 200,   0,  11,     4,  HI,   CB, "WM  CB Loop" },
    { 210,   0,  24,     0,  HI,   CB, "WM  CB East" },
    { 211,   0,  25,     0,  HI,   CB, "WM  CB West" },

    { 220,   0,  11,     0,  HI,   HY, "WM  Deal West" },
    { 221,   0,  10,     4,  HI,   HY, "WM  Deal East" },

    { 230,   0,  11,     0,  HI,   GA, "WM  GA East" },
    { 231,   0,  12,     0,  HI,   GA, "WM  GA West" },
    { 240,   0,  13,     0,  HI,   GA, "WM  GA Loop" },

    { 200, 211,   __,   104,  HI,   DP, "B&O CB Loop" },
    { 211, 221,   __,   105,  HI,   DP, "B&O CB West" },
    { 221, 231,   __,   108,  HI,   DP, "B&O HY West" },
    { 231, 240,   __,   110,  HI,   DP, "B&O HY Sand" },

    { 240, 230,   __,   111,  HI,   DP, "B&O DP Loop" },
    { 230, 220,   __,   109,  HI,   DP, "B&O DP West" },
    { 220, 210,   __,   107,  HI,   DP, "B&O Sand Patch East" },
    { 210, 200,   __,   106,  HI,   DP, "B&O CB East" },

};
const int NsigMap = sizeof(sigMap) / sizeof (SigMap);
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 2
greg ciurpita gregc
sigUpdate() determines the signal indications.  
 
The table entries include variables for the block occupancy.    The code skips entries not for that tower/node.    It initialize the signal state to Clear.   If the block is occupied is sets the signal state to Stop and if the next block is occupied, it sets the state to Approach.    It then sets the LEDs pins appropriately for the signal state.  (additional code handles a display board that hang on the wall).
 
// update signals on node
void sigUpdate (void)
{
 // printf ("%s:\n", __func__);

    SigMap *s = sigMap;
    for (int n = 0; n < NsigMap; n++, s++)  {
        if  (s->twr != twr || 0 == s->idx)
            continue;

        // update state
        s->state = Clear;
        if (s->occ) {
            s->occLst = s->occ;
            s->state  = Stop;
        }
        else if (s->pOccNxt)  {
            if (*s->pOccNxt)
                s->state = Approach;
        }

        // update signal if state change
        if (s->stateLst != s->state)  {
            s->stateLst = s->state;
            delay (50);

            printf (" %s: twr %d %s, blk %d, state %d %s,  %s\n", __func__,
                twr, twrs [twr].sym ,
                s->blk, s->state, StateStr [s->state], s->desc);

            // display pins
            if (100 <= s->idx) {
                i2cWriteBit  (s->idx - 100, Stop == s->state ? s->On : !s->On);
                continue;
            }

            // turn all LEDs off
            SigPin *p = & sigPin [s->idx - 1];

            for (unsigned i = 0; i < P_Size; i++)  {
                if (0xFF != p->LedPins [i])
                    i2cWriteBit  (p->LedPins [i], ! s->On);
            }

            // turn LEDs on
            switch (s->state) {
            case Stop:
                i2cWriteBit  (p->LedPins [P_Rd], s->On);
                break;
            case Approach:
                i2cWriteBit  (p->LedPins [P_Am], s->On);
                break;
            case Clear:
                i2cWriteBit  (p->LedPins [P_Gn], s->On);
                i2cWriteBit  (p->LedPins [P_Wh], s->On);
                break;
            }
        }
    }
}
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 2
greg ciurpita gregc
sigBlkMon() monitors the block occupancy inputs and updates the corresponding table entry when occupied.   The table entry is checked by sigUpdate() described above      
 
A timestamp is continually captured while the block is occupied and the block is only recognized as unoccupied after 2 secs.  This handles any false reports.
// scan blocks on node
void sigBlkMon (void)
{
 // printf ("%s:\n", __func__);

    SigMap *s = sigMap;
    for (int n = 0; n < NsigMap; n++, s++)  {
        if  (s->twr != twr || __ == s->PinBlk)
            continue;

        byte occ = ! i2cReadBit (s->PinBlk);   // active LOW

        // capture timestamp when occupied
        // don't recognize clear until 2 sec later
        if (occ)
            s->msec = msec ? msec : 1;

        if (s->occ != occ)  {
            if (! occ)  {           // ignore false clear
                if (s->msec && msec - s->msec < 2000)
                    return;
                s->msec = 0;
            }

            s->occ = occ;
            printf ("%s: blk %d, pin %d, occ %d\n",
                    __func__, s->blk, s->PinBlk, s->occ);
            sigReport (s);
        }
        else if (2 & debug)  {
            printf ("%s: blk %d, pin %d, occ %d\n",
                    __func__, s->blk, s->PinBlk, s->occ);
        }
    }
    if (2 & debug)
        delay (1000);
}
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 2
greg ciurpita gregc
the I2C routines support the use of the MCP-23017.    These routine accept a single integer bit address that translates to a chip, one of two 8-bit ports and a bit on that port.
 
The eeprom.c routines support  independently reading/writing type-length-values (TLV)   representing parameters that identify unique  information for each tower/node read at startup.
 
pcRead.c supports commands entered thru the Arduino IDE serial monitor.    Commands were used during the development to exercise and test code.     it also handles concatenated commands used to write unique parameters to EEPROM.   The following is an example of four semicolon separated commands to set WiFi credentials and the tower ID, and then update the EEPROM. 
CS _ssid xxxx;_pass yyyy;_host Garrett;U
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 3
greg ciurpita gregc
the data flow diagram below illustrates how monitoring blocks and updating signals are separated in the code.
 
"State" represents a data store  maintaining the status of  the blocks .   The arrowed lines indicate  the functions writing into and and reading from it.
 
block status is updated both locally using sigBlkMon() to monitor block occupancy inputs from local PSX and PSX-AR.    sigBlkMon() updates "State" but also broadcasts the changes of block status via WiFi.       wifiMonitor()  listens for WiFi messaged from other tower/nodes and also updates "State.
 
"State" now not only know the status of blocks monitored by local hardware monitored by other tower/nodes allowing it to indicate Clear, Stop and Approach signals.s
 
rectangular boxes in the diagram indicate hardware input/output (I/O).    the WiFiRx and WiFiTX labels indicate radio data input/output.
 
n2.png

greg - LaVale, MD -- my layout   --  threads of interest

Reply 2
greg ciurpita gregc
here is a summary of the remaining files,  sizes, function they contain and their argument
 
72 Node2.ino
        void  ledStatus       ()
        void  loop            ()
        void  setup           ()

  107 sigMap.cpp

  204 eeprom.cpp
        int   eepromAddr      (byte type)
        void  eepromClear     ()
        void  eepromLoad      ();
        void  eepromInit      ()
        void  eepromLoad      ()
        bool  eepromRead      (byte type, char *buf, int bufSize)
        void  eepromScan      ()
        void  eepromWrite     (byte type, const char *text);
        void  eepromUpdate    ()
        void  eepromWrite     (byte type, const char *text)

  368 i2c.cpp
        void  i2cWrite        (byte chip, byte port, byte val)
        void  i2cWritePortBit (byte adr, byte port, bool b)
        void  i2cWriteBit     (byte adr, bool b)
        byte  i2cRead         (byte chip, byte port)
        bool  i2cReadBit      (byte adr)
        bool  i2cReadPortBit  (byte adr, byte port)
        void  i2cCfg          ()
        void  i2cList         ()
        void  i2cDump         (byte chip)
        void  i2cScan         ()
        void  test            (int chip)
        void  tglTest         ()
        void  bitTgl          (byte chip, byte reg, byte bit)
        void  i2cReset        ()
        void  i2cInit         ()
        void  i2cMon          ()

  232 pcRead.cpp
        void  pcRead.void pcRead ()

  333 signals.cpp
        void  sigDisp         ()
        void  sigReport       (SigMap *q)
        void  sigBlkMon       ()
        void  sigBlkSet       (byte blk, byte occ)
        void  sigMsg          (const char *msg)
        void  sigUpdate       ()
        void  sigCheck        ()
        void  sigInit         ()

  231 wifi.cpp
        void  _wifiScan       ();
        void  _wifiMsg        (const char *msg);
        void  _wifiInit       ()
        void  wifiReset       ()
        void  wifiSend        (const char *msg)
        void  wifiMonitor     ()

   72 Node2.ino
  107 sigMap.cpp
  204 eeprom.cpp
  231 wifi.cpp
  232 pcRead.cpp
  333 signals.cpp
  368 i2c.cpp
 1547 total
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 1
greg ciurpita gregc
future?
 
having ABS during open houses was a big help during open houses to keep trains running smoothly without yelling, "Joe, stop your train".      It also lit up signals that had been dark for many years.
 
Now that we're trying to have op-sessions and may need cross between mainline tracks, I'm wondering about adding a LocoNet receiver and monitoring switch positions to control signals.
 
maintaining autonomous nodes that operate independent of each other or the need for a PC has worked out well.   Turnouts are controlled thru Digitrax controllers using stationary decoders (DS64s)

greg - LaVale, MD -- my layout   --  threads of interest

Reply 0
David Emery deemery2
How do you get from power-on to ready-to-go?  (Setting up consistent initial state can be a challenge on physical systems...  😉 )
 
dave
Modeling 1890s northeast US
Reply 0
greg ciurpita gregc
How do you get from power-on to ready-to-go?
the signals at least reflect what the node reads from the block detectors..   it may take a few seconds to  receive a message indicating that the next block is occupied and to indicate Approach.   
 
there's no attempt to be perfect (it's a model RR).   If indications are incorrect they'll eventually get fixed.    messages could be sent out periodically indicating state instead of state change.

greg - LaVale, MD -- my layout   --  threads of interest

Reply 2
David Emery deemery2
the signals at least reflect what the node reads from the block detectors..   it may take a few seconds to  receive a message indicating that the next block is occupied and to indicate Approach.   
 
there's no attempt to be perfect (it's a model RR).   If indications are incorrect they'll eventually get fixed.    messages could be sent out periodically indicating state instead of state change.
When I was doing my 'ball signal plus crossing' control, I needed to make sure the signal state (ball up or ball down) and the crossing state (narrow gauge tracks powered or standard gauge tracks powered) were consistent when powered-on.  So I just moved the ball and set the track to the known starting state (standard gauge).    A related issue when doing the state transition was avoiding a power surge by setting all of the relays to the correct position, so I did those sequentially with a small delay in between.  (True both on start-up and whenever one of the 2 'change the crossing' buttons were pushed.)
 
If I were doing Arduino controlled turnouts and associated control panel lights, I think the right answer would be some sort of feedback from each turnout's mechanical position, i.e. a microswitch, that could be polled during startup.  But I haven't thought very hard about that problem.
 
On a relatively complex automated layout, I do wonder if there are surge problems on system power-on as everything comes on line.  
 
dave
Modeling 1890s northeast US
Reply 0
YoHo
Yeah, getting feed back via microswitch is the easy answer with servos. 
Reply 1
greg ciurpita gregc
getting feed back via microswitch is the easy answer with servos. 
see
 
a momentary switch could "toggle" a turnout and multiple switches could be wired to a single input.
 
this is where having an LED driven by the switch motor rather than driven by the processor works well.    there could be multiple LEDs drive by a single processor pin.
 

greg - LaVale, MD -- my layout   --  threads of interest

Reply 1
Alex Wood AlexW
On a relatively complex automated layout, I do wonder if there are surge problems on system power-on as everything comes on line.
Not power per se if you have the right power supplies, but LocoNet can get overloaded on system start-up, so you may have to phase the power-on or use multiple LocoNets.

Modeling the modern era freelanced G&W Connecticut Northern

Reply 0
Jim at BSME
The next iteration put an esp32 and MCP 23017
Why the addition of the MCP23017? ESP32s have lots of I/O pins. Also  I don't see the MCP23017 pin configuration table on your github repository, do you have it there?
- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: Facebook, Instagram
Reply 0
greg ciurpita gregc

 

Also  I don't see the MCP23017 pin configuration table
was referring to the top part of sigMap 
#include "signals.h"
 
//   MCP23017
//   8 - GPB0  1  28 GPA7 - 7
//   9 - GPB1  2  27 GPA6 - 6
//  10 - GPB2  3  26 GPA5 - 5
//  11 - GPB3  4  25 GPA4 - 4
//  12 - GPB4  5  24 GPA3 - 3
//  13 - GPB5  6  23 GPA2 - 2
//  14 - GPB6  7  22 GPA1 - 1
//  15 - GPB7  8  21 GPA0 - 0
 
//        Vdd  9  20 INTA
//        Vss 10  19 INTB
//         NC 11  18 Nreset
//        SCL 12  17 A2
//        SDA 13  16 A1
//         NC 14  15 A0
 
Why the addition of the MCP23017?
i envision the need for more I/O

greg - LaVale, MD -- my layout   --  threads of interest

Reply 0
Jim at BSME
the description table is in its own file.   It includes comments describing the MCP 23017 pins, the tower/nodes, the signal LED pins and the block description table
This comment made me believe there was another file that I didn't see - Thanks.
- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: Facebook, Instagram
Reply 0
Reply