-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlc.cpp
107 lines (82 loc) · 2.01 KB
/
Plc.cpp
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
#include "Plc.h"
#include <thread>
Plc::Plc(PlcIo *io) {
state = STOP;
this->io = io;
// Beim erstmaligen Start der SPS wird der Speicher mit 0 initialisiert
for (int i = 0; i < PLC_I_SIZE; i++) {
Inputs[i] = 0;
}
for (int i = 0; i < PLC_Q_SIZE; i++) {
Outputs[i] = 0;
}
for (int i = 0; i < PLC_F_SIZE; i++) {
Flags[i] = 0;
}
for (int i = 0; i < PLC_R_SIZE; i++) {
RFlags[i] = 0;// TODO: Remanente Flags aus Datei lesen
}
for (int i = 0; i < PLC_S_SIZE; i++) {
SFlags[i] = 0;
}
}
Plc::~Plc() {
end();
}
Plc::State Plc::getState() const {
return state;
}
PlcIo *Plc::getIo() const {
return io;
}
unsigned int Plc::getCooldownCycleTime() const {
return cooldownCycleTime;
}
void Plc::setCooldownCycleTime(unsigned int _cooldownCycleTime) {
cooldownCycleTime = _cooldownCycleTime;
}
void Plc::begin() {
// Bei begin() bleibt der Speicher im Zustand in dem er ist
lastCycle = std::chrono::system_clock::now();
startTime = std::chrono::system_clock::now();
S_usTicks = 0;
io->begin();
task->begin();
state = RUN;
}
void Plc::end() {
state = STOP;
task->end();
io->end();
}
bool Plc::cycle() {
if (!io) {
state = ERROR;
return false;
}
if (state != RUN) {
return false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(cooldownCycleTime));
plc_timepoint now = std::chrono::system_clock::now();
S_usTicks = ((now - startTime).count() * std::micro::den) / plc_timepoint::duration::period::den;
S_cycleActMs = task->getCycleActMs();
S_minCycleActMs = task->getMinCycleActMs();
S_maxCycleActMs = task->getMaxCycleActMs();
try {
io->read();
task->run();
io->write();
return true;
} catch (...) {
state = ERROR;
return false;
}
lastCycle = now;
}
PlcTask *Plc::getTask() const {
return task;
}
void Plc::setTask(PlcTask *task) {
this->task = task;
}