/* C-Language example for the Crandun Technologies CTI-HSIF software library. Copyright (c) 2001, Crandun Technologies Inc. This example demonstrates how to use the library's filtering functions to exclude samples that are not of interest. In this particular example, filtering is done based on the sample's range (distance). However, samples may also be filtered based on numerous other criteria. (Please see the Programmers Guide.) */ #include "CTI_HSIF.h" /* required header */ #include #include /* for Sleep function */ #define MAXSAMPLES 5000 /* A helper macro for checking return codes */ #define CHK_RETURN_VALUE(retCode, fnName) \ if (retCode < 0) { \ printf("ERROR: " #fnName " returned error code: %ld\n", rc); \ setCommClosed(sensorHandle); \ printf("Please enter any character to exit: "); \ scanf("%c\n", &c); \ return (-1); \ } int main() { char c; long rc; HSIF_DATA_PT rangeData[MAXSAMPLES]; long sensorHandle; printf("Crandun Technologies CTI-HSIF Library data filtering example.\n"); sensorHandle = -1; sensorHandle = getNewCTIHSIF(); if (sensorHandle < 0) { printf("ERROR: getNewCTIHSIF returned error code %ld\n", sensorHandle); printf("Please enter any character to exit: "); scanf("%c\n", &c); return -1; } /* Set board I/O address and interrupt number (0=don't use interrupts) */ rc = setBoardParams(sensorHandle, "100", 0); if (rc != CTI_SUCCESS) { printf("ERROR: setBoardParams returned error %d\n", rc); printf("Please enter any character to exit: "); scanf("%c\n", &c); return -1; } /* Set calibration data file to use. MUST match the actual sensor used */ rc = setCalibrationFile(sensorHandle, "C:\\lookuphs"); if (rc != CTI_SUCCESS) { printf("ERROR: setCalibrationFile returned error %d\n", rc); printf("Please enter any character to exit: "); scanf("%c\n", &c); return -1; } /* Open communications to the serial port and high-speed interface */ printf("Opening the serial port...\n"); rc = setCommOpen(sensorHandle, "COM1:", 9600); if (rc != CTI_SUCCESS) { printf("ERROR: setCommOpen returned error %d\n", rc); printf("Please enter any character to exit: "); scanf("%c\n", &c); return -1; } printf("Successfully opened the serial port!\n"); printf("Setting maximum valid range to 4 feet.\n"); rc = setMaxValidRange(sensorHandle, 48.0); CHK_RETURN_VALUE(rc, setMaxValidRange); printf("Setting minimum valid range to 2 feet.\n"); rc = setMinValidRange(sensorHandle, 24.0); CHK_RETURN_VALUE(rc, setMinValidRange); printf("Telling library to discard invalid samples.\n"); setDiscardInvalidOn(sensorHandle); // always returns CTI_SUCCESS, so don't need to check return code /* Set a slower sample rate (must change max range to do this - see the Acuity HW manual) */ printf("Setting sensor max range and sample rate.\n\n"); rc = setSensorMaxRange(sensorHandle, 9950); CHK_RETURN_VALUE(rc, setSensorMaxRange); rc = setSamplesPerSec(sensorHandle, 50); CHK_RETURN_VALUE(rc, setSamplesPerSec); printf("Approximately 10 seconds of data will now be captured.\n"); printf("Please move the sensor slowly, so that it reflects off a wide range of\n"); printf("targets, from less than 2 feet in distance to more than 4 feet in distance.\n\n"); printf("Please enter any character to continue: "); scanf("%c\n", &c); setResetHSIFBoard(sensorHandle); // purge anything currently in buffer (always returns success) setClearBuffer(sensorHandle); // always returns success printf("Sleeping for 10 seconds - please slowly move the sensor.\n"); Sleep(10000); /* Sensor sample rate is 50 samples/second, so the number of possible samples over 10 seconds is about 500. However, some should be filtered by the library, so we should get somewhat less than 500 returned. */ rc = getSamples(sensorHandle, rangeData, MAXSAMPLES, 1, 12000); CHK_RETURN_VALUE(rc, getSamples); setCommClosed(sensorHandle); printf("Read %d range samples from the sensor.\n", rc); printf("Please enter any character to continue: "); scanf("%c\n", &c); return 0; }