Ana səhifə

Leopard II manual Table of Contents


Yüklə 3.21 Mb.
səhifə5/16
tarix24.06.2016
ölçüsü3.21 Mb.
1   2   3   4   5   6   7   8   9   ...   16

2Programming the Leopard II



2.1The C-Max programming model

The C-Max utility that comes with the Leopard II serves as the program editor for the controller. The editor is the very first screen that you see when C-Max is started. Before we explain the available instruction set and start to create programs, we will look at the programming model that the Leopard II and other ADI controllers use to accomplish their tasks. Understanding this aspect of the controller is key to obtaining satisfactory results and realizing the full potential that the Leopard II has to offer. The average PC user familiar with languages like BASIC or Visual BASIC may find this model unfamiliar so the following paragraphs will explain the history behind the model used and the reasons for it’s adoption for the Leopard II.



2.2Ladder Logic

The programming model used for the Leopard II is called “Ladder Logic”. This language model (as opposed to a language as such) has its roots in the industrial control field. The early control logic for industrial process control consisted mainly of switches, pushbuttons, relays, etc. and this is still used in some of the simpler applications. Many relay contacts and timers were used to accomplish the necessary sequencing and timing for the entire process. As machines grew in complexity, the number of relays and other mechanical components needed to do everything became rather large and the mechanical nature of these controls needed a lot of maintenance. Logic diagrams for these circuits often consisted of two long lines representing the power source (often “common” and 24 volts) for the controls and then each relay or button was illustrated as connecting across the two power lines. These diagrams thus resembled ladders and the schematic diagrams were called ladder logic diagrams. When computer technology advanced to the point of becoming fast and affordable enough to replace the logic part of the circuit with a computer program, the Programmable Logic Controller (PLC) was born.


In the mechanical circuits, an action like pressing a button usually triggers a series of events happening in quick succession, like closing a contact, which then activates a relay coil. The relay contacts need a few hundredths of a second to close before the next action can take place but generally, the action seems almost instantaneous in relation to the task that needs to be performed such as stopping a conveyor belt. When this functionality was transposed to a PLC, the computer needed a way to look for any one of several possible events and then respond in the appropriate way. Since a computer can only do one thing at a time, the solution is to have it scan all the possible sources for triggering events continuously (polling) and then have it perform the needed actions as quickly as possible. This is so that it could get back to looking out for any other possible triggering events. As long as the computer can react to a triggering event fast enough so that its always ready for the next event, it appears to be doing many things at once.
The Leopard II is in fact a PLC specifically designed with home automation in mind. The Ladder Logic programming model is particularly well suited to such an application because it allows the controller to acquire and maintain general status information about certain aspects of the home like temperatures, HVAC status, alarm system armed/disarmed, etc.. The program can then make logical decisions based on any or all of these facts. For example, you could have a short program segment that just sets a variable to indicate that the alarm system is armed. This variable can then be used anywhere else in the code where knowing the alarm system is part of the decision process, such a using motion sensors to turn lights on automatically if the home is occupied, but to trigger an alarm if the alarm system is armed. This allows your motion sensors to do two totally separate yet useful functions by virtue of program logic. The various expansion modules available gives the controller the ability to acquire information based on many types of inputs such as contact closures, analog voltages, environmental data, etc.


2.3Program Flow Rules

To successfully create your home automation application it is important that you understand the rules that govern program flow. The following paragraphs will explain, by way of examples, how the logic tests function and how you can combine them to create complex logic decisions based on a few well-established rules.


In ladder logic systems, the entire program runs as one long loop, never stopping, waiting, or looping at one spot. In fact, pure ladder logic is nothing but IF/THEN statements looking for conditions and then updating flags or actually activating outputs. Here is a sample C-Max program to illustrate this. The program looks for an A/1 ON command, and sends a B/1 On command 3 seconds later (note: The program listings shown were produced using the “print to file” utility in C-Max. This is explained in the utilities section of the manual):
Example #1
STATEMENT COMMENT
0001 IF X10 House A/ Unit 1, from ON Command Pair //If X-10 A-1 ON received

0002 THEN Timer #0 = 1 //start timer 0 (set it to 1)

0003 IF Timer #0 becomes > 3 //the first time timer 0 reaches 4

0004 THEN Timer #0 = 0 //stop the timer

0005 THEN X10 House B/ Unit 1, Turn ON //and send an X-10 B-1 ON command

0006 END

Look at lines 1 and 2. The A-1 ON command simply starts a timer (setting a timer to a non zero value causes to increment from that value upwards once per second). The code segment from lines 3 to 5 looks at the timer on-the-fly as the program loops to see if it is time for the next step, and will send the B/1 On command when it’s condition (the timer having become greater then 3) tests true. Don’t forget that the program loops continuously, so line 3 will test false several times before finally testing true. Each loop through a program is called a pass so we can say that several passes will be executed until line 3 tests true. Also note that the timer will increment on it’s own once per second and that no program lines are needed to accomplish that. When line 3 finally tests true, the X-10 output command will be put in a transmit buffer, freeing the program right away to keep looping. Thus functions like sending X-10 commands, incrementing timers, etc. are done “behind the scenes” to allow the looping to never stop. You can see that the program is actually multitasking when it becomes a long loop with many segments, each one looking for it’s condition to being true during any given pass.


An example of multitasking is shown in Example# 2. This program checks if an X-10 A-1 ON command is received 3 times within 10 seconds of receiving the first one, and if so sends a B-1 ON. If you simply look at the program now, you may find it difficult to grasp the entire logic; it just appears as somewhat related logic tests. If we break down the desired task into logic rules, then it becomes much more “natural” looking. Here are the rules:


  1. If we receive an A-1 ON and we had not yet started to time the 10 second interval, then count the command as being the first one and start timing.

  2. If we receive an A-1 ON and we were already timing the 10 second interval, then just count the command as one more.

  3. If we have reached a count of three A-1 ON commands, then stop timing and send the B-1 ON command since the target count was attained.

  4. If the timer shows that 10 seconds have elapsed since the first command, then the time has effectively run out and we stop timing and reset the count to zero.

Now look at the program code:
Example #2
0001 IF X10 House A/ Unit 1, from ON Command Pair //If X-10 A-1 ON received

0002 AND Timer# 0 = 0 //but timer was not yet started

0003 THEN Variable# 0 = 1 //then start with count of 1

0004 THEN Timer# 0 = 1 //and start timer

0005 IF X10 House A/ Unit 1, from ON Command Pair //If X-10 A-1 ON received

0006 AND Timer# 0 > 0 //and timer is already started

0007 THEN Variable# 0 + 1 //then increment count

0008 IF Variable# 0 > 2 //if count has reached 3

0009 THEN Variable# 0 = 0 //then reset count

0010 THEN Timer# 0 = 0 //and stop timer

0011 THEN X10 House B/ Unit 1, Turn ON //and send X-10 B-1 ON command

0012 IF Timer# 0 becomes > 10 //if 10 seconds have elapsed

0013 THEN Timer# 0 = 0 //then stop timer

0014 THEN Variable# 0 = 0 //and reset count

0015 End Program

Notice how each program segment (test commands followed by action commands) corresponds to one rule. By breaking the problem down to a set of rules and conditions, coding comes more naturally. In essence, the program continuously evaluates the conditions against the rules and immediately applies any required action or updates.




1   2   3   4   5   6   7   8   9   ...   16


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©atelim.com 2016
rəhbərliyinə müraciət