Echelon NodeBuilder FX/PL Examples User Manual

Page 28

Advertising
background image

20

Using the NodeBuilder FX/PL Example

#define AI_FILTERSIZE 4
#define AI_CHANNELS

AnalogInput_FBLOCK_COUNT


mtimer ai_timer;
// the buffer for the averaging filter:
unsigned long ai_rawdata[AI_CHANNELS][AI_FILTERSIZE];
// recent value (required to detect changes for minimum NV updates)
unsigned long ai_rawrecent[AI_CHANNELS];


//{{NodeBuilder Code Wizard Start

The Gizmo 4's PIC controller does not provide an interrupt upon the availability of new analog
data. Therefore, this example reads both channels every cpUpdateRate interval, which defaults to
1 minute (the PIC converts every 100ms). The minimum sample rate is 0.1s, which matches the
PIC controller's real sample rate. This example averages the last AI_FILTERSIZE values
obtained for an improved signal quality, where the filter size defaults to 4 and should not be less
than two. This implementation will only update the output network variable if the value has been
changed.

16. Still in AnalogInput.nc, add the following code in bold to the FBC_WHEN_RESET else/if

statement in the AnalogInputDirector() function:

else if ((TFblock_command)iCommand == FBC_WHEN_RESET)

// reset filter and start sampling timer

memset(ai_rawdata,

0,

sizeof(ai_rawdata));

memset(ai_rawrecent,

0,

sizeof(ai_rawrecent));

ai_timer = AnalogInput[0]::cpUpdateRate * 100L;

//

get

going:

setLockedOutBit(uFblockIndex,

FALSE);

This code clears out the filter and starts sampling the hardware input when the device is reset.

17. Still in AnalogInput.nc, add the following code in bold to the FBC_WHEN_ONLINE else-if

statement in the AnalogInputDirector() function:

else if ((TFblock_command)iCommand == FBC_WHEN_ONLINE)

//

start

sampling

timer:

ai_timer = AnalogInput[0]::cpUpdateRate * 100L;

This code starts the sampling the hardware input when the device is set online.

18. Still in AnalogInput.nc, add the following code in bold to the FBC_WHEN_OFFLINE else/if

statement in the AnalogInputDirector() function:

else if ((TFblock_command)iCommand == FBC_WHEN_OFFLINE)

//

stop

sampling

timer:

ai_timer

=

0L;

This code stops sampling the hardware input when the device is set offline.

19. Still in AnalogInput.nc, add the following code in bold to process expiry of the sampling timer:

#endif //_HAS_INPUT_NV_

when (timer_expires(ai_timer)) {
int

iIndex;

int

iChannel;

unsigned long ulValue;

// are we in business?

if (fblockNormalNotLockedOut(AnalogInput[0]::global_index)) {

// yes we are. Repeat for each channel:

for (iChannel = 0; iChannel < AI_CHANNELS; ++iChannel) {

//

Move

historic

data:

for (iIndex = 0; iIndex < AI_FILTERSIZE-1; ++iIndex) {

Advertising