• No results found

Now right-click on rung 1 and choose Insert Empty Box from the context menu Inside the new box, where the three question marks are, type TON and press enter Then where the three

In document BECKHOFF-Twincat 3 Tutorial (Page 53-57)

question marks are above the box, type StartTON and press enter. Your rung #1 should look like

this:

The IN input of the timer controls when the timer is timing. The PT input is the “preset time” which is how long the timer will run before turning on the Q output. The ET output gives you the current “elapsed time” which is how long it has been since the IN input turned on. We want this timer to start timing right away, so replace the three question marks above the contact that feeds the IN input with the constant value TRUE. We want this timer to fire very shortly after the PLC starts, so enter the time constant T#1ms in place of the three question marks next to the PT input. That will make the timer Q output turn on one scan after the PLC starts (I did this to make sure theWritePersistentData function block will see a transition from off to on). Since we don’t need the ET output, right click on the timer and choose Remove unused

FB call parameters from the context menu. That will remove both the Q and ET outputs. Now your timer rung should look

like this:

The timer’s output Q will transition from FALSE to TRUE 1 ms after the program starts. Use the output to drive the WritePersistentData function block’s START input (highlighted):

Now we need to create another timer that will fire repeatedly after a certain interval. Insert another rung (between rungs #1 and #2), add another TON function block, this time using IntervalTON as the timer instance. Set the preset time to 1 minute. We want this timer to start after the initial startup timer is complete, and we want it to reset every time the WritePersistentData function block is busy:

Now modify rung #3 (the WritePersistentData rung) so that both timers will trigger the function block’s START input. Begin by selecting the StartTON.Q contact:

Now right click on the contact and choose Insert Contact Parallel (below) from the context menu:

Finally, there remains one problem. The StartTON.Q variable will be on almost all the time, but we’re only interested in the rising edge. TwinCAT 3 allows you to change a contact from a normally open contact into a rising edge or falling edge (also called “differentiate up” and “differentiate down”) by selecting the contact and pressing the “P” or “N” keys, respectively. Select the StartTON.Q contact and press the “P” key:

Finally, we don’t want to use the ERR and ERRID outputs. Rather than making them disappear, just delete the three question marks next to those outputs and the compiler will just leave them disconnected for now. Eventually you should consider using the ERRoutput to create an alarm to notify someone that there’s a problem saving the persistent variables. Here is the complete program:

Here’s how it works:

1. The StartTON timer fires almost immediately when the PLC starts, and that gives a single pulse to the START input. 2. During the initial write process, the BUSY output is on, which preventsIntervalTON from running.

3. When the initial write is complete, BUSY turns off and then IntervalTON starts running.

4. One minute later the IntervalTON timer completes and turns on the Q output. This triggers the START input again, which makes the BUSY output turn on, which resets IntervalTON until the write is complete.

5. When the write completes, we go back to step 3.

Before this program will run, you have to call it from your MAIN program. The QuickStartchapter explained how to call

other programs from your MAIN program so I won’t repeat that here. Remember to login with an online change to start running this new program.

When you execute this code, you can watch the C:\TwinCAT\3.1\Boot\Plcfolder and see the Port_851.bootdata file created, and then see the file timestamp update every minute after that.

Now we have a copy of the persistent data on the hard drive that is at most about 1 minute old. That will protect us if there is a runtime crash or the UPS stops working but it still doesn’t protect us from a corrupt file or a hard drive that stops working. To make sure you can recover from these scenarios, you need to make backups of the persistent data file at regular intervals to another computer.

To restore a backup of your persistent data file:

1. Restart your runtime in Config mode

2. Replace the .bootdata file with the backed up one 3. Restart your runtime in Run mode

Note that in the event of a crash, the restored persistent variables might be up to 1 minute old. That’s still a problem for part presence data, but at least you won’t lose all your setpoints. The persistent data saved when the runtime does a clean shutdown is the primary method for protecting the consistency of your persistent data. That means a good UPS connected to the PC with the USB cable and running the UPS software so it will do a clean shutdown in the event of a power loss is absolutely necessary for every production system using TwinCAT 3.

In document BECKHOFF-Twincat 3 Tutorial (Page 53-57)