Industrial Communications

cancel
Showing results for 
Search instead for 
Did you mean: 

Drawback of putting producer and consumer in one loop. [Ethernet IP]

Highlighted

The "Create Assembly Instance" exapmple vi have two separte loops. One for the input (producing data) and the other for the output (consuming data). Would it be possible to combine everything into one loop? Are there any drawbacks to using one loop for the input and the output?

 

and i know one drawbacks will be that both consumer and producer will have to have the same rate.. 

0 Kudos
Message 1 of 12
(3,934 Views)
Highlighted

Why even have a producer and a consumer if not in different loops. You might as well pipe your input to whatever output process you are using the data in. The purpose of have separate producer and consumer loops is the assumption that one of the two is running at a different rate than the other.  Why do you not want them in their own loops?

Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



0 Kudos
Message 2 of 12
(3,925 Views)
Highlighted

I wanted to cut back on the number of loops running in my code. Still I already have two independent loops I didn’t want to add two more loops.

 

FYI:

  1. The producer is what I am sending to the plc and the consumer is the data I am reading from the plc.
  2. I am using cRIO 9068
0 Kudos
Message 3 of 12
(3,919 Views)
Highlighted

Putting the producer and consumer in the same loop is a lot like putting your bike on top of your car and saying you're riding your bike to work.

 

Once you put them into the same loop, you no longer have a producer/consumer architecture.  The different loops is what makes this architecture.

 

Can you put everything in the same loop?  That really depends on your application.  Some applications work just fine with everything in the same loop.  The main benefit of the producer/consumer is it allows you to "produce" new data points at a rate higher than your "consumer" can handle the processing you require.  If you had to process each point as it came in, you'd end up overflowing the buffer.  Is this a concern for you in your application?  If so, you probably want to avoid this.

 

It sounds like you currently have three loops.  Is there a reason you cannot combine the other two loops? What's happening in those loops?  You really haven't given us a great deal of information to go with to tell you what the drawbacks will be in your specific application.  There may be none.  As it stands, it sounds like you have the idea of a producer/consumer backwards.  The producer should be reading from your plc.  The consumer should be sending the plc values after calculations.  Have you used the terminals to the left of your timed loop to determine how much time you have leftover after each of these loops is complete?  Can you finish both within your timing requirements?


Keep in mind, your cRIO has two cores.  Dropping to less than two loops won't provide you with any benefit.  If you have more than two loops, using priority will allow the compiler to handle a lot of what you're trying to do manually now.

0 Kudos
Message 4 of 12
(3,900 Views)
Highlighted

I figured since I am using the data just to send data and receive data it did not matter if I used one loop but you brought up several good points why I should not combine all in one loop.

 

 

Background information:

Sorry for lack of information. I currently have 2 loops. One that is used for my state machine (non deterministic loop) and another loop that is used to acquire data (deterministic loop which is a timed loop).  I currently have a working code that utilized digital I/O. In the next phase of the code I want to replace some of the digital line with Ethernet IP.  The digital line use Shave variable, which are global variable, but if I use Ethernet IP I would have to add two more loops. One for producer Ethernet IP items and another one for consumer Ethernet IP items. To reduce workload on the CPU I was going combine both into one loop. I was also fearful that having four loops would take away from the deterministic of the data acquisition loop. 

 

That way I wanted to know if I could just use three loops. One loop that will be used for the state machine, one for data acquisition (highest priority), and the other for Ethernet IP stuffs.  

 

 

* I have attached consumer & producer from NI viewpoint. This is the example code that came with NI.

0 Kudos
Message 5 of 12
(3,852 Views)
Highlighted

That's an interesting piece of code.  It's not so much a producer/consumer as it is two separate loops handling input and output.  Take a look at this link to see what producer/consumer is: http://www.ni.com/white-paper/3023/en/  You can also find the code by going to File->New and opening the "From Template" folder under VI.

 

What controls your state machine?  With events not being possible on a RT system, I'd expect you'd have some form of polling.  Polling would still use a time period between polls.  Granted, the network requirement would definitely break determinism, as you've already noted.  If you're doing this, why not put it into a timed loop?  Timed loops aren't always determinstic.  That's why there is a "Finished Late?" terminal.  It's not the loop that makes the code deterministic, it's the way you put your program together. By moving it into a timed loop, you gain the ability to give it a priority.  I'll explain why this is important in a minute.

 

You're welcome to combine the two into a single loop if you still meet your timing requirements.  That's a design choice that is up to you.  I don't know what your other Ethernet/IP "stuffs" is, but I'd likely combine this into my output loop if possible.  I'm assuming it has something to do with the data you care to send.

 

