class documentation

class TrkMon:

Constructor: TrkMon()

View In Hierarchy

Monitor Track and Booster Power State

DRV8874 fault may be: - over current trip - under volage on VM or charge pump - over temperature

Current Sense

The DRV8874 current sense pin is a current source that produces 450 µA/A of output current. The load resistor on the Pololu board is 2.49 kΩ. I.e .45 mA * 2.49kΩ => 1.1205 V at the sense pin per amp output. The over current circuit will trip when sense pin V is c. 3.3 V - it's the logic high (false) level at _sleep. => 2.95 A. However the continuous rating for the DRV8874 as mounted on the Pololu board is 2.1 A.

The sense reading is a 16 bit number with 65535 corresponding to 3.3 volts. We set thresholds at 1A and 2A.

The thresholds are used to set the NeoPixel colour: - < 1A = pure green - < 2A = yellowish - > 2A = orange The DCC monitor is a singleton class that monitors the DCC status and updates the NeoPixel accordingly. It uses a statistical median filter and an Infinite Impulse Response (IIR) filter to smooth the current sense reading and the zero offset to remove the zero offset when the DCC is asleep. The statistical median filter is non linear and removes outlying values.The IIR filter is implemented using a simple formula:

new_value = (old_value * (FILTER_FACTOR - 1) /FILTER_FACTOR + (new_reading / FILTER_FACTOR)

where FILTER_FACTOR is a constant that determines the amount of filtering applied. The zero offset is calculated when the DCC is asleep and is used to remove the zero offset from the current sense reading when the DCC is awake. The zero offset is also filtered using the same IIR filter formula.

This code is intended to run on the second core (core1) and cannot use preemptive code such as timer callbacks or interrupts.

Class Method get_instance Get the Track Monitor instance.
Method __init__ Initialise the Track Monitor
Method get_current Get the current
Method scan Scan the DCC status and update the NeoPixel accordingly.
Constant BRIGHT NeoLed default brightness.
Constant DRV_CURRENT_RATIO convert the raw sensor reading to current in mA.
Constant FILTER_FACTOR The IIR filter factor for reading.
Constant FILTER_RATIO The IIR filter ratio ((filter factor - 1)/filter factor)
Constant SCAN_INTERVAL The time interval between scans in ms.
Constant STAT_MEDIAN_INDEX The index of the median value within the value list.
Constant STAT_MEDIAN_SIZE The size of the statistical median filter (must be odd).
Constant THRESHOLD_1 The threshold for 1A in the current sense reading.
Constant THRESHOLD_2 The threshold for 2A in the current sense reading.
Constant Z_FILTER_FACTOR IIR filter factor for zero offest
Constant Z_FILTER_RATIO The 0 offset filter ratio ((filter factor - 1)/filter factor)
Class Variable _this_mon Undocumented
Instance Variable _enable_pin Undocumented
Instance Variable _fault_pin Undocumented
Instance Variable _led Undocumented
Instance Variable _next_run_time Undocumented
Instance Variable _readings Undocumented
Instance Variable _sense Undocumented
Instance Variable _sense_pin Undocumented
Instance Variable _sense_zero Undocumented
Instance Variable _sleep_pin Undocumented
@classmethod
def get_instance(cls):

Get the Track Monitor instance.

This returns the singleton instance of the Track/Booster Monitor.

Instantiate the singleton on the first call.

Parameters
cls
Returns
The Track monitor instance
def __init__(self):

Initialise the Track Monitor

This initialises the Monitor with the pins that it will use to monitor the DCC status.

Reads the following from hardware configuration
_sleep_pin: The pin that is low when the DRV8874 is asleep. _enable_pin: The pin that is high when the DRV8874 is enabled. _fault_pin: The pin that is low when there is a fault. _sense_pin: The pin that reads the current sense voltage.
def get_current(self):

Get the current

Returns
the current as derrived from the DRV8874 current sense pin in mA
def scan(self):

Scan the DCC status and update the NeoPixel accordingly.

This scans the DCC status and updates the NeoPixel accordingly. This method is called periodically to update the DCC status and NeoPixel. It checks the fault pin, sleep pin, and enable pin to determine the DCC status and updates the NeoPixel accordingly. If the fault pin is low, it indicates a fault condition and the NeoPixel is set to blue. If the sleep pin is low, it indicates that the DCC is asleep and the current sense reading is updated to the zero offset. If the enable pin is low, it indicates that the DCC is in a cutout condition and the NeoPixel is set to blue if the fault pin is low, otherwise it is set to red.

If the DCC is awake, the current sense reading is updated and the NeoPixel is set to green, yellowish, or orange depending on the current sense reading. The NeoPixel is updated with the appropriate RGB values based on the current sense reading.

The method also updates the next run time to be 100 ms in the future.

BRIGHT =

NeoLed default brightness.

Value
const(12)
DRV_CURRENT_RATIO =

convert the raw sensor reading to current in mA.

Value
const((3300 / 0.45 * 2.49 * 65535))
FILTER_FACTOR =

The IIR filter factor for reading.

Value
const(10)
FILTER_RATIO =

The IIR filter ratio ((filter factor - 1)/filter factor)

Value
const(((FILTER_FACTOR - 1) / FILTER_FACTOR))
SCAN_INTERVAL =

The time interval between scans in ms.

Value
const(100)
STAT_MEDIAN_INDEX =

The index of the median value within the value list.

Value
const((STAT_MEDIAN_SIZE // 2))
STAT_MEDIAN_SIZE =

The size of the statistical median filter (must be odd).

Value
const(5)
THRESHOLD_1 =

The threshold for 1A in the current sense reading.

Value
const((65535 / 3.3))
THRESHOLD_2 =

The threshold for 2A in the current sense reading.

Value
const((THRESHOLD_1 * 2))
Z_FILTER_FACTOR =

IIR filter factor for zero offest

Value
const(100)
Z_FILTER_RATIO =

The 0 offset filter ratio ((filter factor - 1)/filter factor)

Value
const(((Z_FILTER_FACTOR - 1) / Z_FILTER_FACTOR))
_this_mon =

Undocumented

_enable_pin =

Undocumented

_fault_pin =

Undocumented

_led =

Undocumented

_next_run_time =

Undocumented

_readings =

Undocumented

_sense: float =

Undocumented

_sense_pin =

Undocumented

_sense_zero =

Undocumented

_sleep_pin =

Undocumented