source: flair-src/branches/mavlink/demos/Sinus/src/Sinus.cpp@ 89

Last change on this file since 89 was 16, checked in by Bayard Gildas, 9 years ago

Reformatting

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 <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 {
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 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);
44 output->SetValue(0, 0, 0);
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
68Sinus::~Sinus() {
69
70 SafeStop();
71 Join();
72
73 // main_tab has the FrameworkManager as parent; it will be destroyed when
74 // FrameworkManager is destroyed
75 // it is cleaner to delete it manually, because main_tab is unnecessary when
76 // Sinus is deleted
77 delete mainTab;
78}
79
80void Sinus::UseDefaultPlot(void) {
81 Tab *plotTab = new Tab(tabWidget, "Graph");
82 plot = new DataPlot1D(plotTab->NewRow(), "Sinus", -10, 10);
83 plot->AddCurve(output->Element(0), DataPlot::Red);
84}
85
86GridLayout *Sinus::GetSetupLayout(void) const { return setupLayout; }
87
88DataPlot1D *Sinus::GetPlot(void) const {
89 if (plot == NULL)
90 Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n");
91 return plot;
92}
93
94cvmatrix *Sinus::GetMatrix(void) const { return output; }
95
96float Sinus::GetValue(void) const { return output->Value(0, 0); }
97
98// main function, where we compute the signal
99void Sinus::Run(void) {
100
101 SetPeriodMS(period->Value());
102
103 // warn if changing from primary to secondary mode when in real time
104 WarnUponSwitches(true);
105
106 Time initTime = GetTime();
107
108 while (!ToBeStopped()) {
109 WaitPeriod();
110
111 if (period->ValueChanged() == true)
112 SetPeriodMS(period->Value());
113
114 // compute sinus
115 Time actualTime = GetTime();
116 float value =
117 offset->Value() +
118 amplitude->Value() *
119 sinf(2 * PI * frequency->Value() * (actualTime - initTime) /
120 1000000000.); // les temps sont en nanosecondes
121
122 // put the result in output matrix
123 output->SetValue(0, 0, value);
124 // put corresponding time
125 output->SetDataTime(actualTime);
126
127 // ProcessUpdate is very important
128 // we must call it after updating the output matrix
129 // it allows:
130 // -to save value in the logs
131 // -to automatically call the next filter UpdateFrom method
132 ProcessUpdate(output);
133 }
134}
135} // end namespace sensor
136} // end namespace flair
Note: See TracBrowser for help on using the repository browser.