ardupilot/libraries/AP_AHRS/examples/AHRS_Test/AHRS_Test.cpp

103 lines
2.4 KiB
C++
Raw Normal View History

//
// Simple test for the AP_AHRS interface
//
#include <AP_AHRS/AP_AHRS.h>
#include <AP_HAL/AP_HAL.h>
#include <AP_BoardConfig/AP_BoardConfig.h>
2017-03-10 22:45:55 -04:00
#include <GCS_MAVLink/GCS_Dummy.h>
void setup();
void loop();
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
static AP_BoardConfig board_config;
static AP_InertialSensor ins;
2014-11-27 19:40:52 -04:00
static Compass compass;
2012-11-14 12:10:15 -04:00
static AP_GPS gps;
static AP_Baro barometer;
static AP_SerialManager serial_manager;
2012-03-23 02:27:37 -03:00
2016-11-30 04:59:11 -04:00
class DummyVehicle {
public:
RangeFinder sonar{serial_manager, ROTATION_PITCH_270};
2018-03-05 16:34:26 -04:00
NavEKF2 EKF2{&ahrs, sonar};
NavEKF3 EKF3{&ahrs, sonar};
2018-03-11 04:21:02 -03:00
AP_AHRS_NavEKF ahrs{EKF2, EKF3,
AP_AHRS_NavEKF::FLAG_ALWAYS_USE_EKF};
2016-11-30 04:59:11 -04:00
};
static DummyVehicle vehicle;
2016-11-30 04:59:11 -04:00
// choose which AHRS system to use
2018-03-11 04:21:02 -03:00
// AP_AHRS_DCM ahrs = AP_AHRS_DCM::create(barometer, gps);
AP_AHRS_NavEKF &ahrs = vehicle.ahrs;
void setup(void)
{
board_config.init();
2015-12-26 14:22:18 -04:00
ins.init(100);
ahrs.init();
2015-02-03 02:56:54 -04:00
serial_manager.init();
if( compass.init() ) {
2012-11-14 12:10:15 -04:00
hal.console->printf("Enabling compass\n");
ahrs.set_compass(&compass);
} else {
2012-11-14 12:10:15 -04:00
hal.console->printf("No compass detected\n");
}
2017-06-28 07:36:34 -03:00
gps.init(serial_manager);
}
void loop(void)
{
static uint16_t counter;
static uint32_t last_t, last_print, last_compass;
uint32_t now = AP_HAL::micros();
float heading = 0;
if (last_t == 0) {
last_t = now;
return;
}
last_t = now;
if (now - last_compass > 100 * 1000UL &&
compass.read()) {
heading = compass.calculate_heading(ahrs.get_rotation_body_to_ned());
// read compass at 10Hz
last_compass = now;
}
ahrs.update();
counter++;
2012-11-14 12:10:15 -04:00
if (now - last_print >= 100000 /* 100ms : 10hz */) {
Vector3f drift = ahrs.get_gyro_drift();
hal.console->printf(
"r:%4.1f p:%4.1f y:%4.1f "
"drift=(%5.1f %5.1f %5.1f) hdg=%.1f rate=%.1f\n",
(double)ToDeg(ahrs.roll),
(double)ToDeg(ahrs.pitch),
(double)ToDeg(ahrs.yaw),
(double)ToDeg(drift.x),
(double)ToDeg(drift.y),
(double)ToDeg(drift.z),
(double)(compass.use_for_yaw() ? ToDeg(heading) : 0.0f),
(double)((1.0e6f * counter) / (now-last_print)));
last_print = now;
counter = 0;
}
}
2012-11-14 12:10:15 -04:00
2017-03-10 22:45:55 -04:00
const struct AP_Param::GroupInfo GCS_MAVLINK::var_info[] = {
AP_GROUPEND
};
GCS_Dummy _gcs;
2012-11-14 12:10:15 -04:00
AP_HAL_MAIN();