YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
worker.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#if defined(_WIN32)
7 #pragma warning (disable : 4099)
8 #pragma warning (disable : 4250)
9 #pragma warning (disable : 4520)
10#endif
11
12#include <memory>
13#include <yarp/os/LogStream.h>
14#include "include/worker.h"
15#include "include/mainwindow.h"
16
17#ifdef HAS_OPENCV
18#include <opencv2/imgcodecs/imgcodecs.hpp>
19#endif
20
21using namespace yarp::sig;
22using namespace yarp::sig::file;
23using namespace yarp::os;
24
25#ifdef HAS_OPENCV
26 using namespace cv;
27#endif
28
29/**********************************************************/
30QEngine::QEngine(QUtilities *qutilities, int subDirCnt, QMainWindow *gui, QObject *parent) :
31 QObject(parent),
32 qutils(qutilities),
33 gui(gui)
34{
35 this->numPart = subDirCnt;
36 thread = new QMasterThread();
37 this->setThread(thread);
38 thread->setEngine(*this);
39 this->setUtilities(this->qutils);
40}
41
42/**********************************************************/
44{
45 delete thread;
46}
47
48/**********************************************************/
50{
51 yInfo() << "in step from command";
52 bool sentAll = false;
53 for (int i=0; i < this->numPart; i++){
54 qutils->partDetails[i].sent = 0;
55 }
56 qutils->totalSent = 0;
57
58 while ( !sentAll ){
60 for (int i=0; i < this->numPart; i++){
61 if (qutils->partDetails[i].sent == 1){
62 qutils->totalSent ++;
63 qutils->partDetails[i].sent = 2;
64 }
65 if (qutils->totalSent == this->numPart){
66 sentAll = true;
67 }
68 }
69 }
70
71 for (int i=0; i < this->numPart; i++){
72 qutils->partDetails[i].sent = 0;
73 }
74
75 qutils->totalSent = 0;
76 qutils->qengine->pause();
77 for (int i=0; i < this->numPart; i++){
78 qutils->qengine->virtualTime = qutils->partDetails[i].timestamp[qutils->partDetails[i].currFrame];
79 }
80 qutils->qengine->virtualTime = qutils->partDetails[0].timestamp[qutils->partDetails[0].currFrame];
81 yInfo() << "ok................ ";
82 qutils->qengine->stepfromCmd = false;
83}
84
85/**********************************************************/
87{
88 if(allPartsStatus) return;
89
90 for (int i=0; i < this->numPart; i++){
91 //get a reference to the part we are interested in
92 yarp::yarpDataplayer::PartsData &this_part = qutils->partDetails[i];
93
94 //if we have alredy stopped we have nothing to do
95 if(this_part.hasNotified)
96 continue;
97
98 //if this port is not active, keep progressing though the frames without
99 //sending, so if the part activates it is in synch
100 bool isActive = ((MainWindow*)gui)->getPartActivation(this_part.name.c_str());
101
102 //send all available frames up to the current virtualTime
103 while (this_part.currFrame <= this_part.maxFrame &&
104 this->virtualTime >= this_part.timestamp[this_part.currFrame]) {
105 this_part.worker->sendData(this_part.currFrame++, isActive, this->virtualTime);
106 }
107
108 //if we have sent all frames perform reset/stop
109 if(this_part.currFrame > this_part.maxFrame) {
110 if (qutils->repeat) {
111 this->initThread();
112 this_part.worker->init();
113 } else {
114 yInfo() << "partID: " << i << " has finished";
115 this_part.hasNotified = true;
116
117 //perform a check to see if ALL parts have finished
118 int stopAll = 0;
119 for (int x=0; x < this->numPart; x++){
120 stopAll += qutils->partDetails[x].hasNotified ? 1 : 0;
121 }
122
123 if (stopAll == this->numPart){
124 yInfo() << "All parts have Finished!";
125 if (this_part.currFrame > 1 && this->isRunning()) {
127 }
128 qutils->stopAtEnd();
130 allPartsStatus = true;
131 }
132 }
133 }
134 }
135
136 this->virtualTime += this->diff_seconds() * qutils->speed;
137 this->tick();
138
139 //10 Hz gui update
140 static double gui_tic = 0.0;
141 if(this->virtualTime < gui_tic || this->virtualTime - gui_tic > 0.1 && this->isRunning()) {
143 gui_tic = this->virtualTime;
144 }
145
146}
147
148/**********************************************************/
150{
151 if (this->qEngine->stepfromCmd){
152 this->qEngine->stepFromCmd();
153 } else {
154 this->qEngine->runNormally();
155 }
156}
#define yInfo(...)
Definition Log.h:319
MainWindow class.
Definition display.h:22
void run() override
Definition worker.cpp:149
void setEngine(QEngine &qEngine)
Definition worker.h:52
~QEngine()
Definition worker.cpp:43
QEngine(QUtilities *qutilities, int subDirCnt, QMainWindow *gui, QObject *parent=NULL)
QEngine class.
Definition worker.cpp:30
QMainWindow * gui
Definition worker.h:40
QEngine::QMasterThread * thread
void stepFromCmd()
Definition worker.cpp:49
QUtilities * qutils
Definition worker.h:39
void runNormally()
Definition worker.cpp:86
QEngine * qengine
Definition utils.h:36
void updateGuiThread()
void resetButton()
Definition utils.cpp:77
A mini-server for performing network communication in the background.
An interface to the operating system, including Port based communication.