2011-04-30 05:29:28 -03:00
/*
ADC . cpp - Analog Digital Converter Base Class for Ardupilot Mega
Code by James Goppert . DIYDrones . com
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 2.1 of the License , or ( at your option ) any later version .
*/
# include "AP_OpticalFlow.h"
2011-07-18 18:51:27 -03:00
# define FORTYFIVE_DEGREES 0.78539816
2011-04-30 05:29:28 -03:00
// init - initCommAPI parameter controls whether I2C/SPI interface is initialised (set to false if other devices are on the I2C/SPI bus and have already initialised the interface)
2011-09-16 23:24:57 -03:00
bool
2011-05-11 09:49:19 -03:00
AP_OpticalFlow : : init ( bool initCommAPI )
2011-04-30 05:29:28 -03:00
{
2011-09-16 23:24:57 -03:00
_orientation_matrix = Matrix3f ( 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ) ;
2011-05-11 09:49:19 -03:00
update_conversion_factors ( ) ;
return true ; // just return true by default
}
// set_orientation - Rotation vector to transform sensor readings to the body frame.
void
AP_OpticalFlow : : set_orientation ( const Matrix3f & rotation_matrix )
{
_orientation_matrix = rotation_matrix ;
2011-04-30 05:29:28 -03:00
}
// read latest values from sensor and fill in x,y and totals
int AP_OpticalFlow : : read ( )
{
2011-09-17 00:38:18 -03:00
return 0 ;
2011-04-30 05:29:28 -03:00
}
// reads a value from the sensor (will be sensor specific)
2011-09-16 23:24:57 -03:00
byte
2011-05-11 09:49:19 -03:00
AP_OpticalFlow : : read_register ( byte address )
2011-04-30 05:29:28 -03:00
{
2011-09-17 00:38:18 -03:00
return 0 ;
2011-04-30 05:29:28 -03:00
}
// writes a value to one of the sensor's register (will be sensor specific)
2011-09-16 23:24:57 -03:00
void
2011-05-11 09:49:19 -03:00
AP_OpticalFlow : : write_register ( byte address , byte value )
2011-04-30 05:29:28 -03:00
{
2011-05-11 09:49:19 -03:00
}
// rotate raw values to arrive at final x,y,dx and dy values
2011-09-16 23:24:57 -03:00
void
2011-05-11 09:49:19 -03:00
AP_OpticalFlow : : apply_orientation_matrix ( )
{
Vector3f rot_vector ;
2011-09-16 23:24:57 -03:00
2011-05-11 09:49:19 -03:00
// next rotate dx and dy
2011-09-16 23:24:57 -03:00
rot_vector = _orientation_matrix * Vector3f ( raw_dx , raw_dy , 0 ) ;
2011-05-11 09:49:19 -03:00
dx = rot_vector . x ;
dy = rot_vector . y ;
2011-09-16 23:24:57 -03:00
2011-05-11 09:49:19 -03:00
// add rotated values to totals (perhaps this is pointless as we need to take into account yaw, roll, pitch)
x + = dx ;
y + = dy ;
}
// updatse conversion factors that are dependent upon field_of_view
void
AP_OpticalFlow : : update_conversion_factors ( )
{
2011-09-16 23:24:57 -03:00
conv_factor = ( 1.0 / ( float ) ( num_pixels * scaler ) ) * 2.0 * tan ( field_of_view / 2.0 ) ; // multiply this number by altitude and pixel change to get horizontal move (in same units as altitude)
// 0.00615
radians_to_pixels = ( num_pixels * scaler ) / field_of_view ;
// 162.99
2011-05-11 09:49:19 -03:00
}
// updates internal lon and lat with estimation based on optical flow
void
2011-09-16 23:24:57 -03:00
AP_OpticalFlow : : update_position ( float roll , float pitch , float cos_yaw_x , float sin_yaw_y , float altitude )
2011-05-11 09:49:19 -03:00
{
2011-09-16 23:24:57 -03:00
float diff_roll = roll - _last_roll ;
float diff_pitch = pitch - _last_pitch ;
2011-07-18 18:51:27 -03:00
// only update position if surface quality is good and angle is not over 45 degrees
2011-08-07 10:19:18 -03:00
if ( surface_quality > = 10 & & fabs ( roll ) < = FORTYFIVE_DEGREES & & fabs ( pitch ) < = FORTYFIVE_DEGREES ) {
2011-09-16 23:24:57 -03:00
altitude = max ( altitude , 0 ) ;
2011-07-18 18:51:27 -03:00
// calculate expected x,y diff due to roll and pitch change
exp_change_x = diff_roll * radians_to_pixels ;
2011-08-07 10:19:18 -03:00
exp_change_y = - diff_pitch * radians_to_pixels ;
2011-09-16 23:24:57 -03:00
2011-07-18 18:51:27 -03:00
// real estimated raw change from mouse
change_x = dx - exp_change_x ;
change_y = dy - exp_change_y ;
2011-05-11 09:49:19 -03:00
2011-09-17 00:38:18 -03:00
float avg_altitude = ( altitude + _last_altitude ) * 0.5 ;
2011-07-18 18:51:27 -03:00
// convert raw change to horizontal movement in cm
x_cm = - change_x * avg_altitude * conv_factor ; // perhaps this altitude should actually be the distance to the ground? i.e. if we are very rolled over it should be longer?
y_cm = - change_y * avg_altitude * conv_factor ; // for example if you are leaned over at 45 deg the ground will appear farther away and motion from opt flow sensor will be less
2011-09-16 23:24:57 -03:00
vlon = x_cm * sin_yaw_y - y_cm * cos_yaw_x ;
vlat = y_cm * sin_yaw_y - x_cm * cos_yaw_x ;
2011-07-18 18:51:27 -03:00
}
2011-09-17 00:38:18 -03:00
_last_altitude = altitude ;
_last_roll = roll ;
_last_pitch = pitch ;
2011-09-16 23:24:57 -03:00
}
/*
{
// only update position if surface quality is good and angle is not over 45 degrees
if ( surface_quality > = 10 & & fabs ( _dcm - > roll ) < = FORTYFIVE_DEGREES & & fabs ( _dcm - > pitch ) < = FORTYFIVE_DEGREES ) {
altitude = max ( altitude , 0 ) ;
Vector3f omega = _dcm - > get_gyro ( ) ;
// calculate expected x,y diff due to roll and pitch change
float exp_change_x = omega . x * radians_to_pixels ;
float exp_change_y = - omega . y * radians_to_pixels ;
// real estimated raw change from mouse
float change_x = dx - exp_change_x ;
float change_y = dy - exp_change_y ;
// convert raw change to horizontal movement in cm
float x_cm = - change_x * altitude * conv_factor ; // perhaps this altitude should actually be the distance to the ground? i.e. if we are very rolled over it should be longer?
float y_cm = - change_y * altitude * conv_factor ; // for example if you are leaned over at 45 deg the ground will appear farther away and motion from opt flow sensor will be less
vlon = ( float ) x_cm * sin_yaw_y - ( float ) y_cm * cos_yaw_x ;
vlat = ( float ) y_cm * sin_yaw_y - ( float ) x_cm * cos_yaw_x ;
}
}
2011-09-17 00:38:18 -03:00
*/