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
|
|
|
|
2014-08-13 10:59:22 -03:00
|
|
|
AP_InertialSensor_HIL::AP_InertialSensor_HIL() :
|
|
|
|
AP_InertialSensor(),
|
|
|
|
_sample_period_usec(0),
|
|
|
|
_last_sample_usec(0)
|
|
|
|
{
|
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:
|
2014-02-26 17:10:42 -04:00
|
|
|
_sample_period_usec = 20000;
|
2012-12-03 08:27:21 -04:00
|
|
|
break;
|
|
|
|
case RATE_100HZ:
|
2014-02-26 17:10:42 -04:00
|
|
|
_sample_period_usec = 10000;
|
2012-12-03 08:27:21 -04:00
|
|
|
break;
|
|
|
|
case RATE_200HZ:
|
2014-02-26 17:10:42 -04:00
|
|
|
_sample_period_usec = 5000;
|
2012-12-03 08:27:21 -04:00
|
|
|
break;
|
2014-02-18 19:29:00 -04:00
|
|
|
case RATE_400HZ:
|
2014-02-26 17:10:42 -04:00
|
|
|
_sample_period_usec = 2500;
|
2014-02-18 19:29:00 -04:00
|
|
|
break;
|
2012-12-03 08:27:21 -04:00
|
|
|
}
|
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 ) {
|
2014-02-26 17:10:42 -04:00
|
|
|
uint32_t now = hal.scheduler->micros();
|
2014-08-09 08:22:51 -03:00
|
|
|
_last_sample_usec = now;
|
2012-08-17 03:19:56 -03:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-13 02:42:20 -04:00
|
|
|
|
2014-01-02 20:46:24 -04:00
|
|
|
float AP_InertialSensor_HIL::get_delta_time() const {
|
2014-02-26 17:10:42 -04:00
|
|
|
return _sample_period_usec * 1.0e-6f;
|
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
|
|
|
{
|
2014-08-09 08:22:51 -03:00
|
|
|
uint32_t tnow = hal.scheduler->micros();
|
|
|
|
bool have_sample = false;
|
|
|
|
while (tnow - _last_sample_usec > _sample_period_usec) {
|
|
|
|
have_sample = true;
|
|
|
|
_last_sample_usec += _sample_period_usec;
|
|
|
|
}
|
|
|
|
return have_sample;
|
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;
|
|
|
|
}
|
2014-08-09 08:22:51 -03:00
|
|
|
uint32_t start = hal.scheduler->micros();
|
2013-10-08 03:28:39 -03:00
|
|
|
while ((hal.scheduler->millis() - start) < timeout_ms) {
|
2014-08-09 08:22:51 -03:00
|
|
|
uint32_t tnow = hal.scheduler->micros();
|
|
|
|
uint32_t tdelay = (_last_sample_usec + _sample_period_usec) - tnow;
|
|
|
|
if (tdelay < 100000) {
|
|
|
|
hal.scheduler->delay_microseconds(tdelay);
|
|
|
|
}
|
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
|
|
|
|
2014-02-22 17:16:33 -04:00
|
|
|
void AP_InertialSensor_HIL::set_accel(uint8_t instance, const Vector3f &accel)
|
2013-11-06 22:53:59 -04:00
|
|
|
{
|
2014-02-27 15:32:54 -04:00
|
|
|
if (instance >= INS_MAX_INSTANCES) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-22 17:16:33 -04:00
|
|
|
_previous_accel[instance] = _accel[instance];
|
|
|
|
_accel[instance] = accel;
|
|
|
|
_last_accel_usec[instance] = hal.scheduler->micros();
|
2013-11-06 22:53:59 -04:00
|
|
|
}
|
|
|
|
|
2014-02-22 17:16:33 -04:00
|
|
|
void AP_InertialSensor_HIL::set_gyro(uint8_t instance, const Vector3f &gyro)
|
2013-11-06 22:53:59 -04:00
|
|
|
{
|
2014-02-27 15:32:54 -04:00
|
|
|
if (instance >= INS_MAX_INSTANCES) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-22 17:16:33 -04:00
|
|
|
_gyro[instance] = gyro;
|
|
|
|
_last_gyro_usec[instance] = hal.scheduler->micros();
|
2013-11-06 22:53:59 -04:00
|
|
|
}
|
|
|
|
|
2014-02-22 17:16:33 -04:00
|
|
|
bool AP_InertialSensor_HIL::get_gyro_health(uint8_t instance) const
|
2013-11-06 22:53:59 -04:00
|
|
|
{
|
2014-02-27 15:32:54 -04:00
|
|
|
if (instance >= INS_MAX_INSTANCES) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-22 17:16:33 -04:00
|
|
|
return (hal.scheduler->micros() - _last_gyro_usec[instance]) < 40000;
|
2013-11-06 22:53:59 -04:00
|
|
|
}
|
2014-02-22 17:16:33 -04:00
|
|
|
|
|
|
|
bool AP_InertialSensor_HIL::get_accel_health(uint8_t instance) const
|
|
|
|
{
|
2014-02-27 15:32:54 -04:00
|
|
|
if (instance >= INS_MAX_INSTANCES) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-22 17:16:33 -04:00
|
|
|
return (hal.scheduler->micros() - _last_accel_usec[instance]) < 40000;
|
|
|
|
}
|
|
|
|
|
2014-02-23 04:10:07 -04:00
|
|
|
uint8_t AP_InertialSensor_HIL::get_gyro_count(void) const
|
|
|
|
{
|
|
|
|
if (get_gyro_health(1)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AP_InertialSensor_HIL::get_accel_count(void) const
|
|
|
|
{
|
|
|
|
if (get_accel_health(1)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|