Serial Wombat Arduino Library
Public Types | Public Member Functions | List of all members
SerialWombatAbstractProcessedInput Class Referenceabstract

SerialWombatAnalogInput, SerialWombatPulseTimer, SerialWombatResistanceInput and others inherit from this class. More...

#include <SerialWombatAbstractProcessedInput.h>

Inheritance diagram for SerialWombatAbstractProcessedInput:
SerialWombat18CapTouch SerialWombatAnalogInput_18AB SerialWombatHSCounter SerialWombatProcessedInputPin SerialWombatPulseTimer_18AB SerialWombatQuadEnc_18AB SerialWombatResistanceInput SerialWombatUltrasonicDistanceSensor

Public Types

enum  Period {
  PERIOD_1mS = 0, PERIOD_2mS = 1, PERIOD_4mS = 2, PERIOD_8mS = 3,
  PERIOD_16mS = 4, PERIOD_32mS = 5, PERIOD_64mS = 6, PERIOD_128mS = 7,
  PERIOD_256mS = 8, PERIOD_512mS = 9, PERIOD_1024mS = 10
}
 
enum  OutputValue { RAW = 0, FIRST_ORDER_FILTERED = 1, AVERAGE = 2 }
 
enum  Transform { NONE = 0, SCALE_RANGE = 1, LINEAR_MXB = 2 }
 

Public Member Functions

 SerialWombatAbstractProcessedInput (SerialWombatChip &sw)
 Constructor for the SerialWombatAbstractScaledOutput Class. More...
 
int16_t writeInverted (bool inverted)
 if enabled subtract the input value from 65535 before doing any other processing. More...
 
int16_t writeFirstOrderFilteringConstant (uint16_t constant)
 Set a first order filtering constant to be applied to the signal Higher is heavier filtering. More...
 
int16_t writeAveragingNumberOfSamples (uint16_t numberOfSamples)
 Set a number of samples to average for each update of the downstream signal. More...
 
int16_t writeExcludeBelowAbove (uint16_t low, uint16_t high)
 Sets input value ranges which are discarded rather than processed. More...
 
int16_t configureQueue (SerialWombatQueue *queue, Period period, bool queueHighByte=true, bool queueLowByte=true)
 Sets up the queueing feature for this pin mode. Queue must have been initialized prior to this queue. More...
 
int16_t configureOutputValue (OutputValue outputValue)
 Configures whether the pin's public data value is averaged, filtered, or neither. More...
 
int16_t writeTransformScaleRange (uint16_t min, uint16_t max)
 Scale incoming values to a range of 0 to 65535. More...
 
int16_t writeTransformLinearMXB (int32_t m, int32_t b)
 Scale incoming values based on an mx+b linear equation. More...
 
int16_t configureIntegrator (uint16_t negativeMaxIndex, uint16_t negativeMidIndex, uint16_t negativeDeadZone, uint16_t positiveDeadZone, uint16_t positiveMidIndex, uint16_t positiveMaxIndex, uint16_t midIncrement, uint16_t maxIncrement, uint16_t initialValue, uint8_t updateFrequencyMask=0)
 Sort incoming data into one of 5 ranges, and integrate based on linear interpolation in those ranges. More...
 
int16_t writeProcessedInputEnable (bool enabled)
 Enables or disables all input processing functions If disabled, the raw input value is placed directly in the pin's 16 bit public data buffer. More...
 
uint16_t readMinimum (bool resetAfterRead=false)
 Retreive the maximum public data output value since the last call with reset= true. More...
 
uint16_t readMaximum (bool resetAfterRead=false)
 Retreive the minimum public data output value since the last call with reset= true. More...
 
uint16_t readAverage ()
 Retreive the last completed averaged value. More...
 
uint16_t readFiltered ()
 Retreive the filtered value. More...
 
virtual uint8_t pin ()=0
 Used for inheritance. More...
 
virtual uint8_t swPinModeNumber ()=0
 Used for ineheritance. More...
 

Detailed Description

SerialWombatAnalogInput, SerialWombatPulseTimer, SerialWombatResistanceInput and others inherit from this class.

