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

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

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

File size: 3.8 KB
RevLine 
[14]1// created: 2013/06/26
2// filename: Sinus.cpp
3//
4// author: Guillaume Sanahuja
5// Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7// version: $Id: $
8//
9// purpose: class generating a sinus signal
10//
11//
12/*********************************************************************/
13
14#include "Sinus.h"
15#include <FrameworkManager.h>
16#include <TabWidget.h>
17#include <Tab.h>
18#include <GridLayout.h>
19#include <GroupBox.h>
20#include <DoubleSpinBox.h>
21#include <SpinBox.h>
22#include <DataPlot1D.h>
23#include <cvmatrix.h>
24#include <math.h>
25
26#define PI ((float)3.14159265358979323846)
27
28using namespace std;
29using namespace flair::core;
30using namespace flair::gui;
31
[16]32namespace flair {
33namespace sensor {
[14]34
[16]35Sinus::Sinus(const FrameworkManager *parent, string name, int priority)
36 : IODevice(parent, name), Thread(parent, name, priority) {
[14]37
[16]38 plot = NULL;
[14]39
[16]40 // 1*1 output matrix
41 cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1);
42 desc->SetElementName(0, 0, "value"); // name will be used for graphs and logs
43 output = new cvmatrix((IODevice *)this, desc, floatType, name);
[148]44 delete desc;
[14]45
[16]46 AddDataToLog(output);
[14]47
[16]48 // interface initialisation
49 mainTab = new Tab(parent->GetTabWidget(), name);
50 tabWidget = new TabWidget(mainTab->NewRow(), name);
[14]51
[16]52 Tab *settingsTab = new Tab(tabWidget, "Settings");
53 GroupBox *sinusGroupBox = new GroupBox(settingsTab->NewRow(), name);
54 frequency = new DoubleSpinBox(sinusGroupBox->NewRow(), "frequence:", " Hz", 0,
55 100, 1);
56 amplitude = new DoubleSpinBox(sinusGroupBox->LastRowLastCol(), "amplitude:",
57 0, 10, 1);
58 offset =
59 new DoubleSpinBox(sinusGroupBox->LastRowLastCol(), "offset:", 0, 10, 1);
60 period =
61 new SpinBox(settingsTab->NewRow(), "period thread:", " ms", 1, 1000, 1);
[14]62
[16]63 setupLayout = new GridLayout(
64 settingsTab->NewRow(),
65 "setup"); // layout sui servira à placer des filtres par exemple
[157]66
67 SetIsReady(true);
[14]68}
69
70Sinus::~Sinus() {
71
[16]72 SafeStop();
73 Join();
[14]74
[16]75 // main_tab has the FrameworkManager as parent; it will be destroyed when
76 // FrameworkManager is destroyed
77 // it is cleaner to delete it manually, because main_tab is unnecessary when
78 // Sinus is deleted
79 delete mainTab;
[14]80}
81
82void Sinus::UseDefaultPlot(void) {
[16]83 Tab *plotTab = new Tab(tabWidget, "Graph");
84 plot = new DataPlot1D(plotTab->NewRow(), "Sinus", -10, 10);
85 plot->AddCurve(output->Element(0), DataPlot::Red);
[14]86}
87
[16]88GridLayout *Sinus::GetSetupLayout(void) const { return setupLayout; }
[14]89
[16]90DataPlot1D *Sinus::GetPlot(void) const {
91 if (plot == NULL)
92 Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n");
93 return plot;
[14]94}
95
[16]96cvmatrix *Sinus::GetMatrix(void) const { return output; }
[14]97
[16]98float Sinus::GetValue(void) const { return output->Value(0, 0); }
[14]99
[16]100// main function, where we compute the signal
[14]101void Sinus::Run(void) {
102
[16]103 SetPeriodMS(period->Value());
[14]104
[16]105 // warn if changing from primary to secondary mode when in real time
106 WarnUponSwitches(true);
[14]107
[16]108 Time initTime = GetTime();
[14]109
[16]110 while (!ToBeStopped()) {
111 WaitPeriod();
[14]112
[16]113 if (period->ValueChanged() == true)
114 SetPeriodMS(period->Value());
[14]115
[16]116 // compute sinus
117 Time actualTime = GetTime();
118 float value =
119 offset->Value() +
120 amplitude->Value() *
121 sinf(2 * PI * frequency->Value() * (actualTime - initTime) /
122 1000000000.); // les temps sont en nanosecondes
[14]123
[16]124 // put the result in output matrix
125 output->SetValue(0, 0, value);
126 // put corresponding time
127 output->SetDataTime(actualTime);
[14]128
[16]129 // ProcessUpdate is very important
130 // we must call it after updating the output matrix
131 // it allows:
132 // -to save value in the logs
133 // -to automatically call the next filter UpdateFrom method
134 ProcessUpdate(output);
135 }
[14]136}
[16]137} // end namespace sensor
138} // end namespace flair
Note: See TracBrowser for help on using the repository browser.