mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-08 17:03:57 -04:00
uncrustify libraries/Filter/ModeFilter.h
This commit is contained in:
parent
406abb3fa0
commit
5948f825ba
@ -20,17 +20,17 @@
|
|||||||
template <class T, uint8_t FILTER_SIZE>
|
template <class T, uint8_t FILTER_SIZE>
|
||||||
class ModeFilter : public FilterWithBuffer<T,FILTER_SIZE>
|
class ModeFilter : public FilterWithBuffer<T,FILTER_SIZE>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ModeFilter(uint8_t return_element);
|
ModeFilter(uint8_t return_element);
|
||||||
|
|
||||||
// apply - Add a new raw value to the filter, retrieve the filtered result
|
// apply - Add a new raw value to the filter, retrieve the filtered result
|
||||||
virtual T apply(T sample);
|
virtual T apply(T sample);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// private methods
|
// private methods
|
||||||
uint8_t _return_element;
|
uint8_t _return_element;
|
||||||
void isort(T sample, bool drop_high_sample);
|
void isort(T sample, bool drop_high_sample);
|
||||||
bool drop_high_sample; // switch to determine whether to drop the highest or lowest sample when new value arrives
|
bool drop_high_sample; // switch to determine whether to drop the highest or lowest sample when new value arrives
|
||||||
};
|
};
|
||||||
|
|
||||||
// Typedef for convenience
|
// Typedef for convenience
|
||||||
@ -59,34 +59,34 @@ typedef ModeFilter<uint16_t,7> ModeFilterUInt16_Size7;
|
|||||||
|
|
||||||
template <class T, uint8_t FILTER_SIZE>
|
template <class T, uint8_t FILTER_SIZE>
|
||||||
ModeFilter<T,FILTER_SIZE>::ModeFilter(uint8_t return_element) :
|
ModeFilter<T,FILTER_SIZE>::ModeFilter(uint8_t return_element) :
|
||||||
FilterWithBuffer<T,FILTER_SIZE>(),
|
FilterWithBuffer<T,FILTER_SIZE>(),
|
||||||
_return_element(return_element),
|
_return_element(return_element),
|
||||||
drop_high_sample(true)
|
drop_high_sample(true)
|
||||||
{
|
{
|
||||||
// ensure we have a valid return_nth_element value. if not, revert to median
|
// ensure we have a valid return_nth_element value. if not, revert to median
|
||||||
if( _return_element >= FILTER_SIZE )
|
if( _return_element >= FILTER_SIZE )
|
||||||
_return_element = FILTER_SIZE / 2;
|
_return_element = FILTER_SIZE / 2;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public Methods //////////////////////////////////////////////////////////////
|
// Public Methods //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
template <class T, uint8_t FILTER_SIZE>
|
template <class T, uint8_t FILTER_SIZE>
|
||||||
T ModeFilter<T,FILTER_SIZE>::apply(T sample)
|
T ModeFilter<T,FILTER_SIZE>:: apply(T sample)
|
||||||
{
|
{
|
||||||
// insert the new items into the samples buffer
|
// insert the new items into the samples buffer
|
||||||
isort(sample, drop_high_sample);
|
isort(sample, drop_high_sample);
|
||||||
|
|
||||||
// next time drop from the other end of the sample buffer
|
// next time drop from the other end of the sample buffer
|
||||||
drop_high_sample = !drop_high_sample;
|
drop_high_sample = !drop_high_sample;
|
||||||
|
|
||||||
// return results
|
// return results
|
||||||
if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
|
if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
|
||||||
// middle sample if buffer is not yet full
|
// middle sample if buffer is not yet full
|
||||||
return FilterWithBuffer<T,FILTER_SIZE>::samples[(FilterWithBuffer<T,FILTER_SIZE>::sample_index / 2)];
|
return FilterWithBuffer<T,FILTER_SIZE>::samples[(FilterWithBuffer<T,FILTER_SIZE>::sample_index / 2)];
|
||||||
}else{
|
}else{
|
||||||
// return element specified by user in constructor
|
// return element specified by user in constructor
|
||||||
return FilterWithBuffer<T,FILTER_SIZE>::samples[_return_element];
|
return FilterWithBuffer<T,FILTER_SIZE>::samples[_return_element];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -94,45 +94,45 @@ T ModeFilter<T,FILTER_SIZE>::apply(T sample)
|
|||||||
// drops either the highest or lowest sample depending on the 'drop_high_sample' parameter
|
// drops either the highest or lowest sample depending on the 'drop_high_sample' parameter
|
||||||
//
|
//
|
||||||
template <class T, uint8_t FILTER_SIZE>
|
template <class T, uint8_t FILTER_SIZE>
|
||||||
void ModeFilter<T,FILTER_SIZE>::isort(T new_sample, bool drop_high)
|
void ModeFilter<T,FILTER_SIZE>:: isort(T new_sample, bool drop_high)
|
||||||
{
|
{
|
||||||
int8_t i;
|
int8_t i;
|
||||||
|
|
||||||
// if the buffer isn't full simply increase the #items in the buffer (i.e. sample_index)
|
// if the buffer isn't full simply increase the #items in the buffer (i.e. sample_index)
|
||||||
// the rest is the same as dropping the high sample
|
// the rest is the same as dropping the high sample
|
||||||
if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
|
if( FilterWithBuffer<T,FILTER_SIZE>::sample_index < FILTER_SIZE ) {
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::sample_index++;
|
FilterWithBuffer<T,FILTER_SIZE>::sample_index++;
|
||||||
drop_high = true;
|
drop_high = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( drop_high ) { // drop highest sample from the buffer to make room for our new sample
|
if( drop_high ) { // drop highest sample from the buffer to make room for our new sample
|
||||||
|
|
||||||
// start from top. Note: sample_index always points to the next open space so we start from sample_index-1
|
// start from top. Note: sample_index always points to the next open space so we start from sample_index-1
|
||||||
i = FilterWithBuffer<T,FILTER_SIZE>::sample_index-1;
|
i = FilterWithBuffer<T,FILTER_SIZE>::sample_index-1;
|
||||||
|
|
||||||
// if the next element is higher than our new sample, push it up one position
|
// if the next element is higher than our new sample, push it up one position
|
||||||
while( FilterWithBuffer<T,FILTER_SIZE>::samples[i-1] > new_sample && i > 0 ) {
|
while( FilterWithBuffer<T,FILTER_SIZE>::samples[i-1] > new_sample && i > 0 ) {
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i-1];
|
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i-1];
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add our new sample to the buffer
|
// add our new sample to the buffer
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
|
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
|
||||||
|
|
||||||
}else{ // drop lowest sample from the buffer to make room for our new sample
|
}else{ // drop lowest sample from the buffer to make room for our new sample
|
||||||
|
|
||||||
// start from the bottom
|
// start from the bottom
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
// if the element is lower than our new sample, push it down one position
|
// if the element is lower than our new sample, push it down one position
|
||||||
while( FilterWithBuffer<T,FILTER_SIZE>::samples[i+1] < new_sample && i < FilterWithBuffer<T,FILTER_SIZE>::sample_index-1 ) {
|
while( FilterWithBuffer<T,FILTER_SIZE>::samples[i+1] < new_sample && i < FilterWithBuffer<T,FILTER_SIZE>::sample_index-1 ) {
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i+1];
|
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = FilterWithBuffer<T,FILTER_SIZE>::samples[i+1];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add our new sample to the buffer
|
// add our new sample to the buffer
|
||||||
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
|
FilterWithBuffer<T,FILTER_SIZE>::samples[i] = new_sample;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user