123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include <AP_HAL/AP_HAL.h>
- #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
- #include "AP_OpticalFlow_SITL.h"
- extern const AP_HAL::HAL& hal;
- AP_OpticalFlow_SITL::AP_OpticalFlow_SITL(OpticalFlow &_frontend) :
- OpticalFlow_backend(_frontend),
- _sitl(AP::sitl())
- {
- }
- void AP_OpticalFlow_SITL::init(void)
- {
- }
- void AP_OpticalFlow_SITL::update(void)
- {
- if (!_sitl->flow_enable) {
- return;
- }
-
- uint32_t now = AP_HAL::millis();
- if (now - last_flow_ms < 1000*(1.0f/_sitl->flow_rate)) {
- return;
- }
- last_flow_ms = now;
- Vector3f gyro(radians(_sitl->state.rollRate),
- radians(_sitl->state.pitchRate),
- radians(_sitl->state.yawRate));
- OpticalFlow::OpticalFlow_state state;
-
- Vector3f velocity(_sitl->state.speedN,
- _sitl->state.speedE,
- _sitl->state.speedD);
-
- Matrix3f rotmat;
- rotmat.from_euler(radians(_sitl->state.rollDeg),
- radians(_sitl->state.pitchDeg),
- radians(_sitl->state.yawDeg));
- state.surface_quality = 51;
-
- Vector3f posRelSensorBF = _sitl->optflow_pos_offset;
-
- float range;
- if (rotmat.c.z > 0.05f && _sitl->height_agl > 0) {
- Vector3f relPosSensorEF = rotmat * posRelSensorBF;
- range = (_sitl->height_agl - relPosSensorEF.z) / rotmat.c.z;
- } else {
- range = 1e38f;
- }
-
- Vector3f relVelSensor = rotmat.mul_transpose(velocity);
-
- relVelSensor += gyro % posRelSensorBF;
-
-
-
-
- state.flowRate.x = -relVelSensor.y/range + gyro.x + _sitl->flow_noise * rand_float();
- state.flowRate.y = relVelSensor.x/range + gyro.y + _sitl->flow_noise * rand_float();
-
-
-
- state.bodyRate = Vector2f(gyro.x, gyro.y);
- 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) {
-
- if (_sitl->flow_delay > 0 &&
- (uint8_t)(_sitl->flow_delay) > ARRAY_SIZE(optflow_data)) {
- _sitl->flow_delay = ARRAY_SIZE(optflow_data);
- }
- optflow_delay = _sitl->flow_delay;
- for (uint8_t i=0; i<optflow_delay; i++) {
- optflow_data[i] = state;
- }
- }
- _applyYaw(state.flowRate);
-
-
- _update_frontend(state);
- }
- #endif
|