This class allows a common set of services to be applied to Serial Wombat inputs which inherit from it. These include SerialWombatAnalogInput, SerialWombatPulseTimer, SerialWombatResistanceInput and will include others in the future.

This class is only applicable to the Serial Wombat 18AB firmware.

This class allows various transformations and filters to be performed on incoming measurements within the Serial Wombat firmware using the Serial Wombat chip's cpu cycles. Since this class is processed every 1mS for each pin configured to an input class, it can do tasks like filtering or averaging much more quickly and consistently than could be achieved by sampling the value over I2C or UART and doing the computation on the host device. Minimum and Maximum measured values are also tracked for retreival by the host

Additionally, this class is capable of limiting input (for example any value below 10000 is processed as 10000, and any value above 62331 is processed as 62331), scaling input (e.g. an expected input range of 3000 to 7000 is scaled linearly to the full Serial Wombat Range of 0 to 65535), mx+b linear transformations, exclusion of outlier data (e.g. any value over 50000 is ignored, and the previous valid measurement is substituted in its place).

Inputs can be inverted (scaled from 0-65535 to 65535-0 by substracting the raw value from 65535). This is useful for reversing the direction of things like analog measured potentiometers.

The final output of the SerialWombatAbstractProcessedInput operations can be queued in a User Memory Area queue on a periodic basis. This allows synchronsous sampling and storage of input data for retreival and processing by the host. This allows waveforms to be stored and processed. Sampling period is an enumerated type ranging from 1mS to 1024mS in power of 2 intervals

Data processing happens in the following order each 1mS for any enabled feature:

To use this class first configure the pin to its mode using the normal begin() call for that pin mode (the derived class). Then call any configuratioon commands ( writeInverted, writeTransformLinearMXB, etc) then call writeProcessedInputEnable(true) to enable processing.

Member Enumeration Documentation

◆ OutputValue

Enumerator
RAW 

Use the unfiltered signal for the pin's public data.

FIRST_ORDER_FILTERED 

Use a first order filtered signal for the pin's public data.

AVERAGE 

Use an averaged signal for the pin's public data (updates less often)

◆ Period

Enumerator
PERIOD_1mS 
PERIOD_2mS 
PERIOD_4mS 
PERIOD_8mS 
PERIOD_16mS 
PERIOD_32mS 
PERIOD_64mS 
PERIOD_128mS 
PERIOD_256mS 
PERIOD_512mS 
PERIOD_1024mS 

◆ Transform

Enumerator
NONE 

Don't transform the input signal.

SCALE_RANGE 

Scale the input signal to a 0-65535 value based on input high and low limits.

LINEAR_MXB 

Scale the input signal based on a linear mx+b equation.

Constructor & Destructor Documentation

◆ SerialWombatAbstractProcessedInput()

SerialWombatAbstractProcessedInput::SerialWombatAbstractProcessedInput ( SerialWombatChip sw)
inline

Constructor for the SerialWombatAbstractScaledOutput Class.

Parameters
swA reference to a previously declared SerialWombatPin to which the output is connected.

Member Function Documentation

◆ configureIntegrator()

int16_t SerialWombatAbstractProcessedInput::configureIntegrator ( uint16_t  negativeMaxIndex,
uint16_t  negativeMidIndex,
uint16_t  negativeDeadZone,
uint16_t  positiveDeadZone,
uint16_t  positiveMidIndex,
uint16_t  positiveMaxIndex,
uint16_t  midIncrement,
uint16_t  maxIncrement,
uint16_t  initialValue,
uint8_t  updateFrequencyMask = 0 
)
inline

Sort incoming data into one of 5 ranges, and integrate based on linear interpolation in those ranges.

This funciton is designed to allow a binary or analog input to proportionally increment or decrement a value, such as allowing a joystick to control the position of a servo by changing it over time proportional to the position of the stick.

