A
On Delay Timer for PLC's.
The following is the algorithm/program for timers in PLC's specifically
for linux PLC's as it uses the jiffies variable which is available in
linux OS. the benefit of this algorithm is that it gives AB like
programming flexibility for the PLC front end design. The same logic can
be used in STL, FBD, LAD etc and user needs no special programming
skills (once final product is ready).
Please note that at this stage this is a preliminary document. The
author's knowledge of C dates to 1992, basic used intermittently and
database used in 1998. Thus at this moment the usage has several
combinations and many shortcomings in syntaxt and semantics. variable
declarations are required, and the program may have to leave the use of
database for time of executions sake. But algorithm is solid!
the database tables can be changed to Files but further "refreshment" of
the C section of authors Brain needs to be done. Also further refinement
of the logic is in progress as the timer enable and disable is
frequently set now (frequent execution of the functions) and better
means to enable it once when logic becomes true and disable once when
the conditions become untrue is being worked on.
Similar logic can also be applied for retentive timers, except that
Disable is invoked only when the reset bit for the particular timer is
set. in other words, the disable logic moves out of the else condition
to another set of if conditions.
Notes:
1.the timers are of three types, based on their time base of 0.01
seconds, 0.1 seconds and 1 second.
2.The timers are stored in a database. Each time base has its own
database. The three tables used are t001t for 0.01 second time base
timers, t010t for 0.1 second timebase timer and t100t for 1 second time
base timers.
3.The fields of all these tables are
Num the timer number (integer).
En Enable bit (boolean)
T Timing bit (boolean)
D Done bit (boolean)
Pre The preset value (integer)
Acc The Accumulated value (integer)
4.The logic uses the jiffies value defined in <linux/sched.h> and the
default Hz setting of 100Hz is assumed. The main program of the Linux
PLC can also set the frequency to 100Hz, but this may require
recompiling of all modules.
5.Several integers that are used are
t001b the intermediate time base for 0.01 second timers.
t010b the intermediate time base for 0.1 second timers.
t100b the intermediate time base for 1.0 second timers..
tempj A long unsigned value used for jiffy changeover check
Algorithm flow:
1.. Initially the intermediate values are set to zero.
2.. At each 100Hz signal (jiffies increments), the 0.01 second timer
base is incremented.
3.. Then the database is used and the table t001t which has data
regarding the 0.01 second timers is updated. All the timers with enable
bit set are incremented provided they arenot done (i.e. acc value = preset values).
4.. If any timer has acc value=preset value on updation then the
done bit is set and timing bit is reset.
5.. When 10 such cycles have occured then the t001b/10 = |t001b/10|.
This is basically a convention i used in a old version of basic and
looking for an alternative value in C. what we do here is see that
truncated value of t001b/10 and t001b/10 are same or reminder=0 will
also do.
6.. The database cycle for 0.1 second timer as described in step 3
follows.
7.. the step 5 and 6 are then done for the 1 second timers.
8.. Functions tent001b, tent010b and tent100b are the three functions
defined for doing the enabling part of the timer with time bases 0.01,
0.1 and 1 seconds respectively. This function needs to be executed once
when the logic conditions become true for a particular timer.
9.. Functions tdbt001b, tdbt010b and tdbt100b are the three functions
defined for doing the disbling part of the timer with time bases 0.01,
0.1 and 1 seconds respectively. This function needs to be executed once
when the logic conditions become false for a particular timer.
In the main PLC program when the PLC is initialized or started the
values of t001b, t010b, t100b is set to zero and a tempj value set equal
to jiffies.
Initialization done once by main program when the PLC starts up.
t001b=0;
t010b=0;
t100b=0;
tempj=jiffies;
Once the initialization is done then the timer routine is started in the
main PLC program.
if tempj<>jiffies then
t001b=t001b+1; /*increment 0.01 second timer*/
update too1t set acc=acc+1 where en=1 and pre>acc
update t001t set d=1 where pre=acc
update t001t set t=0 where pre=acc
if t001b/10=|t001b/10| then /*check if ten 0.01 seconds have
elapsed*/
update t010t set acc=acc+1 where en=1 and pre>acc
update t010t set d=1 where pre=acc
update t010t set t=0 where pre=acc
t010b=t010b+1;
endif;
if t010b/10=|t010b/10| then /*check if ten 0.1 seconds have
elapsed*/
t100b=t100b+1;
update t100t set acc=acc+1 where en=1 and pre>acc
update t100t set d=1 where pre=acc
update t100t set t=0 where pre=acc
endif;
tempb=jiffies;
endif;
Function tent001b(int fno) \* This function is performed when a timer
with base 0.01 seconds is enabled by the logic*/
Update t001t set en=1,t=1 where no=fno;
return;
Function tent010b(int fno) \* This function is performed when a timer
with base 0.1 seconds is enabled by the logic*/
Update t010t set en=1,t=1 where no=fno;
return;
Function tent100b(int fno) \* This function is performed when a timer
with base 1 second is enabled by the logic*/
Update t100t set en=1,t=1 where no=fno;
return;
Function tdbt001b (int fno) \* This function is performed when a timer
with base 0.01 second is disabled by the logic*/
Update t001t set en=0,t=0, d=0, acc=0, where no=fno;
return;
Function tdbt010b (int fno) \* This function is performed when a timer
with base 0.1 second is disabled by the logic*/
Update t010t set en=0,t=0, d=0, acc=0, where no=fno;
return;
Function tdbt100b (int fno) \* This function is performed when a timer
with base 1 second is disabled by the logic*/
Update t100t set en=0,t=0, d=0, acc=0, where no=3Dfno;
return;
In the user program the timer is either enabled by the logic or
disabled. In this case the user program performs the routine:
the following shows the 0.01 base timer number 125 being enabled or
disabled .
If ......... then /* if conditions that enable the timer*/
tent001b(125); /* executed if the conditions are true and enables is
set*/
else
tdbt001b(125) /*executed if the enable is not set*/
endif;
/* Please be patient as more time is required to make this into a final
product*/
All Comments and criticisms are welcome.
Anand Krishnan Iyer.
_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
The following is the algorithm/program for timers in PLC's specifically
for linux PLC's as it uses the jiffies variable which is available in
linux OS. the benefit of this algorithm is that it gives AB like
programming flexibility for the PLC front end design. The same logic can
be used in STL, FBD, LAD etc and user needs no special programming
skills (once final product is ready).
Please note that at this stage this is a preliminary document. The
author's knowledge of C dates to 1992, basic used intermittently and
database used in 1998. Thus at this moment the usage has several
combinations and many shortcomings in syntaxt and semantics. variable
declarations are required, and the program may have to leave the use of
database for time of executions sake. But algorithm is solid!
the database tables can be changed to Files but further "refreshment" of
the C section of authors Brain needs to be done. Also further refinement
of the logic is in progress as the timer enable and disable is
frequently set now (frequent execution of the functions) and better
means to enable it once when logic becomes true and disable once when
the conditions become untrue is being worked on.
Similar logic can also be applied for retentive timers, except that
Disable is invoked only when the reset bit for the particular timer is
set. in other words, the disable logic moves out of the else condition
to another set of if conditions.
Notes:
1.the timers are of three types, based on their time base of 0.01
seconds, 0.1 seconds and 1 second.
2.The timers are stored in a database. Each time base has its own
database. The three tables used are t001t for 0.01 second time base
timers, t010t for 0.1 second timebase timer and t100t for 1 second time
base timers.
3.The fields of all these tables are
Num the timer number (integer).
En Enable bit (boolean)
T Timing bit (boolean)
D Done bit (boolean)
Pre The preset value (integer)
Acc The Accumulated value (integer)
4.The logic uses the jiffies value defined in <linux/sched.h> and the
default Hz setting of 100Hz is assumed. The main program of the Linux
PLC can also set the frequency to 100Hz, but this may require
recompiling of all modules.
5.Several integers that are used are
t001b the intermediate time base for 0.01 second timers.
t010b the intermediate time base for 0.1 second timers.
t100b the intermediate time base for 1.0 second timers..
tempj A long unsigned value used for jiffy changeover check
Algorithm flow:
1.. Initially the intermediate values are set to zero.
2.. At each 100Hz signal (jiffies increments), the 0.01 second timer
base is incremented.
3.. Then the database is used and the table t001t which has data
regarding the 0.01 second timers is updated. All the timers with enable
bit set are incremented provided they arenot done (i.e. acc value = preset values).
4.. If any timer has acc value=preset value on updation then the
done bit is set and timing bit is reset.
5.. When 10 such cycles have occured then the t001b/10 = |t001b/10|.
This is basically a convention i used in a old version of basic and
looking for an alternative value in C. what we do here is see that
truncated value of t001b/10 and t001b/10 are same or reminder=0 will
also do.
6.. The database cycle for 0.1 second timer as described in step 3
follows.
7.. the step 5 and 6 are then done for the 1 second timers.
8.. Functions tent001b, tent010b and tent100b are the three functions
defined for doing the enabling part of the timer with time bases 0.01,
0.1 and 1 seconds respectively. This function needs to be executed once
when the logic conditions become true for a particular timer.
9.. Functions tdbt001b, tdbt010b and tdbt100b are the three functions
defined for doing the disbling part of the timer with time bases 0.01,
0.1 and 1 seconds respectively. This function needs to be executed once
when the logic conditions become false for a particular timer.
In the main PLC program when the PLC is initialized or started the
values of t001b, t010b, t100b is set to zero and a tempj value set equal
to jiffies.
Initialization done once by main program when the PLC starts up.
t001b=0;
t010b=0;
t100b=0;
tempj=jiffies;
Once the initialization is done then the timer routine is started in the
main PLC program.
if tempj<>jiffies then
t001b=t001b+1; /*increment 0.01 second timer*/
update too1t set acc=acc+1 where en=1 and pre>acc
update t001t set d=1 where pre=acc
update t001t set t=0 where pre=acc
if t001b/10=|t001b/10| then /*check if ten 0.01 seconds have
elapsed*/
update t010t set acc=acc+1 where en=1 and pre>acc
update t010t set d=1 where pre=acc
update t010t set t=0 where pre=acc
t010b=t010b+1;
endif;
if t010b/10=|t010b/10| then /*check if ten 0.1 seconds have
elapsed*/
t100b=t100b+1;
update t100t set acc=acc+1 where en=1 and pre>acc
update t100t set d=1 where pre=acc
update t100t set t=0 where pre=acc
endif;
tempb=jiffies;
endif;
Function tent001b(int fno) \* This function is performed when a timer
with base 0.01 seconds is enabled by the logic*/
Update t001t set en=1,t=1 where no=fno;
return;
Function tent010b(int fno) \* This function is performed when a timer
with base 0.1 seconds is enabled by the logic*/
Update t010t set en=1,t=1 where no=fno;
return;
Function tent100b(int fno) \* This function is performed when a timer
with base 1 second is enabled by the logic*/
Update t100t set en=1,t=1 where no=fno;
return;
Function tdbt001b (int fno) \* This function is performed when a timer
with base 0.01 second is disabled by the logic*/
Update t001t set en=0,t=0, d=0, acc=0, where no=fno;
return;
Function tdbt010b (int fno) \* This function is performed when a timer
with base 0.1 second is disabled by the logic*/
Update t010t set en=0,t=0, d=0, acc=0, where no=fno;
return;
Function tdbt100b (int fno) \* This function is performed when a timer
with base 1 second is disabled by the logic*/
Update t100t set en=0,t=0, d=0, acc=0, where no=3Dfno;
return;
In the user program the timer is either enabled by the logic or
disabled. In this case the user program performs the routine:
the following shows the 0.01 base timer number 125 being enabled or
disabled .
If ......... then /* if conditions that enable the timer*/
tent001b(125); /* executed if the conditions are true and enables is
set*/
else
tdbt001b(125) /*executed if the enable is not set*/
endif;
/* Please be patient as more time is required to make this into a final
product*/
All Comments and criticisms are welcome.
Anand Krishnan Iyer.
_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc