-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
331 lines (272 loc) · 11.6 KB
/
main.c
File metadata and controls
331 lines (272 loc) · 11.6 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// -------------------------
// Generic Grafcet Framework
// -------------------------
// MIT License:
//
// Copyright 2018 Gonçalo Santos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <termios.h>
#include <stropts.h>
// Linux (POSIX) implementation of _kbhit().
// Morgan McGuire, morgan@cs.brown.edu
int _kbhit() {
static const int STDIN = 0;
static bool initialized = false;
if (! initialized) {
// Use termios to turn off line buffering
struct termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
#define clear() printf("\033[H\033[J")
#define green(text) "\x1B[32m" text "\x1B[0m"
#define blue(text) "\x1B[34m" text "\x1B[0m"
#define ArrayCount(arr) ((sizeof(arr))/sizeof(*arr))
#define STATE_OUTPUT_FUNCTION(Name) void Name()
typedef STATE_OUTPUT_FUNCTION(state_output_function);
#define TRANSITION_CONDITION_FUNCTION(Name) bool Name()
typedef TRANSITION_CONDITION_FUNCTION(transition_condition_function);
#define ARR(...) {__VA_ARGS__}
#include "preprocessor_output.h"
// NOTE(nox): The ... in the macros are the place for the Output and Condition, respectively
#define newState(Grafcet, Name_, ...) \
do { \
state_id Id = State_X##Name_; \
Grafcets[Grafcet].States[Grafcets[Grafcet].StateCount++] = Id; \
strncpy(States[Id].Name, #Name_, NAME_LENGTH); \
States[Id].Output = stateAction_X##Name_; \
} while(0)
#define newTransition(Grafcet, Name_, PrevStates, NextStates_, ...) \
do { \
transition_id Id = Transition_##Name_; \
state_id P[] = PrevStates; \
state_id N[] = NextStates_; \
Grafcets[Grafcet].Transitions[Grafcets[Grafcet].TransitionCount++] = Id; \
strncpy(Transitions[Id].Name, #Name_, NAME_LENGTH); \
Transitions[Id].Condition = transitionCondition_##Name_; \
Transitions[Id].PreviousStatesCount = ArrayCount(P); \
for(int I = 0; I < ArrayCount(P); ++I) { Transitions[Id].PreviousStates[I] = P[I]; } \
Transitions[Id].NextStatesCount = ArrayCount(N); \
for(int I = 0; I < ArrayCount(N); ++I) { Transitions[Id].NextStates[I] = N[I]; } \
} while(0)
#define GRAFCET_COUNT 2
#define GRAFCET_MAX_STATES 1024
#define GRAFCET_MAX_TRANSITIONS 1024
#define NAME_LENGTH 1<<4
typedef struct {
bool Active;
char Name[NAME_LENGTH];
uint64_t Timer;
state_output_function *Output;
} state;
typedef struct {
bool Active;
char Name[NAME_LENGTH];
int PreviousStatesCount;
state_id PreviousStates[GRAFCET_MAX_STATES];
int NextStatesCount;
state_id NextStates[GRAFCET_MAX_STATES];
transition_condition_function *Condition;
} transition;
typedef struct {
int StateCount;
state_id States[GRAFCET_MAX_STATES];
int TransitionCount;
transition_id Transitions[GRAFCET_MAX_TRANSITIONS];
bool Frozen;
} grafcet;
static grafcet Grafcets[GRAFCET_COUNT];
static state States[StateCount];
static transition Transitions[TransitionCount];
// NOTE(nox): Inputs and Outputs
#define ioEnumWriter(Name, ...) IO_##Name
typedef struct {
bool Active;
bool Modified;
char Name[25];
char Key;
} input;
#define inputMacro(W) W(QUIT, 'q'), W(M_MAX, 'a'), W(M_MIN, 's'), W(PRATO1, 'd'), W(PRATO2, 'f'), \
W(PARAGEM, 'p'), W(CICLO, 'c')
#define inputStructWriter(Name, Key) { false, false, #Name, Key }
static input Inputs[] = { inputMacro(inputStructWriter) };
typedef enum { inputMacro(ioEnumWriter) } inputLabel;
typedef struct {
bool Active;
char Name[25];
} output;
#define outputStructWriter(Name) { false, #Name }
#define outputMacro(W) W(ESQUERDA), W(BOMBA_V5), W(MOTOR_PA), W(V1), W(V2), W(V3), W(V4), W(V7)
static output Outputs[] = { outputMacro(outputStructWriter) };
typedef enum { outputMacro(ioEnumWriter) } outputLabel;
#define input(Label) Inputs[IO_##Label].Active
#define RE(Label) (input(Label) && Inputs[IO_##Label].Modified)
#define FE(Label) (!input(Label) && Inputs[IO_##Label].Modified)
#define output(Label) Outputs[IO_##Label].Active = true
#define freeze(Id) Grafcets[Id].Frozen = true
#define active(Name) States[State_X##Name].Active
#define timer(Name) States[State_X##Name].Timer
#define OUTPUTS_AND_CONDITIONS
#include "preprocessor_output.h"
#undef OUTPUTS_AND_CONDITIONS
static bool checkTransitionState(transition *Transition) {
bool AllPrevious = true;
for(int PrevIndex = 0; PrevIndex < Transition->PreviousStatesCount; ++PrevIndex) {
if(!States[Transition->PreviousStates[PrevIndex]].Active) {
AllPrevious = false;
break;
}
}
if(AllPrevious) {
return Transition->Condition();
}
return false;
}
int main(int Argc, char *Argv[]) {
// NOTE(nox): Control Grafcet
newState(1, 1, {});
States[State_X1].Active = true;
newTransition(1, 1, ARR(State_X1), ARR(State_X2, State_X4, State_X6), (input(CICLO)));
newState(1, 2, { output(V1); });
newState(1, 3, {});
newTransition(1, 2, ARR(State_X2), ARR(State_X3), (input(PRATO1)));
newState(1, 4, { output(V3); });
newState(1, 5, {});
newTransition(1, 3, ARR(State_X4), ARR(State_X5), (input(PRATO2)));
newState(1, 6, { if(!input(M_MAX)) output(BOMBA_V5); });
newTransition(1, 4, ARR(State_X3, State_X5, State_X6), ARR(State_X7), (input(M_MAX)));
newState(1, 7, { output(ESQUERDA); output(MOTOR_PA); output(V2); output(V4); });
newTransition(1, 5, ARR(State_X7), ARR(State_X8), (timer(7)>=30));
newState(1, 8, { output(ESQUERDA); output(MOTOR_PA); });
newTransition(1, 6, ARR(State_X8), ARR(State_X9), (timer(8)>=40));
newState(1, 9, { output(V7); });
newTransition(1, 7, ARR(State_X9), ARR(State_X1), (input(M_MIN)));
// NOTE(nox): Supervisor Grafcet
newState(0, s1, {});
States[State_Xs1].Active = true;
newTransition(0, s1, ARR(State_Xs1), ARR(State_Xs2), ((active(2) || active(4) || active(6) || active(7)) &&
RE(PARAGEM)));
newState(0, s2, { freeze(1); });
newTransition(0, s2, ARR(State_Xs2), ARR(State_Xs1), (!input(PARAGEM)));
// NOTE(nox): Generic grafcet logic ----------------------------------------
for(;;) {
if(input(QUIT)) {
break;
}
// NOTE(nox): Reset outputs and inputs modification flag
for(int Index = 0; Index < ArrayCount(Outputs); ++Index) {
Outputs[Index].Active = false;
}
for(int Index = 0; Index < ArrayCount(Inputs); ++Index) {
Inputs[Index].Modified = false;
}
// NOTE(nox): Read inputs
while(_kbhit()) {
char C = getchar();
for(int Index = 0; Index < ArrayCount(Inputs); ++Index) {
if(Inputs[Index].Key == C) {
Inputs[Index].Active = !Inputs[Index].Active;
Inputs[Index].Modified = true;
break;
}
}
}
// NOTE(nox): Update timers
for(int Index = 0; Index < StateCount; ++Index) {
state *State = States + Index;
if(State->Active) {
++State->Timer;
}
}
clear();
for(int GrafcetId = 0; GrafcetId < GRAFCET_COUNT; ++GrafcetId) {
grafcet *Grafcet = Grafcets + GrafcetId;
// NOTE(nox): Calculate transitions
for(int Index = 0; Index < Grafcet->TransitionCount; ++Index) {
transition *Transition = Transitions + Grafcet->Transitions[Index];
Transition->Active = Grafcet->Frozen ? false : checkTransitionState(Transition);
}
// NOTE(nox): Deactivate above
for(int Index = 0; Index < Grafcet->TransitionCount; ++Index) {
transition *Transition = Transitions + Grafcet->Transitions[Index];
if(Transition->Active) {
for(int PrevIndex = 0; PrevIndex < Transition->PreviousStatesCount; ++PrevIndex) {
States[Transition->PreviousStates[PrevIndex]].Active = false;
}
}
}
// NOTE(nox): Activate below
for(int Index = 0; Index < Grafcet->TransitionCount; ++Index) {
transition *Transition = Transitions + Grafcet->Transitions[Index];
if(Transition->Active) {
for(int NextIndex = 0; NextIndex < Transition->NextStatesCount; ++NextIndex) {
States[Transition->NextStates[NextIndex]].Active = true;
States[Transition->NextStates[NextIndex]].Timer = 0;
}
}
}
// NOTE(nox): Outputs
for(int Index = 0; Index < Grafcet->StateCount; ++Index) {
state *State = States + Grafcet->States[Index];
if(State->Active) {
State->Output();
}
}
// NOTE(nox): Print debug information
printf("Grafcet %d %s\n", GrafcetId, Grafcet->Frozen ? blue("FROZEN") : "");
for(int Index = 0; Index < Grafcet->StateCount; ++Index) {
if(States[Grafcet->States[Index]].Active) {
printf("%5s: " green("Active") " %.1lfs\n", States[Grafcet->States[Index]].Name,
(double)States[Grafcet->States[Index]].Timer/10);
} else {
printf("%5s: Inactive\n", States[Grafcet->States[Index]].Name);
}
}
puts("");
// NOTE(nox): Disable freeze
Grafcet->Frozen = false;
}
printf("Inputs:\n");
for(int Index = 1; Index < ArrayCount(Inputs); ++Index) {
printf("%10s (%c): %s\n", Inputs[Index].Name, Inputs[Index].Key,
Inputs[Index].Active ? green("Active") : "Inactive");
}
puts("");
printf("Outputs:\n");
for(int Index = 0; Index < ArrayCount(Outputs); ++Index) {
printf("%10s: %s\n", Outputs[Index].Name, Outputs[Index].Active ? green("Active") : "Inactive");
}
puts("\n");
usleep(100000);
}
return 0;
}