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

Last change on this file since 405 was 318, checked in by Sanahuja Guillaume, 5 years ago
File size: 3.8 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 <Matrix.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 {
33namespace sensor {
34
35Sinus::Sinus(const FrameworkManager *parent, string name, int priority)
36 : IODevice(parent, name), Thread(parent, name, priority) {
37
38 plot = NULL;
39
40 // 1*1 output matrix
41 MatrixDescriptor *desc = new MatrixDescriptor(1, 1);
42 desc->SetElementName(0, 0, "value"); // name will be used for graphs and logs
43 output = new Matrix((IODevice *)this, desc, floatType, name);
44 delete desc;
45
46 AddDataToLog(output);
47
48 // interface initialisation
49 mainTab = new Tab(parent->GetTabWidget(), name);
50 tabWidget = new TabWidget(mainTab->NewRow(), name);
51
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);
62
63 setupLayout = new GridLayout(
64 settingsTab->NewRow(),
65 "setup"); // layout sui servira à placer des filtres par exemple
66
67 SetIsReady(true);
68}
69
70Sinus::~Sinus() {
71
72 SafeStop();
73 Join();
74
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;
80}
81
82void Sinus::UseDefaultPlot(void) {
83 Tab *plotTab = new Tab(tabWidget, "Graph");
84 plot = new DataPlot1D(plotTab->NewRow(), "Sinus", -10, 10);
85 plot->AddCurve(output->Element(0), DataPlot::Red);
86}
87
88GridLayout *Sinus::GetSetupLayout(void) const { return setupLayout; }
89
90DataPlot1D *Sinus::GetPlot(void) const {
91 if (plot == NULL)
92 Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n");
93 return plot;
94}
95
96Matrix *Sinus::GetMatrix(void) const { return output; }
97
98float Sinus::GetValue(void) const { return output->Value(0, 0); }
99
100// main function, where we compute the signal
101void Sinus::Run(void) {
102
103 SetPeriodMS(period->Value());
104
105 // warn if changing from primary to secondary mode when in real time
106 WarnUponSwitches(true);
107
108 Time initTime = GetTime();
109
110 while (!ToBeStopped()) {
111 WaitPeriod();
112
113 if (period->ValueChanged() == true)
114 SetPeriodMS(period->Value());
115
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
123
124 // put the result in output matrix
125 output->SetValue(0, 0, value);
126 // put corresponding time
127 output->SetDataTime(actualTime);
128
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 }
136}
137} // end namespace sensor
138} // end namespace flair
Note: See TracBrowser for help on using the repository browser.