Hammers

Hammers Library

This library has a compilation of tools including:

  • accumulate - Accumulates the delta of the input parameter.
  • accumulate distance - Used to add a user defined amount to a distance with a user defined period.
  • blink - Used to add blinking functionality to as digital output.
  • mean - Used to calculate the mean of an array of data.
  • standard deviation - Used to calculate the standard deviation of an array of data.
  • scale analog input - Used to scale in integer analog input to a real.
  • subtract with a rollover - Used to find difference in two numbers with a rollover taken into account.
  • hamTempSensorFn - Used from user defined temperature warnings.
  • crc_16 - Calculates the 16 bits Cyclic Redundancy Check.
  • FastTON - Implements a switch on delay.
  • Interval - Implements a periodic switch on delay.

Accumulate FUB

This function block is used to accumulate a value after a rollover occurs. Input will be have a possible range of period. Output of FUB will be the input value if no rollover occurred. Note: If period is 0, output is always input.

Direction Name Type Description
In input REAL Value to be accumulated
In period REAL Max value in the rollover range
In setToInput BOOL This bit will equate the outputs to the current input value
Out outputREAL REAL Output in REAL format
Out outputLREAL LREAL Output in LREAL format
Out outputCYCLIC_POSITION hamCYCLIC_POSITION Output broke down to Integer (whole number) and REAL (decimal)
internal initialized BOOL Indicates the function has an oldInput store from previous use of function
internal inputOld REAL The last input used in the function block
internal delta LREAL The difference between the current input and inputOld
internal numPeriods DINT Number of times the count has rolled over

Usage

void _INIT ProgramInit(void)
{
    //Using the degrees in a circle as an example.
	period = 360; // +- 180 values
}

void _CYCLIC ProgramCyclic(void)
{
	if (needsAccumulation){
		needsAccumulation = 0;
		AccumulateFB.input = input;
		AccumulateFB.period = period;
		Accumulate(&AccumulateFB);
		
		postRolloverOut = AccumulateFB.outputREAL;
		numOfCircles = AccumulateFB.numPeriods;
	}
}

Example

Using code above the following values show postRolloverOut after several accumulations.

Iteration input postRolloverOut numOfCircles
1 100 100 0
2 150 150 0
3 -175 185 1
4 175 175 0

AccumulateDistance FUB

This function accumulates distance of an axis or other source. While similar to Accumulate FUB, this FUB while output total absolute distance traveled correcting for rollover as opposed to current position correcting for rollover. AccumulateDistance also expects an outside memory location to store distance traveled.

Direction Name Type Description
In enable BOOL Bit that enables the function block
In input REAL Value you want to perform an accumulation operation on
In period REAL Max value in the rollover range
In pDistance UDINT A pointer to an LREAL where the distance output will be stored
Out outputREAL REAL Output in REAL format
Out outputLREAL LREAL Output in LREAL format
Out outputCYCLIC_POSITION hamCYCLIC_POSITION Output broke down to Integer (whole number) and REAL (decimal)

Usage

void _INIT ProgramInit(void)
{
    // Using the degrees in a circle as an example
	encoderPeriod = 150000; // +- 75,000 values
}

void _CYCLIC ProgramCyclic(void)
{
	if (AccumulateDistFB.enable){
		AccumulateDistFB.input = aiEncoder;
		AccumulateDistFB.period = encoderPeriod;
		Accumulate(&AccumulateDistFB);
		travelDistance = AccumulateDistFB.outputREAL;
	}
}
Iteration input travelDistance
1 75,000 75,000
2 50,000 100,000
3 75,000 125,000
4 -50,000 150,000

Mean()

This function is used to calculate the mean of a data set.

Direction Name Type Description
In pData UDINT Pointer to the data set. This is expected to be an array of LREAL values
In n LREAL Number of data points in the Data array
Out Mean LREAL The function returns the mean of the data set provided

Usage

n = 5;
data[0] = 1.025;
data[1] = 1.235;
data[2] = 0.978;
data[3] = 1.213;
data[4] = 1.098;
mean = Mean(&data, n);

// mean = 1.1098

StandardDeviation()

This function is used to calculate the standard deviation of a data set.

Direction Name Type Description
In pData UDINT Pointer to the data set. This is expected to be an array of LREAL values
In n LREAL Number of data points in the Data array
Out Standard Deviation LREAL The function returns the standard deviation of the data set provided

Usage

n = 5;
data[0] = 1.025;
data[1] = 1.235;
data[2] = 0.978;
data[3] = 1.213;
data[4] = 1.098;
standardDeviation = StandardDeviation(&data, n);

