Changeset 16 in flair-src for trunk/demos/Sinus/src


Ignore:
Timestamp:
Apr 8, 2016, 3:48:40 PM (9 years ago)
Author:
Bayard Gildas
Message:

Reformatting

Location:
trunk/demos/Sinus/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/demos/Sinus/src/Loop.cpp

    r14 r16  
    1111/*********************************************************************/
    1212
    13 //include header of the class
     13// include header of the class
    1414#include "Loop.h"
    15 //include only header of class declared in Loop.h (forward declaration)
     15// include only header of class declared in Loop.h (forward declaration)
    1616#include "Sinus.h"
    1717#include "MeanFilter.h"
     
    2626#include <cvmatrix.h>
    2727
    28 
    2928using namespace std;
    3029using namespace flair::core;
     
    3332using namespace flair::sensor;
    3433
    35 Loop::Loop(FrameworkManager* parent,string name,int priority): Thread(parent,name,priority) {
    36     Tab *mainTab=new Tab(parent->GetTabWidget(),ObjectName());
    37     killButton=new PushButton(mainTab->NewRow(),"kill");
    38     startLogButton=new PushButton(mainTab->NewRow(),"start_log");
    39     stopLogButton=new PushButton(mainTab->LastRowLastCol(),"stop_log");
    40     period=new SpinBox(mainTab->NewRow(),"period thread:"," ms",10,1000,1);
     34Loop::Loop(FrameworkManager *parent, string name, int priority)
     35    : Thread(parent, name, priority) {
     36  Tab *mainTab = new Tab(parent->GetTabWidget(), ObjectName());
     37  killButton = new PushButton(mainTab->NewRow(), "kill");
     38  startLogButton = new PushButton(mainTab->NewRow(), "start_log");
     39  stopLogButton = new PushButton(mainTab->LastRowLastCol(), "stop_log");
     40  period = new SpinBox(mainTab->NewRow(), "period thread:", " ms", 10, 1000, 1);
    4141
    42     sinus=new Sinus(parent,"sinus");
    43     sinus->UseDefaultPlot();
     42  sinus = new Sinus(parent, "sinus");
     43  sinus->UseDefaultPlot();
    4444
    45     //1st order law pass filter on raw signal, its parent is the sinus
    46     firstLowPass=new LowPassFilter(sinus,sinus->GetSetupLayout()->NewRow(),"1st order lawpass filter");
    47     sinus->GetPlot()->AddCurve(firstLowPass->Matrix()->Element(0),DataPlot::Blue);//add output of the filter to signal's graph
     45  // 1st order law pass filter on raw signal, its parent is the sinus
     46  firstLowPass = new LowPassFilter(sinus, sinus->GetSetupLayout()->NewRow(),
     47                                   "1st order lawpass filter");
     48  sinus->GetPlot()->AddCurve(
     49      firstLowPass->Matrix()->Element(0),
     50      DataPlot::Blue); // add output of the filter to signal's graph
    4851
    49     //3rd order law pass filter on raw signal, its parent is the sinus
    50     thirdLowPass=new ButterworthLowPass(sinus,sinus->GetSetupLayout()->NewRow(),"3rd order lawpass filter",3);
    51     sinus->GetPlot()->AddCurve(thirdLowPass->Matrix()->Element(0),DataPlot::Yellow);//add output of the filter to signal's graph
     52  // 3rd order law pass filter on raw signal, its parent is the sinus
     53  thirdLowPass = new ButterworthLowPass(
     54      sinus, sinus->GetSetupLayout()->NewRow(), "3rd order lawpass filter", 3);
     55  sinus->GetPlot()->AddCurve(
     56      thirdLowPass->Matrix()->Element(0),
     57      DataPlot::Yellow); // add output of the filter to signal's graph
    5258
    53     //mean filter on raw signal, its parent is the sinus
    54     mean=new MeanFilter(sinus,sinus->GetSetupLayout()->NewRow(),"Mean filter");
    55     sinus->GetPlot()->AddCurve(mean->GetMatrix()->Element(0),DataPlot::Green);//add output of the filter to signal's graph
     59  // mean filter on raw signal, its parent is the sinus
     60  mean =
     61      new MeanFilter(sinus, sinus->GetSetupLayout()->NewRow(), "Mean filter");
     62  sinus->GetPlot()->AddCurve(
     63      mean->GetMatrix()->Element(0),
     64      DataPlot::Green); // add output of the filter to signal's graph
    5665
    57     //mean filter on 1st order law pass filter, its parent is the 1st order law pass filter
    58     meanOnfirstLowPass=new MeanFilter(firstLowPass,sinus->GetSetupLayout()->NewRow(),"Mean filter on 1st order lawpass filter");
    59     sinus->GetPlot()->AddCurve(meanOnfirstLowPass->GetMatrix()->Element(0),DataPlot::Black);//add output of the filter to signal's graph
     66  // mean filter on 1st order law pass filter, its parent is the 1st order law
     67  // pass filter
     68  meanOnfirstLowPass =
     69      new MeanFilter(firstLowPass, sinus->GetSetupLayout()->NewRow(),
     70                     "Mean filter on 1st order lawpass filter");
     71  sinus->GetPlot()->AddCurve(
     72      meanOnfirstLowPass->GetMatrix()->Element(0),
     73      DataPlot::Black); // add output of the filter to signal's graph
    6074
    61     //set ojects to be logged
    62     //as the law pass filters and the mean filters have the sinus as parent, they are automatically logged
    63     parent->AddDeviceToLog(sinus);
     75  // set ojects to be logged
     76  // as the law pass filters and the mean filters have the sinus as parent, they
     77  // are automatically logged
     78  parent->AddDeviceToLog(sinus);
    6479}
    6580
    66 Loop::~Loop() {
     81Loop::~Loop() {}
    6782
     83// main loop of the Thread
     84void Loop::Run(void) {
     85  // warn if changing from primary to secondary mode when in real time
     86  WarnUponSwitches(true);
     87
     88  sinus->Start();
     89
     90  SetPeriodMS(period->Value());
     91
     92  while (1) {
     93    WaitPeriod();
     94
     95    if (period->ValueChanged())
     96      SetPeriodMS(period->Value());
     97
     98    if (killButton->Clicked())
     99      break;
     100    if (startLogButton->Clicked())
     101      getFrameworkManager()->StartLog();
     102    if (stopLogButton->Clicked())
     103      getFrameworkManager()->StopLog();
     104
     105    // nothing more to do
     106    // this is a very simple example
     107
     108    // normaly, we should use results of filters to calculate a control law
     109  }
     110
     111  WarnUponSwitches(false);
    68112}
    69 
    70 //main loop of the Thread
    71 void Loop::Run(void) {
    72     //warn if changing from primary to secondary mode when in real time
    73     WarnUponSwitches(true);
    74 
    75     sinus->Start();
    76 
    77     SetPeriodMS(period->Value());
    78 
    79     while(1) {
    80         WaitPeriod();
    81 
    82         if(period->ValueChanged()) SetPeriodMS(period->Value());
    83 
    84         if(killButton->Clicked()) break;
    85         if(startLogButton->Clicked()) getFrameworkManager()->StartLog();
    86         if(stopLogButton->Clicked()) getFrameworkManager()->StopLog();
    87 
    88         //nothing more to do
    89         //this is a very simple example
    90 
    91         //normaly, we should use results of filters to calculate a control law
    92     }
    93 
    94     WarnUponSwitches(false);
    95 }
  • trunk/demos/Sinus/src/Loop.h

    r14 r16  
    1414#define LOOP_H
    1515
    16 //necessary include, as Thread is a base of our class
     16// necessary include, as Thread is a base of our class
    1717#include <Thread.h>
    1818
    19 //forward declaration for other classes
     19// forward declaration for other classes
    2020namespace flair {
    21     namespace core {
    22        class FrameworkManager;
    23     }
    24     namespace gui {
    25         class PushButton;
    26         class SpinBox;
    27     }
    28     namespace sensor {
    29         class Sinus;
    30     }
    31     namespace filter {
    32         class LowPassFilter;
    33         class ButterworthLowPass;
    34         class MeanFilter;
    35     }
     21namespace core {
     22class FrameworkManager;
     23}
     24namespace gui {
     25class PushButton;
     26class SpinBox;
     27}
     28namespace sensor {
     29class Sinus;
     30}
     31namespace filter {
     32class LowPassFilter;
     33class ButterworthLowPass;
     34class MeanFilter;
     35}
    3636}
    3737
     38class Loop : public flair::core::Thread {
     39public:
     40  /*!
     41  * \brief Constructor
     42  *
     43  * Builds main loop
     44  *
     45  * \param parent the FrameworkManager to use
     46  * \param name object name
     47  * \param priority Thread priority, 51 by default (1:mini, 99:maxi)
     48  */
     49  Loop(flair::core::FrameworkManager *parent, std::string name,
     50       int priority = 51);
    3851
    39 class Loop : public flair::core::Thread
    40 {
    41     public:
    42         /*!
    43         * \brief Constructor
    44         *
    45         * Builds main loop
    46         *
    47         * \param parent the FrameworkManager to use
    48         * \param name object name
    49         * \param priority Thread priority, 51 by default (1:mini, 99:maxi)
    50         */
    51         Loop(flair::core::FrameworkManager* parent,std::string name,int priority=51);
     52  /*!
     53  * \brief Destructor
     54  */
     55  ~Loop();
    5256
    53         /*!
    54         * \brief Destructor
    55         */
    56         ~Loop();
     57private:
     58  void Run(void);
    5759
    58     private:
    59         void Run(void);
    60 
    61         flair::sensor::Sinus *sinus;
    62         flair::filter::LowPassFilter *firstLowPass;
    63         flair::filter::ButterworthLowPass *thirdLowPass;
    64         flair::filter::MeanFilter *mean,*meanOnfirstLowPass;
    65         flair::gui::PushButton *startLogButton,*stopLogButton,*killButton;
    66         flair::gui::SpinBox *period;
     60  flair::sensor::Sinus *sinus;
     61  flair::filter::LowPassFilter *firstLowPass;
     62  flair::filter::ButterworthLowPass *thirdLowPass;
     63  flair::filter::MeanFilter *mean, *meanOnfirstLowPass;
     64  flair::gui::PushButton *startLogButton, *stopLogButton, *killButton;
     65  flair::gui::SpinBox *period;
    6766};
    6867
  • trunk/demos/Sinus/src/MeanFilter.cpp

    r14 r16  
    2222using namespace flair::gui;
    2323
    24 namespace flair { namespace filter {
     24namespace flair {
     25namespace filter {
    2526
    26 MeanFilter::MeanFilter(const IODevice* parent,const LayoutPosition* position,string name): IODevice(parent,name) {
    27     //interface initialisation
    28     groupBox=new GroupBox(position,name);
    29     numberOfElements=new SpinBox(groupBox->NewRow(),"numberOfElements:",1,MAX_NUMBER_OF_ELEMENTS,1);//saturated to MAX_NUMBER_OF_ELEMENTS
     27MeanFilter::MeanFilter(const IODevice *parent, const LayoutPosition *position,
     28                       string name)
     29    : IODevice(parent, name) {
     30  // interface initialisation
     31  groupBox = new GroupBox(position, name);
     32  numberOfElements = new SpinBox(groupBox->NewRow(), "numberOfElements:", 1,
     33                                 MAX_NUMBER_OF_ELEMENTS,
     34                                 1); // saturated to MAX_NUMBER_OF_ELEMENTS
    3035
    31     //init storage
    32     for(int i=0; i<MAX_NUMBER_OF_ELEMENTS; i++) previousValues[i]=0;
     36  // init storage
     37  for (int i = 0; i < MAX_NUMBER_OF_ELEMENTS; i++)
     38    previousValues[i] = 0;
    3339
    34     //1*1 output matrix
    35     cvmatrix_descriptor* desc=new cvmatrix_descriptor(1,1);
    36     desc->SetElementName(0,0,"mean filter");//name will be used for graphs and logs
    37     output=new cvmatrix(this,desc,floatType,name);
     40  // 1*1 output matrix
     41  cvmatrix_descriptor *desc = new cvmatrix_descriptor(1, 1);
     42  desc->SetElementName(0, 0,
     43                       "mean filter"); // name will be used for graphs and logs
     44  output = new cvmatrix(this, desc, floatType, name);
    3845
    39     AddDataToLog(output);
     46  AddDataToLog(output);
    4047}
    4148
    42 MeanFilter::~MeanFilter() {
    43 }
     49MeanFilter::~MeanFilter() {}
    4450
    45 cvmatrix *MeanFilter::GetMatrix() const {
    46     return output;
    47 }
     51cvmatrix *MeanFilter::GetMatrix() const { return output; }
    4852
    49 float MeanFilter::GetValue(void) const {
    50     return output->Value(0,0);
    51 }
     53float MeanFilter::GetValue(void) const { return output->Value(0, 0); }
    5254
    53 //UpdateFrom, where we implement the filter
    54 //this method is automatically called when the parent IODevice calls ProcessUpdate
    55 //in our case it is the sinus or the 1st orde law pass filter
     55// UpdateFrom, where we implement the filter
     56// this method is automatically called when the parent IODevice calls
     57// ProcessUpdate
     58// in our case it is the sinus or the 1st orde law pass filter
    5659//(see in Sinus::Run the call to ProcessUpdate)
    5760void MeanFilter::UpdateFrom(const io_data *data) {
    5861
    59     float result=0;
    60     //get input argument in a cvmatrix
    61     cvmatrix *input=(cvmatrix*)data;
     62  float result = 0;
     63  // get input argument in a cvmatrix
     64  cvmatrix *input = (cvmatrix *)data;
    6265
    63     //simple (and not efficent!) implementation of the filter
    64     previousValues[numberOfElements->Value()-1]=input->Value(0,0);
    65     for(int i=0; i<numberOfElements->Value(); i++) result+=previousValues[i];
    66     for(int i=1; i<numberOfElements->Value(); i++) previousValues[i-1]=previousValues[i];
     66  // simple (and not efficent!) implementation of the filter
     67  previousValues[numberOfElements->Value() - 1] = input->Value(0, 0);
     68  for (int i = 0; i < numberOfElements->Value(); i++)
     69    result += previousValues[i];
     70  for (int i = 1; i < numberOfElements->Value(); i++)
     71    previousValues[i - 1] = previousValues[i];
    6772
    68     //put the result in output matrix
    69     output->SetValue(0,0,result/numberOfElements->Value());
    70     //put corresponding time
    71     output->SetDataTime(data->DataTime());
     73  // put the result in output matrix
     74  output->SetValue(0, 0, result / numberOfElements->Value());
     75  // put corresponding time
     76  output->SetDataTime(data->DataTime());
    7277
    73     //ProcessUpdate is very important
    74     //we must call it after updating the output matrix
    75     //it allows:
    76     // -to save value in the logs
    77     // -to automatically call the next filter UpdateFrom method
    78     ProcessUpdate(output);
     78  // ProcessUpdate is very important
     79  // we must call it after updating the output matrix
     80  // it allows:
     81  // -to save value in the logs
     82  // -to automatically call the next filter UpdateFrom method
     83  ProcessUpdate(output);
    7984}
    80 }// end namespace filter
    81 }// end namespace flair
     85} // end namespace filter
     86} // end namespace flair
  • trunk/demos/Sinus/src/MeanFilter.h

    r14 r16  
    1515#define MEANFILTER_H
    1616
    17 //we fix the max number of elements to compute the mean
    18 //this is a simple example, it should be dynamical
     17// we fix the max number of elements to compute the mean
     18// this is a simple example, it should be dynamical
    1919#define MAX_NUMBER_OF_ELEMENTS 200
    2020
    21 //necessary include, as IODevice is a base of our class
     21// necessary include, as IODevice is a base of our class
    2222#include <IODevice.h>
    2323
    24 //forward declaration for other classes
     24// forward declaration for other classes
    2525namespace flair {
    26     namespace core {
    27         class cvmatrix;
    28     }
    29     namespace gui {
    30         class LayoutPosition;
    31         class GroupBox;
    32         class SpinBox;
    33     }
     26namespace core {
     27class cvmatrix;
     28}
     29namespace gui {
     30class LayoutPosition;
     31class GroupBox;
     32class SpinBox;
     33}
    3434}
    3535
    36 //MeanFilter is a class computing a mean
     36// MeanFilter is a class computing a mean
    3737// based on a parametrizable number of elements
    3838// it derives on IODevice as it as an input and an output
    3939// it is a filter, we extend the namespace
    40 namespace flair { namespace filter {
    41     class MeanFilter : public core::IODevice {
     40namespace flair {
     41namespace filter {
     42class MeanFilter : public core::IODevice {
    4243
    43         public:
    44             /*!
    45             * \brief Constructor
    46             *
    47             * Builds a mean filter. \n
    48             * After calling this function, position will be deleted as it is no longer usefull. \n
    49             *
    50             * \param parent IODevice to use as parent
    51             * \param position where to place settings
    52             * \param name name of the object
    53             */
    54             MeanFilter(const core::IODevice* parent,const gui::LayoutPosition* position,std::string name);
     44public:
     45  /*!
     46  * \brief Constructor
     47  *
     48  * Builds a mean filter. \n
     49  * After calling this function, position will be deleted as it is no longer
     50  *usefull. \n
     51  *
     52  * \param parent IODevice to use as parent
     53  * \param position where to place settings
     54  * \param name name of the object
     55  */
     56  MeanFilter(const core::IODevice *parent, const gui::LayoutPosition *position,
     57             std::string name);
    5558
    56             /*!
    57             * \brief Destructor
    58             */
    59             ~MeanFilter();
     59  /*!
     60  * \brief Destructor
     61  */
     62  ~MeanFilter();
    6063
    61             /*!
    62             * \brief Output matrix
    63             *
    64             * allows to access output matrix, to get signal value or to put it in a graph.  \n
    65             *
    66             * \return pointer to the output matrix
    67             */
    68             core::cvmatrix *GetMatrix(void) const;
     64  /*!
     65  * \brief Output matrix
     66  *
     67  * allows to access output matrix, to get signal value or to put it in a graph.
     68  *\n
     69  *
     70  * \return pointer to the output matrix
     71  */
     72  core::cvmatrix *GetMatrix(void) const;
    6973
    70             /*!
    71             * \brief Value
    72             *
    73             * this method is equivalent to GetMatrix()->Value(0,0)
    74             *
    75             * \return actual mean value
    76             */
    77             float GetValue(void) const;
     74  /*!
     75  * \brief Value
     76  *
     77  * this method is equivalent to GetMatrix()->Value(0,0)
     78  *
     79  * \return actual mean value
     80  */
     81  float GetValue(void) const;
    7882
    79         private:
    80             //UpdateFrom method from base class IODevice
    81             void UpdateFrom(const core::io_data *data);
    82             gui::GroupBox *groupBox;
    83             gui::SpinBox *numberOfElements;
    84             core::cvmatrix *output;
    85             float previousValues[MAX_NUMBER_OF_ELEMENTS];//previous values storage
    86 
    87     };
    88 }// end namespace filter
    89 }// end namespace flair
     83private:
     84  // UpdateFrom method from base class IODevice
     85  void UpdateFrom(const core::io_data *data);
     86  gui::GroupBox *groupBox;
     87  gui::SpinBox *numberOfElements;
     88  core::cvmatrix *output;
     89  float previousValues[MAX_NUMBER_OF_ELEMENTS]; // previous values storage
     90};
     91} // end namespace filter
     92} // end namespace flair
    9093#endif // MEANFILTER_H
  • trunk/demos/Sinus/src/Sinus.cpp

    r14 r16  
    3030using namespace flair::gui;
    3131
    32 namespace flair { namespace sensor {
     32namespace flair {
     33namespace sensor {
    3334
    34 Sinus::Sinus(const FrameworkManager* parent,string name,int priority) : IODevice(parent,name),Thread(parent,name,priority) {
     35Sinus::Sinus(const FrameworkManager *parent, string name, int priority)
     36    : IODevice(parent, name), Thread(parent, name, priority) {
    3537
    36     plot=NULL;
     38  plot = NULL;
    3739
    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);
     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);
    4345
     46  AddDataToLog(output);
    4447
    45     AddDataToLog(output);
     48  // interface initialisation
     49  mainTab = new Tab(parent->GetTabWidget(), name);
     50  tabWidget = new TabWidget(mainTab->NewRow(), name);
    4651
    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);
    5062
    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
    5966}
    60 
    6167
    6268Sinus::~Sinus() {
    6369
    64     SafeStop();
    65     Join();
     70  SafeStop();
     71  Join();
    6672
    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;
    7078}
    7179
    7280void 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);
    7684}
    7785
    78 GridLayout* Sinus::GetSetupLayout(void) const {
    79     return setupLayout;
     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;
    8092}
    8193
    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 }
     94cvmatrix *Sinus::GetMatrix(void) const { return output; }
    8695
    87 cvmatrix *Sinus::GetMatrix(void) const {
    88     return output;
    89 }
     96float Sinus::GetValue(void) const { return output->Value(0, 0); }
    9097
    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
    9699void Sinus::Run(void) {
    97100
    98     SetPeriodMS(period->Value());
     101  SetPeriodMS(period->Value());
    99102
    100     //warn if changing from primary to secondary mode when in real time
    101     WarnUponSwitches(true);
     103  // warn if changing from primary to secondary mode when in real time
     104  WarnUponSwitches(true);
    102105
    103     Time initTime=GetTime();
     106  Time initTime = GetTime();
    104107
    105     while(!ToBeStopped()) {
    106         WaitPeriod();
     108  while (!ToBeStopped()) {
     109    WaitPeriod();
    107110
    108         if(period->ValueChanged()==true) SetPeriodMS(period->Value());
     111    if (period->ValueChanged() == true)
     112      SetPeriodMS(period->Value());
    109113
    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
    113121
    114         //put the result in output matrix
    115         output->SetValue(0,0,value);
    116         //put corresponding time
    117         output->SetDataTime(actualTime);
     122    // put the result in output matrix
     123    output->SetValue(0, 0, value);
     124    // put corresponding time
     125    output->SetDataTime(actualTime);
    118126
    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     }
     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  }
    126134}
    127 }// end namespace sensor
    128 }// end namespace flair
     135} // end namespace sensor
     136} // end namespace flair
  • trunk/demos/Sinus/src/Sinus.h

    r14 r16  
    1515#define SINUS_H
    1616
    17 //necessary includes, as IODevice and Thread are bases of our class
     17// necessary includes, as IODevice and Thread are bases of our class
    1818#include <IODevice.h>
    1919#include <Thread.h>
    2020
    21 //forward declaration for other classes
     21// forward declaration for other classes
    2222namespace flair {
    23     namespace core {
    24         class FrameworkManager;
    25         class cvmatrix;
    26     }
    27     namespace gui {
    28         class Tab;
    29         class TabWidget;
    30         class GridLayout;
    31         class DoubleSpinBox;
    32         class SpinBox;
    33         class DataPlot1D;
    34     }
     23namespace core {
     24class FrameworkManager;
     25class cvmatrix;
     26}
     27namespace gui {
     28class Tab;
     29class TabWidget;
     30class GridLayout;
     31class DoubleSpinBox;
     32class SpinBox;
     33class DataPlot1D;
     34}
    3535}
    3636
    37 //sinus is a class generating a sinus signal
    38 //in this example, it emulates a sensonr, so we extend the namespace sensor
    39 //it derives frome
     37// sinus is a class generating a sinus signal
     38// in this example, it emulates a sensonr, so we extend the namespace sensor
     39// it derives frome
    4040// IODevice: as it has an ouput
    4141// Thread: is it a thread
    42 namespace flair { namespace sensor {
    43     class Sinus : public core::IODevice, public core::Thread {
    44         public:
    45             /*!
    46             * \brief Constructor
    47             *
    48             * Builds a sinus generator
    49             *
    50             * \param parent the FrameworkManager to use
    51             * \param name object name
    52             * \param priority Thread priority, 50 by default (1:mini, 99:maxi)
    53             */
    54             Sinus(const core::FrameworkManager* parent,std::string name,int priority=50);
     42namespace flair {
     43namespace sensor {
     44class Sinus : public core::IODevice, public core::Thread {
     45public:
     46  /*!
     47  * \brief Constructor
     48  *
     49  * Builds a sinus generator
     50  *
     51  * \param parent the FrameworkManager to use
     52  * \param name object name
     53  * \param priority Thread priority, 50 by default (1:mini, 99:maxi)
     54  */
     55  Sinus(const core::FrameworkManager *parent, std::string name,
     56        int priority = 50);
    5557
    56             /*!
    57             * \brief Destructor
    58             */
    59             ~Sinus();
     58  /*!
     59  * \brief Destructor
     60  */
     61  ~Sinus();
    6062
    61             /*!
    62             * \brief Output matrix
    63             *
    64             * allows to access output matrix, to get signal value or to put it in a graph.  \n
    65             *
    66             * \return un pointeur vers la matrice de sortie
    67             */
    68             core::cvmatrix *GetMatrix(void) const;
     63  /*!
     64  * \brief Output matrix
     65  *
     66  * allows to access output matrix, to get signal value or to put it in a graph.
     67  *\n
     68  *
     69  * \return un pointeur vers la matrice de sortie
     70  */
     71  core::cvmatrix *GetMatrix(void) const;
    6972
    70             /*!
    71             * \brief Value
    72             *
    73             * this method is equivalent to GetMatrix()->Value(0,0)
    74             *
    75             * \return actual signal value
    76             */
    77             float GetValue(void) const;
     73  /*!
     74  * \brief Value
     75  *
     76  * this method is equivalent to GetMatrix()->Value(0,0)
     77  *
     78  * \return actual signal value
     79  */
     80  float GetValue(void) const;
    7881
    79             /*!
    80             * \brief Use defautl plot
    81             *
    82             * this method put a graph in a specific tab
    83             *
    84             */
    85             void UseDefaultPlot(void);
     82  /*!
     83  * \brief Use defautl plot
     84  *
     85  * this method put a graph in a specific tab
     86  *
     87  */
     88  void UseDefaultPlot(void);
    8689
    87             /*!
    88             * \brief SetupLayout
    89             *
    90             * this method allows to add other widgets in the sinus tab.
    91             *
    92             * \return the GridLayout
    93             */
    94             gui::GridLayout *GetSetupLayout(void) const;
     90  /*!
     91  * \brief SetupLayout
     92  *
     93  * this method allows to add other widgets in the sinus tab.
     94  *
     95  * \return the GridLayout
     96  */
     97  gui::GridLayout *GetSetupLayout(void) const;
    9598
    96             /*!
    97             * \brief Plot
    98             *
    99             * this method allows to add other curves in the graph
    100             *
    101             * \return the DataPlot1D
    102             */
    103             gui::DataPlot1D *GetPlot(void) const;
     99  /*!
     100  * \brief Plot
     101  *
     102  * this method allows to add other curves in the graph
     103  *
     104  * \return the DataPlot1D
     105  */
     106  gui::DataPlot1D *GetPlot(void) const;
    104107
    105         private:
    106             //UpdateFrom method from base class IODevice
    107             //sinus is like a sensor, so it does not have input; we define an empty method
    108             void UpdateFrom(const core::io_data *data){};
    109             void Run(void);
     108private:
     109  // UpdateFrom method from base class IODevice
     110  // sinus is like a sensor, so it does not have input; we define an empty
     111  // method
     112  void UpdateFrom(const core::io_data *data){};
     113  void Run(void);
    110114
    111             core::cvmatrix *output;
    112             gui::Tab* mainTab;
    113             gui::TabWidget* tabWidget;
    114             gui::DataPlot1D* plot;
    115             gui::DoubleSpinBox *frequency,*amplitude,*offset;
    116             gui::SpinBox *period;
    117             gui::GridLayout* setupLayout;
    118     };
    119 }// end namespace sensor
    120 }// end namespace flair
     115  core::cvmatrix *output;
     116  gui::Tab *mainTab;
     117  gui::TabWidget *tabWidget;
     118  gui::DataPlot1D *plot;
     119  gui::DoubleSpinBox *frequency, *amplitude, *offset;
     120  gui::SpinBox *period;
     121  gui::GridLayout *setupLayout;
     122};
     123} // end namespace sensor
     124} // end namespace flair
    121125#endif // SINUS_H
  • trunk/demos/Sinus/src/main.cpp

    r14 r16  
    1616#include <stdio.h>
    1717
    18 //we use namespace std, to avoid writing for examlpe std::string
     18// we use namespace std, to avoid writing for examlpe std::string
    1919using namespace std;
    20 //idem for TCLAP
     20// idem for TCLAP
    2121using namespace TCLAP;
    22 //idem for flair
     22// idem for flair
    2323using namespace flair::core;
    2424
    25 //global variables for the command line arguments
    26 string logPath;//path for logs
    27 string address;//address for FlairGCS
    28 int port;//port for FlairGCS
    29 string xmlFile;//setup xml file
    30 string name;//name
     25// global variables for the command line arguments
     26string logPath; // path for logs
     27string address; // address for FlairGCS
     28int port; // port for FlairGCS
     29string xmlFile; // setup xml file
     30string name; // name
    3131
    32 void parseOptions(int argc, char** argv);
     32void parseOptions(int argc, char **argv);
    3333
    34 int main(int argc, char* argv[]) {
    35     //get command line arguments (name,port,xml_file,log_path)
    36     parseOptions(argc,argv);
     34int main(int argc, char *argv[]) {
     35  // get command line arguments (name,port,xml_file,log_path)
     36  parseOptions(argc, argv);
    3737
    38     //creation of FrameworkManager
    39     FrameworkManager *manager;
    40     manager= new FrameworkManager(name);
    41     manager->SetupConnection(address,port);
    42     manager->SetupUserInterface(xmlFile);
    43     manager->SetupLogger(logPath);
     38  // creation of FrameworkManager
     39  FrameworkManager *manager;
     40  manager = new FrameworkManager(name);
     41  manager->SetupConnection(address, port);
     42  manager->SetupUserInterface(xmlFile);
     43  manager->SetupLogger(logPath);
    4444
    45     //creation of the
    46     Loop* example=new Loop(manager,"Loop");
     45  // creation of the
     46  Loop *example = new Loop(manager, "Loop");
    4747
    48     //start loop
    49     example->Start();
     48  // start loop
     49  example->Start();
    5050
    51     //wait for loop ends (with kill button)
    52     example->Join();
     51  // wait for loop ends (with kill button)
     52  example->Join();
    5353
    54     delete manager;
     54  delete manager;
    5555}
    5656
     57void parseOptions(int argc, char **argv) {
     58  try {
     59    CmdLine cmd("Command description message", ' ', "0.1");
    5760
    58 void parseOptions(int argc, char** argv) {
    59     try {
    60         CmdLine cmd("Command description message", ' ', "0.1");
     61    // name of the FrameworkManager, Sinus by default
     62    ValueArg<string> nameArg("n", "name", "program name", false, "Sinus",
     63                             "string");
     64    cmd.add(nameArg);
    6165
    62         //name of the FrameworkManager, Sinus by default
    63         ValueArg<string> nameArg("n","name","program name",false,"Sinus","string");
    64         cmd.add( nameArg );
     66    // setup xml file, ./Sinus.xml by default
     67    ValueArg<string> xmlArg("x", "xml", "xml file", false, "./Sinus.xml",
     68                            "string");
     69    cmd.add(xmlArg);
    6570
    66         //setup xml file, ./Sinus.xml by default
    67         ValueArg<string> xmlArg("x","xml","xml file",false,"./Sinus.xml","string");
    68         cmd.add( xmlArg );
     71    // log path, ./ by defaults
     72    ValueArg<string> logArg("l", "log", "log path", false, "/tmp", "string");
     73    cmd.add(logArg);
    6974
    70         //log path, ./ by defaults
    71         ValueArg<string> logArg("l","log","log path",false,"/tmp","string");
    72         cmd.add( logArg );
     75    // address for FlairGCS
     76    ValueArg<string> addressArg("a", "address",
     77                                "address for FlairGCS station sol", false,
     78                                "127.0.0.1", "string");
     79    cmd.add(addressArg);
    7380
    74         //address for FlairGCS
    75         ValueArg<string> addressArg("a","address","address for FlairGCS station sol",false,"127.0.0.1","string");
    76         cmd.add( addressArg );
     81    // port for FlairGCS, 9000 by default
     82    ValueArg<int> portArg("p", "port", "port for FlairGCS", false, 9000, "int");
     83    cmd.add(portArg);
    7784
    78         //port for FlairGCS, 9000 by default
    79         ValueArg<int> portArg("p","port","port for FlairGCS",false,9000,"int");
    80         cmd.add( portArg );
     85    cmd.parse(argc, argv);
    8186
    82         cmd.parse( argc, argv );
     87    // Get the value parsed by each arg.
     88    logPath = logArg.getValue();
     89    port = portArg.getValue();
     90    xmlFile = xmlArg.getValue();
     91    name = nameArg.getValue();
     92    address = addressArg.getValue();
    8393
    84         // Get the value parsed by each arg.
    85         logPath = logArg.getValue();
    86         port=portArg.getValue();
    87         xmlFile = xmlArg.getValue();
    88         name=nameArg.getValue();
    89         address=addressArg.getValue();
    90 
    91     } catch (ArgException &e) { // catch any exceptions
    92         cerr << "error: " << e.error() << " for arg " << e.argId() << endl;
    93     }
     94  } catch (ArgException &e) { // catch any exceptions
     95    cerr << "error: " << e.error() << " for arg " << e.argId() << endl;
     96  }
    9497}
Note: See TracChangeset for help on using the changeset viewer.