Programming Cai WebControl PLC

Hello and Merry Christmas !

I would like to start a new topic with programming examples and questions
regarding the WebControl PLC.

I start with a question about running hours.
I would like to have a counter that register ie. for how long time a DO or DI had the status 1.
Is this possible ?


Regards
Staffan
 
I would create a second counter (or minute or hour counter depending on your desired resolution).
Whenever a new second starts, the subroutine would set the variable for the next second, and check the status of the output and count how many seconds it has been on for.

I will write it out in common English, you can turn it into plc code.

Main
If current second = variable 1
then goto subroutine increase second
End


Subroutine increase second
If variable 1 is less than 60
then add 1 to variable 1
else set variable 1 to 0
If input 1 is equal to 1
then increase variable 2 by 1
else set variable 2 to 0
return

In summary, the main body will send you to the subroutine once per second. The subroutine will increase the variable which tracks the current second so the main body program knows when a new second has started. The subroutine also counts how many seconds an output has been on for. You could add as many outputs/inputs to the subroutine as you want.
 
Hi Lou Apo !
Thanks for your answer. It looks intresting that its doable, however,
I forgot to tell that I´m a rookie at programming, so if you, or anybody else
would like to give me an example in "webcontrol language" I would be very happy !

/S
 
I think the first two lines I would type in PLC code are:
start
end

Then I would add logic to it one line at a time. Almost all the TSTxyz commands are required to have an action line following it, which means, if the TEST result is TRUE, it will execute that line, if that is FALSE, the line right next to the TEST command will be skipped.

For tracking how many seconds, do you do that 24 hours a day? I noticed by accident that CT actually has value of the seconds of the day. If you do
SET VAR1 CT
you will see the seconds is being updated. But at the beginning of the day, that would become zero. You will need to add that with CD (current day) to avoid getting zero value at the beginning of each day.
 
START
TESTEQ VAR1 CS
CALLSUB COUNT
END

COUNT
INC VAR1
TSTGT VAR1 59
SET VAR1 0
TSTEQ IN1 1
INC VAR2
TSTEQ IN1 0
SET VAR2 0
RET

I have not tested this, but the above program would start to become functional at the start of the next minute (or when the current second is equal to variable 1 should variable 1 not be starting off at 0). Note: Variable 1 must have an initial value between 0 and 59 so if you have changed via some other program to a number out of that range, you must reset it. After that, the subroutine will run once per second at the beginning of the second. If input 1 is on, it will add 1 to variable 2. If input 1 is off, it will reset it to 0.
 
In the firmware 3.2.11 for HW rev 2.2.2 board, we introduced a new operand, CTS, Current Total Seconds, seconds since 1/1/2000. CTS is a 32 bit signed integer, size like VAR or RAM, but read only. We also added four 32 bit read only parameters, UROM1...UROM4. The purpose of UROM1-4 is for anyone developed his own PLC code, but need space to store some tunable parameters.

SET VAR1 CT
may work, but not accurate and not recommended. If you need accurate time reference, please use CTS:

SET VAR1 CTS
 
I just sent my board in for the new firmware. When I get it back, I hope to be able to contribute to this discussion. I need multiple timer capabilities and the new CTS operand will be welcomed. I will be using my board for solar hot air collector and blower control/monitoring.

I just joined here to participate in Webcontrol discussions. I am very impresssed with the board capabilies and have five temp sensors, a humidity sensor and a couple of solid state relays.

My regular hangout is in the Yahoo Simply Solar Group. We are active in solar air and water DIY projects. I hope to share my code with the groups.

Many thanks to this group and to CAI for providing this support forum. It is nice to see this level of vendor support for end users.

Marty in SC
 
EDIT: I thought of a better way to do this. What I wrote in this post should work, but I was able to shorten the program, use one less variable, and add the option for additional features that would only happen on bootup.

CTS I'm sure will be a nice feature. I rewrote the above program using CTS. The program needed a few more lines but it will startup immediately on boot instead of waiting to the top of the minute. It will make no difference what VAR1 is at boot, it will correct VAR1 to allow for instant activiation of the input/output time counters.

The premise is that VAR1 stays one second ahead of cts and as soon as cts catches up to VAR1, it runs the subroutine which
1) checks the input/output status and
2) sets var1 ahead 1 more second again to wait for cts to catch up again and trigger the subroutine again.

I added two provisions for the immediate startup regardless of what var1 might be relative to the current total secdons on boot. First, var1 should never be less than cts, so if it is, it resets it to cts. Second, var1 should never be more than 1 second higher than cts, so if it is, then it gets reset to cts. This second provision requires the use of another variable, I used ram1.

