rodrigo_rolle
Hi,

I tried to set up a PID block but I think I was unable to declare the variables properly. I'm attaching the screenshot. Could someone please explain the right form to declare these variables or send an example?

Thanks!
att.png
Reply 2 0
rodrigo_rolle
Nice advice, thanks! I tested some things here and got some conclusions. First of all, one thing I had already noticed: the first read the UDP Receive block gets is always a 65536 value, but anyway that's not the main problem. As you told me, I set the scan time to 100ms on OpenPLC and the comm_delay to 50ms. I wrote a constant value (1024) in the output QW0 in order to check how long it takes to the UDP Receive block get value changes in Simulink simulation time (I put -1 as sample time in the block). That was the result (the first step indicates the start of the communication):

2.png 
Correct me if I'm wrong, but that led me to the conclusion that the "simulink time" between two UDP messages is about 4 seconds, that's probably too slow for control purposes. Now I'm working to slow down the simulation environment, so that 1 second in simulation time is also 1 second in real time. Do you know a way to do something like this?
Reply 0 0
thiagoralves
The communication should not delay for 4 seconds. If this is the first packet OpenPLC is sending, you may see some delay until OpenPLC loads up and everything is in place, but you should not expect 4 seconds for every packet, this is too much. Perhaps your problem is due to your simulation time being faster than the wall clock time, as you said. To fix this, try using this block:  https://www.mathworks.com/matlabcentral/fileexchange/30953-simulink-block-for-real-time-execution

Just make sure you have Mingw installed (Matlab -> Add-Ons -> Get Add-Ons -> Mingw). Then compile the .c file with the command mex -O sfun_rttime.c from inside matlab. Your working dir must have the .c file in it for it to work. Then, just by dropping the real time block in your simulation, it be slowed down to match the wall clock time.
Reply 1 0
rodrigo_rolle
This block helped a lot, thanks! I found another similar block right after I wrote my last post but it didn't work well with some SimHydraulics tests I was also running. Now that first step caused by the communication takes approximately 30-40ms in simulation time (I set 30ms as "comm_delay" and sample time in the UDP Receive block).
 
Another reason for instability was the second-order system response, that sometimes can bounce and assume negative values. These negative values were "translated" to high values by the UDP send block (50000-plus) and after that happened, the responses got "crazy", so I inserted a saturation before the UDP Send block in order to avoid negative values.

Now I restarted trying to tune the PID block (actually I have an "off-line" simulation with a simple parallel PID block to get the parameters before trying them on OpenPLC) but the PLC output is always zero, although my setpoint is 2000. To avoid any problems I removed the 'PID enable' variable and set the PID block to always "AUTO" in the ladder logic. I also set the setpoint as an internal variable (I was setting it via UDP and I could read it on Radzio) but the problem persists. I'm using 100ms as cycle time in the PLC (resource and CYCLE parameters). The PLC gets the system output normally, but doesn't "react" in order to led the system to the setpoint. Suggestions?
Reply 0 0
thiagoralves
Can you send me your PLC project (XML file)?
Reply 0 0
rodrigo_rolle
Yes, of course. I also attached the Simulink model.
The interface.cfg code is:


num_stations = "1"
comm_delay = "30"
# ------------
#   SIMULINK
# ------------
simulink.ip = "127.0.0.1"
# ------------
#  STATION 0
# ------------
station0.ip = "127.0.0.1"
station0.add(digital_in) = "10001"  #enable do PID
station0.add(analog_in) = "10011"   #variável de processo
station0.add(analog_in) = "10010"   #setpoint
station0.add(analog_out) = "10012"  #sinal de controle


As I told you, I temporarily disabled the enable and setpoint inputs.
Reply 0 0
rodrigo_rolle
Hello, Thiago!
Did you find out something wrong in the project? I'm still struggling with this problem, I tried to redo the xml project based on that PID example that you sent, but still coudn't make it work. If you find a mistake in the project, please let me know. Thanks!
Reply 0 0
thiagoralves
Sorry Rodrigo, I'm very busy on my finals week, so I had to stall this for a while. As soon as I get back to normal life I'll take a look at your problem.
Reply 0 0
rodrigo_rolle
Hello, Thiago!
I'm following another topic in which another user ("eightbitsandwich") mentioned Wireshark as a means of debugging UDP communications. I installed the program and tried to find UDP packets while running the hello world example, but I wasn't able to find any messages. As I'm running everything on 'localhost', do you think that I won't be able to find these packets using Wireshark?
Thanks!
Reply 0 0
thiagoralves
Hi Rodrigo. Wireshark on Windows cannot capture loopback packages (which is your case). There is a workaround explained here:  https://wiki.wireshark.org/CaptureSetup/Loopback but it seems to be quite complicated. Ideally, using VMs is the preferred method to create SCADA simulations since each VM behaves as an independent computer on a virtual network. This makes the simulation more accurate to real life.

By the way, I never got an answer back about your PID problem. I don't know if you managed to find a solution for your problem, but I'm finishing a PID model on Simulink+SimLink+OpenPLC+ScadaBR. Once everything is ready, I'll send you the files.
Reply 0 0
rodrigo_rolle
Hello, Thiago. Sorry if it sounds a little bit off-topic. I'm using a Debian Linux VM (running on VirtualBox) in order to test some possibilites (especially capturing packages with Wireshark as you mentioned). I could successfully run OpenPLC on it and I'm still running Simulink on Windows. Some quick questions here:
1- Do I need to run SimLink on both sides (Windows and VM) or just on Windows side?
2- How do I discover the IP address of the VM? Can I use the IPv4 address that is provided in the network configurations page?