// standardDeviation = 0.10102158185259233

This is used to create a simple blinking signal similar to a PWM. This is commonly used for flashing LEDs.

Direction Name Type Description
In Enable BOOL Command to enables the function block
In Time DINT Time of blink frequency in milliseconds
In Percentage UDINT Percentage of time you want the output to be high within the User defined blink time
Out Out BOOL Boolean output meant to be connected to digital IO, most commonly a LED

Usage

BlinkRedLight.Enable = 1;
BlinkRedLight.Time = 1000; // One second blink frequency
BlinkRedLight.Percentage = 50;// Allow for the output to be high half of second

Blink(&BlinkRedLight);

doRedLed = BlinkRedLight.Out;

ScaleAnalogInput FUB

Configuration

  • hamAIO_CONFIG_V - Configuration for voltage scaling.
  • hamAIO_CONFIG_0_20_MA - Configuration for 20 mA scaling.
  • hamAIO_CONFIG_4_20_MA - Configuration for 420 mA scaling.
Direction Name Type Description
In enable BOOL Enable bit for the function block
In input INT The integer analog value to be converted
In configuration hamAIO_CONFIG This variable dictates which scaling factor will be used
In V REAL[0..HAM_MAI_SCALE_POINTS] This array will be used as the points to be scaled when using hamAIO_CONFIG_V as the configuration
In mA REAL[0..HAM_MAI_SCALE_POINTS] This array will be used as the points to be used for linear interpolation hamAIO_CONFIG_0_20_MA or hamAIO_CONFIG_4_20_MA as the configuration
In units REAL[0..HAM_MAI_SCALE_POINTS] Output value for corresponding V or mA value
In numberOfPoints USINT Determines the amount of points to be scaled
In update BOOL Parameters are updated on a rising edge
Out output REAL Scaled value
Out status DINT Status of conversion
Out updateDone BOOL After an update has been performed, this bit will signify its success

Usage


void _INIT ProgramInit(void)
{
	// Add in configuration parameters
	config = hamAIO_CONFIG_V;
	scaleAnalog.numberOfPoints = 25; //cannot exceed 50
	scaleAnalog.update = 1;

	for (i = 0; i <= scaleAnalog.numberOfPoints; i++){
		scaleAnalog.V[i] = Configuration.V[i]
	}
}

void _CYCLIC ProgramCyclic(void)
{
	scaleAnalog.input = aiTempReading;
	scaleAnalog.enable = 1;

	// Call function block
	ScaleAnalogInput(&scaleAnalog);

	// Set out put of no errors and update is complete
	if (!scaleAnalog.update || scaleAnalog.updateDone){
		if (scaleAnalog.status == 0){
			scaledOutput = scaleAnalog.output;
		} else {
			scaleError = 1;
		}
	}

}

Errors

  • -1058012184 - NumberOfNodes > 50
  • -1058012183 - X-coordinates not sorted
  • -1058012179 - Invalid mode
  • -2131754008 - NumberOfNodes > 50
  • -2131754007 - X-coordinates not sorted
  • -2131754003 - Invalid mode

subtractWithRollover()

Subtract two REALs and return the difference, corrected for rollover. The output value will have a range of rollOverPeriod centered around 0. Note: If rollOverPeriod is 0, output will just be the subtraction of minuend and subtrahend.

Direction Name Type Description
In minuend REAL The number being subtracted from
In subtrahend REAL The amount being subtracted from the minuend
In rolloverPeriod REAL The largest range before roll over occurs
Out output REAL The function outputs the difference of the two inputs

Usage

difference = subtractWithRollover(minuend, subtrahend, 360);

Example

Using the code above, the following situation shows inputs for minuend and subtrahend and the resulting difference.

minuend subtrahend difference
120 220 -100
0 220 140
360 0 0
360 120 -120
-420 120 -240

Notice how the last example is well outside its bounds and is still outside its bounds after function call.

crc_16()

Calculates the 16 bits CRC16 (Cyclic Redundancy Check) in one pass for a byte string of which the beginning has been passed to the function. The number of bytes to check is also a parameter. CRC is used to check data for corruption. Once calculated the saved CRC can be compared to a freshly calculated CRC to check for data corruption after the data is saved, or written over some communication protocol.

Direction Name Type Description
In input_str UDINT String to be calculate CRC
In num_bytes UDINT Number of bytes im input_str to check
Return check UINT CRC value

Usage

