2016-11-26 01:22:18 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AP_IRLock_I2C.cpp
|
|
|
|
*
|
|
|
|
* Based on AP_IRLock_PX4 by MLandes
|
2016-11-26 01:22:18 -04:00
|
|
|
*
|
|
|
|
* See: http://irlock.com/pages/serial-communication-protocol
|
2016-11-26 01:22:18 -04:00
|
|
|
*/
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AP_IRLock_I2C.h"
|
|
|
|
#include <stdio.h>
|
2016-12-01 02:42:42 -04:00
|
|
|
#include <utility>
|
|
|
|
#include <AP_HAL/I2CDevice.h>
|
2016-11-26 01:22:18 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#define IRLOCK_I2C_ADDRESS 0x54
|
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
#define IRLOCK_SYNC 0xAA55AA55
|
2016-11-26 01:22:18 -04:00
|
|
|
|
|
|
|
void AP_IRLock_I2C::init()
|
|
|
|
{
|
2016-11-26 01:22:18 -04:00
|
|
|
dev = std::move(hal.i2c_mgr->get_device(1, IRLOCK_I2C_ADDRESS));
|
|
|
|
if (!dev) {
|
2016-11-26 01:22:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
sem = hal.util->new_semaphore();
|
2016-11-26 01:22:18 -04:00
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
// read at 50Hz
|
|
|
|
printf("Starting IRLock on I2C\n");
|
2016-11-26 01:22:18 -04:00
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&AP_IRLock_I2C::read_frames, void));
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
/*
|
|
|
|
synchronise with frame start. We expect 0xAA55AA55 at the start of
|
|
|
|
a frame
|
2016-11-26 01:22:18 -04:00
|
|
|
*/
|
|
|
|
bool AP_IRLock_I2C::sync_frame_start(void)
|
|
|
|
{
|
2016-11-26 01:22:18 -04:00
|
|
|
uint32_t sync_word;
|
|
|
|
if (!dev->transfer(nullptr, 0, (uint8_t *)&sync_word, 4)) {
|
2016-11-26 01:22:18 -04:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-27 23:08:17 -04:00
|
|
|
|
|
|
|
// record sensor successfully responded to I2C request
|
|
|
|
_last_read_ms = AP_HAL::millis();
|
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
uint8_t count=40;
|
|
|
|
while (count-- && sync_word != IRLOCK_SYNC && sync_word != 0) {
|
|
|
|
uint8_t sync_byte;
|
|
|
|
if (!dev->transfer(nullptr, 0, &sync_byte, 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (sync_byte == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sync_word = (sync_word>>8) | (uint32_t(sync_byte)<<24);
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
2016-11-26 01:22:18 -04:00
|
|
|
return sync_word == IRLOCK_SYNC;
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
converts IRLOCK pixels to a position on a normal plane 1m in front of the lens
|
|
|
|
based on a characterization of IR-LOCK with the standard lens, focused such that 2.38mm of threads are exposed
|
|
|
|
*/
|
|
|
|
void AP_IRLock_I2C::pixel_to_1M_plane(float pix_x, float pix_y, float &ret_x, float &ret_y)
|
|
|
|
{
|
|
|
|
ret_x = (-0.00293875727162397f*pix_x + 0.470201163459835f)/(4.43013552642296e-6f*((pix_x - 160.0f)*(pix_x - 160.0f)) +
|
|
|
|
4.79331390531725e-6f*((pix_y - 100.0f)*(pix_y - 100.0f)) - 1.0f);
|
|
|
|
ret_y = (-0.003056843086277f*pix_y + 0.3056843086277f)/(4.43013552642296e-6f*((pix_x - 160.0f)*(pix_x - 160.0f)) +
|
|
|
|
4.79331390531725e-6f*((pix_y - 100.0f)*(pix_y - 100.0f)) - 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-11-26 01:22:18 -04:00
|
|
|
read a frame from sensor
|
2016-11-26 01:22:18 -04:00
|
|
|
*/
|
2016-11-26 01:22:18 -04:00
|
|
|
bool AP_IRLock_I2C::read_block(struct frame &irframe)
|
2016-11-26 01:22:18 -04:00
|
|
|
{
|
|
|
|
if (!dev->transfer(nullptr, 0, (uint8_t*)&irframe, sizeof(irframe))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-27 23:08:17 -04:00
|
|
|
// record sensor successfully responded to I2C request
|
|
|
|
_last_read_ms = AP_HAL::millis();
|
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
/* check crc */
|
|
|
|
uint32_t crc = irframe.signature + irframe.pixel_x + irframe.pixel_y + irframe.pixel_size_x + irframe.pixel_size_y;
|
|
|
|
if (crc != irframe.checksum) {
|
|
|
|
// printf("bad crc 0x%04x 0x%04x\n", crc, irframe.checksum);
|
2016-11-26 01:22:18 -04:00
|
|
|
return false;
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_IRLock_I2C::read_frames(void)
|
2016-11-26 01:22:18 -04:00
|
|
|
{
|
|
|
|
if (!sync_frame_start()) {
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
|
|
|
struct frame irframe;
|
|
|
|
|
|
|
|
if (!read_block(irframe)) {
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
2016-11-26 01:22:18 -04:00
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
int16_t corner1_pix_x = irframe.pixel_x - irframe.pixel_size_x/2;
|
2016-11-26 01:22:18 -04:00
|
|
|
int16_t corner1_pix_y = irframe.pixel_y - irframe.pixel_size_y/2;
|
|
|
|
int16_t corner2_pix_x = irframe.pixel_x + irframe.pixel_size_x/2;
|
|
|
|
int16_t corner2_pix_y = irframe.pixel_y + irframe.pixel_size_y/2;
|
2016-11-26 01:22:18 -04:00
|
|
|
|
2016-11-26 01:22:18 -04:00
|
|
|
float corner1_pos_x, corner1_pos_y, corner2_pos_x, corner2_pos_y;
|
|
|
|
pixel_to_1M_plane(corner1_pix_x, corner1_pix_y, corner1_pos_x, corner1_pos_y);
|
|
|
|
pixel_to_1M_plane(corner2_pix_x, corner2_pix_y, corner2_pos_x, corner2_pos_y);
|
|
|
|
|
2017-02-18 00:41:00 -04:00
|
|
|
if (sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2016-11-26 01:22:18 -04:00
|
|
|
/* convert to angles */
|
|
|
|
_target_info.timestamp = AP_HAL::millis();
|
|
|
|
_target_info.pos_x = 0.5f*(corner1_pos_x+corner2_pos_x);
|
|
|
|
_target_info.pos_y = 0.5f*(corner1_pos_y+corner2_pos_y);
|
|
|
|
_target_info.size_x = corner2_pos_x-corner1_pos_x;
|
|
|
|
_target_info.size_y = corner2_pos_y-corner1_pos_y;
|
|
|
|
sem->give();
|
|
|
|
}
|
2016-11-26 01:22:18 -04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// debugging
|
|
|
|
static uint32_t lastt;
|
|
|
|
if (_target_info.timestamp - lastt > 2000) {
|
|
|
|
lastt = _target_info.timestamp;
|
|
|
|
printf("pos_x:%.5f pos_y:%.5f size_x:%.6f size_y:%.5f\n",
|
|
|
|
_target_info.pos_x, _target_info.pos_y,
|
|
|
|
_target_info.size_x, _target_info.size_y);
|
|
|
|
}
|
|
|
|
#endif
|
2016-11-26 01:22:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve latest sensor data - returns true if new data is available
|
|
|
|
bool AP_IRLock_I2C::update()
|
|
|
|
{
|
|
|
|
bool new_data = false;
|
2016-11-26 01:22:18 -04:00
|
|
|
if (!dev || !sem) {
|
2016-11-26 01:22:18 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-18 00:41:00 -04:00
|
|
|
if (sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2016-11-26 01:22:18 -04:00
|
|
|
if (_last_update_ms != _target_info.timestamp) {
|
|
|
|
new_data = true;
|
|
|
|
}
|
|
|
|
_last_update_ms = _target_info.timestamp;
|
2016-11-27 23:08:17 -04:00
|
|
|
_flags.healthy = (AP_HAL::millis() - _last_read_ms < 100);
|
2016-11-26 01:22:18 -04:00
|
|
|
sem->give();
|
|
|
|
}
|
|
|
|
// return true if new data found
|
|
|
|
return new_data;
|
|
|
|
}
|