Turnout Sensor Commands

{From the laptop (4) to the Raspberry Pi (3)}


To add 24 additional sensors to the system, a IO Pi plus HAT was added to the Raspberry Pi to provide the GPIO inputs needed.


The Raspberry Pi will accept the DCC++ sensor commands <S...> with an ID greater than 50 and return results identical to the Base Station i.e. <O>, <X> or <Q ID PIN PULLUP>. Process <Q> commands to report the current status of each defined sensor. In addition, the <H> and <h> commands will be accepted to store or clear EEPROM in this Raspberry PI, similar to the <E>, <e> commands for the Base Station or <N>, <n> commands for signals.


Command summary

    • < -- Start of added command
    • H|h|Q|S -- command character
    • Zero, 1 or 3 data bytes
      • ID -- The numeric ID (51 - 74) of the sensor
      • PIN -- The IO Pi plus HAT port and pin number the sensor is connected to.
      • PULLUP -- 1 = Use the internal pull-up resistor for the PIN, 0 = don't use the internal pull-up resistor for the PIN
    • > -- End of added command-

 Command response summary

    • < -- Start of response
    • h|O|Q|q|X -- Response character
    • Zero or 3 data bytes
      • (see above)
    • > -- End of response


  • Create a new sensor with ID > 50

IF ID > 50 Associate the ID with a GPIO PIN and PULLUP. If sensor ID already exists, it is updated with the specified PIN and PULLUP.

IF ID <= 50 pass command on to Arduino (2).

<S ID PIN PULLUP> 3 bytes

Returns: <O> if successful or <X> if unsuccessful (e.g. out of memory)

IF OK:        Set RAM only

(See <H> below)


  • Delete definition of sensor with ID > 50

IF ID > 50 Delete definition of sensor ID

IF ID <= 50 pass command on to Arduino (2).

<S ID> 1 byte

Returns: <O> if successful or <X> if unsuccessful (e.g. ID does not exist)

IF OK:        Set RAM only

(See <H> below)


  • List all defined sensors

List all defined sensors stored on this Raspberry Pi (ID > 50) and pass the command, unchanged on to the Arduino for additional processing.

<S> 0 bytes

Returns: <Q ID PIN PULLUP> for each defined senor stored on this Raspberry Pi or <X> if none found.

NOTE: The base station does not place a space between the “Q” and sensor ID.


  • List the status of all sensors

List the status of all sensors defined on this Raspberry Pi (ID > 50) and pass the command, unchanged on to the Arduino for additional processing.

<Q> 0 bytes

Return: <Q ID> when sensor ID is active or <q ID> when sensor ID is inactive for each ID define.

NOTE: The base station does not place a space between the “Q” or “q” and sensor ID.


  • Store Raspberry Pi (3) RAM to EPPROM

       Store sensors with ID > 50 in this Raspberry Pi RAM into Pi EPPROM

<H> 0 bytes

       Returns: <h nsensors>


  • Erase all Raspberry Pi (3) EPPROM

       Erase all sensors data from this Raspberry Pi EPPROM

<h> 0 bytes

Returns: <U> Done

               Erase all EPPROM data. Read EPROM into RAM


Sensor Status Change


When the status of a sensor connected to Raspberry Pi (3) and associated with an ID changes, Raspberry Pi (3) will return one of the following to the laptop:


  • When a sensor goes active, IO Pi plus HAT pin transitions from a HIGH state to a LOW state

       <Q ID>


  • When a sensor goes inactive, IO Pi plus HAT pin transitions from a LOW state to a HIGH state

       <q ID>


NOTE: The base station does not place a space between the “Q” or “q” and sensor ID.



All sensors defined as described above are repeatedly and sequentially checked within a thread of the Raspberry Pi (3) program. For each sensor two values are stored: Last reported state and Last read state. The following logic is used to debounce and detect a change in a sensor state.


Read the NEW_State of the sensor

IF NEW_State != LAST_State

       Set LAST_State = NEW_State

ELSE

IF NEW_State != LAST_Reported

IF NEW_State == LOW

output <Q ID>

ELSE

output <q ID>

Set LAST_Reported = NEW_State

ELSE

     do nothing


RAM and EPPROM

ID

PIN

PULLUP

PORT

BIT

>50

1 - 3

0 - 7

0 - 1


When a Wabbit changes the state of a turnout, the CLEAR and THROWN sensors for that turnout change.

Each sensor is connected to a pin/bit of a IO Pi port. When a bit on a IO Pi port changes an interrupt is generated.

Each IO Pi interrupt is connected to a Raspberry Pi GPIO pin which generates a Raspberry Pi interrupt when the IO Pi interrupt occurs.

The Raspberry Pi interrupt causes the software to read and process the port that changed.

PORT

GPIO Interrupt pin

IO Pi Bus

Bus port

IO Pi Bus

I2C address

1

4

1

0

0x24

2

0

1

1

3

1

2

0

0x25

(not used)

26

2

1