previousCheck = currentCheck;
currentCheck = crc_16(&myData, strlen(myData));
if(previousCheck != currentCheck) {
	// Data has changed or is corrupted
}

hamTempSensorFn()

This function is used to take an analog temperature sensor input convert it to a temp output. It also has the functionality to alert the user if the temperature is in either a warning or high range defined by the user.

hamTempSensor_typ

A instance of hamTempSensor_typ must be declared to use this function.

Prefix Name Type Description
In Par aiValue INT Analog value coming from the temperature sensor
In Cfg ScaleFactor REAL Analog to temp scaling value (default 0.1 [°C])
In Cfg HighTemp REAL Maximum temperature unit can reach in Celsius
In Cfg WarningPercentage REAL This structure member specifies the percentage of the HighTemp will cause a warning alarm (default 85 [%])
In Cfg HighPercentage REAL This structure member specifies the percentage of the HighTemp will cause a high temp alarm (default 95 [%])
Out Temperature REAL This is the current temperature the sensor is reading, after the scaling factor has been applied
Out TempLevel TEMP_LEVEL_enum Enum representation of the temperature level compared to the specified warning and high ranges
Out WarningTempAlarm REAL Flag set when the temperature reaches the warning zone specified by the WarningPercentage
Out HighTempAlarm REAL Flag set when the temperature reaches the warning zone specified by the HighPercentage

TEMP_LEVEL_enum

  • TEMP_LEVEL_OK - This output will display 0 when the temperature level is in the normal range.
  • TEMP_LEVEL_WARNING - This output will display 1 when the temperature level is in the warning range.
  • TEMP_LEVEL_HIGH - This output will display 2 when the temperature level is in the high range.

Usage

void _INIT ProgramInit(void)
{
	//Add in configuration parameters
	TempSensor.IN.CFG.HighPercentage = 90;
	TempSensor.IN.CFG.HighTemp = 125;
	TempSensor.IN.CFG.WarningPercentage = 75;
}

void _CYCLIC ProgramCyclic(void)
{

	if(checkTempSensor){
		checkTempSensor = 0;
		TempSensor.IN.PAR.aiValue = aiTempSensor;
		hamTempSensorFn(&TempSensor);
		currentTemp = TempSensor.OUT.Temperature;
		tempWarning = TempSensor.OUT.WarningTempAlarm;
		tempHigh = TempSensor.OUT.HighTempAlarm;
	}
}

FastTON()

This function implements a switch on delay, or a timer. It uses the AsIOTimeCyclicStart() function, so it is precise to the cycle time of the task class it is called in.

FastTON_typ

Direction Name Type Description
In IN BOOL Input signal. Timer only runs while IN is held high.
In PT UDINT Delay time in microseconds. PT must not exceed 70 minutes.
Out ET UDINT Elapsed time in microseconds
Out Q BOOL Output signal. The rising edge of the input signal is delayed by PT
Internal start UDINT Cyclic start time of the rising edge of IN
Internal started BOOL Used for IN edge detection

Usage

void _INIT ProgramInit(void)
{
	// set delay time
	FastTON.PT = 2000; // 2 ms
}

void _CYCLIC ProgramCyclic(void)
{
	// condition start
	if(enableTimer){
		FastTON.IN = 1;
	}

	// cyclically call FUB
	FastTON();

	// check output
	if (FastTON.Q){
		// at least 2 ms has elapsed since the rising edge of enableTimer/IN
	}
}

Interval()

This function implements a periodic switch on delay, or an interval timer. It uses the AsIOTimeCyclicStart() function, so it is precise to the cycle time of the task class it is called in.

FastTON_typ

Direction Name Type Description
In IN BOOL Input signal. Interval timer only runs while IN is held high.
In PT UDINT Interval time in microseconds. PT must not exceed 70 minutes.
Out ET UDINT Elapsed time in microseconds of the current signal period
Out Q BOOL Output signal. Q goes high PT after the rising edge of IN, and every PT after that
Internal start UDINT Cyclic start time of the current signal period
Internal started BOOL Used for IN edge detection

Usage

void _INIT ProgramInit(void)
{
	// set interval time
	Interval.PT = 2000; // 2 ms
}

void _CYCLIC ProgramCyclic(void)
{
	// condition start
	if(enableIntervalTimer){
		Interval.IN = 1;
	}

	// cyclically call FUB
	Interval();

	// check output
	if (Interval.Q){
		// 2 ms has elapsed since beginning of signal period
		// Q is true for exactly one cycle every interval
	}
}