1909 Old Mountain Creek Road, Greenville, SC 29609 tel:864.233.8330 fax:864.233.8331
 

SMt Applications

PLC Type Control

In the example below, by using SMt, the Parallax BS2 is able to emulate 4 Batch Counters effectively in parallel, where each handles Counting, End of Batch Output with Timing, and PC communication. The only additional resources compared to  partially emulating one B/C at a time, is 1 bit + 4 nibbles of RAM. 

PLC Template:

Both the Robotics & PLC Templates take care of the general organization. User code is essentially the same as for single-tasking, except that execution never waits for a required condition.

The PLC Template introduces two important functions not needed by the Robotics example - Individual Timers and Sequencers. 

Timers:

The Timers are effectively a background task, they are updated immediately after each new Tick, and prior to reaching the User code. They operate on whatever variable size the User chooses for each Timer - no other resources are needed - see Fig 2B below. The finest resolution is determined by the User selected Tick period.

One factor in deciding that Tick period is when Counters are used. The ideal count signal would be one change of state per count, as with the Robotics wheel encoders. But normally there will be two transitions per count. To be sure that counts are not lost, the Tick duration must be less than the shortest high or low period of the signal being counted.

The Template also includes a 'SnapShot' of inputs after handling the Timers. It ensures that all the subsequent logic decisions will be using the same data.

Sequencers:

Programmable controls are especially valuable when dealing with batch type processes. In those applications the control needs will vary with each stage of the sequence and are easily accommodated by a Stamp type controller. Especially if many parallel Timers are available.

When writing code for such a sequence, progression from one of those stages to the next is dependent upon specific conditions. Once those conditions are met, then that section of code is 'left behind', and will not be re-entered until the whole sequence is repeated. It is an inherent part of the program flow - there is no need for any mechanism to prevent that code from being repeated. 

That holds true for single-tasking, and also for interrupt driven multi-tasking. The 'no overheads' SMt does not switch Tasks transparently, but openly exits code whenever it does not see the condition it is seeking. This 'planned' non-interrupt exit has the advantage that there is nothing to be preserved. But by abandoning all stages of the sequence there is no inherent indication of what has been completed up to that point.

Fig 2C shows that this is resolved by maintaining a record of the last Stage# reached in a single nibble for each Task. The next time the Task receives CPU time, it re-enters its Sequencer always at the same point, but is then re-directed according to its Stage#. 

Not all applications will need sequential control, and therefore in some cases each pass may use the same logic. In such a case the Sequencer could be thought of as having just one stage.

 

 

 

 

Combining with microGUI for a Visual Demo:

Hardware:

The hardware shown is taken from the Robotics example but the needs of both examples are identical.

Software:

BS2 Code:

With the hardware still fresh in mind, take a copy of Demo_PLC.BS2 from the CD's \Green directory and re-allocate the DPin, CPin and Tick pins as needed. Already the code for the End of Batch outputs has been commented out and the count inputs are simulated. This Demo can be run without regard for port pins other than those just connected above.

At the same time look for the following code under 'INITIALIZATION':
Shiftout DPin, C1Pin, MsbFirst,[$05,$1D,$00,6]
If you are using an RS232 buffer rather than the resister shown above, then change the
$1D to $15. Adding '8' to baud rate#5, says 'Using a resister / not-inverted'. It is now ready to be loaded.

 

PC Code:

There is a Demo version of microGUI provided as a simple means of interfacing with the PC. It is self-extracting and can be found here: \ Demos \ microGUI \ microGUI.exe.

Once installed, replace the startup project with \ Projects \ Demo_PLC.Prj to achieve the screen below. The Comm settings need 28,800 Baud, Full Duplex and Header Length options.

The sole purpose is for the Stamp to run as many Timers and Counters in parallel whilst still communicating with the PC bi-directionally at 28k baud. The fact that as a system it is not very practical, does not matter here.

Five times a second the 4 gauges are updated with their current remaining count. At the same time the BS2 is fed the 4 slider values as being the counts for the next batch.

 If watching the gauges drop down, there is a slight delay at zero as they output a high for a timed duration before they restart using the latest slider value. All the time each Batch Counter is running its own Sequence independently.

The accuracy of Batch counts is considered important so the BS2 used this SMt setup of 5, 16+8+5, 0, 6 where the addition of 16 (see table at end of page) calls for a checksum on all 'PC to Stamp' data. 

The returned data could also have a checksum but it is not important if for one fifth of a second the user sees the gauge with an incorrect length. It is there if needed.

Note that there are not sufficient I/O pins on the BS2 for all counters to have an output, and so the listing has those outputs commented out. But they still use the  Timing and Counting functions.

Sequencing a Batch Counter

Sequencer Code for one of the Demo Batch Counters:

TASK0:

 

Branch Stage_B0, [NewBatch_0, Count_0, EndOfBatch_0]

NewBatch_0:

 

Cnt_B0 = Next_B0

Load the batch size (from Slider)

Stage_B0 = 1: GoTo Exit_B0

Adjust Stage then Exit

Count_0:

 

IF Old_B0 = Inp_B0 THEN Exit_B0

If Count signal unchanged then Exit

Old_B0 = Inp_B0

 else record the change

IF Inp_B0 = 0 THEN Exit_B0

If a -ve edge then Exit

Cnt_B0 = Cnt_B0 - 1

 else decrement required count

IF Cnt_B0 > 0 THEN Exit_B0

If count not complete then Exit

