thiagoralves / OpenPLC_v2

OpenPLC with the new IEC ST to C compiler
http://www.openplcproject.com
GNU General Public License v3.0
189 stars 108 forks source link

Fix PLC scan cycle for OpenPLC (implemented for Raspberry Pi only) #4

Closed 0ussama closed 6 years ago

0ussama commented 6 years ago

The PLC scan cycle is not correctly implemented on OpenPLC. The scan cycle has been tested with an oscilloscope on the Raspberry Pi. The scan cycle time that was given in the plc program was doubled in the signal tested.

PLC program - Scan cycle experiment Note:Setting the input variable to output, proved the error was with the buffers not updating correctly.

plc program scan cycle experiment

The reason for this result has been researched in the software. The result was that the plc logic was executing on input signals from the previous cycle. This meaning the buffers for the input signals have to be updated before the plc program logic executes. I have implemented the code for the Pi.

IMPORTANT: The code for the main loop can only be merged in master after the updateBuffersOut() and updateBuffersIn() have been implemented for the other hardware layers.

Proposal / Pull request The result is a proposal as follows. The glueVars() method has been pulled out of the loop, It would never hurt to leave it in ofcourse (for Modbus?). Next we update the input buffers before executing the plc program logic, and then update the output buffers.

In contrast to updating both input and output after executing plc program logic.

openplc scan cycle proposal

PLC program processing In the image below we can see how PLCs process programs. And how the PLC scan cycle is implemented in the Melsec Fx Family PLCs form Mitsubishi Electric:

plc scan cycle

MELSEC FX Family Programmable Logic Controllers Beginner's Manual

Original OpenPLC scan cycle

original openplc scan cycle
0ussama commented 6 years ago

I don't have the other hardware boards to test the modifications on. That's the reason the modifications on those hardware layers are not included in the pull request. I wanted to make the issue known, and provide the solution for the Raspberry Pi (which I have tested it on).

Good luck on the major driver restructuring, this project is really solid, and with certified hardware coming around like the Pigeon RB100 (based on Raspberry Pi) for less than 150usd. OpenPLC is really promising! Keep up the good work.

thiagoralves commented 6 years ago

If you just make similar changes to all drivers it should work fine. You just need to add the input update and output update functions, and split the code inside updateBuffers into both functions, like you did with the raspberry pi driver. If you have time to do it, it will be great!

dewomser commented 6 years ago

I've tried the patch for Raspberry Pi, but but I cannot see any improvement in cycle time I use a Raspberry Pi 3 In spite of these errors, my program is compiled and running can you help ? Thank you

compiling new program... POUS.c POUS.h LOCATED_VARIABLES.h VARIABLES.csv Config0.c Config0.h Res0.c

Program compiled successfully moving files... compiling OpenPLC... Generating object files... Generating glueVars.cpp varName: IX0_2 varType: BOOL varName: __IX0_3 varType: BOOL varName: QX0_0 varType: BOOL varName: QX0_1 varType: BOOL varName: __QX0_2 varType: BOOL varName: QX0_3 varType: BOOL varName: QX0_4 varType: BOOL varName: __QX0_5 varType: BOOL varName: QX0_6 varType: BOOL varName: __MD0 varType: DWORD Compiling main program

main.cpp: In function ‘int main(int, char**)’: main.cpp:155:19: error: ‘updateBuffersIn’ was not declared in this scope updateBuffersIn(); //read input image ^ main.cpp:161:20: error: ‘updateBuffersOut’ was not declared in this scope updateBuffersOut(); //write output image ^ main.cpp:165:29: error: ‘common_ticktime__’ was not declared in this scope sleep_until(&timer_start, common_ticktime__); ^

compiled without errors

thiagoralves commented 6 years ago

It seems that you applied the patch but didn't recompile the project. The best thing to do is to get the most recent version of OpenPLC, apply the patch and then compile everything with ./build.sh

0ussama commented 6 years ago

Agree with Thiago, the functions you're calling are not being used. During the build the raspberrypi.cpp gets copied to hardware_layer.cpp (See build.sh). You're still using the version from before you've applied the patch. If you look at hardware_layer.cpp you'll find that the updateBuffersIn() and updateBuffersOut() functions are not declared.

dewomser commented 6 years ago

Compiled Oussamas fork. And it works like a charm. My test-program on the Pi is a steppermotor controller. Motor turns 4 times faster now. Great work !

For your Info: I've tried again to patch Thiagos masterbranch . It does not build now.

screenshot_20170918_191003

0ussama commented 6 years ago

@thiagoralves please review latest commit, double check fischertechnik.cpp

thiagoralves commented 6 years ago

Good work Oussama, thanks! I just had to fix a small issue on the fischertechnik driver and now the patch is merged to the master branch.