I still coudn't solve my PID problem, so I'm really hopeful that your model will help me to discover what is wrong in my setup. Thank you very much!
Reply 0 0
thiagoralves
Hi Rodrigo,

1. You run SimLink only once on whatever side you want. Just note that the interface.cfg file and your UDP Send blocks will be different depending on where you run SimLink. If you run SimLink on the VM, the UDP Send blocks in your Simulink model will have to have the IP address of your VM, instead of "localhost". After all, Simulink must communicate with SimLink, and if SimLink is running on the VM, the UDP Send block must have the VM IP Address. Also, on the interface.cfg for SimLink, you can leave the station0.ip as "localhost", but you will have to insert your host IP address on the simulink.ip parameter. 

Now, if you run SimLink on your Windows host, then you can leave your UDP blocks as is (which means, with "localhost" in them) but then you will have to change the station0.ip to be your VM IP address. After all, SimLink must also communicate with your running instance of OpenPLC, and it can only do it if it knows its IP Address. Usually it is easier to run SimLink on the same machine where your Simulink model is running, so just keep it running on Windows.

2. To find out the IP address of your VM, open terminal and type "ifconfig". You should see a list of all your network interfaces with their respective IP addresses. To verify that the IP address is correct, open Command Prompt on your windows host and type "ping IP-ADDRESS" where, obviously, IP-ADDRESS means your VM IP Address that you discovered. If you get answers from your ping command, it means that the IP address is probably correct and that the communication is working.

3. I have here attached my Heat Exchanger project. It has the simulink model, the interface.cfg file for SimLink and the PLC program as well. If you have ScadaBR installed, it also has a little HMI screen for it as well.
Reply 0 0
rodrigo_rolle
Hello, Thiago!

Thank you very much for sharing your project! It's being REALLY helpful. I tried to test a generic Transfer Function as "process" using your model as background (as shown in the screenshot). I just included some displays to avoid warnings such as "block not connected" and used a different block for slowing down the simulation. (I'm also using your interface.cfg and .st files)

test.png 
When I connect your process to the "Rate Transition" block (it means, when I run your process - wire "1" shown), everything works as usual (the PLC controls the plant). When I connect my process to the block (wire "2" shown), the PLC response remains always zero. I included a Product block before the TF (that multiplies the UDP Receive data by 1) because the TF needs a 'double' input, while the UDP receive provides a 'uint16' value. I was using a data type conversion block but I had problems with a similar block in previous projects, so I decided to try this solution because I noticed that it worked for your project, although you use the product for a different purpose.

The only difference I noticed between these processes is that the initial state of my TF is zero. The heat exchanger has a initial state different of zero. I think it shouldn't be a problem, do you agree?

Another (off-topic) question: I got struck while configuring my Debian Linux VM. When I set my network to NAT, I can ping from the VM to the host but I can't ping from the host to the VM. When I set my network to "Bridged mode", I can ping from the host to the VM but I can't ping from the VM to the host IP. What configuration do you use in your setup?

Thanks again for your support!
Reply 0 0
thiagoralves
Since the PLC only work with integers, we have the plant values multiplied by 100 before sending them to the PLC, and also we have the input values received from the PLC divided by 100. This is a work-around for us to have at least two decimal digits. Therefore, internally the PLC is working with numbers 100x bigger. On your transfer function, I noticed that you're multiplying the received value by 1 instead of 0.01. This multiplication, as I explained before, is not to "convert" uint16 into double, but to adjust the range between the PLC internal values and my plant. If you multiply by 1 only at the input, it means that the PLC output is 100x bigger than it should. I believe that fixing that might solve your problem. If it doesn't, it might be useful to debug what the PLC is doing. On my PLC program the PID output is stored at register %MD3 (modbus holding address 2054-2055 as 32-bit sw float). You can use Radzio to check the value inside that register to see what the PID algorithm is doing (don't forget to check 32bit sw. float or you will read garbage).

About your VM thing, it depends on which VM software you're using. Each one has a different approach on virtual networks. I like to use VMWare Player but Virtualbox works as well. I noticed that Virtualbox gives me more network issues than VMWare. Ideally, the best configuration would be "bridged", while you define only one ethernet adapter to bridge. The bridged configuration is like transforming your network adapter into two, one for your host machine and another for your VM. In this way, the router on your network will see your VM as another computer connected to the network, which makes it possible to ping and connect to your VM not only from your host machine but from any machine on the network. You can google more about networking on VMs to get more information about it.
Reply 1 0
rodrigo_rolle
Hello, Thiago!
Reading the Modbus addresses, I discovered that the PID block was providing negative values (probably because of incorrect tuning). Maybe the transfer function I picked up for testing isn't as simple as I thought it would be. But that's OK, now I just need to improve the block tuning and everything is gonna be fine.

Just one last thing I noticed: I could read, edit and write correctly the %MD addresses (Kp, Tr, Td and Output) when I chose the display option "32-bit float" instead of "32-bit sw. float" that you mentioned before.

1.png 

I think my problems with the PID block are solved for now. Thank you very much!

Edit: my VM networking problem was caused by Windows firewall. I disabled the firewall and it's working perfectly.
Reply 0 0
LiZF
Hello, may I ask how the ScadaBR in this project(heat exchange) should be set to connect with OpenPLC and receive Modbus TCP
Reply 0 0
Reply