Program PLC

Frapp99

Member
Join Date
Jan 2022
Location
Italia
Posts
3
Hello! I'm an automation engineering student and I would have a problem with sampling a signal from plc. I basically have a signal that I would like to sample each tot ms and put it in an array to make statistics. The problem is that I create an array and a Tof timer that when the Q output is active takes the value and assigns it to the position of the current array through a for loop. The result is that in the array I have the first measured value in all positions, does anyone have tips to me? Thank you in advance!
 
First, understand the scan cycle of a PLC. It executes the code, then starts over. It does this as fast as possible, with one scan occurring every few milliseconds.*
If your timer's Q is 1, the PLC will execute the code that is dependent on that 1 every cycle.
You need some way of recording the value only once for each time the Q equals 1. Search this forum for the terms Rising Edge, Falling Edge, and One Shot.

When you think you have a solution, post your code.


*There are several ways of setting up the scan cycle, depending on which PLC you use. This is the most common.
 
Hello! I'm an automation engineering student and I would have a problem with sampling a signal from plc. I basically have a signal that I would like to sample each tot ms and put it in an array to make statistics. The problem is that I create an array and a Tof timer that when the Q output is active takes the value and assigns it to the position of the current array through a for loop. The result is that in the array I have the first measured value in all positions, does anyone have tips to me? Thank you in advance!


What make and model of PLC are you using? What language are you using? How are you triggering the TOF instruction? Can you post your code (e.g. a screenshot: press PrintSC; open Paint; paste into Paint; crop the image; save as PNG; attach that PNG to a post here)?

As @Rvaughan noted, the problem is that you are filling that array on every scan that the Tof.Q boolean is 1. TOF instructions execute a "Time delay-OFf" algorithm, so the TOF .Q bit becomes 1 on any scan when the TOF instruction's input rung is true, but that same TOF .Q bit stays 1 for some time delay after the input rung goes false, and several, perhaps hundreds or thousands of, scans will occur during that delay, and on each scan, perhaps less than a ms after the previous scan, the value will be written into the next location in the array.

Do you have the TOF in a circuit where it repeats, in which case perhaps the TOF .Q bit my be 0 (zero) for no more than one contiguous scan at a time? If that is the case, try sampling the measured value into the array when .Q is 0 instead of when it is 1.
 
Last edited:
Welcome to the forum.

so, this is what you have
1. a signal you want to sample every x milliseconds/seconds/ when the process is done.
2. you created an index array.
3 when the process is done, you want to take (1) sample and place it in the array
4. increment the array index
Problem, the sample taken is placed in ALL the array!
Question for you, what would cause this sample to be in all the index registers since you only want to execute the sample 1 time?
Could it be that your sampling code is running more than 1 cycle?
Hint - what instruction an you use that only runs one cycle?
or how would you create an instruction that will only execute 1 cycle while the tof timer is running?
this should help you.
also, post your code (zip it first-forum rules) and we will look at it.
we will offer hints and suggestions, but will not do your homework for you.
regards,
james
 
I use beckhof twincat 3 with structured text.
I state that I don't know whether to sample every moment or sample every 50ms, but to be able to average all these samples, for example, could it be enough to do it every 50ms?
My code:

