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 using namespace Dashel;
00019 using namespace Aseba;
00020 using namespace std;
00021
00022 class EventDataWrapper : public QwtData
00023 {
00024 private:
00025 vector<double>& _x;
00026 vector<sint16>& _y;
00027
00028 public:
00029 EventDataWrapper(vector<double>& _x, vector<sint16>& _y) :
00030 _x(_x),
00031 _y(_y)
00032 { }
00033 virtual QwtData * copy () const { return new EventDataWrapper(*this); }
00034 virtual size_t size () const { return _x.size(); }
00035 virtual double x (size_t i) const { return _x[i]; }
00036 virtual double y (size_t i) const { return (double)_y[i]; }
00037 };
00038
00039 class EventLogger : public Hub, public QwtPlot
00040 {
00041 protected:
00042 Stream* stream;
00043 int eventId;
00044 vector<vector<sint16> > values;
00045 vector<double> timeStamps;
00046 QTime startingTime;
00047 ofstream outputFile;
00048
00049 public:
00050 EventLogger(const char* target, int eventId, int eventVariablesCount, const char* filename) :
00051 QwtPlot(QwtText(QString(tr("Plot for event %0")).arg(eventId))),
00052 eventId(eventId),
00053 values(eventVariablesCount)
00054 {
00055 stream = Hub::connect(target);
00056 cout << "Connected to " << stream->getTargetName() << endl;
00057
00058 startingTime = QTime::currentTime();
00059
00060 setCanvasBackground(Qt::white);
00061 setAxisTitle(xBottom, tr("Time (seconds)"));
00062 setAxisTitle(yLeft, tr("Values"));
00063
00064 QwtLegend *legend = new QwtLegend;
00065
00066 insertLegend(legend, QwtPlot::BottomLegend);
00067
00068 for (size_t i = 0; i < values.size(); i++)
00069 {
00070 QwtPlotCurve *curve = new QwtPlotCurve(QString("%0").arg(i));
00071 curve->setData(EventDataWrapper(timeStamps, values[i]));
00072 curve->attach(this);
00073 curve->setPen(QColor::fromHsv((i * 360) / values.size(), 255, 100));
00074 }
00075
00076 resize(1000, 600);
00077
00078 if (filename)
00079 outputFile.open(filename);
00080
00081 startTimer(10);
00082 }
00083
00084 protected:
00085 virtual void timerEvent ( QTimerEvent * event )
00086 {
00087 if (!step(0))
00088 close();
00089 }
00090
00091 void incomingData(Stream *stream)
00092 {
00093 Message *message = Message::receive(stream);
00094 UserMessage *userMessage = dynamic_cast<UserMessage *>(message);
00095 if (userMessage)
00096 {
00097 if (userMessage->type == eventId)
00098 {
00099 double elapsedTime = (double)startingTime.msecsTo(QTime::currentTime()) / 1000.;
00100 if (outputFile.is_open())
00101 outputFile << elapsedTime;
00102 timeStamps.push_back(elapsedTime);
00103 for (size_t i = 0; i < values.size(); i++)
00104 {
00105 if (i < userMessage->data.size())
00106 {
00107 if (outputFile.is_open())
00108 outputFile << " " << userMessage->data[i];
00109 values[i].push_back(userMessage->data[i]);
00110 }
00111 else
00112 {
00113 if (outputFile.is_open())
00114 outputFile << " " << 0;
00115 values[i].push_back(0);
00116 }
00117 }
00118 if (outputFile.is_open())
00119 outputFile << endl;
00120 replot();
00121 }
00122 }
00123 delete message;
00124 }
00125
00126 void connectionClosed(Stream *stream, bool abnormal)
00127 {
00128 dumpTime(cerr);
00129 cout << "Connection closed to " << stream->getTargetName();
00130 if (abnormal)
00131 cout << " : " << stream->getFailReason();
00132 cout << endl;
00133 stop();
00134 }
00135 };
00136
00137 int main(int argc, char *argv[])
00138 {
00139 if (argc < 4)
00140 {
00141 cerr << "Usage " << argv[0] << " target event_id event_variables_count [output file]" << endl;
00142 return 1;
00143 }
00144
00145 QApplication app(argc, argv);
00146
00147 int res;
00148 try
00149 {
00150 EventLogger logger(argv[1], atoi(argv[2]), atoi(argv[3]), (argc > 4 ? argv[4] : 0));
00151 logger.show();
00152 res = app.exec();
00153 }
00154 catch(Dashel::DashelException e)
00155 {
00156 std::cerr << e.what() << std::endl;
00157 return 2;
00158 }
00159
00160 return res;
00161 }