2010-12-21 08:34:24 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 3; indent-tabs-mode: t -*-
|
|
|
|
/*
|
2011-06-16 13:30:37 -03:00
|
|
|
AP_RangeFinder.cpp - Arduino Library for Sharpe GP2Y0A02YK0F
|
2010-12-21 08:34:24 -04:00
|
|
|
infrared proximity sensor
|
|
|
|
Code by Jose Julio and Randy Mackay. 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.
|
|
|
|
|
|
|
|
This has the basic functions that all RangeFinders need implemented
|
|
|
|
*/
|
|
|
|
|
|
|
|
// AVR LibC Includes
|
|
|
|
#include "WConstants.h"
|
|
|
|
#include "RangeFinder.h"
|
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// Constructor /////////////////////////////////////////////////////////////////
|
2011-05-04 16:12:27 -03:00
|
|
|
RangeFinder::RangeFinder() :
|
|
|
|
_ap_adc(NULL),
|
|
|
|
_num_averages(AP_RANGEFINDER_NUM_AVERAGES),
|
|
|
|
_history_ptr(0)
|
2011-01-03 00:17:43 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-21 08:34:24 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2011-01-03 00:17:43 -04:00
|
|
|
void RangeFinder::init(int analogPort, AP_ADC *ap_adc)
|
|
|
|
{
|
|
|
|
// local variables
|
|
|
|
int i;
|
2011-06-16 13:30:37 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// store the analog port to be used
|
|
|
|
_analogPort = analogPort;
|
2011-06-16 13:30:37 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// set the given analog port to an input
|
2011-06-16 13:30:37 -03:00
|
|
|
if( analogPort != AP_RANGEFINDER_PITOT_TUBE ){
|
2011-01-03 00:17:43 -04:00
|
|
|
pinMode(analogPort, INPUT);
|
|
|
|
}else{
|
|
|
|
_num_averages = 0; // turn off averaging for pitot tube because AP_ADC does this for us
|
|
|
|
}
|
2011-06-16 13:30:37 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// capture the AP_ADC object if passed in
|
|
|
|
if( ap_adc != NULL )
|
2011-06-16 13:30:37 -03:00
|
|
|
_ap_adc = ap_adc;
|
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// make first call to read to get initial distance
|
|
|
|
read();
|
2011-06-16 13:30:37 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// initialise history
|
2011-06-16 13:30:37 -03:00
|
|
|
for(i = 0; i < AP_RANGEFINDER_NUM_AVERAGES; i++)
|
|
|
|
_history[i] = distance;
|
2011-01-03 00:17:43 -04:00
|
|
|
}
|
|
|
|
|
2010-12-21 08:34:24 -04:00
|
|
|
void RangeFinder::set_orientation(int x, int y, int z)
|
|
|
|
{
|
2011-06-16 13:30:37 -03:00
|
|
|
orientation_x = x;
|
|
|
|
orientation_y = y;
|
2010-12-21 08:34:24 -04:00
|
|
|
orientation_z = z;
|
|
|
|
}
|
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// Read Sensor data - only the raw_value is filled in by this parent class
|
|
|
|
int RangeFinder::read()
|
2010-12-21 08:34:24 -04:00
|
|
|
{
|
2011-01-03 00:17:43 -04:00
|
|
|
// local variables
|
|
|
|
int temp_dist;
|
2011-06-16 13:30:37 -03:00
|
|
|
int total = 0;
|
2011-01-03 00:17:43 -04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// read from the analog port or pitot tube
|
2011-06-16 13:30:37 -03:00
|
|
|
if(_analogPort == AP_RANGEFINDER_PITOT_TUBE){
|
|
|
|
if(_ap_adc != NULL){
|
|
|
|
raw_value = _ap_adc->Ch(AP_RANGEFINDER_PITOT_TUBE_ADC_CHANNEL) >> 2; // values from ADC are twice as big as you'd expect
|
|
|
|
}else{
|
|
|
|
raw_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert analog value to distance in cm (using child implementation most likely)
|
|
|
|
temp_dist = convert_raw_to_distance(raw_value);
|
|
|
|
|
|
|
|
// ensure distance is within min and max
|
|
|
|
distance = constrain(temp_dist, min_distance, max_distance);
|
|
|
|
|
|
|
|
// return distance
|
|
|
|
return distance;
|
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
}else{
|
2011-06-16 13:30:37 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
// read raw sensor value and convert to distance
|
|
|
|
raw_value = analogRead(_analogPort);
|
2011-06-16 13:30:37 -03:00
|
|
|
|
|
|
|
// convert analog value to distance in cm (using child implementation most likely)
|
|
|
|
temp_dist = convert_raw_to_distance(raw_value);
|
|
|
|
|
|
|
|
// ensure distance is within min and max
|
|
|
|
distance = constrain(temp_dist, min_distance, max_distance);
|
|
|
|
|
|
|
|
// filter the results
|
|
|
|
if( _num_averages > 1 ){
|
|
|
|
_history_ptr = (_history_ptr + 1) % _num_averages;
|
|
|
|
_history[_history_ptr] = distance;
|
|
|
|
for(i = 0; i < _num_averages; i++ ) total += _history[i];
|
|
|
|
distance = total / _num_averages;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return distance
|
|
|
|
return distance;
|
2011-01-03 00:17:43 -04:00
|
|
|
}
|
2011-05-04 16:12:27 -03:00
|
|
|
}
|
2011-06-16 13:30:37 -03:00
|
|
|
|
|
|
|
|