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-07-17 16:05:18 -03:00
|
|
|
|
2011-01-03 00:17:43 -04:00
|
|
|
|
2010-12-21 08:34:24 -04:00
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2011-07-30 17:35:36 -03:00
|
|
|
void RangeFinder::set_analog_port(int analogPort)
|
2011-01-03 00:17:43 -04:00
|
|
|
{
|
|
|
|
// store the analog port to be used
|
|
|
|
_analogPort = analogPort;
|
2011-07-30 17:35:36 -03:00
|
|
|
pinMode(analogPort, INPUT);
|
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-06-30 19:32:26 -03:00
|
|
|
// Read Sensor data - only the raw_value is filled in by this parent class
|
|
|
|
int RangeFinder::read()
|
|
|
|
{
|
|
|
|
// read from the analog port or pitot tube
|
2011-07-30 17:35:36 -03:00
|
|
|
if( _ap_adc != NULL ){
|
|
|
|
raw_value = _ap_adc->Ch_raw(AP_RANGEFINDER_PITOT_TUBE_ADC_CHANNEL) >> 2; // values from ADC are twice as big as you'd expect
|
2011-06-30 19:32:26 -03:00
|
|
|
}else{
|
2011-07-17 16:05:18 -03:00
|
|
|
// read raw sensor value and convert to distance
|
|
|
|
raw_value = analogRead(_analogPort);
|
2011-06-30 19:32:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert analog value to distance in cm (using child implementation most likely)
|
2011-07-17 16:05:18 -03:00
|
|
|
raw_value = convert_raw_to_distance(raw_value);
|
2011-07-30 17:35:36 -03:00
|
|
|
|
2011-06-30 19:32:26 -03:00
|
|
|
// ensure distance is within min and max
|
2011-07-17 16:05:18 -03:00
|
|
|
raw_value = constrain(raw_value, min_distance, max_distance);
|
2011-01-03 00:17:43 -04:00
|
|
|
|
2011-07-30 17:35:36 -03:00
|
|
|
distance = _mode_filter->get_filtered_with_sample(raw_value);
|
2011-07-17 16:05:18 -03:00
|
|
|
return distance;
|
2011-05-04 16:12:27 -03:00
|
|
|
}
|
2011-06-16 13:30:37 -03:00
|
|
|
|