ardupilot/libraries/AP_RangeFinder/RangeFinder.cpp
rmackay9@yahoo.com c2db64116a AP_RangeFinder - bug fix for cases when analog value is zero.
- addition of filter call which averages the past 4 values

git-svn-id: https://arducopter.googlecode.com/svn/trunk@1224 f9c3cf11-9bcb-44bc-f272-b75c42450872
2010-12-21 12:34:24 +00:00

38 lines
1.2 KiB
C++

// -*- tab-width: 4; Mode: C++; c-basic-offset: 3; indent-tabs-mode: t -*-
/*
AP_RangeFinder.cpp - Arduino Library for Sharpe GP2Y0A02YK0F
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"
// Public Methods //////////////////////////////////////////////////////////////
void RangeFinder::set_orientation(int x, int y, int z)
{
orientation_x = x;
orientation_y = y;
orientation_z = z;
}
// Protected Methods //////////////////////////////////////////////////////////
int RangeFinder::filter(int latestValue)
{
int i;
int total = 0;
_history_ptr = (_history_ptr + 1) % AP_RANGEFINDER_NUM_AVERAGES;
_history[_history_ptr] = latestValue;
for(i=0; i<AP_RANGEFINDER_NUM_AVERAGES; i++ )
total += _history[i];
return total / AP_RANGEFINDER_NUM_AVERAGES;
}