<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/</id><updated>2020-02-27T14:02:13.373000Z</updated><subtitle>Recent posts to Recursive calls of POUs</subtitle><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#7571" rel="alternate"/><published>2020-02-27T14:02:13.373000Z</published><updated>2020-02-27T14:02:13.373000Z</updated><author><name>schnasseldag</name><uri>https://forge.codesys.com/u/schnasseldag/</uri></author><id>https://forge.codesys.com54cdc251787541fae57483b4e2285371e6908b9f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Not sure whether there are enough of right answers given to judge on the usage or avoidance of recursion in motion controlled systems. However, in order to answer myself a question on where VAR_INST variables are located I created a "5-line" recursive function. You'd find it here: &lt;a class="" href="https://forum-de.codesys.com/viewtopic.php?f=10&amp;amp;t=5514" rel="nofollow"&gt;https://forum-de.codesys.com/viewtopic.php?f=10&amp;amp;t=5514&lt;/a&gt;&lt;br/&gt;
So in principle it is possible but I'd avoid it too if there's another way of doing the same. &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#c969" rel="alternate"/><published>2020-02-27T14:02:13.085000Z</published><updated>2020-02-27T14:02:13.085000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.coma4783d1fc5bed925a8717f3bec671b874be78c1c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; rickj&lt;/p&gt;
&lt;p&gt;Like everything else, recursion done incorrectly can lead to big problems.  However, the converse is also true.&lt;/p&gt;
&lt;p&gt;Use methods, don't pass whole data structures as parameters, pass index/ptr instead, don't pass unnecessary parameters, limit absolute and per scan depth and observe rules of re-entrancy.  Of course, the same advice applies to iteration methodology just as well.&lt;/p&gt;
&lt;p&gt;And of course there is always the possibility to create separate (lower priority) tasks for data intensive algorithms whose execution time exceeds process control scan time requirements.&lt;/p&gt;
&lt;p&gt;Several years ago I built a control system comprised of hierarchically linked objects.  Each object kept a directory of it's immediate children.  I implemented a very simple search-by-name method that took a path string as input and returned an object reference.  The method stripped out the first element of the path, if a child of that name was found in the local directory, then the remaining path was passed to the child object's find method.  The process would continue until the remaining path was empty or maxdepth was reached.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#b0de" rel="alternate"/><published>2020-02-27T14:02:12.815000Z</published><updated>2020-02-27T14:02:12.815000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.comc8e33e82b35738c4a9e499b943c7619f4476a4e7</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;It s clear that a recursive code could cause stack troubles. But this kind of code could avoid the use of nested "fors".&lt;/p&gt;
&lt;p&gt;A recursive code could:&lt;/p&gt;
&lt;p&gt;Launch an stack exception&lt;br/&gt;
Launch an timeout exception.&lt;/p&gt;
&lt;p&gt;The exception stops the plc code and safety plc would have to go to safe state (if it is an ethercat slave f.ex)&lt;/p&gt;
&lt;p&gt;If we use "fors" (which normally runs over arrays) can cause the code is more complicated, dificult to read, and it is not dificult to make sintatic mistakes, and boundary problems on arrays. This errors can be more dangerous (if the array manages axes positions f ex.) than an exception, because the plc don t stops, has an error, and we don t know where.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#424f" rel="alternate"/><published>2020-02-27T14:02:11.959000Z</published><updated>2020-02-27T14:02:11.959000Z</updated><author><name>schaepper</name><uri>https://forge.codesys.com/u/schaepper/</uri></author><id>https://forge.codesys.com947d93cc07bdf9cca9795ad9913bd522fa308d76</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;In general i would also try to avoid recursion, even in non Machinecontrol environments. But there are several cases where its way better than "doing it with iterations".&lt;br/&gt;
If you have do it for conceptional reasons, keep in mind that doing it with a "Function" will always copy all your variables on a call, on a function block it doesn't. Depending on what you want to achieve this is a not so obvious difference between Functions and Function Blocks.&lt;/p&gt;
&lt;p&gt;Function Block:&lt;br/&gt;
&lt;a class="" href="https://help.codesys.com/webapp/cds-obj-function-block;product=codesys;version=3.5.11.0" rel="nofollow"&gt;https://help.codesys.com/webapp/cds-obj ... n=3.5.11.0&lt;/a&gt;&lt;br/&gt;
"After execution the values of the output variables and the internal variables are retained until the next execution. This means that the function block does not necessarily supply the same output values with the same input variables when called repeatedly."&lt;/p&gt;
&lt;p&gt;Function:&lt;br/&gt;
&lt;a class="" href="https://help.codesys.com/webapp/cds-obj-function;product=codesys;version=3.5.11.0" rel="nofollow"&gt;https://help.codesys.com/webapp/cds-obj ... n=3.5.11.0&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;"Functions have no internal status information, which means that functions do not save the values of their variables until the next call. Calls of a function with the same input variable values always supply the same output value. Therefore, functions may not use global variables and addresses!"&lt;/p&gt;
&lt;p&gt;Keep also in mind that if you do it with methods, all the variables provided in the method siganture are copied to the stack. If you would pass large objects in functions method and you have deep callstacks because of the recursion, callstack size resp. memory "waste" could be worth looking at.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#2b52" rel="alternate"/><published>2020-02-27T14:02:11.602000Z</published><updated>2020-02-27T14:02:11.602000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.come16cce4dfa545d482cf41e5c35f5c858ebe5725a</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks Scott.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#4a07" rel="alternate"/><published>2020-02-27T14:02:11.182000Z</published><updated>2020-02-27T14:02:11.182000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.comdeff96f91e136968ffc71f97f7f7cec2a5dc2eac</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;You can find the programming guidelines at the PLCopen.org website:&lt;/p&gt;
&lt;p&gt;&lt;a class="" href="http://www.plcopen.org/pages/pc2_training/index.htm" rel="nofollow"&gt;http://www.plcopen.org/pages/pc2_training/index.htm&lt;/a&gt; m &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#f41e" rel="alternate"/><published>2020-02-27T14:02:10.796000Z</published><updated>2020-02-27T14:02:10.796000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.comf9eb68783ea1fdfcee96866a2799bf3b8e6f14e6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;"This" is simply the pointrer to the fb from which "this" is called ( like c++ or java...).&lt;/p&gt;
&lt;p&gt;But this^() executes the code wrote in the body of the function block.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#84aa" rel="alternate"/><published>2020-02-27T14:02:09.734000Z</published><updated>2020-02-27T14:02:09.734000Z</updated><author><name>Sinuhe</name><uri>https://forge.codesys.com/u/sinuhe/</uri></author><id>https://forge.codesys.com832d96f38c2fab3d76a26cfc1290419a1e114bb1</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks for Your answers.&lt;br/&gt;
Of course i know the danger of using recursion, but in first step I just want to check possibilities and the calculation if any will be made out of machine process. The next step would be change the recursion to iteration. &lt;/p&gt;
&lt;p&gt;Josep M. Rams, please tell me, where can I find more information about &lt;span style="color: #BF0000;"&gt;THIS^()&lt;/span&gt; directive? &lt;/p&gt;
&lt;p&gt;Best Regards&lt;br/&gt;
Sinuhe&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#1604" rel="alternate"/><published>2020-02-27T14:02:09.350000Z</published><updated>2020-02-27T14:02:09.350000Z</updated><author><name>josepmariarams</name><uri>https://forge.codesys.com/u/josepmariarams/</uri></author><id>https://forge.codesys.com09b7d159b0554ecb2ec5aadb80671ee2eb94f5b4</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;scott_cunningham hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
Be careful with recursion and machine controls.  The PLCopen programming guidelines state recursion should not be used in an application.  Iteration should be used instead.  See attached PNG of the pages.  Computer theory loves the "tidiness" of recursion, but it can be big trouble for hardware.  It's no big deal if your PC hangs an extra 10 or 20 ms because of some deep recursion (you may not even notice) - but for machine coordinated motion, this is a big no-no.&lt;br/&gt;
Good luck!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Hi Scot.&lt;/p&gt;
&lt;p&gt;It is possible to know where to find this document?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Recursive calls of POUs</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/77f4b0197f/?limit=25#6ef2" rel="alternate"/><published>2020-02-27T14:02:08.932000Z</published><updated>2020-02-27T14:02:08.932000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com2884e0cdc32e6ca642ff71163a3dab15532cb0e6</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;Be careful with recursion and machine controls.  The PLCopen programming guidelines state recursion should not be used in an application.  Iteration should be used instead.  See attached PNG of the pages.  Computer theory loves the "tidiness" of recursion, but it can be big trouble for hardware.  It's no big deal if your PC hangs an extra 10 or 20 ms because of some deep recursion (you may not even notice) - but for machine coordinated motion, this is a big no-no.&lt;/p&gt;
&lt;p&gt;Good luck!&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: plcopen_coding_guidelines_v10_recursion1.png" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3054"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="IMG: plcopen_coding_guidelines_v10_recursion2.png" rel="nofollow" src="https://forum.codesys.com//download/file.php?id=3055"/&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>