eventlogger.cpp
Go to the documentation of this file.
00001 #include "../msg/msg.h"
00002 #include "../utils/utils.h"
00003 #include <dashel/dashel.h>
00004 #include <cmath>
00005 #include <QtGui>
00006 #include <cassert>
00007 #include <fstream>
00008 #include <iostream>
00009 
00010 #ifdef _MSC_VER
00011 #define QWT_DLL
00012 #endif // _MSC_VER
00013 
00014 #include <qwt_plot.h>
00015 #include <qwt_plot_curve.h>
00016 #include <qwt_legend.h>
00017 
00018 #if QWT_VERSION >= 0x060000
00019         #include <qwt_series_data.h>
00020 #else
00021         #include <qwt_data.h>
00022 #endif
00023 
00024 using namespace Dashel;
00025 using namespace Aseba;
00026 using namespace std;
00027 
00028 #if QWT_VERSION >= 0x060000
00029 class EventDataWrapper : public QwtSeriesData<QPointF>
00030 {
00031 private:
00032         std::vector<double>& _x;
00033         std::vector<sint16>& _y;
00034         
00035 public:
00036         EventDataWrapper(std::vector<double>& _x, std::vector<sint16>& _y) :
00037                 _x(_x),
00038                 _y(_y)
00039         { }
00040         virtual QRectF boundingRect () const { return qwtBoundingRect(*this); }
00041         virtual QPointF sample (size_t i) const { return QPointF(_x[i], double(_y[i])); }
00042         virtual size_t size () const { return _x.size(); }
00043 };
00044 #else
00045 class EventDataWrapper : public QwtData
00046 {
00047 private:
00048         std::vector<double>& _x;
00049         std::vector<sint16>& _y;
00050         
00051 public:
00052         EventDataWrapper(std::vector<double>& _x, std::vector<sint16>& _y) :
00053                 _x(_x),
00054                 _y(_y)
00055         { }
00056         virtual QwtData *   copy () const { return new EventDataWrapper(*this); }
00057         virtual size_t   size () const { return _x.size(); }
00058         virtual double x (size_t i) const { return _x[i]; }
00059         virtual double y (size_t i) const { return (double)_y[i]; }
00060 };
00061 #endif
00062 
00063 class EventLogger : public Hub, public QwtPlot
00064 {
00065 protected:
00066         Stream* stream;
00067         int eventId;
00068         vector<vector<sint16> > values;
00069         vector<double> timeStamps;
00070         QTime startingTime;
00071         ofstream outputFile;
00072         
00073 public:
00074         EventLogger(const char* target, int eventId, int eventVariablesCount, const char* filename) :
00075                 QwtPlot(QwtText(QString(tr("Plot for event %0")).arg(eventId))),
00076                 eventId(eventId),
00077                 values(eventVariablesCount)
00078         {
00079                 stream = Hub::connect(target);
00080                 cout << "Connected to " << stream->getTargetName() << endl;
00081                 
00082                 startingTime = QTime::currentTime();
00083                 
00084                 setCanvasBackground(Qt::white);
00085                 setAxisTitle(xBottom, tr("Time (seconds)"));
00086                 setAxisTitle(yLeft, tr("Values"));
00087                 
00088                 QwtLegend *legend = new QwtLegend;
00089                 //legend->setItemMode(QwtLegend::CheckableItem);
00090                 insertLegend(legend, QwtPlot::BottomLegend);
00091                 
00092                 for (size_t i = 0; i < values.size(); i++)
00093                 {
00094                         QwtPlotCurve *curve = new QwtPlotCurve(QString("%0").arg(i));
00095                         #if QWT_VERSION >= 0x060000
00096                         curve->setData(new EventDataWrapper(timeStamps, values[i]));
00097                         #else
00098                         curve->setData(EventDataWrapper(timeStamps, values[i]));
00099                         #endif
00100                         curve->attach(this);
00101                         curve->setPen(QColor::fromHsv((i * 360) / values.size(), 255, 100));
00102                 }
00103                 
00104                 resize(1000, 600);
00105                 
00106                 if (filename)
00107                         outputFile.open(filename);
00108                 
00109                 startTimer(10);
00110         }
00111 
00112 protected:
00113         virtual void timerEvent ( QTimerEvent * event )
00114         {
00115                 if (!step(0))
00116                         close();
00117         }
00118         
00119         void incomingData(Stream *stream)
00120         {
00121                 Message *message = Message::receive(stream);
00122                 UserMessage *userMessage = dynamic_cast<UserMessage *>(message);
00123                 if (userMessage)
00124                 {
00125                         if (userMessage->type == eventId)
00126                         {
00127                                 double elapsedTime = (double)startingTime.msecsTo(QTime::currentTime()) / 1000.;
00128                                 if (outputFile.is_open())
00129                                         outputFile << elapsedTime;
00130                                 timeStamps.push_back(elapsedTime);
00131                                 for (size_t i = 0; i < values.size(); i++)
00132                                 {
00133                                         if (i < userMessage->data.size())
00134                                         {
00135                                                 if (outputFile.is_open())
00136                                                         outputFile << " " << userMessage->data[i];
00137                                                 values[i].push_back(userMessage->data[i]);
00138                                         }
00139                                         else
00140                                         {
00141                                                 if (outputFile.is_open())
00142                                                         outputFile << " " << 0;
00143                                                 values[i].push_back(0);
00144                                         }
00145                                 }
00146                                 if (outputFile.is_open())
00147                                         outputFile << endl;
00148                                 replot();
00149                         }
00150                 }
00151                 delete message;
00152         }
00153         
00154         void connectionClosed(Stream *stream, bool abnormal)
00155         {
00156                 dumpTime(cerr);
00157                 cout << "Connection closed to " << stream->getTargetName();
00158                 if (abnormal)
00159                         cout << " : " << stream->getFailReason();
00160                 cout << endl;
00161                 stop();
00162         }
00163 };
00164 
00165 int main(int argc, char *argv[])
00166 {
00167         if (argc < 4)
00168         {
00169                 cerr << "Usage " << argv[0] << " target event_id event_variables_count [output file]" << endl;
00170                 return 1;
00171         }
00172         
00173         QApplication app(argc, argv);
00174         
00175         int res;
00176         try
00177         {
00178                 EventLogger logger(argv[1], atoi(argv[2]), atoi(argv[3]), (argc > 4 ? argv[4] : 0));
00179                 logger.show();
00180                 res = app.exec();
00181         }
00182         catch(Dashel::DashelException e)
00183         {
00184                 std::cerr << e.what() << std::endl;
00185                 return 2;
00186         }
00187         
00188         return res;
00189 }


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:38