Wait(in:=true,pt:=t#50ms);
For I:=0 to b do
If wait.Q then
Arr := L1_Voltage;
End_if
I:=I+1;
End_for

Wait is a tof, b length array and Arr is an array of REAL
 
My code:
Code:
Wait(in:=true,pt:=t#50ms);
For I:=0 to b do
   If wait.Q then
      Arr[i] := L1_Voltage;
   End_if
   I:=I+1;
End_for
It is what everyone is saying: where you put the value in the array is inside your For-Next loop. Thus, one value is being placed in the entire array in one PLC scan.

Do 2 things:
  1. Get rid of the For-next loop. PLCs always execute their entire code every scan. This makes them act very unlike the Basic/C/etc. language that you may used to. In essence, the PLC has a built-in, infinite For-Next loop. What you need to keep cognizant of is not just how the PLC will resolve the logic this scan, but next scan as well.
  2. Without the For-Next loop, you are going to have to manage 'I' yourself. You do half the job with I:=I+1. But that's going to execute every time your wait timer is done. That might not be a good idea after several seconds.

Seriously, watch those videos that Dr.BitBoy posted. They will compliment and expand on whatever you are being taught about how PLCs think.
 
I use beckhof twincat 3 with structured text.
I state that I don't know whether to sample every moment or sample every 50ms, but to be able to average all these samples, for example, could it be enough to do it every 50ms?
My code:

Wait(in:=true,pt:=t#50ms);
For I:=0 to b do
If wait.Q then
Arr[I] := L1_Voltage;
End_if
I:=I+1;
End_for


Wait is a tof, b length array and Arr is an array of REAL

Ah, @Rvaughan called it: there is a fundamental understanding how the PLC scan cycle works.

There is also something OP needs to understand about programming computers: a computer, or a PLC, does not care a whit what you want it to do, but it will always do exactly what you tell it to do. When it does something you do not want it to do, you can look at its behavior, knowing it is doing exactly what you told it, and use that to understan why it did that something, and that can lead to figuring out how to tell it to do what you want it to do.

Specifically, when I see a "problem statement" like the one in this thread:
The problem is that I create an array and a Tof timer that when the Q output is active takes the value and assigns it to the position of the current array through a for loop. The result is that in the array I have the first measured value in all positions
I say to myself, that is not the problem; that PLC is doing exactly what OP asked it to do. The problem is that the OP wants it to do something else, something other than what they told it to do.

But enough of philosophy ... ;)

The variable [I] will increment from 0 to [b] on each scan.; it will not take 50ms and several scans for [I] to increment over that range.

So, on each scan, the (typically) single value for L1_Voltage will be assigned to all the elements of Arr, from Arr[0] to Arr[b].

Furthermore, Wait.Q will always be True, on every scan; it will never be false.

Suggestions

  • Watch those first few videos, even though they use Ladder, because they explain the scan cycle very well, and that is where part of the misunderstanding is.
  • For the TOF, read the instruction documentation at this link.
    • The important part to understand is this:
When IN is TRUE, Q is TRUE and ET is 0

  • The timing diagrams at that link should also be illuminating.
  • Consider, until you understand, what the name of the TOF instruction means: Timer OFf-delay
  • Look at the documentation for the TON instruction as well.
  • In the code posted by the OP, the IN argument to the TOF function is always assigned a value of TRUE, so the wait.Q value will alway be a value of 1 (TRUE)
  • Ask The Google to search for examples of using the TOF and TON function.
  • When you post your ST code, put [/ladder] after it, and
     before it, so that the indentation is preserved.
    [*]Once you understand how the scan cycle works and how the TOF function works,
    • Write down in detail how you want the process to work e.g.
      • On the first scan at the beginning of a period of 50ms, assign 0 to the value of I, to point to the first element of Arr
      • On that first scan, and all subsequent scans until the 50ms elapses,
        • Assign the value of L1_Voltage to the value of array element Arr
          [*]Increment the value of I by 1, to point to the next element of Arr for the next scan


        [*]On the first scan after the 50ms have elapsed,
        • Do something with the values in the array.
        • Prepare to start another period of 50ms on the next scan



      [*]Once you have an acceptable description of how the process should work, you can start to write the code to make the PLC model the process, using the programming tools availableL
      • := assignment;
      • + operator;
      • IF statements;
      • TOF/TON functions;
      • etc.





 
  1. Get rid of the For-next loop...
  2. Without the For-Next loop,...
+1 I could not have said it better myself (even though I try and use an order of magnitude more words ;))

So let me try to add to this a bit.

  • A PLC is already, and always, running a loop;
  • it runs the entire program once in each pass through that loop;
  • each pass through the loop is called a scan;
  • when it gets to the end of a pass through the loop, i.e. to the end of one scan, the PLC reads the inputs and writes the outputs
  • when it finishes reading the inputs and writing the inputs, it starts another pass through the loop i.e. starts another scan.
  • The PLC repeats this cycle as long as the PLC
  • is powered up
  • AND
  • is in run mode
  • AND
  • has not faulted
 
the variable will increment from 0 to on each scan.; it will not take 50ms and several scans for to increment over that range.


+1

To the OP-

Like with conventional textual programming languages, you can set break points. I would suggest trying that to further understand what's happening in your code. However, do not set break points on a connected, live machine/process! Or at the very least, make sure everything is safe when the breakpoint is executed because it will lock all inputs and outputs in the state they were in where/when the breakpoint/s were executed.
 
  • when it gets to the end of a pass through the loop, i.e. to the end of one scan, the PLC reads the inputs and writes the outputs
  • when it finishes reading the inputs and writing the inputs (!?), it starts another pass through the loop i.e. starts another scan.

A) Usually the PLC reads the inputs before each logic pass and writes the outputs after. From a functional standpoint this is a mostly useless distinction though as it's only relevant on the first scan.
B) This is only true for PLCs that use synchronous I/O. That may be the case here (not particularly familiar with Beckhoff), but since OP doesn't seem familiar with scan cycles it's good to note for future reference regardless.

There is also something OP needs to understand about programming computers: a computer, or a PLC, does not care a whit what you want it to do, but it will always do exactly what you tell it to do.
Tends to be true these days with built-in error-checking and the like, but my dad could tell you about having a previously-working program suddenly start spitting out wrong numbers and tracing the cause to a stuck bit in RAM.
 

Similar Topics

I'm a beginner in the automation field and I've set up an automation system connecting several devices (datalogger, radio, etc.) via Modbus RS485...
Replies
5
Views
179
Hi All, want to ask. I have PLC a programme to control the valve. The existing programme is to control valve A (Y22), and I want to change to...
Replies
2
Views
136
can anyone has a good program to learn plc programming online. i have the basic looking into improve my skills thanks
Replies
1
Views
134
Hi, I am new to ladder logic. I have completed a code in the Xinje PLC XC3-32RT-E for one of the machines I am developing. Currently the program...
Replies
30
Views
935
Hi everyone i have a customer, who wants to show an alarm on the machine, if the I/O forces are enabled and set, on at ControlLogix L81E with...
Replies
3
Views
221
Back
Top Bottom