The overhead from the loop isn't enough to worry about the worload on the CPU.  Ultimately, the code within the loop determines how rough you're being on the CPU.  That's true in one loop or in four loops.  Splitting code into multiple loops just lets you prioritize code.  If everything is in a single loop, it all must run before the next iteration.  If your code is split into ten loops, as an example, only what is inside each loop must be run on that loop's iteration.  Using priority, as you've mentioned, ensures you determine which loop runs first.  Let's say that loop completes and gives the other loops time to run on the CPU.  Before they complete, the loop wants to run again.  The CPU will go back to that loop and run.  By splitting the loops up, you've ensured this piece of your code will run even if the CPU can't handle processing all of the code within that period.  Rather than hurting determinism, you've aided it.  The parts of your code that you aren't worried about being deterministic happen when the CPU has time for them.  The parts that you NEED to be deterministic happen deterministically and push those other parts of the code out of the way.

 

Looking at the example code you're showing, I'd really want to know what it is you plan to do with the code and what you need to be deterministic.  I'd assume you plan to read the data, process it, and send a corresponding output.  If you need ALL of this to be deterministic, I'd put it within a single loop or use queues to send data from the input loop (commented as consumer) to the output loop (commented as producer).  This decision would really just depend on how fast you care to acquire data and how deterministic you desire the output to be.  Without using the queues, you create something called "race conditions."  When you send an output, is that related to the newest input or one before it?  You simply cannot tell.  

0 Kudos
Message 6 of 12
(3,837 Views)
Highlighted

Hi sticyfinger,

 

Some info about the Real-Time Certification Program we have done on the EIP toolkit. The goal of the RT Certification program is to characterize the expected execution behaviors of all software that is developed for an NI real-time platform.

EIP toolkit receives a value of Level 2 - 1st Order, meaning it's non-deterministic execution: safe as a lower priority task, and the jitter is low and allows for higher system performance.

So I don't recommend that you should put EIP into timed while loop.

 

How much one addtional loop instead of a bigger loop affects the system should better be observed than assumed, right? I suggest you try Distributed System Manager. (Choose from Tools->Distributed System Manager ). Make sure you have installed System State Publisher on the target first, which will publish information like CPU and memory usage to Distributed System Manager.

0 Kudos
Message 7 of 12
(3,822 Views)
Highlighted

@sticyfinger wrote:

 

Background information:

Sorry for lack of information. I currently have 2 loops. One that is used for my state machine (non deterministic loop) and another loop that is used to acquire data (deterministic loop which is a timed loop).  I currently have a working code that utilized digital I/O. In the next phase of the code I want to replace some of the digital line with Ethernet IP.  The digital line use Shave variable, which are global variable, but if I use Ethernet IP I would have to add two more loops. One for producer Ethernet IP items and another one for consumer Ethernet IP items. To reduce workload on the CPU I was going combine both into one loop. I was also fearful that having four loops would take away from the deterministic of the data acquisition loop. 

 

That way I wanted to know if I could just use three loops. One loop that will be used for the state machine, one for data acquisition (highest priority), and the other for Ethernet IP stuffs.  


Why is your data acquisition loop highest priority but your state machine is low priority? Admittedly I have no clue what you're actually building, but generally you care about the round-trip behavior of the system. Meaning you would want to read data, perform some calculation, and return it to the hardware within some deadline. So what I'd build is 3 loops. 1 high priority loop which is your control loop and reads from any deterministic I/O (scan engine), performs the control task, and writes back to that deterministic I/O. Then use 2 RT FIFOs to get data into this loop and out of this loop from your E/IP assemblies. Then have two loops, just as shown in the example, for getting data from the E/IP API and passing it into the fifo. This keeps your control loop deterministic but lets you get the most recent information from E/IP at whatever rate makes sense for your application.

 

 


Some info about the Real-Time Certification Program we have done on the EIP toolkit. The goal of the RT Certification program is to characterize the expected execution behaviors of all software that is developed for an NI real-time platform.

EIP toolkit receives a value of Level 2 - 1st Order, meaning it's non-deterministic execution: safe as a lower priority task, and the jitter is low and allows for higher system performance.

So I don't recommend that you should put EIP into timed while loop.


I'm actually curious about this behavior myself for another application. What is actually happening when you call read or write? Is it actively sending a UDP packet/waiting on the next UDP packet, or does it just put stuff on some internal queue? If I remember correctly, there is a timeout error, but no timeout input. So what is the timeout behavior of those nodes?

0 Kudos
Message 8 of 12
(3,810 Views)
Highlighted

Hi smithd,

 

EIP toolkit has several read and write VIs.

This pair you see sends UDP, and the read/write are actually dealing with internal buffer. It's not a queue. The communication part is handled automatically once you call the EIP_Add_Assembly VI.

For other read/write which are essentially what's called explicit messaging, they go TCP, and they have time_out terminals and are blocking.

Message 9 of 12
(3,799 Views)
Highlighted

Forgot to mention, EIP_Add_Assembly VI has time out related, hidden settings in the session property to set TimerTickInterval and TimerTickPreemption for the protocol stack operation.

0 Kudos
Message 10 of 12
(3,797 Views)