/* C-Language example for the Crandun Technologies CTI-AR4000 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_AR4000.h" /* required header */ #include #include /* for Sleep function */ #define MAXSAMPLES 100 /* 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", &c); \ return (-1); \ } int main() { char c; long i; long rc; float rangeData[MAXSAMPLES]; long numSkipped; long sensorHandle; printf("Crandun Technologies CTI-AR4000 Library data filtering example.\n"); sensorHandle = -1; sensorHandle = getNewCTIAR4000(); if (sensorHandle < 0) { printf("ERROR: getNewCTIAR4000 returned error code %ld\n", sensorHandle); printf("Please enter any character to exit: "); \ scanf("%c", &c); \ return -1; } printf("Opening the serial port...\n"); rc = setCommOpen(sensorHandle, "COM1:", 9600); CHK_RETURN_VALUE(rc, setCommOpen) 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\n"); rc = setDiscardInvalidOn(sensorHandle); // always returns CTI_SUCCESS, so don't need to check return code 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", &c); rc = setClearBuffer(sensorHandle); // purge anything currently in buffer (always returns success) printf("Sleeping for 10 seconds - please slowly move the sensor.\n"); Sleep(10000); /* Sensor sample rate is factory default of 5 samples/second, so the number of possible samples is about 50. However, some should be filtered by the library, so we should get somewhat less than 50 returned. */ rc = getSamples(sensorHandle, rangeData, MAXSAMPLES, 1); CHK_RETURN_VALUE(rc, getSamples); printf("Read %d range samples from the sensor.\n", rc); for (i = 0; i < rc; i++) printf("Range %d is %8.3f inches.\n", i, rangeData[i]); numSkipped = getNumBytesSkipped(sensorHandle); printf("%ld bytes of data were skipped.\n", numSkipped); setCommClosed(sensorHandle); printf("Please enter any character to continue: "); scanf("%c", &c); return 0; }