[10] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[10] | 4 | // %flair:license}
|
---|
[7] | 5 | // created: 2011/05/01
|
---|
| 6 | // filename: EulerDerivative.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: objet permettant le calcul d'une derivee d'Euler
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "EulerDerivative.h"
|
---|
| 19 | #include "EulerDerivative_impl.h"
|
---|
[214] | 20 | #include <Matrix.h>
|
---|
[7] | 21 | #include <Layout.h>
|
---|
| 22 | #include <GroupBox.h>
|
---|
| 23 | #include <DoubleSpinBox.h>
|
---|
| 24 |
|
---|
| 25 | using std::string;
|
---|
| 26 | using namespace flair::core;
|
---|
| 27 | using namespace flair::gui;
|
---|
| 28 | using namespace flair::filter;
|
---|
| 29 |
|
---|
[15] | 30 | EulerDerivative_impl::EulerDerivative_impl(EulerDerivative *self,
|
---|
| 31 | const LayoutPosition *position,
|
---|
| 32 | string name,
|
---|
[214] | 33 | const Matrix *init_value) {
|
---|
| 34 | this->self = self;
|
---|
[272] | 35 | first_update = true;
|
---|
[147] | 36 |
|
---|
| 37 | if (init_value != NULL) {
|
---|
| 38 | // init output matrix of same size as init
|
---|
[318] | 39 | MatrixDescriptor *desc =new MatrixDescriptor(init_value->Rows(), init_value->Cols());
|
---|
[7] | 40 |
|
---|
[147] | 41 | for (int i = 0; i < init_value->Rows(); i++) {
|
---|
| 42 | for (int j = 0; j < init_value->Cols(); j++) {
|
---|
| 43 | desc->SetElementName(i, j, init_value->Name(i, j));
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
[214] | 46 | output = new Matrix(self, desc,init_value->GetDataType().GetElementDataType(), name);
|
---|
[148] | 47 | delete desc;
|
---|
[147] | 48 | for (int i = 0; i < init_value->Rows(); i++) {
|
---|
| 49 | for (int j = 0; j < init_value->Cols(); j++) {
|
---|
| 50 | output->SetValue(i, j, init_value->Value(i,j));
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[15] | 53 | } else {
|
---|
| 54 | // if NULL, assume dimension 1, and init=0
|
---|
[318] | 55 | MatrixDescriptor *desc = new MatrixDescriptor(1, 1);
|
---|
[15] | 56 | desc->SetElementName(0, 0, "output");
|
---|
[214] | 57 | output = new Matrix(self, desc, floatType, name);
|
---|
[148] | 58 | delete desc;
|
---|
[15] | 59 | }
|
---|
[147] | 60 |
|
---|
| 61 |
|
---|
[318] | 62 | MatrixDescriptor *desc = new MatrixDescriptor(output->Rows(), output->Cols());
|
---|
[256] | 63 | prev_input = new Matrix(self, desc, output->GetDataType().GetElementDataType(), name);
|
---|
| 64 | prev_output = new Matrix(self, desc, output->GetDataType().GetElementDataType(), name);
|
---|
| 65 | delete desc;
|
---|
[7] | 66 |
|
---|
[147] | 67 |
|
---|
[15] | 68 | // init UI
|
---|
| 69 | GroupBox *reglages_groupbox = new GroupBox(position, name);
|
---|
[256] | 70 | T = new DoubleSpinBox(reglages_groupbox->NewRow(), "period, 0 for auto:"," s", 0, 1, 0.01);
|
---|
| 71 | sat = new DoubleSpinBox(reglages_groupbox->NewRow(), "saturation, -1 to disable:",-1,100000,1,1,-1);
|
---|
[7] | 72 | }
|
---|
| 73 |
|
---|
[15] | 74 | EulerDerivative_impl::~EulerDerivative_impl() {}
|
---|
[7] | 75 |
|
---|
[15] | 76 | void EulerDerivative_impl::UpdateFrom(const io_data *data) {
|
---|
| 77 | float delta_t;
|
---|
[214] | 78 | const Matrix* input = dynamic_cast<const Matrix*>(data);
|
---|
| 79 |
|
---|
| 80 | if (!input) {
|
---|
| 81 | self->Warn("casting %s to Matrix failed\n",data->ObjectName().c_str());
|
---|
| 82 | return;
|
---|
| 83 | }
|
---|
[7] | 84 |
|
---|
[15] | 85 | // on prend une fois pour toute les mutex et on fait des accès directs
|
---|
| 86 | output->GetMutex();
|
---|
| 87 | input->GetMutex();
|
---|
[7] | 88 |
|
---|
[15] | 89 | if (first_update == true) {
|
---|
| 90 | for (int i = 0; i < input->Rows(); i++) {
|
---|
| 91 | for (int j = 0; j < input->Cols(); j++) {
|
---|
[256] | 92 | prev_input->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
|
---|
[15] | 93 | }
|
---|
[7] | 94 | }
|
---|
[15] | 95 | first_update = false;
|
---|
| 96 | } else {
|
---|
| 97 | if (T->Value() == 0) {
|
---|
[223] | 98 | delta_t = (float)(data->DataDeltaTime()) / 1000000000.;
|
---|
[15] | 99 | } else {
|
---|
| 100 | delta_t = T->Value();
|
---|
| 101 | }
|
---|
[7] | 102 |
|
---|
[223] | 103 | if(delta_t!=0) {
|
---|
| 104 | for (int i = 0; i < input->Rows(); i++) {
|
---|
| 105 | for (int j = 0; j < input->Cols(); j++) {
|
---|
[256] | 106 | float result=(input->ValueNoMutex(i, j) - prev_input->ValueNoMutex(i, j)) / delta_t;
|
---|
| 107 | if(sat->Value()!=-1) {
|
---|
| 108 | if(result>sat->Value() && result>0) result=sat->Value();
|
---|
| 109 | if(result<-sat->Value() && result<0) result=-sat->Value();
|
---|
| 110 | }/* filter by acc
|
---|
| 111 | float acc=result-prev_output->ValueNoMutex(i, j)/delta_t;
|
---|
| 112 | if(acc>20 || acc<-20) {
|
---|
| 113 | printf("%s %f\n",self->ObjectName().c_str(),acc);
|
---|
| 114 | } else {*/
|
---|
| 115 | output->SetValueNoMutex(i, j, result);
|
---|
| 116 | //}
|
---|
| 117 | prev_output->SetValueNoMutex(i, j, result);
|
---|
| 118 | prev_input->SetValueNoMutex(i, j, input->ValueNoMutex(i, j));
|
---|
| 119 |
|
---|
[223] | 120 | }
|
---|
[15] | 121 | }
|
---|
[7] | 122 | }
|
---|
[15] | 123 | }
|
---|
[7] | 124 |
|
---|
[15] | 125 | input->ReleaseMutex();
|
---|
| 126 | output->ReleaseMutex();
|
---|
[7] | 127 |
|
---|
[15] | 128 | output->SetDataTime(data->DataTime());
|
---|
[7] | 129 | }
|
---|