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

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

Reformatting

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);
44 output->SetValue(0, 0, 0);
[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
[14]66}
67
68Sinus::~Sinus() {
69
[16]70 SafeStop();
71 Join();
[14]72
[16]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;
[14]78}
79
80void Sinus::UseDefaultPlot(void) {
[16]81 Tab *plotTab = new Tab(tabWidget, "Graph");
82 plot = new DataPlot1D(plotTab->NewRow(), "Sinus", -10, 10);
83 plot->AddCurve(output->Element(0), DataPlot::Red);
[14]84}
85
[16]86GridLayout *Sinus::GetSetupLayout(void) const { return setupLayout; }
[14]87
[16]88DataPlot1D *Sinus::GetPlot(void) const {
89 if (plot == NULL)
90 Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n");
91 return plot;
[14]92}
93
[16]94cvmatrix *Sinus::GetMatrix(void) const { return output; }
[14]95
[16]96float Sinus::GetValue(void) const { return output->Value(0, 0); }
[14]97
[16]98// main function, where we compute the signal
[14]99void Sinus::Run(void) {
100
[16]101 SetPeriodMS(period->Value());
[14]102
[16]103 // warn if changing from primary to secondary mode when in real time
104 WarnUponSwitches(true);
[14]105
[16]106 Time initTime = GetTime();
[14]107
[16]108 while (!ToBeStopped()) {
109 WaitPeriod();
[14]110
[16]111 if (period->ValueChanged() == true)
112 SetPeriodMS(period->Value());
[14]113
[16]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
[14]121
[16]122 // put the result in output matrix
123 output->SetValue(0, 0, value);
124 // put corresponding time
125 output->SetDataTime(actualTime);
[14]126
[16]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 }
[14]134}
[16]135} // end namespace sensor
136} // end namespace flair
Note: See TracBrowser for help on using the repository browser.