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-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()
|
|
|
|
{
|
2011-12-11 03:40:59 -04:00
|
|
|
int temp_dist;
|
|
|
|
|
2011-11-12 23:16:31 -04:00
|
|
|
raw_value = _analog_source->read();
|
2011-06-30 19:32:26 -03:00
|
|
|
|
|
|
|
// convert analog value to distance in cm (using child implementation most likely)
|
2011-12-11 03:40:59 -04:00
|
|
|
temp_dist = 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-12-11 03:40:59 -04:00
|
|
|
temp_dist = constrain(temp_dist, min_distance, max_distance);
|
2011-01-03 00:17:43 -04:00
|
|
|
|
2011-12-11 03:40:59 -04:00
|
|
|
distance = _mode_filter->get_filtered_with_sample(temp_dist);
|
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
|
|
|
|