datastream_sample.cpp
Go to the documentation of this file.
1 #include "datastream_sample.h"
2 #include <QTextStream>
3 #include <QFile>
4 #include <QMessageBox>
5 #include <QDebug>
6 #include <thread>
7 #include <mutex>
8 #include <chrono>
9 #include <thread>
10 #include <math.h>
11 
13 {
14  QStringList words_list;
15  words_list << "siam" << "tre" << "piccoli" << "porcellin"
16  << "mai" << "nessun" << "ci" << "dividera" << "_sin" << "_cos";
17 
18  int N = words_list.size();
19 
20  foreach( const QString& name, words_list)
21  {
23  if(name == "_sin"){
24  param.A = 2;
25  param.B = 1;
26  param.C = 0;
27  param.D = 0;
28  }
29  else if (name == "_cos"){
30  param.A = 2;
31  param.B = 1;
32  param.C = 1.5708;
33  param.D = 0;
34  }
35  else{
36  param.A = qrand()/(double)RAND_MAX * 6 - 3;
37  param.B = qrand()/(double)RAND_MAX *3;
38  param.C = qrand()/(double)RAND_MAX *3;
39  param.D = qrand()/(double)RAND_MAX *2 -1;
40  }
41 
42  const std::string name_str = name.toStdString();
43 
44  dataMap().addNumeric(name_str);
45  _parameters.insert( std::make_pair( name_str, param) );
46  }
47  dataMap().addNumeric("empty");
48 }
49 
50 bool DataStreamSample::start(QStringList*)
51 {
52  _running = true;
54  _thread = std::thread([this](){ this->loop();} );
55  return true;
56 }
57 
59 {
60  _running = false;
61  if( _thread.joinable()) _thread.join();
62 }
63 
64 bool DataStreamSample::isRunning() const { return _running; }
65 
67 {
68  shutdown();
69 }
70 
71 bool DataStreamSample::xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const
72 {
73  return true;
74 }
75 
76 bool DataStreamSample::xmlLoadState(const QDomElement &parent_element)
77 {
78  return true;
79 }
80 
82 {
83  std::lock_guard<std::mutex> lock( mutex() );
84 
85  using namespace std::chrono;
86  static std::chrono::high_resolution_clock::time_point initial_time = high_resolution_clock::now();
87  const double offset = duration_cast< duration<double>>( initial_time.time_since_epoch() ).count() ;
88 
89  auto now = high_resolution_clock::now();
90  for (auto& it: dataMap().numeric )
91  {
92  if( it.first == "empty") continue;
93  auto par = _parameters[it.first];
94 
95  auto& plot = it.second;
96  const double t = duration_cast< duration<double>>( now - initial_time ).count() ;
97  double y = par.A*sin(par.B*t + par.C) + par.D*t*0.05;
98 
99  plot.pushBack( PlotData::Point( t + offset, y ) );
100  }
101 }
102 
104 {
105  _running = true;
106  while( _running )
107  {
108  pushSingleCycle();
109  std::this_thread::sleep_for ( std::chrono::milliseconds(10) );
110  }
111 }
std::map< std::string, Parameters > _parameters
virtual bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
bool param(const std::string &param_name, T &param_val, const T &default_val)
PlotDataMapRef & dataMap()
std::mutex & mutex()
virtual bool start(QStringList *) override
TFSIMD_FORCE_INLINE const tfScalar & y() const
virtual const char * name() const override
virtual void shutdown() override
virtual bool isRunning() const override
virtual bool xmlLoadState(const QDomElement &parent_element) override
std::unordered_map< std::string, PlotData >::iterator addNumeric(const std::string &name)
Definition: plotdata.h:147


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17