2011-12-24 21:01:00 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
|
|
|
//
|
2012-10-11 21:27:19 -03:00
|
|
|
// Simple test for the AP_InertialSensor driver.
|
2011-12-24 21:01:00 -04:00
|
|
|
//
|
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
#include <stdarg.h>
|
2011-12-24 21:01:00 -04:00
|
|
|
#include <AP_Common.h>
|
2012-10-11 21:27:19 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include <AP_HAL_AVR.h>
|
|
|
|
#include <AP_Math.h>
|
2012-08-20 20:22:44 -03:00
|
|
|
#include <AP_Param.h>
|
2012-10-11 21:27:19 -03:00
|
|
|
#include <AP_ADC.h>
|
|
|
|
#include <AP_InertialSensor.h>
|
2011-12-24 21:01:00 -04:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
#define APM_HARDWARE_APM1 1
|
|
|
|
#define APM_HARDWARE_APM2 2
|
|
|
|
|
|
|
|
#define CONFIG_APM_HARDWARE APM_HARDWARE_APM2
|
|
|
|
//#define CONFIG_APM_HARDWARE APM_HARDWARE_APM1
|
|
|
|
|
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
2012-10-11 21:27:19 -03:00
|
|
|
#define SAMPLE_UNIT 1
|
|
|
|
#define A_LED_PIN 27
|
|
|
|
#define C_LED_PIN 25
|
2012-11-05 00:27:03 -04:00
|
|
|
#else
|
2012-10-11 21:27:19 -03:00
|
|
|
// we need 5x as many samples on the oilpan
|
|
|
|
#define SAMPLE_UNIT 5
|
|
|
|
#define A_LED_PIN 37
|
|
|
|
#define C_LED_PIN 35
|
2012-11-05 00:27:03 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
2012-10-11 21:27:19 -03:00
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL_AVR_APM2;
|
2012-09-28 07:21:59 -03:00
|
|
|
AP_InertialSensor_MPU6000 ins;
|
2012-10-11 21:27:19 -03:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
#else
|
2012-10-11 21:27:19 -03:00
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL_AVR_APM1;
|
2012-11-05 00:27:03 -04:00
|
|
|
AP_ADC_ADS7844 adc;
|
|
|
|
AP_InertialSensor_Oilpan ins(&adc);
|
2012-10-11 21:27:19 -03:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
#endif
|
2011-12-24 21:01:00 -04:00
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
static void flash_leds(bool on) {
|
|
|
|
hal.gpio->write(A_LED_PIN, on);
|
|
|
|
hal.gpio->write(C_LED_PIN, ~on);
|
|
|
|
}
|
|
|
|
|
2011-12-24 21:01:00 -04:00
|
|
|
void setup(void)
|
|
|
|
{
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->println("AP_InertialSensor startup...");
|
2011-12-24 21:01:00 -04:00
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.gpio->pinMode(A_LED_PIN, GPIO_OUTPUT);
|
|
|
|
hal.gpio->pinMode(C_LED_PIN, GPIO_OUTPUT);
|
2011-12-24 21:01:00 -04:00
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM2
|
2012-08-17 03:19:56 -03:00
|
|
|
// we need to stop the barometer from holding the SPI bus
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.gpio->pinMode(40, GPIO_OUTPUT);
|
|
|
|
hal.gpio->write(40, 1);
|
|
|
|
#endif
|
2011-12-24 21:26:46 -04:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
#if CONFIG_APM_HARDWARE == APM_HARDWARE_APM1
|
|
|
|
adc.Init(&scheduler); // APM ADC library initialization
|
|
|
|
#endif
|
2012-11-29 07:56:13 -04:00
|
|
|
ins.init(AP_InertialSensor::COLD_START,
|
|
|
|
AP_InertialSensor::RATE_100HZ,
|
2012-10-11 21:27:19 -03:00
|
|
|
NULL);
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
// display initial values
|
|
|
|
display_offsets_and_scaling();
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->println("Complete. Reading:");
|
2011-12-24 21:01:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void)
|
|
|
|
{
|
2012-11-05 00:27:03 -04:00
|
|
|
int16_t user_input;
|
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->println();
|
|
|
|
hal.console->println_P(PSTR(
|
|
|
|
"Menu:\r\n"
|
|
|
|
" c) calibrate accelerometers\r\n"
|
|
|
|
" d) display offsets and scaling\r\n"
|
|
|
|
" l) level (capture offsets from level)\r\n"
|
|
|
|
" t) test"));
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
// wait for user input
|
2012-10-11 21:27:19 -03:00
|
|
|
while( !hal.console->available() ) {
|
|
|
|
hal.scheduler->delay(20);
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read in user input
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
user_input = hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
if( user_input == 'c' || user_input == 'C' ) {
|
|
|
|
run_calibration();
|
|
|
|
display_offsets_and_scaling();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( user_input == 'd' || user_input == 'D' ) {
|
|
|
|
display_offsets_and_scaling();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( user_input == 'l' || user_input == 'L' ) {
|
|
|
|
run_level();
|
|
|
|
display_offsets_and_scaling();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( user_input == 't' || user_input == 'T' ) {
|
|
|
|
run_test();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_calibration()
|
|
|
|
{
|
|
|
|
// clear off any other characters (like line feeds,etc)
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
2012-10-11 21:27:19 -03:00
|
|
|
ins.calibrate_accel(NULL, hal.console);
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_offsets_and_scaling()
|
|
|
|
{
|
|
|
|
Vector3f accel_offsets = ins.get_accel_offsets();
|
|
|
|
Vector3f accel_scale = ins.get_accel_scale();
|
|
|
|
Vector3f gyro_offsets = ins.get_gyro_offsets();
|
|
|
|
|
|
|
|
// display results
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->printf_P(
|
|
|
|
PSTR("\nAccel Offsets X:%10.8f \t Y:%10.8f \t Z:%10.8f\n"),
|
2012-11-05 00:27:03 -04:00
|
|
|
accel_offsets.x,
|
|
|
|
accel_offsets.y,
|
|
|
|
accel_offsets.z);
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->printf_P(
|
|
|
|
PSTR("Accel Scale X:%10.8f \t Y:%10.8f \t Z:%10.8f\n"),
|
2012-11-05 00:27:03 -04:00
|
|
|
accel_scale.x,
|
|
|
|
accel_scale.y,
|
|
|
|
accel_scale.z);
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->printf_P(
|
|
|
|
PSTR("Gyro Offsets X:%10.8f \t Y:%10.8f \t Z:%10.8f\n"),
|
2012-11-05 00:27:03 -04:00
|
|
|
gyro_offsets.x,
|
|
|
|
gyro_offsets.y,
|
|
|
|
gyro_offsets.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_level()
|
|
|
|
{
|
|
|
|
// clear off any input in the buffer
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// display message to user
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->print("Place APM on a level surface and press any key..\n");
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
// wait for user input
|
2012-10-11 21:27:19 -03:00
|
|
|
while( !hal.console->available() ) {
|
|
|
|
hal.scheduler->delay(20);
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// run accel level
|
2012-10-11 21:27:19 -03:00
|
|
|
ins.init_accel(flash_leds);
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
// display results
|
|
|
|
display_offsets_and_scaling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_test()
|
|
|
|
{
|
|
|
|
Vector3f accel;
|
|
|
|
Vector3f gyro;
|
2012-08-17 03:19:56 -03:00
|
|
|
float temperature;
|
2012-11-05 00:27:03 -04:00
|
|
|
float length;
|
|
|
|
|
|
|
|
// flush any user input
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
2012-08-17 03:19:56 -03:00
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
// clear out any existing samples from ins
|
2012-08-17 03:19:56 -03:00
|
|
|
ins.update();
|
|
|
|
|
2012-11-05 00:27:03 -04:00
|
|
|
// loop as long as user does not press a key
|
2012-10-11 21:27:19 -03:00
|
|
|
while( !hal.console->available() ) {
|
2012-11-05 00:27:03 -04:00
|
|
|
|
|
|
|
// wait until we have 8 samples
|
|
|
|
while( ins.num_samples_available() < 8 * SAMPLE_UNIT ) {
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.scheduler->delay(1);
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read samples from ins
|
|
|
|
ins.update();
|
|
|
|
accel = ins.get_accel();
|
|
|
|
gyro = ins.get_gyro();
|
|
|
|
temperature = ins.temperature();
|
|
|
|
|
|
|
|
length = sqrt(accel.x*accel.x + accel.y*accel.y + accel.z*accel.z);
|
|
|
|
|
|
|
|
// display results
|
2012-10-11 21:27:19 -03:00
|
|
|
hal.console->printf_P(PSTR("Accel X:%4.2f \t Y:%4.2f \t Z:%4.2f \t len:%4.2f \t Gyro X:%4.2f \t Y:%4.2f \t Z:%4.2f \t Temp:%4.2f\n"),
|
2012-11-05 00:27:03 -04:00
|
|
|
accel.x, accel.y, accel.z, length, gyro.x, gyro.y, gyro.z, temperature);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear user input
|
2012-10-11 21:27:19 -03:00
|
|
|
while( hal.console->available() ) {
|
|
|
|
hal.console->read();
|
2012-11-05 00:27:03 -04:00
|
|
|
}
|
2012-11-24 06:08:06 -04:00
|
|
|
}
|
2012-10-11 21:27:19 -03:00
|
|
|
|
|
|
|
AP_HAL_MAIN();
|