So es ging weiter. Ich glaube, dass der jetzige Zustand so weit ausgereift ist, dass als nächstes dieser an die reale Bewässerung angeschlossen werden kann. Dazu muss ich jetzt aber erstmal das Projektimport Thema auf die Reihe bekommen
Und hier der Code, sowie im Anhang meine .TWL files (ich entwickle die Scripte in VSCode und hab mir da angefangen eine kleine Extension geschrieben. Bisher kann sie hauptsächlich nur Highlighting aber ich hab da noch mehr geplant...). Nicht wundern, ist als ZIP, da das Forum keine TWL Files erlaubt
Code: Alles auswählen
/**
* File: Irrigation_SystemControl.twl V1.0 ALPHA 4
* Logic for Garden irrigation. This module is the SystemControl which controls the pump and other related systems (like power supplies).
* Author: Hans Martin
*
*/
{
"Input": [
["System Request", "(0/1) Indicates whether the system should be activated", "$VAR<SystemRequest?>", "c"],
["System Forerun", "(T_s) Time how long the system should run before the pump is activated", "$SystemForerun", "u"],
["Pump Forerun", "(T_s) Time how long the pump should run before system goes into the OK state", "$PumpForerun", "u"],
["System FollowUp", "(T_s) Time how long the system should run after the pump is shut down", "$SystemFollowUp", "u"],
["Pump FollowUp", "(T_s) Time how long the pump should run after the request gone", "$PumpFollowUp", "u"],
["Inhibit", "", "$VAR<Inhibit?>", "c"]
],
"Output": [
["System State","(0/1) Indicates whether the system has a request","$SystemState","c"],
["System OK", "(0/1) Indicates that the system is in fully operational state", "$SystemOk", "c"],
["System Warning", "(0/1) Indicates that the system is partially operating", "$SystemWarn", "c"],
["System Error", "(0/1) Indicates that the system is in faulted state", "$SystemError", "c"],
["System", "(0/1) Output to activate the system devices (e.g. power supply)", "$SystemOut", "c"],
["Pump", "(0/1) Output to activate the pump", "$PumpOut", "c"]
],
"Level": [
// Inputs
["$VAR<Inhibit?>","bool",false],
["$VAR<SystemRequest?>", "bool", false],
["$SystemForerun", "float", 0],
["$PumpForerun", "float", 0],
["$SystemFollowUp", "float", 0],
["$PumpFollowUp", "float", 0],
// Outputs
["$SystemState", "bool", false],
["$SystemOk", "bool", false],
["$SystemWarn", "bool", false],
["$SystemError", "bool", false],
["$SystemOut", "bool", false],
["$PumpOut", "bool", false],
// Consts
["$Konst1", "int", 1],
["$Konst5", "int", 5],
//Internals
["$State","integer",0],
["$InternalSystemWarn", "bool", false],
["$SystemRequestInternal", "bool", false]
],
"Module": [
["Break", ["$VAR<Inhibit?>"]], // this is a problem! Fix it! (If system is running and inhibit is set, there is no way to reset the system!)
["Or", ["$VAR<SystemRequest?>"], "$SystemRequestInternal"],
// build state
["Statemachine",
// 0: System Off
// 1: System Forerun
// 2: Pump Forerun
// 3: Running
// 4: Pump FollowUp
// 5: System FollowUp
[
// [condition , current state, next state, timeout]
["$SystemRequestInternal", 0, 1, "$SystemForerun"], // new request
["-$SystemRequestInternal", 1, 0, 0], // request canceled during system forerun
[0, 1, 2, "$PumpForerun"], // system forerun done
["-$SystemRequestInternal", 2, 0, 0], // request canceled during pump forerun
[0, 2, 3, 0], // pump forerun done, system fully operating
["$SystemRequestInternal", 3, 3, 0], // request still true
["-$SystemRequestInternal", 3, 4, "$PumpFollowUp"], // pump follow up
["$SystemRequestInternal", 4, 3, 0], // request within pump follow up
[0, 4, 5, "$SystemFollowUp"], //system follow up
["$SystemRequestInternal", 5, 2, "$PumpForerun"], // request within system follow up
[0, 5, 0, 0] // system off
],
"$State"
],
// map state to output
["Comparator", "$State", "$SystemOut", 0], // System Out (only 1 when state > 0)
["Multiplexer", [0, 0, "$Konst1", "$Konst1", "$Konst1", 0], "$PumpOut", "$State"], // Pump Out (1 when 5 > state > 1)
// map state to state outputs
["Comparator", "$State", "$SystemState", 0], // SystemState (only 1 when state > 0)
["Multiplexer", [0, "$Konst1", "$Konst1", 0, "$Konst1", "$Konst1"], "$InternalSystemWarn", "$State"], // system has warning if not in state 3 and has request
["And", ["-$SystemError", "$InternalSystemWarn"], "$SystemWarn"], // system has only warning exposed if no error is active
["And", ["-$SystemError", "-$SystemWarn", "$SystemRequestInternal"], "$SystemOk" ] // system is ok if no error and no warning is active and request was made
]
}
Code: Alles auswählen
/**
* File: Irrigation_Sequencer.twl V1.0 ALPHA 3
* Logic for Garden irrigation. This module is the sequencer which controls an automatic run of all circuits
* Author: Hans Martin
*
*/
{
"Input": [
["Start", "(RISING) Trigger to start an automatic irrigation cycle", "$TriggerInpStart", "c"],
["Stop", "(RISING) Trigger to stop an automatic irrigation cycle", "$TriggerInpStop", "c"],
["Next", "(RISING) Trigger to step to next circuit", "$TriggerInpNext", "a"],
["Number of circuits", "(0-20) Configure the number of active circuits", "$NumberOfSteps", "u"],
["Inhibit", "", "$VAR<Inhibit?>", "c"]
],
"Output": [
["Sequencer Active", "(0/1) State of sequencer", "$SequencerActive", "c"],
["Sequencer Number", "(R+) Current active output number", "$SequencerNumber", "c"],
["Stop", "(RISING) Signal to stop all irrigation circuits", "$SequencerStop", "c"],
// repeat as many time as desired irrigation circuits
["Channel 1", "(0/1) Trigger for irrigation circuit", "$Circuit_1_?", "c"],
["Channel 2", "(0/1) Trigger for irrigation circuit", "$Circuit_2_?", "c"],
["Channel 3", "(0/1) Trigger for irrigation circuit", "$Circuit_3_?", "c"],
["Channel 4", "(0/1) Trigger for irrigation circuit", "$Circuit_4_?", "c"],
["Channel 5", "(0/1) Trigger for irrigation circuit", "$Circuit_5_?", "c"],
["Channel 6", "(0/1) Trigger for irrigation circuit", "$Circuit_6_?", "c"],
["Channel 7", "(0/1) Trigger for irrigation circuit", "$Circuit_7_?", "c"],
["Channel 8", "(0/1) Trigger for irrigation circuit", "$Circuit_8_?", "c"],
["Channel 9", "(0/1) Trigger for irrigation circuit", "$Circuit_9_?", "c"],
["Channel 10", "(0/1) Trigger for irrigation circuit", "$Circuit_10_?", "c"],
["Channel 11", "(0/1) Trigger for irrigation circuit", "$Circuit_11_?", "c"],
["Channel 12", "(0/1) Trigger for irrigation circuit", "$Circuit_12_?", "c"],
["Channel 13", "(0/1) Trigger for irrigation circuit", "$Circuit_13_?", "c"],
["Channel 14", "(0/1) Trigger for irrigation circuit", "$Circuit_14_?", "c"],
["Channel 15", "(0/1) Trigger for irrigation circuit", "$Circuit_15_?", "c"],
["Channel 16", "(0/1) Trigger for irrigation circuit", "$Circuit_16_?", "c"],
["Channel 17", "(0/1) Trigger for irrigation circuit", "$Circuit_17_?", "c"],
["Channel 18", "(0/1) Trigger for irrigation circuit", "$Circuit_18_?", "c"],
["Channel 19", "(0/1) Trigger for irrigation circuit", "$Circuit_19_?", "c"],
["Channel 20", "(0/1) Trigger for irrigation circuit", "$Circuit_20_?", "c"]
],
"Level": [
// Inputs
["$VAR<Inhibit?>","bool",false],
["$TriggerInpStart","bool",false],
["$TriggerInpStop","bool",false],
["$TriggerInpNext","bool",false],
// Outputs
["$SequencerActive","bool",false], // also used as internal state
["$SequencerNumber","int",0],
["$SequencerStop","bool",false],
// Channels
// Configure number of circuits
["$NumberOfSteps","int",0],
// channel outs - repeat as many irrigation circuits are configured above
["$Circuit_1_","bool",false],
["$Circuit_2_","bool",false],
["$Circuit_3_","bool",false],
["$Circuit_4_","bool",false],
["$Circuit_5_","bool",false],
["$Circuit_6_","bool",false],
["$Circuit_7_","bool",false],
["$Circuit_8_","bool",false],
["$Circuit_9_","bool",false],
["$Circuit_10_","bool",false],
["$Circuit_11_","bool",false],
["$Circuit_12_","bool",false],
["$Circuit_13_","bool",false],
["$Circuit_14_","bool",false],
["$Circuit_15_","bool",false],
["$Circuit_16_","bool",false],
["$Circuit_17_","bool",false],
["$Circuit_18_","bool",false],
["$Circuit_19_","bool",false],
["$Circuit_20_","bool",false],
// Constants
["$Konst1", "int", 1],
["$KonstFalse", "bool", false],
["$KonstTrue", "bool", true],
// Internals
["$CurrentStep","int",0],
["$NextStep","int",0],
["$StepOverflow", "bool", false],
["$ResetSystem", "bool", false],
["$Inhibit","bool",false],
["$TriggerInpStartRising","bool",false],
["$TriggerInpStopRising","bool",false],
["$TriggerInpNextRising","bool",false],
["$TriggerInpStartLast","bool",false],
["$TriggerInpStopLast","bool",false],
["$TriggerInpNextLast","bool",false]
],
"Module": [
["Or", ["$VAR<Inhibit?>"], "$Inhibit"],
// calculate rising edges
["And", ["$TriggerInpStart","-$TriggerInpStartLast", "-$SequencerActive"], "$TriggerInpStartRising"],
["And", ["$TriggerInpStop","-$TriggerInpStopLast", "$SequencerActive"], "$TriggerInpStopRising"],
["And", ["$TriggerInpNext","-$TriggerInpNextLast", "$SequencerActive"], "$TriggerInpNextRising"],
// set last trigger variables
["And", ["$TriggerInpStart"], "$TriggerInpStartLast"],
["And", ["$TriggerInpStop"], "$TriggerInpStopLast"],
["And", ["$TriggerInpNext"], "$TriggerInpNextLast"],
// calculate start of system
["Multiplexer", ["$SequencerActive", "$Konst1"], "$SequencerActive", "$TriggerInpStartRising"],
// calculate next step
["Polynomial", "$Konst1", "$NextStep", ["$CurrentStep", "$Konst1"] ], // ++$CurrentStep
["Comparator", "$NextStep", "$StepOverflow", "$NumberOfSteps"],
["Multiplexer", ["$NextStep", 0], "$NextStep", "$StepOverflow"],
// set next step when next was triggered or system was started
["Multiplexer", ["$CurrentStep","$NextStep"], "$CurrentStep", "$TriggerInpNextRising"],
["Multiplexer", ["$CurrentStep","$NextStep"], "$CurrentStep", "$TriggerInpStartRising"],
// reset current step when stop was issued or inhibit was set
["Or", ["$TriggerInpStopRising", "$Inhibit"], "$ResetSystem"],
["And", ["$ResetSystem", "$SequencerActive"], "$ResetSystem"], // only reset when system runs
["Multiplexer", ["$CurrentStep",0], "$CurrentStep", "$ResetSystem"],
// start timer to reset stop output later on
["Monoflop","$ResetSystem", 0, 0,"$Konst1",2],
// write common outputs
["Polynomial", 0, "$SequencerNumber", ["$CurrentStep"]],
["Multiplexer", [0, "$Konst1"], "$SequencerStop", "$ResetSystem"],
["Multiplexer", [0, "$SequencerActive"], "$SequencerActive", "$CurrentStep"],
// Reset Inputs
["Latch", "$KonstFalse", "$TriggerInpStart", "$KonstTrue", 0],
["Latch", "$KonstFalse", "$TriggerInpStop", "$KonstTrue", 0],
["Latch", "$KonstFalse", "$TriggerInpNext", "$KonstTrue", 0],
// write circuit outputs
// copy as many circuit exists. Put a zero in front of $Konst1 for each new step.
["Multiplexer", [0, "$Konst1", 0], "$Circuit_1_", "$CurrentStep"],
["Multiplexer", [0, 0, "$Konst1", 0], "$Circuit_2_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, "$Konst1", 0], "$Circuit_3_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, "$Konst1", 0], "$Circuit_4_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_5_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_6_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_7_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_8_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_9_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_10_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_11_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_12_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_13_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_14_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_15_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_16_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_17_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_18_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_19_", "$CurrentStep"],
["Multiplexer", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "$Konst1", 0], "$Circuit_20_", "$CurrentStep"]
]
}
Code: Alles auswählen
/**
* File: Irrigation_CircuitControl.twl V1.0 ALPHA 3
* Logic for Garden irrigation. This module is the Circuit Control which controls one single irrigation circuit
* Author: Hans Martin
*
*/
{
"Input": [
["Trigger Automatic", "(RISING) Trigger to start an automatic irrigation cycle", "$TriggerInpAutomatic", "c"],
["Trigger Start", "(RISING) Activation of irrigation circuit", "$TriggerInpStart", "c"],
["Trigger Stop", "(RISING) Stops any active cycle", "$TriggerInpStop", "c"],
["Automatic Time", "(T_s) Time of one irrigation cycle", "$AutoTime", "u"],
["System OK Feedback", "(0/1) Loopback of SystemControl", "$SystemOk", "c"],
["Inhibit", "", "$VAR<Inhibit?>", "c"]
],
"Output": [
["Valve", "(0/1) Output for the valve", "$ValveOut", "c"],
["System Request", "(0/1) Request for the SystemControl", "$SystemRequest", "c"],
["Circuit State", "(0/1) State indicating whether the circuit is running", "$CircuitState", "c"],
["Circuit finished", "(0/1) Flag indicating that the circuit is finished", "$CircuitFinished", "c"]
],
"Level": [
// Inputs
["$VAR<Inhibit?>","bool",false],
["$TriggerInpAutomatic", "bool", false],
["$TriggerInpStart", "bool", false],
["$TriggerInpStop", "bool", false],
["$AutoTime", "float", 0],
["$SystemOk", "bool", false],
// Output
["$ValveOut", "bool", false],
["$SystemRequest", "bool", false],
["$CircuitState", "bool", false],
["$CircuitFinished", "bool", false],
// Constants
["$Konst1", "int", 1],
["$Konst2", "int", 2],
["$KonstFalse", "bool", false],
["$KonstTrue", "bool", true],
// Internals
["$State","integer",0],
["$Inhibit","bool",false],
["$Cancel","bool",false],
["$ShouldRun", "bool", false],
["$LastShouldRun", "bool", false],
["$ShouldRunAndSysOk", "bool", false],
["$TimerActive", "bool", false],
["$TriggerAutomaticSaved", "bool", false],
["$TimerShouldRun", "bool", false],
["$CancelFinished", "bool", false],
["$TriggerInpStartRising","bool",false],
["$TriggerInpStopRising","bool",false],
["$TriggerInpAutomaticRising","bool",false],
["$TriggerInpStartLast","bool",false],
["$TriggerInpStopLast","bool",false],
["$TriggerInpAutomaticLast","bool",false],
["$ManualOn","bool",false],
["$SetOutputs","bool",false],
["$AutomaticModeActive","bool",false],
["$AutomaticModeActiveOrRequested","bool",false],
["$CancelAndAutoActive","bool",false]
],
"Module": [
// calculate rising edges
["And", ["$TriggerInpStart","-$TriggerInpStartLast"], "$TriggerInpStartRising"],
["And", ["$TriggerInpStop","-$TriggerInpStopLast"], "$TriggerInpStopRising"],
["And", ["$TriggerInpAutomatic","-$TriggerInpAutomaticLast"], "$TriggerInpAutomaticRising"],
// calculate cancel state
["Or", ["$VAR<Inhibit?>"], "$Inhibit"],
["Or", ["$Inhibit", "$TriggerInpStopRising"], "$Cancel"],
// calculate manual state
["Or",["$TriggerInpStartRising", "$ManualOn"],"$ManualOn"],
["And", ["$ManualOn", "-$TimerActive", "-$Cancel"], "$ManualOn"], // if automatic mode was triggered, cancel manual mode
// calculate start of system
["Or", ["$TriggerAutomaticSaved", "$TriggerInpAutomaticRising"], "$TriggerAutomaticSaved"], // save trigger
["And", ["$TriggerAutomaticSaved", "-$TimerActive", "-$Cancel"], "$TriggerAutomaticSaved"], // reset trigger if timer has taken over control or cancel was issued
// Set timer if automatic mode was triggered
["And", ["$TriggerAutomaticSaved", "$SystemOk"], "$TimerShouldRun"], // start timer only if also system ok was announced
["Monoflop", "$TimerShouldRun", "$Cancel", "$TimerActive", "$AutoTime", 2], // not retriggerable, rising-edge controlled, reset on cancel
// build target state (run or not)
["Or", ["$TriggerAutomaticSaved", "$TimerActive"], "$AutomaticModeActive"],
["Or", ["$AutomaticModeActive", "$TriggerInpAutomaticRising"], "$AutomaticModeActiveOrRequested"],
["Or", ["$ManualOn", "$AutomaticModeActive"], "$ShouldRun"],
// build state
["And", ["$ShouldRun", "$SystemOk"], "$ShouldRunAndSysOk"], //bypass to skip state 1
["And", ["$AutomaticModeActiveOrRequested", "$Cancel"], "$CancelAndAutoActive"], // bypass to only announce finished on cancel from state 0, 1 or 2 when automatic mode was active
["Statemachine",
// 0: Circuit Off
// 1: Requesting for SystemOK
// 2: Circuit On
// 3: Finishing
[
// [condition , current state, next state, timeout]
// cancel state transitions
["$CancelAndAutoActive", 0, 3, "$Konst1"],
["$CancelAndAutoActive", 1, 3, "$Konst1"],
["$CancelAndAutoActive", 2, 3, "$Konst1"],
["$Cancel", 1, 0, 0],
["$Cancel", 2, 0, 0],
// normal operation state transitions
["$ShouldRunAndSysOk", 0, 2, 0], // bypass to skip state 1
["$ShouldRun", 0, 1, 0], // new request
["-$ShouldRun", 1, 0, 0], // cancel request
["$SystemOk", 1, 2, 0], // system reported ok
["$ShouldRun", 2, 2, 0], // running
["-$ShouldRun", 2, 3, "$Konst1"], // finishing
[0, 3, 0, 0] // end
],
"$State"
],
// set some outputs only if system should run now or was running in last tick
["Or", ["$ShouldRun", "$LastShouldRun"], "$SetOutputs"],
// Map state to outputs
["Comparator", "$State", "$CircuitFinished", "$Konst2"], // Finished (only 1 when state > 2)
["Comparator", "$State", "$CircuitState", 0], // Circuit State (only 1 when state > 0)
["Comparator", "$State", "$SystemRequest", 0], // SystemRequest (only 1 when state > 0)
["Multiplexer", [0, 0, "$Konst1", 0], "$ValveOut", "$State"], // Valve (1 only in state 2)
["And", ["$SetOutputs", "$CircuitState"], "$CircuitState"],
["And", ["$SetOutputs", "$SystemRequest"], "$SystemRequest"],
// announce finished when canceld or automatic finished
["And", ["$Cancel", "$ShouldRun"], "$CancelFinished"],
["Or", ["$CancelFinished", "$CircuitFinished"], "$CircuitFinished"],
// Reset Trigger
["Multiplexer", [0, "$TriggerAutomaticSaved", "$TriggerAutomaticSaved", "$TriggerAutomaticSaved"], "$TriggerAutomaticSaved", "$State"],
// set last trigger variables
["And", ["$TriggerInpStart"], "$TriggerInpStartLast"],
["And", ["$TriggerInpStop"], "$TriggerInpStopLast"],
["And", ["$TriggerInpAutomatic"], "$TriggerInpAutomaticLast"],
["And", ["$ShouldRun"], "$LastShouldRun"],
// Reset Inputs
["Latch", "$KonstFalse", "$TriggerInpAutomatic", "$KonstTrue", 0],
["Latch", "$KonstFalse", "$TriggerInpStart", "$KonstTrue", 0],
["Latch", "$KonstFalse", "$TriggerInpStop", "$KonstTrue", 0]
]
}