/** \brief Proximity sensor threshold calibration application * * This application illustrates the use of the sensor_calibrate() function * to set the proximity detection thresholds in a 3-channel proximity sensor. * This threshold is the level at which the sensor will report that an object * is near the device. * * The calibration sequence requires three steps, one for each channel. During * each step, an object is placed at the desired distance in front of the * sensor, and the user presses the button on the board to trigger a proximity * reading. * * After Step 3 is completed, the threshold values for the sensor are * calculated and are written to non-volatile (flash) memory on the * microcontroller. These values will continue to be used for future * proximity readings, unless they are overwritten by an application * calling the sensor_set_threshold function for the proximity sensor * channel(s). */ int main(void) { sensor_t prox_dev; /* Proximity sensor device */ sensor_data_t prox_data; /* Proximity data */ int led_num = 0; /* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards) * I/O pin mappings and any other configurable resources selected in * the build configuration. */ sensor_platform_init(); LED_On(ALL_LEDS); /* Wait for user to press button to start */ prompt_user("Press button to start"); /* Attach descriptor and initialize the proximity sensor */ sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0); #if (SET_PROX_CURRENT == true) /* Manually set LED current value for each channel */ /* Otherwise, sensor will use default values */ sensor_set_channel(&prox_dev, SENSOR_CHANNEL_ALL); sensor_set_current(&prox_dev, PROX_CURRENT_mA); #endif /* Set sensor data output formats (for display after calibration * complete) */ prox_data.scaled = true; /* Perform calibration sequence */ /* Step 1 */ printf("Setting channel 1: "); prompt_user("Place object at desired distance and press button"); (void)sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 1, NULL); /* Step 2 */ printf("Setting channel 2: "); prompt_user("Place object at desired distance and press button"); (void)sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 2, NULL); /* Step 3 */ printf("Setting channel 3: "); prompt_user("Place object at desired distance and press button"); if (sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 3, NULL) != true) { if (prox_dev.err == SENSOR_ERR_IO) { printf("Calibration failure: write error\n\r"); } else { printf("Unknown error while calibrating device\n\r"); } while (true) { /* Error occurred, loop forever */ } } int16_t value; /* Display threshold values */ sensor_set_channel(&prox_dev, 1); sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY, &value); printf("Channel 1 threshold = %d\r\n", value); sensor_set_channel(&prox_dev, 2); sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY, &value); printf("Channel 2 threshold = %d\r\n", value); sensor_set_channel(&prox_dev, 3); sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY, &value); printf("Channel 3 threshold = %d\r\n", value); /* Once the calibration is complete, the proximity status is * continuously captured and displayed. */ while (true) { /* Change LED display */ LED_Toggle(led_array [led_num++]); if (led_num >= NUM_BLINK_LEDS) { led_num = 0; } /* Sample proximity and display results for each channel */ sensor_get_proximity(&prox_dev, &prox_data); printf("prox = 1:%s 2:%s 3:%s\r\n", prox_labels[prox_data.proximity.value[0]], prox_labels[prox_data.proximity.value[1]], prox_labels[prox_data.proximity.value[2]]); delay_ms(500); } return 0; }
/** \brief Compass / magnetometer calibration application * * This application illustrates the use of the sensor_calibrate() function * for compass/magnetometer calibration. It prompts the user (via serial * output) to manipulate the sensor board and press a button to continue. * * The calibration process is used to correct for fixed magnetic forces * present on the board where the compass is mounted. If uncorrected, these * fixed forces will prevent accurate measurement of the actual external * magnetic forces (e.g. magnetic North). * * The calibration sequence requires three steps. During each step, the * board is placed in a specific orientation, and the user presses the button * on the board to trigger a compass sensor reading. * * The three orientations are: * -# Board lying flat (on a table, for example). * -# Board rotated 180 degrees (end-for-end), still lying flat. * -# Board flipped (inverted) so that the opposite side is facing up. * * After Step 3 is completed, the calibration values for the sensor are * calculated and are written to non-volatile (flash) memory on the * microcontroller. These values will continue to be used for future * compass heading readings. */ int main(void) { sensor_t compass_dev; /* Compass/magnetometer device */ sensor_data_t compass_data; /* Compass data */ /* Initialize hardware & sensor interfaces */ sensor_platform_init(); LED_On(ALL_LEDS); /* Wait for user to press button to start */ prompt_user("Press button to start"); /* Attach descriptor and initialize the compass device */ sensor_attach(&compass_dev, SENSOR_TYPE_COMPASS, 0, 0); /* Set sensor data output formats (for display after calibration * is complete) */ compass_data.scaled = true; /* Perform calibration sequence */ /* Step 1 */ prompt_user("Lay board flat & press button"); (void)sensor_calibrate(&compass_dev, MANUAL_CALIBRATE, 1, NULL); /* Step 2 */ prompt_user("Rotate 180 degrees & press button"); (void)sensor_calibrate(&compass_dev, MANUAL_CALIBRATE, 2, NULL); /* Step 3 */ prompt_user("Flip board & press button"); if (sensor_calibrate(&compass_dev, MANUAL_CALIBRATE, 3, NULL) == false) { if (compass_dev.err == SENSOR_ERR_IO) { printf("Calibration failure: write error\n\r"); } else { printf("Unknown error while calibrating device\n\r"); } while (true) { /* Error occurred, loop forever */ } } /* Once the calibration is complete, the magnetic heading is * continuously * calculated and displayed. */ while (true) { static uint8_t led_num = 0; /* Change LED display */ LED_Toggle(led_array[led_num++]); if (led_num >= NUM_BLINK_LEDS) { led_num = 0; } /* Sample compass and display heading values */ sensor_get_heading(&compass_dev, &compass_data); printf("Direction = %d, Inclination = %d, Strength = %d uT\r\n", (int)compass_data.heading.direction, (int)compass_data.heading.inclination, (int)compass_data.heading.strength); delay_ms(100); } return 0; }