Returns
Returns a negative value if the configuration caused an error.
Parameters
negativeMaxIndexValues more negative than this will decrement the output value by maxIncrement per sample.
negativeMidIndexValues more negative than this will be linearly scaled with negativeMaxIndex, maxIncrement and midIncrement.
negativeDeadZoneValues between negativeDeadZone and positiveDeadZone will not affect the output value. Values between negativeDeadZone and negativeMidIndex will be scaled linearly based on 0 and midIncrement
positiveDeadZoneValues between negativeDeadZone and positiveDeadZone will not affect the output value. Values between positiveDeadZone and positiveMidIndex will be scaled linearly based on 0 and midIncrement
positiveMidIndexValues more positive than this will nearly increment the value scaled with negativeMaxIndex, maxIncrement and midIncrement.
positiveMaxIndexValues more positive than this will increment the output value by maxIncrement per sample.
midIncrementforms a line for scaling between 0 and midIncrement for values between negative or positive deadzone and negative or positive MidIndex
maxIncrementforms a line for scaling between midIncrement and maxIncrement for values between negative or positive midIncrement and negative or positive maxIncrement
initialValueintial integrator value,
updateFrequencyMaskWhen the frame counter and this mask anded = 0, perform integration. For instance 0 = every mS, 0x0F = every 16 mS, 0x1F = every 2mS

◆ configureOutputValue()

int16_t SerialWombatAbstractProcessedInput::configureOutputValue ( OutputValue  outputValue)
inline

Configures whether the pin's public data value is averaged, filtered, or neither.

Parameters
outputValueAn enumerated type for filtered, averaged, or raw

◆ configureQueue()

int16_t SerialWombatAbstractProcessedInput::configureQueue ( SerialWombatQueue queue,
Period  period,
bool  queueHighByte = true,
bool  queueLowByte = true 
)
inline

Sets up the queueing feature for this pin mode. Queue must have been initialized prior to this queue.

Allows periodic storage of the public data value into a previously initialized queue in the user memory buffer. The high byte, the low byte, or both can be stored into the queue. This is useful to increase the number of Samples that can be stored and transferred if 8 bit resolution is sufficent.

Note that the sampling period is an enumerated type, not a numerical value

Parameters
queueThe index into the User Memory Buffer where the queue is located
periodThe sampling period. See the enumerated type for values
queueHighByteWhether to put the high byte of the sample into the queue
queueLowByteWhether to put the low byte of the sample into the queue
Returns
returns 0 or higher if success, or a negative error code

◆ pin()

virtual uint8_t SerialWombatAbstractProcessedInput::pin ( )
pure virtual

◆ readAverage()

uint16_t SerialWombatAbstractProcessedInput::readAverage ( )
inline

Retreive the last completed averaged value.

Returns
The last completed result of the averaging. Note that because the average is a normal average and not a moving average, this value is unlikely to include the most recent raw samples.

◆ readFiltered()

uint16_t SerialWombatAbstractProcessedInput::readFiltered ( )
inline

Retreive the filtered value.

A 16 bit value representing the First Order IIR filtered result of the input

◆ readMaximum()

uint16_t SerialWombatAbstractProcessedInput::readMaximum ( bool  resetAfterRead = false)
inline

Retreive the minimum public data output value since the last call with reset= true.

Parameters
resetAfterReadIf True, minimum value is set to 65535 after read so that subsequent values become minimum.
Returns
A 16 bit unsigned value indicating the minimum value

◆ readMinimum()

uint16_t SerialWombatAbstractProcessedInput::readMinimum ( bool  resetAfterRead = false)
inline

Retreive the maximum public data output value since the last call with reset= true.

Parameters
resetAfterReadIf True, maximum value is set to 0 after read so that subsequent values become maximum.
Returns
A 16 bit unsigned value indicating the maximum value

◆ swPinModeNumber()

virtual uint8_t SerialWombatAbstractProcessedInput::swPinModeNumber ( )
pure virtual

◆ writeAveragingNumberOfSamples()

int16_t SerialWombatAbstractProcessedInput::writeAveragingNumberOfSamples ( uint16_t  numberOfSamples)
inline

Set a number of samples to average for each update of the downstream signal.

