2015-11-24 05:34:08 -04: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/>.
|
|
|
|
*/
|
2015-12-14 11:20:28 -04:00
|
|
|
#include "AP_OpticalFlow_Onboard.h"
|
|
|
|
|
2015-11-24 05:34:08 -04:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-12-14 11:20:28 -04:00
|
|
|
|
2015-11-24 05:34:08 -04:00
|
|
|
#include "OpticalFlow.h"
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX &&\
|
2015-05-10 21:16:01 -03:00
|
|
|
(CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP ||\
|
2017-03-07 17:00:59 -04:00
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_MINLURE)
|
2016-03-03 17:35:38 -04:00
|
|
|
|
|
|
|
#ifndef OPTICALFLOW_ONBOARD_DEBUG
|
|
|
|
#define OPTICALFLOW_ONBOARD_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
2015-11-24 05:34:08 -04:00
|
|
|
#define OPTICALFLOW_ONBOARD_ID 1
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-11-19 03:18:50 -04:00
|
|
|
AP_OpticalFlow_Onboard::AP_OpticalFlow_Onboard(OpticalFlow &_frontend) :
|
|
|
|
OpticalFlow_backend(_frontend)
|
2015-11-24 05:34:08 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
void AP_OpticalFlow_Onboard::init(void)
|
|
|
|
{
|
|
|
|
/* register callback to get gyro data */
|
2017-01-17 13:18:49 -04:00
|
|
|
hal.opticalflow->init();
|
2015-11-24 05:34:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_OpticalFlow_Onboard::update()
|
|
|
|
{
|
|
|
|
AP_HAL::OpticalFlow::Data_Frame data_frame;
|
|
|
|
// read at maximum 10Hz
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - _last_read_ms < 100) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_last_read_ms = now;
|
|
|
|
|
|
|
|
if (!hal.opticalflow->read(data_frame)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OpticalFlow::OpticalFlow_state state;
|
|
|
|
state.device_id = OPTICALFLOW_ONBOARD_ID;
|
|
|
|
state.surface_quality = data_frame.quality;
|
|
|
|
if (data_frame.delta_time > 0) {
|
|
|
|
const Vector2f flowScaler = _flowScaler();
|
|
|
|
float flowScaleFactorX = 1.0f + 0.001f * flowScaler.x;
|
|
|
|
float flowScaleFactorY = 1.0f + 0.001f * flowScaler.y;
|
|
|
|
|
2017-01-17 12:54:12 -04:00
|
|
|
// delta_time is in microseconds and flow is in milliradians
|
|
|
|
// per second, so multiply by 1000
|
2015-11-24 05:34:08 -04:00
|
|
|
state.flowRate.x = flowScaleFactorX * 1000.0f /
|
|
|
|
float(data_frame.delta_time) *
|
|
|
|
data_frame.pixel_flow_x_integral;
|
|
|
|
|
|
|
|
state.flowRate.y = flowScaleFactorY * 1000.0f /
|
|
|
|
float(data_frame.delta_time) *
|
|
|
|
data_frame.pixel_flow_y_integral;
|
|
|
|
|
2017-01-17 12:54:12 -04:00
|
|
|
// delta_time is in microseconds so multiply to get back to seconds
|
|
|
|
state.bodyRate.x = 1000000.0f / float(data_frame.delta_time) *
|
2015-11-24 05:34:08 -04:00
|
|
|
data_frame.gyro_x_integral;
|
|
|
|
|
2017-01-17 12:54:12 -04:00
|
|
|
state.bodyRate.y = 1000000.0f / float(data_frame.delta_time) *
|
2015-11-24 05:34:08 -04:00
|
|
|
data_frame.gyro_y_integral;
|
2016-11-25 23:23:50 -04:00
|
|
|
|
|
|
|
_applyYaw(state.flowRate);
|
2015-11-24 05:34:08 -04:00
|
|
|
} else {
|
|
|
|
state.flowRate.zero();
|
|
|
|
state.bodyRate.zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy results to front end
|
|
|
|
_update_frontend(state);
|
|
|
|
|
2016-03-03 17:35:38 -04:00
|
|
|
#if OPTICALFLOW_ONBOARD_DEBUG
|
2015-11-24 05:34:08 -04:00
|
|
|
hal.console->printf("FLOW_ONBOARD qual:%u FlowRateX:%4.2f Y:%4.2f"
|
|
|
|
"BodyRateX:%4.2f Y:%4.2f, delta_time = %u\n",
|
|
|
|
(unsigned)state.surface_quality,
|
|
|
|
(double)state.flowRate.x,
|
|
|
|
(double)state.flowRate.y,
|
|
|
|
(double)state.bodyRate.x,
|
|
|
|
(double)state.bodyRate.y,
|
|
|
|
data_frame.delta_time);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|