// // Filtering 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 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_AR200.h" // required header #include // needed for cout #include // needed for Sleep function using namespace std; // we use cout and cerr using namespace Crandun; // all library symbols are in this namespace // A helper macro for checking return codes #define CHK_RETURN_VALUE(retCode, fnName) \ if (retCode < 0) { \ cerr << "ERROR: " #fnName " returned error code: " << rc << endl; \ { char errMsg[300]; if (my_Sensor.getIsError()) { \ my_Sensor.getErrorMessage(errMsg, sizeof(errMsg)); \ cerr << "Library error msg is: " << errMsg << endl; \ } \ }\ my_Sensor.setCommClosed(); \ cerr << "Please enter any character to exit: "; \ cin >> c; \ return (-1); \ } int main() { char c; long rc; const int MAXSAMPLES = 100; float maxRange, minRange, fullScaleSpan, rangeData[MAXSAMPLES]; CTI_AR200 my_Sensor; cout << "Crandun Technologies CTI-AR200 Library data filtering example." << endl << endl; cout << "Enter the Full Scale Span of the AR200 sensor you are using (e.g. 25): "; cin >> fullScaleSpan; rc = my_Sensor.setCommOpen("COM1", 9600, fullScaleSpan); if (rc != CTI_SUCCESS) { cerr << "ERROR: setCommParams returned error: " << rc << endl; return -1; } cout << "Successfully opened the serial port!" << endl; maxRange = 0.75 * fullScaleSpan; cout << "Setting maximum valid range to " << maxRange << " mm." << endl; rc = my_Sensor.setMaxValidRange(maxRange); CHK_RETURN_VALUE(rc, setMaxValidRange); minRange = 0.25 * fullScaleSpan; cout << "Setting minimum valid range to " << minRange << " mm." << endl; rc = my_Sensor.setMinValidRange(minRange); CHK_RETURN_VALUE(rc, setMinValidRange); cout << "Telling library to discard invalid samples." << endl << endl; rc = my_Sensor.setDiscardInvalidOn(); // always returns CTI_SUCCESS, so don't need to check return code cout << "Approximately 10 seconds of data will now be captured." << endl; cout << "Please move the sensor slowly, so that it reflects off a wide range of" << endl; cout << "targets, from less than " << minRange << " mm to more than " << maxRange << " mm in distance." << endl; cout << endl; cout << "Please enter any character to continue: "; cin >> c; rc = my_Sensor.setClearBuffer(); // purge anything currently in buffer (always returns success) cout << "Sleeping for 10 seconds - please slowly move the sensor." << endl; 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 = my_Sensor.getSamples(rangeData, MAXSAMPLES, 1, 1000); CHK_RETURN_VALUE(rc, getSamples); cout << "Read " << rc << " range samples from the sensor." << endl; for (int i = 0; i < rc; i++) cout << "Range " << i << " is " << rangeData[i] << endl; cout << my_Sensor.getNumBytesSkipped() << " bytes of data were skipped." << endl; my_Sensor.setCommClosed(); cout << "Please enter any character to exit: "; cin >> c; return 0; }