START
TSTEQ VAR1 CTS this line and the next keep checking for total current seconds to catch up to var1
CALLSUB COUNT
TSTLT VAR1 CTS this line and the next check if var1 is less than current total seconds
SET VAR1 CTS
SUB VAR1 CTS RAM1 this line and the next 2 checks to see if var1 is more than 1 second higher than CTS
TSTGT RAM1 1 the only way it could be higher is if the time was reset or someone manually set var1 higher.
SET VAR1 CTS
END

COUNT:
INC VAR1
TSTEQ IN1 1
INC VAR2
TSTEQ IN1 0
SET VAR2 0
RET

A nice feature to add to the CAI might be to have a command for doing "on boot" so you could set variables/outputs or run a specific subroutine only on boot.
 
I thought of a better way to do this. It relies on the fact that all the ram values revert to 0 on boot. So, if ram1 = 0, it calls the subroutine onboot. You can put anything you want in that subroutine that you want only to happen on boot up. For example, I had it reset var1 to 0. You also might have it send you an email announcing a power cycle. I can still use ram1 for other purposes provided they never set it to 0.

START
TSTEQ RAM1 0
CALLSUB ONBOOT
TSTEQ RAM1 CTS
CALLSUB COUNT
END



ONBOOT:
SET RAM1 CTS
SET VAR1 0
RET

COUNT:
INC RAM1
TSTEQ IN1 1
INC VAR1
TSTEQ IN1 0
SET VAR1 0
RET
 
Lou, I've searched all over the Internet, and I've decided you are the best coder for CAI PLC out there! (except for CAI Support :-). If there are others, I hope they find us here!

Thanks for sharing your expertise. People like me that want to take these boards to their limits need people like you looking over their shoulders.

Question: Do I understand the new UROM1-4 parameters to be nonvolatile storage for variables that will survive a reboot or power cycling?
 
Another question. Is there anyway to comment out a line of PLC code without deleting the code itself? Or anyway to add a comment to a line of code while keeping the code active?
 
Another question. Is there anyway to comment out a line of PLC code without deleting the code itself? Or anyway to add a comment to a line of code while keeping the code active?

There is certainly nothing written in the manual that states you can put comments in the code. But you could certainly save your plc code in a MS word file and comment all you want on it there.

I don't really understand the UROM. How do these values get set? How do they differ from var1-var8? var1-8 are also stored and survive power cycles.

Also, I do not know how CTS is set after a reboot. Also what happens during a NTP check? The second program I wrote makes no provisions for the clock changing after it has started running. The first program would self correct with a clock change. So perhaps some blending of the two programs would be best.
 
Thank you Lou Apo and CAI !

I have tried the counter, and seems to work. I have changed CS to CM when it suited my aplication better.
A question, can I use RAM1 insted off VAR1 ?
I would like to save the VAR for importent things that I want to see in the GUI.

I think I have to get my hands on the latest HW version :)
(I dont have the latest HW with CTS)

-CAI....any news on the new 32 bit WebControl ?
It would be nice with a data sheet and time of delivery.

/Staffan
 
Yes, you can use RAM storage for anything that doesn't need to survive a power failure or that you don't need displayed or otherwise querried (it can only be tested equal/not equal in PLC programms.
 
I don't really understand the UROM. How do these values get set? How do they differ from var1-var8? var1-8 are also stored and survive power cycles.

Also, I do not know how CTS is set after a reboot. Also what happens during a NTP check? The second program I wrote makes no provisions for the clock changing after it has started running. The first program would self correct with a clock change. So perhaps some blending of the two programs would be best.

Hi Lou,
A customer who wrote his PLC program for controlling certain things. However, his installation environment is different from one to the other. Instead of changing the PLC code, he rather to have tunnable parameters to set. (big house vs. small house, dark beer or light beer, etc) So we added four 32bit signed INT in the "general" screen. User can enter values there and being used in its PLC logic. If the PLC code is for one time use, it probably does not need to use UROM1..4. (VAR1...8 value is assigned in PLC code, they will not keep previous value after restart.) In the PLC logic, UROM1...4 is read only.

CTS is automatically based on current time minus seconds from 1970 to 2000. The reason for doing that is that all the variables is signed 32 bit INT. If we just use total seconds from 1970, by the time 2038, the firmware will be no good. By minus the seconds since 1/1/2000, the firmwre will be able to continue work through 2068. If you don't use NTP, the clock could drift, unless you use DS2417 real time clock chip attached to 1-wire bus. (DS2417 real time clock support added in firmware 3.2.9). No matter what, CTS is based on your clock setting. If you set the clock to 1970, you will get CTS negative number and increasing toward positive.

CAI Support
 
Back
Top