/* C-Language example for the Crandun Technologies CTI-AR200 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 within the range of interest. Please see the Programmers Guide for more information. */ #include "CTI_AR200.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); \ { char errMsg[300]; if (getIsError(sensorHandle)) { \ getErrorMessage(sensorHandle, errMsg, sizeof(errMsg)); \ printf("Library err msg is: %s\n", errMsg); \ } \ } \ setCommClosed(sensorHandle); \ printf("Please enter any character to exit: "); \ scanf("%c\n", &c); \ return (-1); \ } int main() { char c; long i; long rc; float maxRange, minRange; float fullScaleSpan, rangeData[MAXSAMPLES]; long numSkipped; long sensorHandle; printf("Crandun Technologies CTI-AR200 Library data filtering example.\n"); sensorHandle = -1; sensorHandle = getNewCTIAR200(); if (sensorHandle < 0) { printf("ERROR: getNewCTIAR200 returned error code %ld\n", sensorHandle); return -1; } printf("Enter the Full Scale Span of the AR200 sensor you are using (e.g. 25): "); scanf("%f", &fullScaleSpan); printf("Opening the serial port...\n"); rc = setCommOpen(sensorHandle, "COM1", 9600, fullScaleSpan); CHK_RETURN_VALUE(rc, setCommOpen) printf("Successfully opened the serial port!\n"); maxRange = 0.75 * fullScaleSpan; printf("Setting maximum valid range to %8.3f mm.\n", maxRange); rc = setMaxValidRange(sensorHandle, maxRange); CHK_RETURN_VALUE(rc, setMaxValidRange); minRange = 0.25 * fullScaleSpan; printf("Setting minimum valid range to %8.3f mm.\n", minRange); rc = setMinValidRange(sensorHandle, minRange); 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 targets\n"); printf("from less than %8.3f mm in distance to more than %8.3f mm.\n\n", minRange, maxRange); printf("Please enter any character to continue: "); scanf("%c\n", &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, 1000); 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 mm.\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\n", &c); return 0; }