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  _dummy_notification = new QAction(this);
17 
18  connect(_dummy_notification, &QAction::triggered, this, [this]() {
19  QMessageBox::warning(nullptr, "Dummy Notifications",
20  QString("%1 notifications").arg(_notifications_count),
21  QMessageBox::Ok);
22 
23  if (_notifications_count > 0)
24  {
25  _notifications_count = 0;
26  emit notificationsChanged(_notifications_count);
27  }
28  });
29 
30  _notifications_count = 0;
31  for (int i = 0; i < 150; i++)
32  {
33  auto str = QString("data_vect/%1").arg(i).toStdString();
35  param.A = 6 * ((double)rand() / (double)RAND_MAX) - 3;
36  param.B = 3 * ((double)rand() / (double)RAND_MAX);
37  param.C = 3 * ((double)rand() / (double)RAND_MAX);
38  param.D = 20 * ((double)rand() / (double)RAND_MAX);
39  _parameters.insert({ str, param });
40  auto& plotdata = dataMap().addNumeric(str)->second;
41  }
42  //------------
43  dataMap().addStringSeries("color");
44 
45  //------------
46  auto tcGroup = std::make_shared<PJ::PlotGroup>("tc");
47  tcGroup->setAttribute(TEXT_COLOR, QColor(Qt::blue));
48 
49  auto& tc_default = dataMap().addNumeric("tc/default")->second;
50  auto& tc_red = dataMap().addNumeric("tc/red")->second;
51 
52  tc_red.setAttribute(TEXT_COLOR, QColor(Qt::red));
53 }
54 
55 bool DataStreamSample::start(QStringList*)
56 {
57  _running = true;
58  pushSingleCycle();
59  _thread = std::thread([this]() { this->loop(); });
60  return true;
61 }
62 
64 {
65  _running = false;
66  if (_thread.joinable())
67  {
68  _thread.join();
69  }
70 }
71 
73 {
74  return _running;
75 }
76 
78 {
79  shutdown();
80 }
81 
82 bool DataStreamSample::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
83 {
84  return true;
85 }
86 
87 bool DataStreamSample::xmlLoadState(const QDomElement& parent_element)
88 {
89  return true;
90 }
91 
93 {
94  static int count = 0;
95  std::lock_guard<std::mutex> lock(mutex());
96 
97  using namespace std::chrono;
98  static auto initial_time = high_resolution_clock::now();
99  const double offset =
100  duration_cast<duration<double>>(initial_time.time_since_epoch()).count();
101 
102  auto now = high_resolution_clock::now();
103  std::string colors[] = { "RED", "BLUE", "GREEN" };
104 
105  const double stamp =
106  duration_cast<duration<double>>(now - initial_time).count() + offset;
107 
108  for (auto& it : _parameters)
109  {
110  auto& plot = dataMap().numeric.find(it.first)->second;
111  const DataStreamSample::Parameters& param = it.second;
112 
113  double val = param.A * sin(param.B * stamp + param.C) + param.D;
114  plot.pushBack(PlotData::Point(stamp, val));
115  }
116 
117  auto& col_series = dataMap().strings.find("color")->second;
118  col_series.pushBack({ stamp, colors[(count / 10) % 3] });
119 
120  auto& tc_default = dataMap().numeric.find("tc/default")->second;
121  tc_default.pushBack({ stamp, double(count) });
122 
123  auto& tc_red = dataMap().numeric.find("tc/red")->second;
124  tc_red.pushBack({ stamp, double(count) });
125 
126  count++;
127 }
128 
130 {
131  _running = true;
132  size_t count = 1;
133  while (_running)
134  {
135  auto prev = std::chrono::high_resolution_clock::now();
136  pushSingleCycle();
137  emit dataReceived();
138  if (count++ % 200 == 0)
139  {
140  _notifications_count++;
141  emit notificationsChanged(_notifications_count);
142  }
143  std::this_thread::sleep_until(prev + std::chrono::milliseconds(20)); // 50 Hz
144  }
145 }
DataStreamSample::Parameters
Definition: datastream_sample.h:45
DataStreamSample::isRunning
virtual bool isRunning() const override
isRunning
Definition: datastream_sample.cpp:72
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
datastream_sample.h
DataStreamSample::DataStreamSample
DataStreamSample()
Definition: datastream_sample.cpp:14
DataStreamSample::xmlSaveState
virtual bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
Definition: datastream_sample.cpp:82
shutdown
ROSCONSOLE_DECL void shutdown()
DataStreamSample::pushSingleCycle
void pushSingleCycle()
Definition: datastream_sample.cpp:92
DataStreamSample::~DataStreamSample
virtual ~DataStreamSample() override
Definition: datastream_sample.cpp:77
PJ::TimeseriesBase::Point
typename PlotDataBase< double, Value >::Point Point
Definition: timeseries.h:23
PJ::TEXT_COLOR
@ TEXT_COLOR
Definition: plotdatabase.h:43
DataStreamSample::start
virtual bool start(QStringList *) override
start streaming.
Definition: datastream_sample.cpp:55
DataStreamSample::shutdown
virtual void shutdown() override
shutdown Stop streaming
Definition: datastream_sample.cpp:63
PJ
Definition: dataloader_base.h:16
param
T param(const std::string &param_name, const T &default_val)
DataStreamSample::xmlLoadState
virtual bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
Definition: datastream_sample.cpp:87
DataStreamSample::loop
void loop()
Definition: datastream_sample.cpp:129


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44