Changeset 16 in flair-src for trunk/demos/Sinus/src/Sinus.cpp
- Timestamp:
- 04/08/16 15:48:40 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/demos/Sinus/src/Sinus.cpp
r14 r16 30 30 using namespace flair::gui; 31 31 32 namespace flair { namespace sensor { 32 namespace flair { 33 namespace sensor { 33 34 34 Sinus::Sinus(const FrameworkManager* parent,string name,int priority) : IODevice(parent,name),Thread(parent,name,priority) { 35 Sinus::Sinus(const FrameworkManager *parent, string name, int priority) 36 : IODevice(parent, name), Thread(parent, name, priority) { 35 37 36 plot=NULL;38 plot = NULL; 37 39 38 //1*1 output matrix39 cvmatrix_descriptor* desc=new cvmatrix_descriptor(1,1);40 desc->SetElementName(0,0,"value");//name will be used for graphs and logs41 output=new cvmatrix((IODevice*)this,desc,floatType,name);42 output->SetValue(0,0,0);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); 43 45 46 AddDataToLog(output); 44 47 45 AddDataToLog(output); 48 // interface initialisation 49 mainTab = new Tab(parent->GetTabWidget(), name); 50 tabWidget = new TabWidget(mainTab->NewRow(), name); 46 51 47 //interface initialisation 48 mainTab=new Tab(parent->GetTabWidget(),name); 49 tabWidget=new TabWidget(mainTab->NewRow(),name); 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); 50 62 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 63 setupLayout = new GridLayout( 64 settingsTab->NewRow(), 65 "setup"); // layout sui servira à placer des filtres par exemple 59 66 } 60 61 67 62 68 Sinus::~Sinus() { 63 69 64 65 70 SafeStop(); 71 Join(); 66 72 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; 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; 70 78 } 71 79 72 80 void 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);81 Tab *plotTab = new Tab(tabWidget, "Graph"); 82 plot = new DataPlot1D(plotTab->NewRow(), "Sinus", -10, 10); 83 plot->AddCurve(output->Element(0), DataPlot::Red); 76 84 } 77 85 78 GridLayout* Sinus::GetSetupLayout(void) const { 79 return setupLayout; 86 GridLayout *Sinus::GetSetupLayout(void) const { return setupLayout; } 87 88 DataPlot1D *Sinus::GetPlot(void) const { 89 if (plot == NULL) 90 Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n"); 91 return plot; 80 92 } 81 93 82 DataPlot1D* Sinus::GetPlot(void) const { 83 if(plot==NULL) Printf("Sinus::Plot, plot not yet defined, call UseDefaultPlot first\n"); 84 return plot; 85 } 94 cvmatrix *Sinus::GetMatrix(void) const { return output; } 86 95 87 cvmatrix *Sinus::GetMatrix(void) const { 88 return output; 89 } 96 float Sinus::GetValue(void) const { return output->Value(0, 0); } 90 97 91 float Sinus::GetValue(void) const { 92 return output->Value(0,0); 93 } 94 95 //main function, where we compute the signal 98 // main function, where we compute the signal 96 99 void Sinus::Run(void) { 97 100 98 101 SetPeriodMS(period->Value()); 99 102 100 //warn if changing from primary to secondary mode when in real time101 103 // warn if changing from primary to secondary mode when in real time 104 WarnUponSwitches(true); 102 105 103 Time initTime=GetTime();106 Time initTime = GetTime(); 104 107 105 while(!ToBeStopped()) {106 108 while (!ToBeStopped()) { 109 WaitPeriod(); 107 110 108 if(period->ValueChanged()==true) SetPeriodMS(period->Value()); 111 if (period->ValueChanged() == true) 112 SetPeriodMS(period->Value()); 109 113 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 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 113 121 114 //put the result in output matrix115 output->SetValue(0,0,value);116 //put corresponding time117 122 // put the result in output matrix 123 output->SetValue(0, 0, value); 124 // put corresponding time 125 output->SetDataTime(actualTime); 118 126 119 //ProcessUpdate is very important120 //we must call it after updating the output matrix121 //it allows:122 123 124 125 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 } 126 134 } 127 } // end namespace sensor128 } // end namespace flair135 } // end namespace sensor 136 } // end namespace flair
Note:
See TracChangeset
for help on using the changeset viewer.