CurveDataSequencer.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
20 
22 
23 namespace rqt_multiplot {
24 
25 /*****************************************************************************/
26 /* Constructors and Destructor */
27 /*****************************************************************************/
28 
30  QObject(parent),
31  config_(0),
32  broker_(0) {
33 }
34 
36 }
37 
38 /*****************************************************************************/
39 /* Accessors */
40 /*****************************************************************************/
41 
43  if (config != config_) {
44  bool wasSubscribed = isSubscribed();
45 
46  if (config_) {
48  SIGNAL(changed()), this, SLOT(configAxisConfigChanged()));
50  SIGNAL(changed()), this, SLOT(configAxisConfigChanged()));
51  disconnect(config_, SIGNAL(subscriberQueueSizeChanged(size_t)),
52  this, SLOT(configSubscriberQueueSizeChanged(size_t)));
53 
54  unsubscribe();
55  }
56 
57  config_ = config;
58 
59  if (config) {
60  connect(config->getAxisConfig(CurveConfig::X),
61  SIGNAL(changed()), this, SLOT(configAxisConfigChanged()));
62  connect(config->getAxisConfig(CurveConfig::Y),
63  SIGNAL(changed()), this, SLOT(configAxisConfigChanged()));
64  connect(config, SIGNAL(subscriberQueueSizeChanged(size_t)),
65  this, SLOT(configSubscriberQueueSizeChanged(size_t)));
66 
67  if (wasSubscribed)
68  subscribe();
69  }
70  }
71 }
72 
74  return config_;
75 }
76 
78  if (broker != broker_) {
79  bool wasSubscribed = isSubscribed();
80 
81  if (broker_)
82  unsubscribe();
83 
84  broker_ = broker;
85 
86  if (broker && wasSubscribed)
87  subscribe();
88  }
89 }
90 
92  return broker_;
93 }
94 
96  return !subscribedTopics_.isEmpty();
97 }
98 
99 /*****************************************************************************/
100 /* Methods */
101 /*****************************************************************************/
102 
104  if (isSubscribed())
105  unsubscribe();
106 
107  if (config_ && broker_) {
110 
111  if (xAxisConfig->getTopic() == yAxisConfig->getTopic()) {
112  QString topic = xAxisConfig->getTopic();
113 
114  MessageBroker::PropertyMap properties;
115  properties[MessageSubscriber::QueueSize] = QVariant::
116  fromValue<qulonglong>(config_->getSubscriberQueueSize());
117 
118  if (broker_->subscribe(topic, this,
119  SLOT(subscriberMessageReceived(const QString&,
120  const Message&)), properties)) {
123  }
124  }
125  else {
126  QString xTopic = xAxisConfig->getTopic();
127  QString yTopic = yAxisConfig->getTopic();
128 
129  MessageBroker::PropertyMap properties;
130  properties[MessageSubscriber::QueueSize] = QVariant::
131  fromValue<qulonglong>(config_->getSubscriberQueueSize());
132 
133  if (broker_->subscribe(xTopic, this,
134  SLOT(subscriberXAxisMessageReceived(const QString&,
135  const Message&)), properties))
137 
138  if (broker_->subscribe(yTopic, this,
139  SLOT(subscriberYAxisMessageReceived(const QString&,
140  const Message&)), properties))
142  }
143  }
144 
145  if (!subscribedTopics_.isEmpty())
146  emit subscribed();
147 }
148 
150  if (isSubscribed()) {
151  for (QMap<CurveConfig::Axis, QString>::iterator it = subscribedTopics_.
152  begin(); it != subscribedTopics_.end(); ++it)
153  broker_->unsubscribe(it.value(), this);
154 
155  subscribedTopics_.clear();
156  timeFields_.clear();
157  timeValues_.clear();
158 
159  emit unsubscribed();
160  }
161 }
162 
164  if (!config_)
165  return;
166 
169 
170  QPointF point;
171 
172  if (xAxisConfig->getFieldType() == CurveAxisConfig::MessageData) {
173  variant_topic_tools::BuiltinVariant variant = message.getVariant().
174  getMember(xAxisConfig->getField().toStdString());
175 
176  point.setX(variant.getNumericValue());
177  }
178  else
179  point.setX(message.getReceiptTime().toSec());
180 
181  if (yAxisConfig->getFieldType() == CurveAxisConfig::MessageData) {
182  variant_topic_tools::BuiltinVariant variant = message.getVariant().
183  getMember(yAxisConfig->getField().toStdString());
184 
185  point.setY(variant.getNumericValue());
186  }
187  else
188  point.setY(message.getReceiptTime().toSec());
189 
190  emit pointReceived(point);
191 }
192 
194  Message& message) {
195  if (!config_)
196  return;
197 
198  CurveAxisConfig* axisConfig = config_->getAxisConfig(axis);
199 
200  if (axisConfig) {
201  if (!timeFields_.contains(axis)) {
202  timeFields_[axis] = QString();
203 
204  if (axisConfig->getFieldType() == CurveAxisConfig::MessageData) {
205  QStringList fieldParts = axisConfig->getField().split("/");
206 
207  while (!fieldParts.isEmpty()) {
208  fieldParts.removeLast();
209 
210  QString parentField = fieldParts.join("/");
211  variant_topic_tools::MessageVariant variant;
212 
213  if (!parentField.isEmpty())
214  variant = message.getVariant().getMember(fieldParts.join("/").
215  toStdString());
216  else
217  variant = message.getVariant();
218 
219  variant_topic_tools::MessageDataType type = variant.getType();
220 
221  if (type.hasHeader()) {
222  timeFields_[axis] = parentField+"/header/stamp";
223  break;
224  }
225  }
226  }
227  }
228 
229  TimeValue timeValue;
230 
231  if (!timeFields_[axis].isEmpty()) {
232  variant_topic_tools::BuiltinVariant variant = message.getVariant().
233  getMember(timeFields_[axis].toStdString());
234 
235  timeValue.time_ = variant.getValue<ros::Time>();
236  }
237  else
238  timeValue.time_ = message.getReceiptTime();
239 
240  if (axisConfig->getFieldType() == CurveAxisConfig::MessageData) {
241  variant_topic_tools::BuiltinVariant variant = message.getVariant().
242  getMember(axisConfig->getField().toStdString());
243 
244  timeValue.value_ = variant.getNumericValue();
245  }
246  else
247  timeValue.value_ = message.getReceiptTime().toSec();
248 
249  if (timeValues_[axis].isEmpty() ||
250  (timeValue.time_ > timeValues_[axis].last().time_))
251  timeValues_[axis].append(timeValue);
252  }
253 
254  interpolate();
255 }
256 
258  TimeValueList& timeValuesX = timeValues_[CurveConfig::X];
259  TimeValueList& timeValuesY = timeValues_[CurveConfig::Y];
260 
261  while ((timeValuesX.count() > 1) && (timeValuesY.count() > 1)) {
262  while ((timeValuesX.count() > 1) &&
263  ((++timeValuesX.begin())->time_ < timeValuesY.front().time_))
264  timeValuesX.removeFirst();
265 
266  while ((timeValuesY.count() > 1) &&
267  ((++timeValuesY.begin())->time_ < timeValuesX.front().time_))
268  timeValuesY.removeFirst();
269 
270  if ((timeValuesY.front().time_ >= timeValuesX.front().time_) &&
271  (timeValuesX.count() > 1)) {
272  QPointF point;
273 
274  const TimeValue& firstX = timeValuesX.first();
275  const TimeValue& secondX = *(++timeValuesX.begin());
276 
277  point.setX(firstX.value_+(secondX.value_-firstX.value_)*
278  (timeValuesY.front().time_-firstX.time_).toSec()/
279  (secondX.time_-firstX.time_).toSec());
280  point.setY(timeValuesY.front().value_);
281 
282  timeValuesY.removeFirst();
283 
284  emit pointReceived(point);
285  }
286  else if ((timeValuesX.front().time_ >= timeValuesY.front().time_) &&
287  (timeValuesY.count() > 1)) {
288  QPointF point;
289 
290  const TimeValue& firstY = timeValuesY.first();
291  const TimeValue& secondY = *(++timeValuesY.begin());
292 
293  point.setX(timeValuesX.front().value_);
294  point.setY(firstY.value_+(secondY.value_-firstY.value_)*
295  (timeValuesX.front().time_-firstY.time_).toSec()/
296  (secondY.time_-firstY.time_).toSec());
297 
298  timeValuesX.removeFirst();
299 
300  emit pointReceived(point);
301  }
302  }
303 }
304 
305 /*****************************************************************************/
306 /* Slots */
307 /*****************************************************************************/
308 
310  if (isSubscribed()) {
311  unsubscribe();
312  subscribe();
313  }
314 }
315 
317  if (isSubscribed()) {
318  unsubscribe();
319  subscribe();
320  }
321 }
322 
324  const Message& message) {
325  processMessage(message);
326 }
327 
329  const Message& message) {
330  processMessage(CurveConfig::X, message);
331 }
332 
334  const Message& message) {
335  processMessage(CurveConfig::Y, message);
336 }
337 
338 }
const QString & getTopic() const
const variant_topic_tools::MessageVariant & getVariant() const
Definition: Message.cpp:54
void pointReceived(const QPointF &point)
MessageBroker * getBroker() const
size_t getSubscriberQueueSize() const
void configSubscriberQueueSizeChanged(size_t queueSize)
void subscriberMessageReceived(const QString &topic, const Message &message)
CurveAxisConfig * getAxisConfig(Axis axis) const
Definition: CurveConfig.cpp:73
QMap< CurveConfig::Axis, QString > timeFields_
QMap< int, QVariant > PropertyMap
Definition: MessageBroker.h:33
void subscriberXAxisMessageReceived(const QString &topic, const Message &message)
void subscriberYAxisMessageReceived(const QString &topic, const Message &message)
virtual bool subscribe(const QString &topic, QObject *receiver, const char *method, const PropertyMap &properties=PropertyMap(), Qt::ConnectionType type=Qt::AutoConnection)=0
void setConfig(CurveConfig *config)
const QString & getField() const
void processMessage(const Message &message)
QMap< CurveConfig::Axis, QString > subscribedTopics_
QMap< CurveConfig::Axis, TimeValueList > timeValues_
virtual bool unsubscribe(const QString &topic, QObject *receiver, const char *method=0)=0
void setBroker(MessageBroker *broker)
QLinkedList< TimeValue > TimeValueList
const ros::Time & getReceiptTime() const
Definition: Message.cpp:46


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44