Wednesday, February 2, 2022

Turnout & Signal Control Firmware/PC SW (Part 3)

Overview

This post is mostly about the PC software/PIC Firmware API and issues that needed to be resolved with the 10 year old software.  But first a look from a high level what the main issue is.  The combination of PC software and PIC firmware form a bridge between what the user sees and what the hardware does.  The user sees a turnout or a signal a with a number associated with it.  The hardware sees a PIC GPIO port that needs to be manipulated.  Somehow these two perspectives need to be bridged in the PC Software and PIC firmware.

Since I have (or will have) a U shaped layout, turnouts,signals, uncouplers, LED lighting, etc on one side of the helix are even numbered and on the other they odd numbered. Where there are multiple track levels, the top level has 100 added to the number.  The hardware interface to the train layout is 40 connection points that will energize a solenoid for a specified period of time.  To reduce confusion, I will only describe how the turnout works. The remaining layout devices have similar functionality.

As a refresher, each normal turnout has two states.  These two states are controlled by solenoids that are momentarily energized.  Thus a turnout has two control wires, each energizing a solenoid that moves the points to either straight or turn.  For those EEs out there is similar to a dual coil latching relay.  You momentarily energize the the solenoid to position the points (or the signal) to the position that you want.  Thus a single track needs two connection points from the hardware.  

Electrical Color Coding

As a quick aside, in the old Marklin world, electrical connections were color coded by the pin connectors.  These were

  • RED - track power(voltage varies in analog and is constant is digital)
  • BROWN - common
  • YELLOW = accessory power (power for turnout/signal and lights)
  • GREEN - turnouts straight, signals go
  • RED - turnouts turn, signals stop  ( not much chance of confusing this with track power)
  • ORANGE - third solenoid for special accessories
  • GRAY - not used
  • BLUE -I added this for 3 way turnouts, though blue pin connectors were a 3rd party item

I have used these colors in the interface to ease the understanding and implementation of the interface.  Also there are 3 types of turnouts, left/right for straight/curve, a slip turnout and a 3 way turnout (that uses 3 wires to control it) and two types of signals. Regardless they have similar functionality. 

User Interface

The task for the software is take the user number assigned to each turnout, which in turn has two wires connected to the hardware, determine the appropriate PIC GPIO pin and then transmit this data over the MiWi network.  Here is the basic user interface

When the turnout is connected to the hardware, the port number is noted along with the action of that connection, straight (green) or turn (red).  The information in this grid is kept in the EEPROM on the PCB in this format and in an INI file on the PC.  The first two columns are populated by the software depending on the the hardware connected. The Device Type is chosen from a list that is derived from the Port Type column.  The Action and Time(ms) column is chosen from a list that is derived from what is selected in the Device Type column.  The User # is entered as number (1-255) by the user.

How It Works

When the MiWi network initializes, the ports information is sent through the MiWi network to the Master MiWi device which is connected to the PC via a USB HID connection.  Once all of the port connection data is received by the PC software, the data is sorted into another temporary table that is organized and sorted by the layout device (User #).  This table describes which connection ports are connected to each layout device and what action each one of these connection ports does.  Note that there is no requirement to use successive connection ports, it just make it easier to keep track of the layout devices.  PC software and PIC firmware do not care if the green lead is connected to #1 and the red lead is connected #23 connection ports. 

With this temporary table I call the Device Data Table, the GUI can determine which port number will need to be energized to set the turnout points in the correct state.  If I chose to set turnout #35 to turn, the software scans the Device Data Table looking for turnout #35.  Once it finds the table row with turnout #35, it scans the row looking for which port has been assigned to the turn (RED) action.  Now it sends a MiWi message containing the board number, port number and the on time.  The other data in the table is not relevant to energizing the solenoid.  AS described above, it was used in the setup phase to help the user make the correct choices as to options for each layout device.

The PIC firmware then needs to translate this port number into a GPIO pin.  To make this easier, the 40 connection ports are sequentially arranged in 3 MCP23S18 16-bit IO expanders.  The last 8 pins are used for the track control.  It is a simple matter of bit manipulation at this point.  With a zero based port number, the lower 3 bits determine which bit in the 8 bit port needs to be set.  The next 3 bits will choose which of the five 8 bit ports to use.  The firmware then sets the bit, energizing the solenoid.  After the directed time has expired, the firmware clears the bit and the solenoid is de-energized.





No comments:

Post a Comment