-> Hier kostenlos registrieren
Hi,
um einen übersichtlichen (gut lesbaren) Quellcode zu erhalten, habe ich meinen AWL-Code als Zustandsautomaten programmiert.
Sobald ich jedoch Timer verwende wird es schwierig.
Mein erster Versuch (siehe Beispiel A) hat dann auch promt nicht funktioniert, weil der Timer so die steigenden und fallenden Flanken des Eingangssignals (#event bzw. #reset) nicht mehr mitbekommt.
Eine Lösung ist, die Timer aus der Zustandsmaschine wieder herrauszunehmen (siehe Beispiel B), ...
... was jedoch zur Folge hat, dass die Lesbarzeit des Programms leidet (insbesondere wenn man viele Timer benutzen will/braucht).
FRAGE:
Ist es überhaupt sinnvoll einen Zustandautomaten und Timer gleichzeitig zu verwenden, oder ist das schlechter Programmierstil?
Wie sonst könnte man dieses Problem lösen, damit auch große Programme noch gut lesbar bleiben?
Viele Grüße,
Truth
um einen übersichtlichen (gut lesbaren) Quellcode zu erhalten, habe ich meinen AWL-Code als Zustandsautomaten programmiert.
Sobald ich jedoch Timer verwende wird es schwierig.
Mein erster Versuch (siehe Beispiel A) hat dann auch promt nicht funktioniert, weil der Timer so die steigenden und fallenden Flanken des Eingangssignals (#event bzw. #reset) nicht mehr mitbekommt.
Code:
[FONT=Courier 10 Pitch]// Example A:[/FONT]
[FONT=Courier 10 Pitch] L #state[/FONT]
[FONT=Courier 10 Pitch] SPL d[/FONT][FONT=Courier 10 Pitch]ef[/FONT][FONT=Courier 10 Pitch] // switch([/FONT][COLOR=#000000][FONT=Courier 10 Pitch]state[/FONT][/COLOR][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z[/FONT][FONT=Courier 10 Pitch]0 // case: 0 ([/FONT][FONT=Courier 10 Pitch]waiting for event[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z1[/FONT][FONT=Courier 10 Pitch] // case: [/FONT][FONT=Courier 10 Pitch]1[/FONT][FONT=Courier 10 Pitch] ([/FONT][FONT=Courier 10 Pitch]timer started[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z2[/FONT][FONT=Courier 10 Pitch] // case: [/FONT][FONT=Courier 10 Pitch]2[/FONT][FONT=Courier 10 Pitch] ([/FONT][FONT=Courier 10 Pitch]waiting for [/FONT][FONT=Courier 10 Pitch]reset[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch]def: SPA err // default:[/FONT]
[FONT=Courier 10 Pitch]err: NOP 0 // Zustand invalid[/FONT]
[FONT=Courier 10 Pitch] SPA end[/FONT]
[FONT=Courier 10 Pitch]z0: U #event // [/FONT][FONT=Courier 10 Pitch]if(#event)[/FONT]
[FONT=Courier 10 Pitch] L ST5#10s [/FONT]
[FONT=Courier 10 Pitch] SS T1 // [/FONT][FONT=Courier 10 Pitch]start timer[/FONT]
[FONT=Courier 10 Pitch] L 1[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=1[/FONT]
[FONT=Courier 10 Pitch] SPA end:[/FONT]
[FONT=Courier 10 Pitch]z1: [/FONT][FONT=Courier 10 Pitch]U T1[/FONT]
[FONT=Courier 10 Pitch] = #output[/FONT][FONT=Courier 10 Pitch] // … [/FONT][FONT=Courier 10 Pitch]doing something[/FONT]
[FONT=Courier 10 Pitch] L [/FONT][FONT=Courier 10 Pitch]2[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=2[/FONT]
[FONT=Courier 10 Pitch] SPA end: [/FONT]
[FONT=Courier 10 Pitch]z2: U #reset // if(#reset)[/FONT]
[FONT=Courier 10 Pitch]R T1[/FONT]
[FONT=Courier 10 Pitch] L 0[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=0[/FONT]
[FONT=Courier 10 Pitch]end: BEA[/FONT]
Eine Lösung ist, die Timer aus der Zustandsmaschine wieder herrauszunehmen (siehe Beispiel B), ...
Code:
[FONT=Courier 10 Pitch]// Example B:[/FONT]
[FONT=Courier 10 Pitch]L #state[/FONT]
[FONT=Courier 10 Pitch] SPL d[/FONT][FONT=Courier 10 Pitch]ef[/FONT][FONT=Courier 10 Pitch] // switch([/FONT][COLOR=#000000][FONT=Courier 10 Pitch]state[/FONT][/COLOR][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z[/FONT][FONT=Courier 10 Pitch]0 // case: 0 ([/FONT][FONT=Courier 10 Pitch]waiting for event[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z1[/FONT][FONT=Courier 10 Pitch] // case: [/FONT][FONT=Courier 10 Pitch]1[/FONT][FONT=Courier 10 Pitch] ([/FONT][FONT=Courier 10 Pitch]timer started[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch] SPA [/FONT][FONT=Courier 10 Pitch]z2[/FONT][FONT=Courier 10 Pitch] // case: [/FONT][FONT=Courier 10 Pitch]2[/FONT][FONT=Courier 10 Pitch] ([/FONT][FONT=Courier 10 Pitch]waiting for [/FONT][FONT=Courier 10 Pitch]reset[/FONT][FONT=Courier 10 Pitch])[/FONT]
[FONT=Courier 10 Pitch]def: SPA err // default:[/FONT]
[FONT=Courier 10 Pitch]err: NOP 0 // Zustand invalid[/FONT]
[FONT=Courier 10 Pitch]SPA end[/FONT]
[FONT=Courier 10 Pitch]z0: U #event // [/FONT][FONT=Courier 10 Pitch]if(#event)[/FONT]
[FONT=Courier 10 Pitch] S #start_timer[/FONT]
[FONT=Courier 10 Pitch] L 1[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=1[/FONT]
[FONT=Courier 10 Pitch] SPA end:[/FONT]
[FONT=Courier 10 Pitch]z1: [/FONT][FONT=Courier 10 Pitch]U T1[/FONT]
[FONT=Courier 10 Pitch] = #output[/FONT][FONT=Courier 10 Pitch] // … [/FONT][FONT=Courier 10 Pitch]doing something[/FONT]
[FONT=Courier 10 Pitch] L [/FONT][FONT=Courier 10 Pitch]2[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=2[/FONT]
[FONT=Courier 10 Pitch] SPA end: [/FONT]
[FONT=Courier 10 Pitch]z2: U #reset // if(#reset)[/FONT]
[FONT=Courier 10 Pitch] S #reset_timer[/FONT]
[FONT=Courier 10 Pitch] L 0[/FONT]
[FONT=Courier 10 Pitch] T #state // [/FONT][FONT=Courier 10 Pitch]switch to state=0[/FONT]
[FONT=Courier 10 Pitch]end: U #start_timer[/FONT]
[FONT=Courier 10 Pitch] L ST5#10s [/FONT]
[FONT=Courier 10 Pitch] SS T1[/FONT]
[FONT=Courier 10 Pitch] R #start_timer[/FONT]
[FONT=Courier 10 Pitch] U #reset_timer[/FONT]
[FONT=Courier 10 Pitch]R T1[/FONT]
[FONT=Courier 10 Pitch] R #reset_timer[/FONT]
[FONT=Courier 10 Pitch] BEA[/FONT]
... was jedoch zur Folge hat, dass die Lesbarzeit des Programms leidet (insbesondere wenn man viele Timer benutzen will/braucht).
FRAGE:
Ist es überhaupt sinnvoll einen Zustandautomaten und Timer gleichzeitig zu verwenden, oder ist das schlechter Programmierstil?
Wie sonst könnte man dieses Problem lösen, damit auch große Programme noch gut lesbar bleiben?
Viele Grüße,
Truth