Go to the documentation of this file.00001 #include "datastream_sample.h"
00002 #include <QTextStream>
00003 #include <QFile>
00004 #include <QMessageBox>
00005 #include <QDebug>
00006 #include <thread>
00007 #include <mutex>
00008 #include <chrono>
00009 #include <thread>
00010 #include <math.h>
00011
00012 DataStreamSample::DataStreamSample()
00013 {
00014 QStringList words_list;
00015 words_list << "siam" << "tre" << "piccoli" << "porcellin"
00016 << "mai" << "nessun" << "ci" << "dividera" << "_sin" << "_cos";
00017
00018 int N = words_list.size();
00019
00020 foreach( const QString& name, words_list)
00021 {
00022 DataStreamSample::Parameters param;
00023 if(name == "_sin"){
00024 param.A = 2;
00025 param.B = 1;
00026 param.C = 0;
00027 param.D = 0;
00028 }
00029 else if (name == "_cos"){
00030 param.A = 2;
00031 param.B = 1;
00032 param.C = 1.5708;
00033 param.D = 0;
00034 }
00035 else{
00036 param.A = qrand()/(double)RAND_MAX * 6 - 3;
00037 param.B = qrand()/(double)RAND_MAX *3;
00038 param.C = qrand()/(double)RAND_MAX *3;
00039 param.D = qrand()/(double)RAND_MAX *2 -1;
00040 }
00041
00042 const std::string name_str = name.toStdString();
00043
00044 dataMap().addNumeric(name_str);
00045 _parameters.insert( std::make_pair( name_str, param) );
00046 }
00047 dataMap().addNumeric("empty");
00048 }
00049
00050 bool DataStreamSample::start(QStringList*)
00051 {
00052 _running = true;
00053 pushSingleCycle();
00054 _thread = std::thread([this](){ this->loop();} );
00055 return true;
00056 }
00057
00058 void DataStreamSample::shutdown()
00059 {
00060 _running = false;
00061 if( _thread.joinable()) _thread.join();
00062 }
00063
00064 bool DataStreamSample::isRunning() const { return _running; }
00065
00066 DataStreamSample::~DataStreamSample()
00067 {
00068 shutdown();
00069 }
00070
00071 bool DataStreamSample::xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const
00072 {
00073 return true;
00074 }
00075
00076 bool DataStreamSample::xmlLoadState(const QDomElement &parent_element)
00077 {
00078 return true;
00079 }
00080
00081 void DataStreamSample::pushSingleCycle()
00082 {
00083 std::lock_guard<std::mutex> lock( mutex() );
00084
00085 using namespace std::chrono;
00086 static std::chrono::high_resolution_clock::time_point initial_time = high_resolution_clock::now();
00087 const double offset = duration_cast< duration<double>>( initial_time.time_since_epoch() ).count() ;
00088
00089 auto now = high_resolution_clock::now();
00090 for (auto& it: dataMap().numeric )
00091 {
00092 if( it.first == "empty") continue;
00093 auto par = _parameters[it.first];
00094
00095 auto& plot = it.second;
00096 const double t = duration_cast< duration<double>>( now - initial_time ).count() ;
00097 double y = par.A*sin(par.B*t + par.C) + par.D*t*0.05;
00098
00099 plot.pushBack( PlotData::Point( t + offset, y ) );
00100 }
00101 }
00102
00103 void DataStreamSample::loop()
00104 {
00105 _running = true;
00106 while( _running )
00107 {
00108 pushSingleCycle();
00109 std::this_thread::sleep_for ( std::chrono::milliseconds(10) );
00110 }
00111 }