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

Last change on this file since 14 was 14, checked in by Sanahuja Guillaume, 7 years ago

sinus

File size: 3.6 KB
Line 
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
32namespace flair { namespace sensor {
33
34Sinus::Sinus(const FrameworkManager* parent,string name,int priority) : IODevice(parent,name),Thread(parent,name,priority) {
35
36 plot=NULL;
37
38 //1*1 output matrix
39 cvmatrix_descriptor* desc=new cvmatrix_descriptor(1,1);
40 desc->SetElementName(0,0,"value");//name will be used for graphs and logs
41 output=new cvmatrix((IODevice*)this,desc,floatType,name);
42 output->SetValue(0,0,0);
43
44
45 AddDataToLog(output);
46
47 //interface initialisation
48 mainTab=new Tab(parent->GetTabWidget(),name);
49 tabWidget=new TabWidget(mainTab->NewRow(),name);
50
51 Tab* settingsTab=new Tab(tabWidget,"Settings");
52 GroupBox* sinusGroupBox=new GroupBox(settingsTab->NewRow(),name);
53 frequency=new DoubleSpinBox(sinusGroupBox->NewRow(),"frequence:"," Hz",0,100,1);
54 amplitude=new DoubleSpinBox(sinusGroupBox->LastRowLastCol(),"amplitude:",0,10,1);
55 offset=new DoubleSpinBox(sinusGroupBox->LastRowLastCol(),"offset:",0,10,1);
56 period=new SpinBox(settingsTab->NewRow(),"period thread:"," ms",1,1000,1);
57
58 setupLayout=new GridLayout(settingsTab->NewRow(),"setup");//layout sui servira à placer des filtres par exemple
59}
60
61
62Sinus::~Sinus() {
63
64 SafeStop();
65 Join();
66
67 //main_tab has the FrameworkManager as parent; it will be destroyed when FrameworkManager is destroyed
68 //it is cleaner to delete it manually, because main_tab is unnecessary when Sinus is deleted
69 delete mainTab;
70}
71
72void Sinus::UseDefaultPlot(void) {
73 Tab* plotTab=new Tab(tabWidget,"Graph");
74 plot=new DataPlot1D(plotTab->NewRow(),"Sinus",-10,10);
75 plot->AddCurve(output->Element(0),DataPlot::Red);
76}
77
78GridLayout* Sinus::GetSetupLayout(void) const {
79 return setupLayout;
80}
81
82DataPlot1D* Sinus::GetPlot(void) const {
83 if(plot==NULL) Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n");
84 return plot;
85}
86
87cvmatrix *Sinus::GetMatrix(void) const {
88 return output;
89}
90
91float Sinus::GetValue(void) const {
92 return output->Value(0,0);
93}
94
95//main function, where we compute the signal
96void Sinus::Run(void) {
97
98 SetPeriodMS(period->Value());
99
100 //warn if changing from primary to secondary mode when in real time
101 WarnUponSwitches(true);
102
103 Time initTime=GetTime();
104
105 while(!ToBeStopped()) {
106 WaitPeriod();
107
108 if(period->ValueChanged()==true) SetPeriodMS(period->Value());
109
110 //compute sinus
111 Time actualTime=GetTime();
112 float value=offset->Value()+amplitude->Value()*sinf(2*PI*frequency->Value()*(actualTime-initTime)/1000000000.);//les temps sont en nanosecondes
113
114 //put the result in output matrix
115 output->SetValue(0,0,value);
116 //put corresponding time
117 output->SetDataTime(actualTime);
118
119 //ProcessUpdate is very important
120 //we must call it after updating the output matrix
121 //it allows:
122 // -to save value in the logs
123 // -to automatically call the next filter UpdateFrom method
124 ProcessUpdate(output);
125 }
126}
127}// end namespace sensor
128}// end namespace flair
Note: See TracBrowser for help on using the repository browser.