High End_B0: Tmr_B0 = 1

 else start the timed 'End' function

Stage_B0 = 2: GoTo Exit_B0

 adjust the Stage then Exit

EndOfBatch_0:

 

IF Tmr_B0 > 0 THEN Exit_B0

If Timer not complete then Exit

Low End_B0: Stage_B0 = 0

 else adjust for a new cycle and fall

Exit_B0:

 through to Exit

Comparing to Single-tasking Code

If the above code was written for a single-tasking Stamp, every conditional test would be exactly the same as the above. But:

  • Every time a conditional test failed, the code would patiently try again & again. From the entry point to the exit would be the duration time to complete the batch. Whether seconds, minutes or days.

  • Whereas execution of the above SMt code, will typically, never last longer than one millisecond between Entry to Exit. Yes, it will return 40 times a second in the Demo - even so, that is a savings of 960mS every 1 sec - 96% of BS2's CPU time is saved!

A Set of Guidelines to create a Sequencer:

  • Within a specific sequence, every label in single-tasking code that is used for waiting on an event, becomes the start of a new Stage. That label is then included in the entry 'Branch' command.

  • Exit immediately when a test fails

  • When a test succeeds, take the required action, adjust the Stage if necessary and Exit immediately

Remember that for processes larger than the Demo, one sequence may well be nested within another. But every sequence has its own Stage count and hence does not create any problems.

 

 

 

 

More PLC type Code

The Batch Counter example is essentially a PLC type application. It has simultaneous operations to perform, and those operations each have their own sequence to follow. There are several independent timers, plus counters, and everything works effectively in parallel. 

The following BS2 code handles slow process control type operations: to control a filling operation, debounce a level switch, monitor for filling problems, start a motor, and check its speed. Again, virtually impossible for a standard BS2 or other single- tasking Micro on its own.

The SMt setup bytes will be: 40, 8+5, 0, 6 (see table at bottom of page) where the 40 is to create a Tick every 200mS. Nothing is planned for the communication, but it is in place at 28k and no checksums. A user may wish to add code to send level switch states and return Timer values, Stage#, Port bits, and Alarm for display.

The choice of a 200mS Tick is to emphasize how opening and closing industrial process valves does not need 10mS reaction times. At the same time it allows a single pass to use lengthy statements. In this case the PulsIn is used to measure the motor speed. If the motor is not running, the PulsIn command will take the maximum time of 131mS which is well within the 200 target. If longer tasks are needed then 400/1000mS is also OK. Task switching only needs to satisfy the Application's needs - no one else's. 

 

Code for PLC type process operations

FillAndRun:

Branch Stage, [Fill_Start,Fill_Active, Run]

Fill_Start:

High Fill_Valve
Sec_Tmr0 = 120
Stage = 1

Will allocate a Stage to starting so a Timer can be loaded one time, & act as an alarm for low water pressure or similar delay

Fill_Active:

IF Sec_Tmr0 > 0 Then Fill_A0

High Alarm

Exceeded limit, leave valve open for inspection & fall thru to keep monitoring

Fill_A0

 

IF Pump_Lev = 1 THEN Fill_A1

Sec_Tmr1 = 8

With every glitch we restart timing - (2nd Timer now active, many more available)

Fill_A1:

IF Sec_Tmr1 > 0 THEN Exit

Low Fill_Valve
High Pump
Sec_Tmr2 = 7
Sec_Tmr3 = 30
Stage = 1: Goto Exit

Pump level has been maintained glitch free for 8 secs so safe to stop filling and start the Pump - allow 7 secs to get up to speed - call Operator after 30 secs total of running. (Note that Tmr0 & 1 could be re-used, but emphasizing the abundance of BS2 Timers.)

Run:

IF Sec_Tmr2 > 0 THEN Exit

Give Pump chance to reach full speed

PulsIn PmpSnsr, 0, Tmp0

TimeOut comes at 131mS, we have 200 - not putting to use - have demonstrated that Ticks can still accomodate precision timing

IF Sec_Tmr3 > 0 THEN Exit

High Beacon

Could create another Stage here

Exit:

SMt Initialization Bytes:

  Byte#1 Byte#2 Byte#3 Byte#4
Tick Period Baud Rate Qualifier Receive Length
Details Units of 5ms 0-1200
1-2400
2-4800
3-9600
4-19200
5-28,800
6-38,400
7-57,600
Qualifier byte that must be received before accepting data Number of bytes to receive
Special cases None +8 says Resistor Input (no RS-232 buffer IC)

+16 says a CheckSum at end of Data  

Zero says a Qualifier will not be used Zero says the 1st data byte will specify the number of bytes to follow 
Example 5

25mS

8+5

No C/S, Resister I/P, 28k Baud

0

No Qualifier

6

6 Bytes

 

SETTING UP THE SMt:
Shiftout DPin, C1Pin, MsbFirst,[$05,$05,$00,6] 
$05 for Real-Time Clock specifies 5*5=25mS Ticks
$0D for Serial Receive specifies ---/---/---/CSum//INVERT/BD2/bd1/BD0
No checksum, Resistor 'Buffer', 28.8kBd (0-5=1.2 2.4 4.8 9.6 19.2 28.8)
$00 for Qualifier means 'None'
$06 specfies 6 bytes - the same length as set for the PC's Transmit Buffer
PulsOut C1Pin, EOXfer
Says 'End Of Transfer' - avoids specifying message lengths, & allows aborts
 

Download Example Files