plcopen 0.22.0
载入中...
搜索中...
未找到
basic.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <limits>
5
6namespace plcopen::core::fb
7{
8
9struct RTrig
10{
11 bool clk = false;
12 bool q = false;
13 bool memory = false;
14
15 void cycle()
16 {
17 q = clk && !memory;
18 memory = clk;
19 }
20};
21
22struct FTrig
23{
24 bool clk = false;
25 bool q = false;
26 bool memory = false;
27
28 void cycle()
29 {
30 q = !clk && memory;
31 memory = clk;
32 }
33};
34
35struct SR
36{
37 bool set = false;
38 bool reset = false;
39 bool q = false;
40
41 void cycle()
42 {
43 q = set || (q && !reset);
44 }
45};
46
47struct RS
48{
49 bool set = false;
50 bool reset = false;
51 bool q = false;
52
53 void cycle()
54 {
55 q = (q || set) && !reset;
56 }
57};
58
60{
61public:
62 bool set_cycle_time(std::int64_t cycles)
63 {
64 if(cycles <= 0) {
65 return false;
66 }
67 cycle_time_ = cycles;
68 return true;
69 }
70
71protected:
72 std::int64_t cycle_time_ = 1;
73};
74
75struct TON : TimerBase
76{
77 bool in = false;
78 bool q = false;
79 std::int64_t pt = 0;
80 std::int64_t et = 0;
81
82 void cycle()
83 {
84 if(!in) {
85 q = false;
86 et = 0;
87 return;
88 }
89 if(pt == 0) {
90 q = true;
91 et = 0;
92 return;
93 }
94 if(et < pt) {
95 et += cycle_time_;
96 }
97 q = et >= pt;
98 }
99};
100
102{
103 bool in = false;
104 bool q = false;
105 std::int64_t pt = 0;
106 std::int64_t et = 0;
107
108 void cycle()
109 {
110 if(in) {
111 q = true;
112 et = 0;
113 return;
114 }
115 if(pt == 0) {
116 q = false;
117 et = 0;
118 return;
119 }
120 if(q && et < pt) {
121 et += cycle_time_;
122 }
123 q = et < pt;
124 }
125};
126
127struct TP : TimerBase
128{
129 bool in = false;
130 bool q = false;
131 std::int64_t pt = 0;
132 std::int64_t et = 0;
133
134 void cycle()
135 {
136 const bool rising = in && !last_;
137 last_ = in;
138 if(pt == 0) {
139 q = false;
140 et = 0;
141 active_ = false;
142 return;
143 }
144 if(rising && !active_) {
145 active_ = true;
146 q = true;
147 et = 0;
148 }
149 if(active_) {
150 et += cycle_time_;
151 if(et >= pt) {
152 active_ = false;
153 q = false;
154 et = pt;
155 }
156 }
157 }
158
159private:
160 bool last_ = false;
161 bool active_ = false;
162};
163
164struct CTU
165{
166 bool cu = false;
167 bool reset = false;
168 bool q = false;
169 std::int64_t pv = 0;
170 std::int64_t cv = 0;
171
172 void cycle()
173 {
174 const bool rising = initialized_ && cu && !last_;
175 initialized_ = true;
176 last_ = cu;
177 if(reset) {
178 cv = 0;
179 } else if(rising && cv < std::numeric_limits<std::int64_t>::max()) {
180 ++cv;
181 }
182 q = cv >= pv;
183 }
184
185private:
186 bool last_ = false;
187 bool initialized_ = false;
188};
189
190struct CTD
191{
192 bool cd = false;
193 bool load = false;
194 bool q = false;
195 std::int64_t pv = 0;
196 std::int64_t cv = 0;
197
198 void cycle()
199 {
200 const bool rising = initialized_ && cd && !last_;
201 initialized_ = true;
202 last_ = cd;
203 if(load) {
204 cv = pv;
205 } else if(rising && cv > 0) {
206 --cv;
207 }
208 q = cv <= 0;
209 }
210
211private:
212 bool last_ = false;
213 bool initialized_ = false;
214};
215
216struct CTUD
217{
218 bool cu = false;
219 bool cd = false;
220 bool reset = false;
221 bool load = false;
222 bool qu = false;
223 bool qd = true;
224 std::int64_t pv = 0;
225 std::int64_t cv = 0;
226
227 void cycle()
228 {
229 const bool up = initialized_ && cu && !last_up_;
230 const bool down = initialized_ && cd && !last_down_;
231 initialized_ = true;
232 last_up_ = cu;
233 last_down_ = cd;
234
235 if(reset) {
236 cv = 0;
237 } else if(load) {
238 cv = pv;
239 } else if(up != down) {
240 if(up && cv < std::numeric_limits<std::int64_t>::max()) {
241 ++cv;
242 } else if(down && cv > 0) {
243 --cv;
244 }
245 }
246 qu = cv >= pv;
247 qd = cv <= 0;
248 }
249
250private:
251 bool last_up_ = false;
252 bool last_down_ = false;
253 bool initialized_ = false;
254};
255
257{
258 bool enable = false;
259 bool q = false;
260 std::int64_t pdt = 0;
261 std::int64_t dt = 0;
262
263 void cycle()
264 {
265 if(!enable) {
266 q = false;
267 dt = 0;
268 active_ = false;
269 return;
270 }
271 if(!active_) {
272 dt = pdt;
273 active_ = true;
274 } else if(dt <= std::numeric_limits<std::int64_t>::max() - cycle_time_) {
275 dt += cycle_time_;
276 } else {
277 dt = std::numeric_limits<std::int64_t>::max();
278 }
279 q = true;
280 }
281
282private:
283 bool active_ = false;
284};
285
286} // namespace plcopen::core::fb
bool set_cycle_time(std::int64_t cycles)
定义 basic.h:62