source: flair-src/trunk/demos/Sinus/src/MeanFilter.cpp @ 157

Last change on this file since 157 was 157, checked in by Sanahuja Guillaume, 4 years ago

iadded isready to iodevice:
avoid problem of imu not ready in ardrone2

File size: 2.6 KB
Line 
1//  created:    2013/06/27
2//  filename:   MeanFilter.cpp
3//
4//  author:     Guillaume Sanahuja
5//              Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7//  version:    $Id: $
8//
9//  purpose:    object computing a mean filter
10//
11//
12/*********************************************************************/
13
14#include "MeanFilter.h"
15#include <cvmatrix.h>
16#include <LayoutPosition.h>
17#include <GroupBox.h>
18#include <SpinBox.h>
19
20using namespace std;
21using namespace flair::core;
22using namespace flair::gui;
23
24namespace flair {
25namespace filter {
26
27MeanFilter::MeanFilter(const IODevice *parent, const LayoutPosition *position,
28                       string name)
29    : IODevice(parent, name) {
30  // interface initialisation
31  groupBox = new GroupBox(position, name);
32  numberOfElements = new SpinBox(groupBox->NewRow(), "numberOfElements:", 1,
33                                 MAX_NUMBER_OF_ELEMENTS,
34                                 1); // saturated to MAX_NUMBER_OF_ELEMENTS
35
36  // init storage
37  for (int i = 0; i < MAX_NUMBER_OF_ELEMENTS; i++)
38    previousValues[i] = 0;
39
40  // 1*1 output matrix
41  cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1);
42  desc->SetElementName(0, 0,
43                       "mean filter"); // name will be used for graphs and logs
44  output = new cvmatrix(this, desc, floatType, name);
45  delete desc;
46
47  AddDataToLog(output);
48 
49  SetIsReady(true);
50}
51
52MeanFilter::~MeanFilter() {}
53
54cvmatrix *MeanFilter::GetMatrix() const { return output; }
55
56float MeanFilter::GetValue(void) const { return output->Value(0, 0); }
57
58// UpdateFrom, where we implement the filter
59// this method is automatically called when the parent IODevice calls
60// ProcessUpdate
61// in our case it is the sinus or the 1st orde law pass filter
62//(see in Sinus::Run the call to ProcessUpdate)
63void MeanFilter::UpdateFrom(const io_data *data) {
64
65  float result = 0;
66  // get input argument in a cvmatrix
67  cvmatrix *input = (cvmatrix *)data;
68
69  // simple (and not efficent!) implementation of the filter
70  previousValues[numberOfElements->Value() - 1] = input->Value(0, 0);
71  for (int i = 0; i < numberOfElements->Value(); i++)
72    result += previousValues[i];
73  for (int i = 1; i < numberOfElements->Value(); i++)
74    previousValues[i - 1] = previousValues[i];
75
76  // put the result in output matrix
77  output->SetValue(0, 0, result / numberOfElements->Value());
78  // put corresponding time
79  output->SetDataTime(data->DataTime());
80
81  // ProcessUpdate is very important
82  // we must call it after updating the output matrix
83  // it allows:
84  // -to save value in the logs
85  // -to automatically call the next filter UpdateFrom method
86  ProcessUpdate(output);
87}
88} // end namespace filter
89} // end namespace flair
Note: See TracBrowser for help on using the repository browser.