2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-02-14 00:27:07 -04:00
|
|
|
/*
|
2015-02-23 19:17:44 -04:00
|
|
|
* AP_Compass_HIL.cpp - HIL backend for AP_Compass
|
2012-08-17 03:19:22 -03:00
|
|
|
*
|
|
|
|
*/
|
2011-02-14 00:27:07 -04:00
|
|
|
|
|
|
|
|
2015-08-11 03:28:42 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2011-02-14 00:27:07 -04:00
|
|
|
#include "AP_Compass_HIL.h"
|
|
|
|
|
2012-10-11 17:48:39 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-05-02 01:59:33 -03:00
|
|
|
// constructor
|
2014-11-15 21:58:23 -04:00
|
|
|
AP_Compass_HIL::AP_Compass_HIL(Compass &compass):
|
2015-05-15 01:04:00 -03:00
|
|
|
AP_Compass_Backend(compass)
|
2013-05-02 01:59:33 -03:00
|
|
|
{
|
2015-05-15 01:04:00 -03:00
|
|
|
memset(_compass_instance, 0, sizeof(_compass_instance));
|
2014-11-15 21:58:23 -04:00
|
|
|
_compass._setup_earth_field();
|
2013-05-02 01:59:33 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
// detect the sensor
|
|
|
|
AP_Compass_Backend *AP_Compass_HIL::detect(Compass &compass)
|
2013-05-02 01:59:33 -03:00
|
|
|
{
|
2014-11-15 21:58:23 -04:00
|
|
|
AP_Compass_HIL *sensor = new AP_Compass_HIL(compass);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sensor == nullptr) {
|
|
|
|
return nullptr;
|
2014-11-15 21:58:23 -04:00
|
|
|
}
|
|
|
|
if (!sensor->init()) {
|
|
|
|
delete sensor;
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2014-11-15 21:58:23 -04:00
|
|
|
}
|
|
|
|
return sensor;
|
2013-05-02 01:59:33 -03:00
|
|
|
}
|
|
|
|
|
2014-11-15 21:58:23 -04:00
|
|
|
bool
|
|
|
|
AP_Compass_HIL::init(void)
|
2011-02-14 00:27:07 -04:00
|
|
|
{
|
2015-05-15 01:04:00 -03:00
|
|
|
// register two compass instances
|
2015-05-15 18:06:04 -03:00
|
|
|
for (uint8_t i=0; i<HIL_NUM_COMPASSES; i++) {
|
2015-05-15 01:04:00 -03:00
|
|
|
_compass_instance[i] = register_compass();
|
|
|
|
}
|
2014-11-15 21:58:23 -04:00
|
|
|
return true;
|
2014-05-12 06:44:15 -03:00
|
|
|
}
|
|
|
|
|
2015-02-23 19:17:44 -04:00
|
|
|
void AP_Compass_HIL::read()
|
2012-08-26 00:42:00 -03:00
|
|
|
{
|
2015-07-20 16:56:09 -03:00
|
|
|
for (uint8_t i=0; i < ARRAY_SIZE(_compass_instance); i++) {
|
2015-05-15 01:04:00 -03:00
|
|
|
if (_compass._hil.healthy[i]) {
|
2015-03-18 19:16:52 -03:00
|
|
|
uint8_t compass_instance = _compass_instance[i];
|
|
|
|
Vector3f field = _compass._hil.field[compass_instance];
|
|
|
|
rotate_field(field, compass_instance);
|
2017-09-05 22:58:35 -03:00
|
|
|
publish_raw_field(field, compass_instance);
|
2015-03-18 19:16:52 -03:00
|
|
|
correct_field(field, compass_instance);
|
2016-05-04 04:11:02 -03:00
|
|
|
uint32_t saved_last_update = _compass.last_update_usec(compass_instance);
|
2015-03-18 19:16:52 -03:00
|
|
|
publish_filtered_field(field, compass_instance);
|
2016-05-04 04:11:02 -03:00
|
|
|
set_last_update_usec(saved_last_update, compass_instance);
|
2015-05-15 01:04:00 -03:00
|
|
|
}
|
|
|
|
}
|
2012-08-26 00:42:00 -03:00
|
|
|
}
|