2015-01-02 06:50:16 -04:00
|
|
|
/*
|
|
|
|
SITL handling
|
|
|
|
|
|
|
|
This simulates a optical flow sensor
|
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include <AP_Math.h>
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2015-01-02 06:50:16 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2015-01-02 06:50:16 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2015-01-02 06:50:16 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2015-01-05 23:18:54 -04:00
|
|
|
#define MAX_OPTFLOW_DELAY 20
|
|
|
|
static uint8_t next_optflow_index;
|
|
|
|
static uint8_t optflow_delay;
|
|
|
|
static OpticalFlow::OpticalFlow_state optflow_data[MAX_OPTFLOW_DELAY];
|
|
|
|
|
2015-01-02 06:50:16 -04:00
|
|
|
/*
|
|
|
|
update the optical flow with new data
|
|
|
|
*/
|
|
|
|
void SITL_State::_update_flow(void)
|
|
|
|
{
|
|
|
|
Vector3f gyro;
|
2015-01-05 23:02:57 -04:00
|
|
|
static uint32_t last_flow_ms;
|
2015-01-02 06:50:16 -04:00
|
|
|
|
|
|
|
if (!_optical_flow ||
|
2015-05-04 21:59:07 -03:00
|
|
|
!_sitl->flow_enable) {
|
2015-01-02 06:50:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-05 23:02:57 -04:00
|
|
|
// update at the requested rate
|
|
|
|
uint32_t now = hal.scheduler->millis();
|
|
|
|
if (now - last_flow_ms < 1000*(1.0f/_sitl->flow_rate)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_flow_ms = now;
|
|
|
|
|
2015-05-24 19:42:37 -03:00
|
|
|
gyro(radians(_sitl->state.rollRate),
|
|
|
|
radians(_sitl->state.pitchRate),
|
|
|
|
radians(_sitl->state.yawRate));
|
2015-01-02 06:50:16 -04:00
|
|
|
|
|
|
|
OpticalFlow::OpticalFlow_state state;
|
|
|
|
|
2015-01-02 19:57:53 -04:00
|
|
|
// NED velocity vector in m/s
|
2015-05-04 21:59:07 -03:00
|
|
|
Vector3f velocity(_sitl->state.speedN,
|
|
|
|
_sitl->state.speedE,
|
2015-01-02 19:57:53 -04:00
|
|
|
_sitl->state.speedD);
|
|
|
|
|
|
|
|
// a rotation matrix following DCM conventions
|
|
|
|
Matrix3f rotmat;
|
|
|
|
rotmat.from_euler(radians(_sitl->state.rollDeg),
|
|
|
|
radians(_sitl->state.pitchDeg),
|
|
|
|
radians(_sitl->state.yawDeg));
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2015-01-02 19:57:53 -04:00
|
|
|
|
2015-01-02 06:50:16 -04:00
|
|
|
state.device_id = 1;
|
2015-01-03 00:43:30 -04:00
|
|
|
state.surface_quality = 51;
|
2015-01-02 06:50:16 -04:00
|
|
|
|
2015-01-02 23:34:54 -04:00
|
|
|
// estimate range to centre of image
|
|
|
|
float range;
|
2015-06-24 21:08:41 -03:00
|
|
|
if (rotmat.c.z > 0.05f && height_agl() > 0) {
|
2015-01-03 06:47:54 -04:00
|
|
|
range = height_agl() / rotmat.c.z;
|
2015-01-02 23:34:54 -04:00
|
|
|
} else {
|
|
|
|
range = 1e38f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate relative velocity in sensor frame assuming no misalignment between sensor and vehicle body axes
|
2015-01-03 00:43:30 -04:00
|
|
|
Vector3f relVelSensor = rotmat.mul_transpose(velocity);
|
2015-01-02 23:34:54 -04:00
|
|
|
|
|
|
|
// Divide velocity by range and add body rates to get predicted sensed angular
|
|
|
|
// optical rates relative to X and Y sensor axes assuming no misalignment or scale
|
|
|
|
// factor error. Note - these are instantaneous values. The sensor sums these values across the interval from the last
|
|
|
|
// poll to provide a delta angle across the interface
|
|
|
|
state.flowRate.x = -relVelSensor.y/range + gyro.x;
|
|
|
|
state.flowRate.y = relVelSensor.x/range + gyro.y;
|
|
|
|
|
|
|
|
// The flow sensors body rates are assumed to be the same as the vehicle body rates (ie no misalignment)
|
|
|
|
// Note - these are instantaneous values. The sensor sums these values across the interval from the last
|
|
|
|
// poll to provide a delta angle across the interface.
|
|
|
|
state.bodyRate = Vector2f(gyro.x, gyro.y);
|
2015-01-02 06:50:16 -04:00
|
|
|
|
2015-01-05 23:18:54 -04:00
|
|
|
optflow_data[next_optflow_index++] = state;
|
|
|
|
if (next_optflow_index >= optflow_delay+1) {
|
|
|
|
next_optflow_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = optflow_data[next_optflow_index];
|
|
|
|
|
|
|
|
if (_sitl->flow_delay != optflow_delay) {
|
|
|
|
// cope with updates to the delay control
|
|
|
|
if (_sitl->flow_delay > MAX_OPTFLOW_DELAY) {
|
|
|
|
_sitl->flow_delay = MAX_OPTFLOW_DELAY;
|
|
|
|
}
|
|
|
|
optflow_delay = _sitl->flow_delay;
|
|
|
|
for (uint8_t i=0; i<optflow_delay; i++) {
|
|
|
|
optflow_data[i] = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 06:50:16 -04:00
|
|
|
_optical_flow->setHIL(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|