2011-12-21 00:30:22 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-11-13 02:42:20 -04:00
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
#include "AP_InertialSensor_HIL.h"
|
2012-10-11 21:27:19 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
const extern AP_HAL::HAL& hal;
|
2011-11-13 02:42:20 -04:00
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
AP_InertialSensor_HIL::AP_InertialSensor_HIL() : AP_InertialSensor() {
|
2013-12-08 18:50:12 -04:00
|
|
|
_accel[0] = Vector3f(0, 0, -GRAVITY_MSS);
|
2013-04-12 01:30:35 -03:00
|
|
|
}
|
2013-03-31 05:38:48 -03:00
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
uint16_t AP_InertialSensor_HIL::_init_sensor( Sample_rate sample_rate ) {
|
2012-12-03 08:27:21 -04:00
|
|
|
switch (sample_rate) {
|
|
|
|
case RATE_50HZ:
|
|
|
|
_sample_period_ms = 20;
|
|
|
|
break;
|
|
|
|
case RATE_100HZ:
|
|
|
|
_sample_period_ms = 10;
|
|
|
|
break;
|
|
|
|
case RATE_200HZ:
|
|
|
|
_sample_period_ms = 5;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-17 03:19:56 -03:00
|
|
|
return AP_PRODUCT_ID_NONE;
|
2012-05-08 23:26:35 -03:00
|
|
|
}
|
2011-11-13 02:42:20 -04:00
|
|
|
|
|
|
|
/*================ AP_INERTIALSENSOR PUBLIC INTERFACE ==================== */
|
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
bool AP_InertialSensor_HIL::update( void ) {
|
2012-10-11 21:27:19 -03:00
|
|
|
uint32_t now = hal.scheduler->millis();
|
2012-12-03 08:27:21 -04:00
|
|
|
_delta_time_usec = (now - _last_update_ms) * 1000;
|
|
|
|
_last_update_ms = now;
|
2012-08-17 03:19:56 -03:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-13 02:42:20 -04:00
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
float AP_InertialSensor_HIL::get_delta_time() {
|
2013-01-11 06:17:21 -04:00
|
|
|
return _delta_time_usec * 1.0e-6;
|
2012-08-17 03:19:56 -03:00
|
|
|
}
|
2013-10-09 19:38:19 -03:00
|
|
|
|
2013-09-28 06:33:21 -03:00
|
|
|
float AP_InertialSensor_HIL::get_gyro_drift_rate(void) {
|
2013-05-04 02:16:35 -03:00
|
|
|
// 0.5 degrees/second/minute
|
|
|
|
return ToRad(0.5/60);
|
2012-08-17 03:19:56 -03:00
|
|
|
}
|
2013-09-26 21:33:08 -03:00
|
|
|
|
2013-12-08 05:43:53 -04:00
|
|
|
bool AP_InertialSensor_HIL::_sample_available()
|
2012-09-06 23:47:19 -03:00
|
|
|
{
|
2012-10-11 21:27:19 -03:00
|
|
|
uint16_t ret = (hal.scheduler->millis() - _last_update_ms)
|
|
|
|
/ _sample_period_ms;
|
2012-12-03 08:27:21 -04:00
|
|
|
|
2013-09-26 21:33:08 -03:00
|
|
|
return ret > 0;
|
2012-09-06 23:47:19 -03:00
|
|
|
}
|
2013-10-08 03:28:39 -03:00
|
|
|
|
|
|
|
bool AP_InertialSensor_HIL::wait_for_sample(uint16_t timeout_ms)
|
|
|
|
{
|
2013-12-08 05:43:53 -04:00
|
|
|
if (_sample_available()) {
|
2013-10-08 03:28:39 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint32_t start = hal.scheduler->millis();
|
|
|
|
while ((hal.scheduler->millis() - start) < timeout_ms) {
|
|
|
|
hal.scheduler->delay(1);
|
2013-12-08 05:43:53 -04:00
|
|
|
if (_sample_available()) {
|
2013-10-08 03:28:39 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-06 22:53:59 -04:00
|
|
|
|
|
|
|
void AP_InertialSensor_HIL::set_accel(const Vector3f &accel)
|
|
|
|
{
|
2013-12-08 18:50:12 -04:00
|
|
|
_previous_accel[0] = _accel[0];
|
|
|
|
_accel[0] = accel;
|
2013-11-06 22:53:59 -04:00
|
|
|
_last_accel_usec = hal.scheduler->micros();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AP_InertialSensor_HIL::set_gyro(const Vector3f &gyro)
|
|
|
|
{
|
2013-12-08 18:50:12 -04:00
|
|
|
_gyro[0] = gyro;
|
2013-11-06 22:53:59 -04:00
|
|
|
_last_gyro_usec = hal.scheduler->micros();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
try to detect bad accel/gyro sensors
|
|
|
|
*/
|
2013-12-08 05:43:53 -04:00
|
|
|
bool AP_InertialSensor_HIL::healthy(void) const
|
2013-11-06 22:53:59 -04:00
|
|
|
{
|
|
|
|
uint32_t tnow = hal.scheduler->micros();
|
|
|
|
if ((tnow - _last_accel_usec) > 40000) {
|
|
|
|
// accels have not updated
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((tnow - _last_gyro_usec) > 40000) {
|
|
|
|
// gyros have not updated
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-08 18:50:12 -04:00
|
|
|
if (fabs(_accel[0].x) > 30 && fabs(_accel[0].y) > 30 && fabs(_accel[0].z) > 30 &&
|
|
|
|
(_previous_accel[0] - _accel[0]).length() < 0.01f) {
|
2013-11-06 22:53:59 -04:00
|
|
|
// unchanging accel, large in all 3 axes. This is a likely
|
|
|
|
// accelerometer failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|