plcoder
Hello everyone!!
 
I'm a computer programmer and in the world of PLC i'm a newbie. It's different from event-based programming and I've been a bit confused with a PLC scan time. I want to get the level of a tank while it is fully fedding but my program exhibits unexpected behavior when I pooling to read a sensor.
 
analog_reading := sensor;
 
level := tank_level(analog_reading, volt_ref);
 
WHILE level < 99 DO
        VALVE_IN := 1;
        analog_reading := sensor;
        level := tank_level(analog_reading, volt_ref);
END_WHILE;
 
IF level = 99 THEN VALVE_IN := 0;
END_IF;
 
 
an LED simulating a VALVE_IN does not blink in a while loop, it only works correctly with the IF instruction in the PLC cycle time.
 
and also, I flashed a openPLC runtime on a board and wrote a program to read memory locations using libmodbus and when I try to access the holding register that stores the "level" variable, my libmodbus program exits with "Operation timed out" error. This doesn't happen if I don't use the while loop.
 
I tried adding a delay, but it didn't work. What is wrong?!
 
 
level := tank_level(analog_reading, volt_ref);
 
WHILE level < 99 DO
 
        VALVE_IN := 1;
 
        delay(IN:= not delay.Q, PT:= T#2s);
 
        IF (delay.Q) THEN
                analog_reading := sensor;
                level := tank_level(analog_reading, volt_ref);
        END_IF;
END_WHILE;
 
IF level = 99 THEN VALVE_IN := 0;
END_IF;
 
the cycle of my task0 in Resource Res0 is 20ms and my delay in loop is 2 seconds, this is a problem?!
 
taking advantage this topic...
 
Can't read an analog input into a user-defined function? The openplc editor does not allow defining memory locations (%IW0) in local variables of a function, is it true?
 
Thank you all!!
Reply 0 0
thiagoralves
If you're a beginner, I have an important tip for you: NEVER use any WHILE or FOR loops in your program. Treat as if they didn't exist in the language. They are of course useful, but in very specific cases (like filling in arrays, etc) which are not the case for beginners. The main problem with that is that PLCs are cyclic machines. If you put an instruction like WHILE level < 99, the program will get stuck in there forever, as the value of "level" is only updated at the end of the cycle.
 
I don't know exactly what you're trying to accomplish with this program, but modifying it like this might work as you expect:
analog_reading := sensor;
level := tank_level(analog_reading, volt_ref);

IF level < 99 THEN
        VALVE_IN := 1;
ELSE
        VALVE_IN := 0;
END_IF;
 
Reply 0 0
thiagoralves
On your second question, yes, you can't add any located variables in Functions or Function Blocks. Those objects are supposed to be reusable and generic, so it makes no sense to tie them with hardware. You can define variables in these blocks that will receive the value of a hardware IO, but that association needs to be made from a Program POU.
Reply 0 0
plcoder
Thank you Thiago. i got it!!
 
the ideia of PLC are cyclic machines is that are processors with synchronous IO operations: read inputs, executing your logic and write the outputs and then the whole thing happens again for the next cycle. So, one loop in the middle your algorithm will result in a infinite loop due the fact that inputs are read prior to your logic running and next IO mapping only will happen again in the next cycle.
 
if i did 10 analog reads inputs of a sensor and filling one array in a FOR loop? The FOR loop condition will be continuously evaluated until that is false, all in the same scan time? In this case, i only will have the same analog input 10 times?
 
I did written my code this way you suggested with conditional if branches. it's works.  I just have that check a level of tank, its temperature, open and close valves. I thought wrong about getting the I/O data and updating it independently the PCL scan time.
 
So...it is not possible to force the PLC to fetch the current state of a particular analog input?
 
About my second question... function blocks is a POU. I can read a analog input into a function block? already that have a internal memory?
 
thank you!
Reply 0 0
thiagoralves
Hardware I/Os won't update while the runtime is executing your scan cycle. A scan cycle is not one iteration of your FOR loop. A scan cycle is the ENTIRE execution of your project, including all tasks, programs, blocks, functions and function blocks. Everything must run until completion for a scan cycle to be considered "done". Therefore, if you make a FOR loop that iterates over an array 10 times, all 10 times you will be working with the same hardware I/O values. You cannot "force" the PLC to fetch updated hardware data during a scan cycle as that will mess up the runtime scheduling. Furthermore, the hardware driver is the piece of code responsible for bringing hardware data in and out, and you can't even access it from the PLC program.
 
On your second question, as I said, you cannot add any located variables in Functions or Function Blocks. The only POU that allows you to assign located variables are Programs.
Reply 0 0
plcoder
Thats ok!!
 
Now you did wake up my curiosity...
 
I know that in a traditional operating system like the Linux, your program uses a library function in user space. this function calls one or more system calls (libc functions are wrappers around system calls) and, after context switching from user space to kernel space, these system calls (which are part of the kernel itself) are executed on behalf of the library function in kernel space, once its task is complete, it returns to user mode.
 
a device driver is a kernel module that provides hardware functionality to a program and requires system calls to be accessed from user space app.
 
I don't know how PLC software works... it is a firmware or does it run from an RTOS perspective? You said scheduling...so is not a bare metal.
 
I imagine that the PLC scan cycle is an program in user-space. When I write a PLC program, it runs inside the OpenPLC runtime environment, which runs in the RTOS environment?
 
How does the OpenPLC runtime communicate with the device hardware to obtain data? After the driver gets the data, does it return it to the RTOS, which returns it to the plc program? Somehow I can interacting with IO through hardware driver from the PLC program, right? indirectly, like read() write() ioctl() syscalls in a linux kernel. also is possible to map a kernel address space to a user address space, the memory-mapping - mmap() - allow direct memory access between a device and user space. it would be like this in a PLC? i could have a task with high prior that read a mapped input to updated it?
Reply 0 0
thiagoralves
I can't tell you how every PLC runtime works, as that is quite vendor dependent. Each vendor decides to implement it in a different way, varying from a totally interpreted runtime to native machine code runtime. I can tell you how OpenPLC implements this. Even on OpenPLC there are two types of runtimes: one that runs on top of Linux / Windows, and another that runs baremetal on microcontrollers. For the Linux / Windows runtime, OpenPLC runs as a user application. There aren't any kernel modules, everything runs in user-space. The runtime has several modules: network, modbus, slave devices, PLC scan cycle, etc... In general, each one of those modules run on a separate thread. Your program runs inside the PLC scan cycle thread. The PLC scan cycle thread is responsible for reading inputs, processing your program, and writing outputs back. That's the piece of code that needs accurate timing as the scan cycle must be predictable and cyclic. The reading inputs and writing outputs part is just a function call to the current driver that is installed. The driver should handle that function with the appropriate function calls (read GPIO, I2C, SPI, etc) depending on which platform it is running on. This modular approach allows users to easily change hardware drivers as plugins without having to change the whole runtime. As I mentioned before, since the PLC scan cycle needs predictable accurate timing, if you try to make calls to the hardware driver to update I/Os you will mess up with this timing. Also, the driver lives in a different context, so you can't call its functions from inside the PLC program even if you wanted to.
 
On microcontrollers the situation is similar, with the exception that there isn't any OS. The runtime runs baremetal and manages all tasks in a round-robin fashion. Tasks are not preempted, and therefore must run until completion. That's why you get a timeout on Modbus if you make your runtime stuck in the PLC scan cycle task. The modbus task will starve and never be able to respond to your queries.
Reply 0 0
plcoder
I understood.  thanks for your time!
Reply 0 0
kortenbach
Maybe you should make your program using ladder logic. This forces you to think differently about the task at hand. Don't use the jump instructions though (for now).
Reply 0 0
plcoder
thanks for your suggestion but for me programming is write codes not use graphic languages
Reply 0 0
Reply