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/>.
|
|
|
|
*/
|
|
|
|
|
2012-03-09 22:51:28 -04:00
|
|
|
/*
|
2012-11-28 14:32:38 -04:00
|
|
|
* AP_OpticalFlow_ADNS3080.cpp - ADNS3080 OpticalFlow Library for
|
|
|
|
* Ardupilot Mega
|
2012-08-17 03:20:51 -03:00
|
|
|
* Code by Randy Mackay. DIYDrones.com
|
|
|
|
*
|
|
|
|
*/
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
#include <AP_HAL.h>
|
2012-03-09 22:51:28 -04:00
|
|
|
#include "AP_OpticalFlow_ADNS3080.h"
|
2012-10-04 10:46:09 -03:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2012-03-09 22:51:28 -04:00
|
|
|
|
|
|
|
#define AP_SPI_TIMEOUT 1000
|
|
|
|
|
|
|
|
union NumericIntType
|
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
int16_t intValue;
|
|
|
|
uint16_t uintValue;
|
|
|
|
uint8_t byteValue[2];
|
2012-03-09 22:51:28 -04:00
|
|
|
};
|
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
2012-11-28 14:32:38 -04:00
|
|
|
AP_OpticalFlow_ADNS3080::AP_OpticalFlow_ADNS3080(uint8_t reset_pin) :
|
|
|
|
_reset_pin(reset_pin)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-08-17 03:20:51 -03:00
|
|
|
num_pixels = ADNS3080_PIXELS_X;
|
|
|
|
field_of_view = AP_OPTICALFLOW_ADNS3080_08_FOV;
|
|
|
|
scaler = AP_OPTICALFLOW_ADNS3080_SCALER;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
// init - initialise sensor
|
2012-11-28 14:32:38 -04:00
|
|
|
// assumes SPI bus has been initialised but will attempt to initialise
|
|
|
|
// nonstandard SPI3 bus if required
|
2012-03-09 22:51:28 -04:00
|
|
|
bool
|
2012-11-28 14:32:38 -04:00
|
|
|
AP_OpticalFlow_ADNS3080::init()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
int8_t retry = 0;
|
|
|
|
bool retvalue = false;
|
|
|
|
|
|
|
|
// suspend timer while we set-up SPI communication
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->suspend_timer_procs();
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2013-05-28 10:56:11 -03:00
|
|
|
// reset the device if the reset pin has been defined
|
|
|
|
if(_reset_pin != 0) {
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.gpio->pinMode(_reset_pin, GPIO_OUTPUT);
|
2013-05-28 10:56:11 -03:00
|
|
|
}
|
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// reset the device
|
|
|
|
reset();
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2013-05-28 10:56:11 -03:00
|
|
|
// get pointer to the spi bus
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi = hal.spi->device(AP_HAL::SPIDevice_ADNS3080_SPI0);
|
|
|
|
if (_spi == NULL) {
|
|
|
|
retvalue = false; goto finish;
|
2012-08-17 03:20:51 -03:00
|
|
|
}
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2013-05-28 10:56:11 -03:00
|
|
|
// check 3 times for the sensor on standard SPI bus
|
2012-09-14 09:01:33 -03:00
|
|
|
while( retvalue == false && retry < 3 ) {
|
|
|
|
if( read_register(ADNS3080_PRODUCT_ID) == 0x17 ) {
|
|
|
|
retvalue = true;
|
2012-11-28 14:32:38 -04:00
|
|
|
goto finish;
|
2012-09-14 09:01:33 -03:00
|
|
|
}
|
2012-08-17 03:20:51 -03:00
|
|
|
retry++;
|
|
|
|
}
|
2011-09-17 00:38:18 -03:00
|
|
|
|
2013-05-28 10:56:11 -03:00
|
|
|
// if not found, get pointer to the SPI3 bus
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi = hal.spi->device(AP_HAL::SPIDevice_ADNS3080_SPI3);
|
|
|
|
if (_spi == NULL) {
|
|
|
|
retvalue = false; goto finish;
|
|
|
|
}
|
2013-05-28 10:56:11 -03:00
|
|
|
|
|
|
|
// check 3 times on SPI3
|
2012-11-28 14:32:38 -04:00
|
|
|
retry = 0;
|
|
|
|
while( retvalue == false && retry < 3 ) {
|
|
|
|
if( read_register(ADNS3080_PRODUCT_ID) == 0x17 ) {
|
|
|
|
retvalue = true;
|
2012-09-14 09:01:33 -03:00
|
|
|
}
|
2012-11-28 14:32:38 -04:00
|
|
|
retry++;
|
2012-09-14 09:01:33 -03:00
|
|
|
}
|
2013-05-28 10:56:11 -03:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// If we fail to find on SPI3, no connection available.
|
|
|
|
retvalue = false;
|
|
|
|
_spi = NULL;
|
2012-09-14 09:01:33 -03:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
finish:
|
|
|
|
// if device is working register the global static read function to
|
|
|
|
// be called at 1khz
|
2012-09-14 09:01:33 -03:00
|
|
|
if( retvalue ) {
|
2013-09-30 04:01:02 -03:00
|
|
|
hal.scheduler->register_timer_process(AP_HAL_MEMBERPROC(&AP_OpticalFlow_ADNS3080::read));
|
2012-09-14 09:01:33 -03:00
|
|
|
}
|
|
|
|
|
2013-01-03 17:32:56 -04:00
|
|
|
// resume timer
|
|
|
|
hal.scheduler->resume_timer_procs();
|
|
|
|
|
2012-09-14 09:01:33 -03:00
|
|
|
return retvalue;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read a register from the sensor
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t AP_OpticalFlow_ADNS3080::read_register(uint8_t address)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2013-07-11 02:14:37 -03:00
|
|
|
AP_HAL::Semaphore *spi_sem;
|
|
|
|
|
|
|
|
// check that we have an spi bus
|
|
|
|
if (_spi == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get spi bus semaphore
|
|
|
|
spi_sem = _spi->get_semaphore();
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2013-07-11 02:14:37 -03:00
|
|
|
// try to get control of the spi bus
|
|
|
|
if (spi_sem == NULL || !spi_sem->take_nonblocking()) {
|
2013-01-03 17:32:56 -04:00
|
|
|
return 0;
|
2012-10-04 10:46:09 -03:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi->cs_assert();
|
|
|
|
// send the device the register you want to read:
|
|
|
|
_spi->transfer(address);
|
|
|
|
hal.scheduler->delay_microseconds(50);
|
|
|
|
// send a value of 0 to read the first byte returned:
|
|
|
|
uint8_t result = _spi->transfer(0x00);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi->cs_release();
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2013-07-11 02:14:37 -03:00
|
|
|
// release the spi bus
|
|
|
|
spi_sem->give();
|
2012-10-04 10:46:09 -03:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
return result;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// write a value to one of the sensor's registers
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::write_register(uint8_t address, uint8_t value)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2013-07-11 02:14:37 -03:00
|
|
|
AP_HAL::Semaphore *spi_sem;
|
|
|
|
|
|
|
|
// check that we have an spi bus
|
|
|
|
if (_spi == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get spi bus semaphore
|
|
|
|
spi_sem = _spi->get_semaphore();
|
2012-11-28 14:32:38 -04:00
|
|
|
|
2013-07-11 02:14:37 -03:00
|
|
|
// try to get control of the spi bus
|
|
|
|
if (spi_sem == NULL || !spi_sem->take_nonblocking()) {
|
2013-01-03 17:32:56 -04:00
|
|
|
return;
|
2012-10-04 10:46:09 -03:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi->cs_assert();
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// send register address
|
|
|
|
_spi->transfer(address | 0x80 );
|
|
|
|
hal.scheduler->delay_microseconds(50);
|
|
|
|
// send data
|
|
|
|
_spi->transfer(value);
|
2012-10-04 10:46:09 -03:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
_spi->cs_release();
|
2013-01-03 17:32:56 -04:00
|
|
|
|
2013-07-11 02:14:37 -03:00
|
|
|
// release the spi bus
|
|
|
|
spi_sem->give();
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset sensor by holding a pin high (or is it low?) for 10us.
|
|
|
|
void
|
|
|
|
AP_OpticalFlow_ADNS3080::reset()
|
|
|
|
{
|
|
|
|
// return immediately if the reset pin is not defined
|
|
|
|
if( _reset_pin == 0)
|
2012-08-17 03:20:51 -03:00
|
|
|
return;
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// reset sensor
|
|
|
|
hal.gpio->write(_reset_pin, 1);
|
|
|
|
hal.scheduler->delay_microseconds(10);
|
|
|
|
// return sensor to normal
|
|
|
|
hal.gpio->write(_reset_pin, 0);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read latest values from sensor and fill in x,y and totals
|
2012-09-14 09:01:33 -03:00
|
|
|
void
|
2013-09-28 03:29:43 -03:00
|
|
|
AP_OpticalFlow_ADNS3080::update(void)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t motion_reg;
|
2013-01-13 02:53:54 -04:00
|
|
|
surface_quality = read_register(ADNS3080_SQUAL);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
|
|
|
// check for movement, update x,y values
|
2012-08-17 03:20:51 -03:00
|
|
|
motion_reg = read_register(ADNS3080_MOTION);
|
2012-11-28 14:32:38 -04:00
|
|
|
// check if we've had an overflow
|
|
|
|
_overflow = ((motion_reg & 0x10) != 0);
|
2012-08-17 03:20:51 -03:00
|
|
|
if( (motion_reg & 0x80) != 0 ) {
|
2012-09-14 09:01:33 -03:00
|
|
|
raw_dx = ((int8_t)read_register(ADNS3080_DELTA_X));
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-09-14 09:01:33 -03:00
|
|
|
raw_dy = ((int8_t)read_register(ADNS3080_DELTA_Y));
|
2012-08-17 03:20:51 -03:00
|
|
|
_motion = true;
|
|
|
|
}else{
|
|
|
|
raw_dx = 0;
|
|
|
|
raw_dy = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
last_update = hal.scheduler->millis();
|
2012-08-17 03:20:51 -03:00
|
|
|
|
|
|
|
apply_orientation_matrix();
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AP_OpticalFlow_ADNS3080::disable_serial_pullup()
|
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t regVal = read_register(ADNS3080_EXTENDED_CONFIG);
|
2012-08-17 03:20:51 -03:00
|
|
|
regVal = (regVal | ADNS3080_SERIALNPU_OFF);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_EXTENDED_CONFIG, regVal);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// get_led_always_on - returns true if LED is always on, false if only on
|
|
|
|
// when required
|
|
|
|
bool AP_OpticalFlow_ADNS3080::get_led_always_on()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-08-17 03:20:51 -03:00
|
|
|
return ( (read_register(ADNS3080_CONFIGURATION_BITS) & 0x40) > 0 );
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// set_led_always_on - set parameter to true if you want LED always on,
|
|
|
|
// otherwise false for only when required
|
|
|
|
void AP_OpticalFlow_ADNS3080::set_led_always_on( bool alwaysOn )
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t regVal = read_register(ADNS3080_CONFIGURATION_BITS);
|
2011-09-17 00:38:18 -03:00
|
|
|
regVal = (regVal & 0xbf) | (alwaysOn << 6);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_CONFIGURATION_BITS, regVal);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns resolution (either 400 or 1600 counts per inch)
|
2012-11-28 14:32:38 -04:00
|
|
|
int16_t AP_OpticalFlow_ADNS3080::get_resolution()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
|
|
|
if( (read_register(ADNS3080_CONFIGURATION_BITS) & 0x10) == 0 )
|
2012-08-17 03:20:51 -03:00
|
|
|
return 400;
|
|
|
|
else
|
|
|
|
return 1600;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set parameter to 400 or 1600 counts per inch
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::set_resolution(uint16_t resolution)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t regVal = read_register(ADNS3080_CONFIGURATION_BITS);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
if( resolution == ADNS3080_RESOLUTION_400 ) {
|
|
|
|
regVal &= ~0x10;
|
|
|
|
scaler = AP_OPTICALFLOW_ADNS3080_SCALER;
|
|
|
|
}else if( resolution == ADNS3080_RESOLUTION_1600) {
|
|
|
|
regVal |= 0x10;
|
|
|
|
scaler = AP_OPTICALFLOW_ADNS3080_SCALER * 4;
|
|
|
|
}
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_CONFIGURATION_BITS, regVal);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// this will affect conversion factors so update them
|
|
|
|
update_conversion_factors();
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get_frame_rate_auto - return whether frame rate is set to "auto" or manual
|
2012-11-28 14:32:38 -04:00
|
|
|
bool AP_OpticalFlow_ADNS3080::get_frame_rate_auto()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t regVal = read_register(ADNS3080_EXTENDED_CONFIG);
|
2011-09-17 00:38:18 -03:00
|
|
|
if( (regVal & 0x01) != 0 ) {
|
2012-08-17 03:20:51 -03:00
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_frame_rate_auto - set frame rate to auto (true) or manual (false)
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::set_frame_rate_auto(bool auto_frame_rate)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
uint8_t regVal = read_register(ADNS3080_EXTENDED_CONFIG);
|
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
if( auto_frame_rate == true ) {
|
|
|
|
// set specific frame period
|
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_LOWER,0xE0);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_UPPER,0x1A);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
|
|
|
|
// decide what value to update in extended config
|
2012-03-09 22:51:28 -04:00
|
|
|
regVal = (regVal & ~0x01);
|
2012-08-17 03:20:51 -03:00
|
|
|
}else{
|
|
|
|
// decide what value to update in extended config
|
|
|
|
regVal = (regVal & ~0x01) | 0x01;
|
|
|
|
}
|
|
|
|
write_register(ADNS3080_EXTENDED_CONFIG, regVal);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get frame period
|
2012-11-28 14:32:38 -04:00
|
|
|
uint16_t AP_OpticalFlow_ADNS3080::get_frame_period()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
|
|
|
NumericIntType aNum;
|
2012-08-17 03:20:51 -03:00
|
|
|
aNum.byteValue[1] = read_register(ADNS3080_FRAME_PERIOD_UPPER);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
aNum.byteValue[0] = read_register(ADNS3080_FRAME_PERIOD_LOWER);
|
2012-08-17 03:20:51 -03:00
|
|
|
return aNum.uintValue;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set frame period
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::set_frame_period(uint16_t period)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
|
|
|
NumericIntType aNum;
|
2012-08-17 03:20:51 -03:00
|
|
|
aNum.uintValue = period;
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// set frame rate to manual
|
|
|
|
set_frame_rate_auto(false);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// set specific frame period
|
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_LOWER,aNum.byteValue[0]);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_UPPER,aNum.byteValue[1]);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
uint16_t AP_OpticalFlow_ADNS3080::get_frame_rate()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
uint32_t clockSpeed = ADNS3080_CLOCK_SPEED;
|
|
|
|
uint16_t rate = clockSpeed / get_frame_period();
|
2012-08-17 03:20:51 -03:00
|
|
|
return rate;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::set_frame_rate(uint16_t rate)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
uint32_t clockSpeed = ADNS3080_CLOCK_SPEED;
|
|
|
|
uint16_t period = (uint16_t)(clockSpeed / (uint32_t)rate);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
set_frame_period(period);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// get_shutter_speed_auto - returns true if shutter speed is adjusted
|
|
|
|
// automatically, false if manual
|
|
|
|
bool AP_OpticalFlow_ADNS3080::get_shutter_speed_auto()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
uint8_t regVal = read_register(ADNS3080_EXTENDED_CONFIG);
|
2012-08-17 03:20:51 -03:00
|
|
|
if( (regVal & 0x02) > 0 ) {
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_shutter_speed_auto - set shutter speed to auto (true), or manual (false)
|
2012-11-28 14:32:38 -04:00
|
|
|
void AP_OpticalFlow_ADNS3080::set_shutter_speed_auto(bool auto_shutter_speed)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
uint8_t regVal = read_register(ADNS3080_EXTENDED_CONFIG);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
if( auto_shutter_speed ) {
|
|
|
|
// return shutter speed max to default
|
|
|
|
write_register(ADNS3080_SHUTTER_MAX_BOUND_LOWER,0x8c);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_SHUTTER_MAX_BOUND_UPPER,0x20);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
|
|
|
|
// determine value to put into extended config
|
2011-09-17 00:38:18 -03:00
|
|
|
regVal &= ~0x02;
|
2012-08-17 03:20:51 -03:00
|
|
|
}else{
|
|
|
|
// determine value to put into extended config
|
|
|
|
regVal |= 0x02;
|
|
|
|
}
|
|
|
|
write_register(ADNS3080_EXTENDED_CONFIG, regVal);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// get_shutter_speed_auto - returns true if shutter speed is adjusted
|
|
|
|
// automatically, false if manual
|
|
|
|
uint16_t AP_OpticalFlow_ADNS3080::get_shutter_speed()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
|
|
|
NumericIntType aNum;
|
2012-08-17 03:20:51 -03:00
|
|
|
aNum.byteValue[1] = read_register(ADNS3080_SHUTTER_UPPER);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
aNum.byteValue[0] = read_register(ADNS3080_SHUTTER_LOWER);
|
2012-08-17 03:20:51 -03:00
|
|
|
return aNum.uintValue;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// set_shutter_speed_auto - set shutter speed to auto (true), or manual (false)
|
2011-09-17 00:38:18 -03:00
|
|
|
void
|
2012-09-24 18:10:22 -03:00
|
|
|
AP_OpticalFlow_ADNS3080::set_shutter_speed(uint16_t shutter_speed)
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
|
|
|
NumericIntType aNum;
|
2012-08-17 03:20:51 -03:00
|
|
|
aNum.uintValue = shutter_speed;
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// set shutter speed to manual
|
2012-03-09 22:51:28 -04:00
|
|
|
set_shutter_speed_auto(false);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// set specific shutter speed
|
|
|
|
write_register(ADNS3080_SHUTTER_MAX_BOUND_LOWER,aNum.byteValue[0]);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_SHUTTER_MAX_BOUND_UPPER,aNum.byteValue[1]);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// larger delay
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// need to update frame period to cause shutter value to take effect
|
|
|
|
aNum.byteValue[1] = read_register(ADNS3080_FRAME_PERIOD_UPPER);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
aNum.byteValue[0] = read_register(ADNS3080_FRAME_PERIOD_LOWER);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_LOWER,aNum.byteValue[0]);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_FRAME_PERIOD_MAX_BOUND_UPPER,aNum.byteValue[1]);
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// clear_motion - will cause the Delta_X, Delta_Y, and internal motion
|
|
|
|
// registers to be cleared
|
|
|
|
void AP_OpticalFlow_ADNS3080::clear_motion()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-11-28 14:32:38 -04:00
|
|
|
// writing anything to this register will clear the sensor's motion
|
|
|
|
// registers
|
|
|
|
write_register(ADNS3080_MOTION_CLEAR,0xFF);
|
2012-08-17 03:20:51 -03:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
dx = 0;
|
|
|
|
dy = 0;
|
|
|
|
_motion = false;
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-11-28 14:32:38 -04:00
|
|
|
// get_pixel_data - captures an image from the sensor and stores it to the
|
|
|
|
// pixe_data array
|
|
|
|
void AP_OpticalFlow_ADNS3080::print_pixel_data()
|
2012-03-09 22:51:28 -04:00
|
|
|
{
|
2012-09-14 09:01:33 -03:00
|
|
|
int16_t i,j;
|
2012-08-17 03:20:51 -03:00
|
|
|
bool isFirstPixel = true;
|
2012-09-14 09:01:33 -03:00
|
|
|
uint8_t regValue;
|
|
|
|
uint8_t pixelValue;
|
2012-03-09 22:51:28 -04:00
|
|
|
|
|
|
|
// write to frame capture register to force capture of frame
|
2012-08-17 03:20:51 -03:00
|
|
|
write_register(ADNS3080_FRAME_CAPTURE,0x83);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
2012-08-17 03:20:51 -03:00
|
|
|
// wait 3 frame periods + 10 nanoseconds for frame to be captured
|
2012-11-28 14:32:38 -04:00
|
|
|
// min frame speed is 2000 frames/second so 1 frame = 500 nano seconds.
|
|
|
|
// so 500 x 3 + 10 = 1510
|
|
|
|
hal.scheduler->delay_microseconds(1510);
|
2012-03-09 22:51:28 -04:00
|
|
|
|
|
|
|
// display the pixel data
|
2012-08-17 03:20:51 -03:00
|
|
|
for( i=0; i<ADNS3080_PIXELS_Y; i++ ) {
|
|
|
|
for( j=0; j<ADNS3080_PIXELS_X; j++ ) {
|
|
|
|
regValue = read_register(ADNS3080_FRAME_CAPTURE);
|
|
|
|
if( isFirstPixel && (regValue & 0x40) == 0 ) {
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.console->println_P(
|
|
|
|
PSTR("Optflow: failed to find first pixel"));
|
2012-08-17 03:20:51 -03:00
|
|
|
}
|
|
|
|
isFirstPixel = false;
|
2012-11-28 14:32:38 -04:00
|
|
|
pixelValue = ( regValue << 2 );
|
2013-09-23 04:08:27 -03:00
|
|
|
hal.console->print(pixelValue,BASE_DEC);
|
2012-08-17 03:20:51 -03:00
|
|
|
if( j!= ADNS3080_PIXELS_X-1 )
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.console->print_P(PSTR(","));
|
|
|
|
hal.scheduler->delay_microseconds(50);
|
2012-08-17 03:20:51 -03:00
|
|
|
}
|
2012-11-28 14:32:38 -04:00
|
|
|
hal.console->println();
|
2012-08-17 03:20:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// hardware reset to restore sensor to normal operation
|
|
|
|
reset();
|
2012-03-09 22:51:28 -04:00
|
|
|
}
|