Privacy
An open-source, flexible 3D physical simulation framework
DataBrokerPlotter.cpp
Go to the documentation of this file.
1 #include "DataBrokerPlotter.h"
2 #include "DataBrokerPlotterLib.h"
3 
4 #include<QVBoxLayout>
5 
6 namespace data_broker_plotter {
7 
11  std::string _name, QWidget *parent) :
12  mars::main_gui::BaseWidget(parent, cfg, _name),
13  dataBroker(_dataBroker), mainLib(_mainLib),
14  name(_name), nextPlotId(1) {
15 
16  setStyleSheet("background-color:#eeeeee;");
17 
18  colors[0] = QColor(255, 0, 0);
19  colors[1] = QColor(0, 255, 0);
20  colors[2] = QColor(0, 0, 255);
21  colors[3] = QColor(255, 155, 0);
22  colors[4] = QColor(0, 255, 255);
23  colors[5] = QColor(255, 0, 255);
24  colors[6] = QColor(127, 0, 255);
25  colors[7] = QColor(255, 0, 127);
26 
27  qcPlot = new QCustomPlot;
28 
29  qcPlot->xAxis2->setVisible(true);
30  qcPlot->xAxis2->setTickLabels(false);
31  qcPlot->yAxis2->setVisible(true);
32  qcPlot->yAxis2->setTickLabels(false);
33  connect(qcPlot->xAxis, SIGNAL(rangeChanged(QCPRange)),
34  qcPlot->xAxis2, SLOT(setRange(QCPRange)));
35  connect(qcPlot->yAxis, SIGNAL(rangeChanged(QCPRange)),
36  qcPlot->yAxis2, SLOT(setRange(QCPRange)));
37  //qcPlot->setInteraction(QCustomPlot::iSelectPlottables);
38 
39  QVBoxLayout *vLayout = new QVBoxLayout();
40  vLayout->addWidget(qcPlot);
41  setLayout(vLayout);
42  dataLock.lock(),
43  createNewPlot();
44  dataLock.unlock();
45  }
46 
48  dataLock.lock();
49  dataBroker->unregisterSyncReceiver(this, "*", "*");
50 
51  delete qcPlot;
52  }
53 
55  std::vector<Plot*>::iterator it;
56  Plot *p;
57  double x;
58  int ix;
59  double xmin;
60  double sTime, xRange;
61 
62  // first handle panding dataPackages
63  dataLock.lock();
64  while(!packageList.empty()) {
65  mars::data_broker::DataPackage &dataPackage = packageList.front().dp;
66  int callbackParam = packageList.front().callbackParam;
67  for(it=plots.begin(); it!=plots.end(); ++it) {
68  if((*it)->dpId == callbackParam/10) {
69  p = *it;
70 
71  if(dataPackage[0].type == mars::data_broker::DOUBLE_TYPE)
72  dataPackage.get(0, &x);
73  else if(dataPackage[0].type == mars::data_broker::INT_TYPE) {
74  dataPackage.get(0, &ix);
75  x = (double)ix;
76  }
77 
78  if(callbackParam % 10) {
79  x = x*p->yScale.dValue+p->yOffset.dValue;
80  p->yValues.push_back(x);
81  p->gotNewData |= 1;
82  }
83  else {
84  p->xValues.push_back(x);
85  if((xRange = fabs(p->xRange.dValue)) < 0.000001)
86  xRange = fabs(plots[0]->xRange.dValue);
87  if(xRange > 0.0000001) {
88  xmin = x-xRange;
89  while(!p->xValues.empty() && p->xValues.front() < xmin) {
90  p->xValues.pop_front();
91  p->yValues.pop_front();
92  }
93  }
94  p->gotNewData |= 2;
95  }
96  /*
97  if((sTime = fabs(p->sTime.dValue)) < 0.000001) {
98  sTime = fabs(plots[0]->sTime.dValue);
99  }
100  if(sTime > 0.00000001) {
101  if(x-p->xValues.back() < sTime) {
102  dataLock.unlock();
103  return;
104  }
105  }
106  */
107 
108 
109  if(!p->gotData) {
110  p->gotData = true;
111  createNewPlot();
112  }
113 
114  break;
115  }
116  }
117  packageList.pop_front();
118  }
119 
120  bool onlyEnlarge = false;
121  for(it=plots.begin(); it!=plots.end(); ++it) {
122  if((*it)->gotNewData == 3) {
123  (*it)->curve->setData((*it)->xValues, (*it)->yValues);
124  (*it)->curve->rescaleAxes(onlyEnlarge);
125  onlyEnlarge = true;
126  (*it)->gotNewData = 0;
127  }
128  }
129  qcPlot->replot();
130  dataLock.unlock();
131  }
132 
134  const mars::data_broker::DataPackage &dataPackage,
135  int callbackParam) {
136 
137  dataLock.lock();
138 
139  packageList.push_back((PackageData){dataPackage, callbackParam});
140 
141  dataLock.unlock();
142  }
143 
145  char text[50];
146  // create first curve
147  std::string cfgName = name;
148  std::string tmpString;
149  Plot *newPlot = new Plot;
150  QCPGraph *newCurve = qcPlot->addGraph();
151 
152  newCurve->setAntialiasedFill(false);
153 
154  newPlot->dpId = nextPlotId++;
155  sprintf(text, "graph%02d", newPlot->dpId);
156  newPlot->name = std::string(text);
157 
158  cfgName.append("/");
159  cfgName.append(newPlot->name);
160  cfgName.append("/");
161 
162  newCurve->setPen( QPen(colors[(newPlot->dpId-1) % 8], 0.5) );
163  newCurve->setLineStyle( QCPGraph::lsLine );
164 
165  newPlot->curve = newCurve;
166 
167  plots.push_back(newPlot);
168 
169  // add datapackage for first plot
170  mars::data_broker::DataPackage dbPackageX;// = new mars::data_broker::DataPackage;
171  mars::data_broker::DataPackage dbPackageY;// = new mars::data_broker::DataPackage;
172  dbPackageX.add("xvalue", (double)0.0);
173  dbPackageY.add("yvalue", (double)0.0);
174  //dbPackage->add("yvalue", (double)0.0);
175 
176  tmpString = name;
177  tmpString.append("/");
178  tmpString.append(text);
179  tmpString.append("/xvalue");
180  dataBroker->pushData("data_broker_plotter", tmpString,
181  dbPackageX, this,
183  dataBroker->registerSyncReceiver(this, "data_broker_plotter",
184  tmpString, newPlot->dpId*10);
185  tmpString = name;
186  tmpString.append("/");
187  tmpString.append(text);
188  tmpString.append("/yvalue");
189  dataBroker->pushData("data_broker_plotter", tmpString,
190  dbPackageY, this,
192  dataBroker->registerSyncReceiver(this, "data_broker_plotter",
193  tmpString, newPlot->dpId*10+1);
194  newPlot->gotData = 0;
195 
196  tmpString = cfgName;
197  tmpString.append("sTime");
198  newPlot->sTime = cfg->getOrCreateProperty("DataBrokerPlotter",
199  tmpString.c_str(),
200  (double)0.0, this);
201  tmpString = cfgName;
202  tmpString.append("xRange");
203  newPlot->xRange = cfg->getOrCreateProperty("DataBrokerPlotter",
204  tmpString.c_str(),
205  (double)0.0, this);
206  tmpString = cfgName;
207  tmpString.append("yScale");
208  newPlot->yScale = cfg->getOrCreateProperty("DataBrokerPlotter",
209  tmpString.c_str(),
210  (double)1.0, this);
211  tmpString = cfgName;
212  tmpString.append("yOffset");
213  newPlot->yOffset = cfg->getOrCreateProperty("DataBrokerPlotter",
214  tmpString.c_str(),
215  (double)0.0, this);
216 
217  cfgParamIdToPlot[newPlot->sTime.paramId] = newPlot;
218  cfgParamIdToPlot[newPlot->xRange.paramId] = newPlot;
219  cfgParamIdToPlot[newPlot->yScale.paramId] = newPlot;
220  cfgParamIdToPlot[newPlot->yOffset.paramId] = newPlot;
221  newPlot->cfgParamIdProp[newPlot->sTime.paramId] = &newPlot->sTime;
222  newPlot->cfgParamIdProp[newPlot->xRange.paramId] = &newPlot->xRange;
223  newPlot->cfgParamIdProp[newPlot->yScale.paramId] = &newPlot->yScale;
224  newPlot->cfgParamIdProp[newPlot->yOffset.paramId] = &newPlot->yOffset;
225  }
226 
229 
230  // get plot
231  dataLock.lock();
232  std::map<mars::cfg_manager::cfgParamId, Plot*>::iterator it;
233  it = cfgParamIdToPlot.find(_property.paramId);
234  if(it == cfgParamIdToPlot.end()) {
235  dataLock.unlock();
236  return;
237  }
238  Plot *p = it->second;
239  std::map<mars::cfg_manager::cfgParamId, mars::cfg_manager::cfgPropertyStruct*>::iterator it2;
240  it2 = p->cfgParamIdProp.find(_property.paramId);
241  if(it2 == p->cfgParamIdProp.end()) {
242  dataLock.unlock();
243  return;
244  }
245  it2->second->dValue = _property.dValue;
246  dataLock.unlock();
247  }
248 
249  void DataBrokerPlotter::hideEvent(QHideEvent *event) {
250  (void)event;
251 
252  //mainLib->destroyPlotWindow(this);
253  }
254 
255 } // end of namespace: data_broker_plotter
void receiveData(const mars::data_broker::DataInfo &info, const mars::data_broker::DataPackage &dataPackage, int callbackParam)
The DataBroker will call this method to notify the receiver of whenever the condition for which the r...
virtual const cfgPropertyStruct getOrCreateProperty(const std::string &_group, const std::string &_name, bool val, CFGClient *newClient=0)=0
mars::cfg_manager::cfgPropertyStruct yOffset
virtual bool registerSyncReceiver(ReceiverInterface *receiver, const std::string &groupName, const std::string &dataName, int callbackParam=0)=0
register a receiver to receive a synchronous callback for a certain stream
bool get(const std::string &itemName, T *val) const
gets the value of the DataItem with the given name
Definition: DataPackage.h:98
mars::cfg_manager::cfgPropertyStruct yScale
cfg_manager::CFGManagerInterface * cfg
Definition: BaseWidget.h:63
mars::cfg_manager::cfgPropertyStruct xRange
void cfgUpdateProperty(mars::cfg_manager::cfgPropertyStruct _property)
Copyright 2012, DFKI GmbH Robotics Innovation Center.
virtual unsigned long pushData(const std::string &groupName, const std::string &dataName, const DataPackage &dataPackage, const ReceiverInterface *producer, PackageFlag flags)=0
pushes a DataPackage into the DataBroker
virtual bool unregisterSyncReceiver(ReceiverInterface *receiver, const std::string &groupName, const std::string &dataName)=0
unregister a receiver from receiving callbacks for certain group/data
void add(const DataItem &item)
adds the DataItem item to the end of the package.
Definition: DataPackage.h:82
std::map< mars::cfg_manager::cfgParamId, Plot * > cfgParamIdToPlot
Class containing information about a DataPackage.
Definition: DataInfo.h:47
DataBrokerPlotter(DataBrokerPlotterLib *_mainLib, mars::data_broker::DataBrokerInterface *_dataBroker, mars::cfg_manager::CFGManagerInterface *cfg, std::string _name, QWidget *parent=0)
A collection of DataItems.
Definition: DataPackage.h:44
mars::cfg_manager::cfgPropertyStruct sTime
The interface every DataBroker should implement.
std::map< mars::cfg_manager::cfgParamId, mars::cfg_manager::cfgPropertyStruct * > cfgParamIdProp
virtual void cfgUpdateProperty(cfg_manager::cfgPropertyStruct _property)
Definition: BaseWidget.cpp:52
mars::data_broker::DataBrokerInterface * dataBroker