-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRAFCETStep.cpp
More file actions
137 lines (125 loc) · 4.33 KB
/
GRAFCETStep.cpp
File metadata and controls
137 lines (125 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/****************************************************************************
* File name: GRAFCETStep.cpp
* Author: Alejandro Torrejon Harto
* Date: 05/10/2023
* Description: QState wrapper to execute functions, simulating a GRAFCET or SFC(Sequential Function Chart) step (according to EN61131-3).
****************************************************************************/
#include "GRAFCETStep.h"
/**
* @brief Builds GRAFCETStep object
*
* This function takes the name of the step, execution period, function to execute cyclically, function in case of entry and exit, to build the object.
*
* @param name Name of the step.
* @param period_ms Period of cyclic execution.
* @param N Function to execute cyclically when the step is active.
* @param P1 Function to execute when entering the step
* @param P0 Function to execute when exiting the step
*/
GRAFCETStep::GRAFCETStep(QString name, int period_ms, const std::function<void()>& N, const std::function<void()>& P1, const std::function<void()>& P0){
#if DEBUG
std::cout << "Construyendo GRAFCETStep "<< name.toStdString() <<std::endl<<std::flush;
#endif
this->setObjectName(name); // Set object name
this->N = N; // Set function to execute cyclically
this->P1 = P1; //Function to be executed at start step
this->P0 = P0; //Function to be executed at end step
if (this->N != nullptr)
{
this->timer_step = new QTimer(this); //Create cyclic timer
this->timer_step->setInterval(period_ms); //Set time to cyclic timer
//Connecting the cyclic timer to the function
connect(this->timer_step, &QTimer::timeout, this, [this](){this->N();});
}
#if DEBUG
std::cout << "Construido GRAFCETStep"<< this->objectName().toStdString() <<std::endl<<std::flush;
#endif
}
/**
* @brief Destroy GRAFCETStep object
*
* This function destroys and frees the object's memory
*/
GRAFCETStep::~GRAFCETStep()
{
if (this->N != nullptr)
{
this->timer_step->stop(); //Stop cyclic timer
delete this->timer_step; //Delete cyclic timer
}
#if DEBUG
std::cout << "Dell GRAFCETStep"<< this->objectName().toStdString()<<std::endl<<std::flush;
#endif
}
/**
* @brief Changes the period of the cyclic execution
*
* This function modifies the execution period and restarts with the new time in case it is active.
*
* @param period_ms Period of cyclic execution.
*/
void GRAFCETStep::setPeriod(int period_ms)
{
if (this->N != nullptr)
{
//Set new time to cyclic timer
this->timer_step->setInterval(period_ms);
this->mtx.lock();
//Restart cyclic timer
if (this->timer_step->isActive())
{
timer_step->stop();
timer_step->start();
}
this->mtx.unlock();
}
}
/**
* @brief Get the period of the cyclic execution
*
* This function return the execution period.
*
* @return Cyclic execution period otherwise -1.
*/
int GRAFCETStep::getPeriod()
{
if (this->N != nullptr)
return this->timer_step->interval();
else
return -1;
}
/**
* @brief When the step is activated this function is executed.
*
* This function launches the cyclic timer that executes the configured function, event is not used.
*/
void GRAFCETStep::onEntry(QEvent *event)
{
Q_UNUSED(event)
if (this->P1 != nullptr)
this->P1(); //Launches the entry function
if (this->N != nullptr)
this->timer_step->start(); //Launches the cyclic timer
#if DEBUG
std::cout << "Entrando en GRAFCETStep "<< this->objectName().toStdString() <<std::endl<<std::flush;
#endif
}
/**
* @brief When the step is deactivated this function is executed.
*
* This function stops the cyclic timer that executes the configured function, event is not used.
*/
void GRAFCETStep::onExit(QEvent *event)
{
Q_UNUSED(event)
if (this->N != nullptr){
this->mtx.lock();
this->timer_step->stop(); //Stops the cyclic timer
this->mtx.unlock();
}
if (this->P0 != nullptr)
this->P0(); //Launches the exit function
#if DEBUG
std::cout<< "Saliendo de GRAFCETStep "<< this->objectName().toStdString() <<std::endl<<std::flush;
#endif
}