The average is a typical average, not a moving average so the more samples comprise the averaged value the less often the downstream value will update.

Parameters
Anumber of samples to include in each output average. Samples are typically taken each mS, although some modes such as SerialWombatUltrasonicDistanceSensor sample at other periods.
Returns
returns 0 or higher if success, or a negative error code

◆ writeExcludeBelowAbove()

int16_t SerialWombatAbstractProcessedInput::writeExcludeBelowAbove ( uint16_t  low,
uint16_t  high 
)
inline

Sets input value ranges which are discarded rather than processed.

Input values that are below the low parameter or above the high parameter are not processed. The last valid input value is repeated instead. This feature is designed to reject outlier values, not to act as a high or low limiting clamp

Parameters
lowinput values below this value will not be processed
highinput values above this value will not be processsed.
Returns
returns 0 or higher if success, or a negative error code

◆ writeFirstOrderFilteringConstant()

int16_t SerialWombatAbstractProcessedInput::writeFirstOrderFilteringConstant ( uint16_t  constant)
inline

Set a first order filtering constant to be applied to the signal Higher is heavier filtering.

The filter samples at 1kHz. For a good explanation of 1st order IIR filter calculations, see: https://www.monocilindro.com/2017/04/08/how-to-implement-a-1st-order-iir-filter-in-5-minutes/

Some filter cut-off (3dB down) frequency and constant values:

  • 0.5 Hz 65417
  • 1 Hz 65298
  • 2 Hz 65062
  • 5 Hz 64358
  • 10 Hz 63202

Filtering adds lag. The higher the filter constant value, the longer it takes for the filter to settle when given a steady input.

Parameters
constantThe first order filter constant from 0 to 65535. Larger constant is heavier filtering
Returns
returns 0 or higher if success, or a negative error code

◆ writeInverted()

int16_t SerialWombatAbstractProcessedInput::writeInverted ( bool  inverted)
inline

if enabled subtract the input value from 65535 before doing any other processing.

Parameters
invertedFalse - input value isn't changed. True- input value is subtracted from 65535
Returns
returns 0 or higher if success, or a negative error code

◆ writeProcessedInputEnable()

int16_t SerialWombatAbstractProcessedInput::writeProcessedInputEnable ( bool  enabled)
inline

Enables or disables all input processing functions If disabled, the raw input value is placed directly in the pin's 16 bit public data buffer.

◆ writeTransformLinearMXB()

int16_t SerialWombatAbstractProcessedInput::writeTransformLinearMXB ( int32_t  m,
int32_t  b 
)
inline

Scale incoming values based on an mx+b linear equation.

Allows scaling of an input by multiplying by m, dividing by 256, and adding b. The m value can be thought of as a fraction with a divisor of 256. This allows the scaling value to make the input bigger or smaller. After the multiplication, division and addition the result is limited to the range of 0 to 65535. Calling this feature disables writeTransformScaleRange until the pin is reinitialized with begin().

Parameters
mA value between -16777215 and +1677215 representing the number of 256th by which to multiply the input
bA value between -65535 and 65535 to add to the result of the multiplication
Returns
returns 0 or higher if success, or a negative error code

◆ writeTransformScaleRange()

int16_t SerialWombatAbstractProcessedInput::writeTransformScaleRange ( uint16_t  min,
uint16_t  max 
)
inline

Scale incoming values to a range of 0 to 65535.

This function allows configuration of an input scaling range that maps to 0-65535.
For example, if a sensor returns a range from 2000 to 5000, setting the minimum to 2000 and maximum to 5000 will cause values below 2000 to be 0, values above 5000 to be 65535, and values in between will be scaled accordingly. This allows a sensor or other input device to scale to the Serial Wombat philosophy of using a 16 bit resolution number to represent the the possible range of values

Calling this feature disables writeTransformLinearMXB until the pin is reinitialized with begin().

Parameters
minThe minimum value of the input range. Input values less than or equal to that will be scaled to 0
maxThe maximum value of the input range. Input values greater or equal to that will be scaled to 65535
Returns
returns 0 or higher if success, or a negative error code