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 
12 using namespace PJ;
13 
15 {
16  for (int i = 0; i < 150; i++)
17  {
18  auto str = QString("data_vect/%1").arg(i).toStdString();
20  param.A = 6 * ((double)qrand() / (double)RAND_MAX) - 3;
21  param.B = 3 * ((double)qrand() / (double)RAND_MAX);
22  param.C = 3 * ((double)qrand() / (double)RAND_MAX);
23  param.D = 20 * ((double)qrand() / (double)RAND_MAX);
24  _parameters.insert({str, param});
25  dataMap().addNumeric(str);
26  }
27 }
28 
29 bool DataStreamSample::start(QStringList*)
30 {
31  _running = true;
32  pushSingleCycle();
33  _thread = std::thread([this]() { this->loop(); });
34  return true;
35 }
36 
38 {
39  _running = false;
40  if (_thread.joinable())
41  _thread.join();
42 }
43 
45 {
46  return _running;
47 }
48 
50 {
51  shutdown();
52 }
53 
54 bool DataStreamSample::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
55 {
56  return true;
57 }
58 
59 bool DataStreamSample::xmlLoadState(const QDomElement& parent_element)
60 {
61  return true;
62 }
63 
65 {
66  std::lock_guard<std::mutex> lock(mutex());
67 
68  using namespace std::chrono;
69  static auto initial_time = high_resolution_clock::now();
70  const double offset = duration_cast<duration<double>>(initial_time.time_since_epoch()).count();
71 
72  auto now = high_resolution_clock::now();
73  for (auto& it : dataMap().numeric)
74  {
75  auto& plot = it.second;
76  const double t = duration_cast<duration<double>>(now - initial_time).count();
77 
78  const DataStreamSample::Parameters& param = _parameters[it.first];
79  double val = param.A*sin( param.B*t + param.C ) + param.D;
80  plot.pushBack(PlotData::Point(t + offset, val));
81  }
82 }
83 
85 {
86  _running = true;
87  while (_running)
88  {
89  auto prev = std::chrono::high_resolution_clock::now();
90  pushSingleCycle();
91  emit dataReceived();
92  std::this_thread::sleep_until(prev + std::chrono::milliseconds(20)); // 50 Hz
93  }
94 }
virtual bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
virtual ~DataStreamSample() override
virtual bool start(QStringList *) override
virtual void shutdown() override
virtual bool isRunning() const override
virtual bool xmlLoadState(const QDomElement &parent_element) override
typename PlotDataBase< Value >::Point Point
Definition: plotdata.h:290


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:34