Control Loop Timing

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

cgscarbo
New User
Posts: 4
Joined: Wed Feb 22, 2017 7:49 pm

Control Loop Timing

Postby cgscarbo » Wed Feb 22, 2017 7:57 pm

Hello All,

I have created a program that uses a control loop with a delay of 25ms (using Thread.sleep) to update the response of the EV3. In debugging, I have created a Stopwatch to print the timing of the loop to the console on each iteration. I have noticed that this result has fluctuated from around 28 ms up to 70 or 80 ms on each iteration. I was wondering if this is the result of the system level programming of the EV3 or if I should look in my code for possible reasons as to this. Perhaps, a longer delay would resolve the issue?

User avatar
gloomyandy
leJOS Team Member
Posts: 6047
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Control Loop Timing

Postby gloomyandy » Wed Feb 22, 2017 8:55 pm

Hi,
there can be many reasons for your loop taking a variable amount of time, including...
* Your debug output (printing to a network console can delay things)
* The code you are executing in the loop
* The Java JIT compiler
* Other system tasks
* Other threads in your code
* The garbage collector running
* probably other stuff!
Some of these can be fixed or at least made less of an issue, some you will just have to live with...
* Don't produce the output in your thread, instead store the times and print them afterwards, or from another thread.
* Structure your loop so that you take in to account your code execution time...
long loopTime = System.currentTimeMillis();
while(true)
{
loopTime += INTERVAL;
// do your code
long now = System.currentTimeMillis();
Delay.msDelay(loopTime - now);
if (now > loopTime) System.out.println("Timer overrun!");
}
The above code tries to allow for things like the time your code executes for, and for things like the delay code taking longer than you asked for. It is not perfect by any means, but it can help. There are other services provided by Java to do this sort of thing (take a look at the Java Timer class), but that they tend to schedule methods to run and that may require you to preserve state between calls.
* To minimize the impact of the JIT compiler try and arrange things so that your code runs for a large number of loops before it does anything that is critical. That way it will be compiled before it is needed for critical timing.
* The above code will help minimize the impact of other system tasks to some extent, but there is not a lot you can do to avoid them completely.
* You could adjust the priority of less important threads, or try an arrange for them to run when your critical task is sleeping.

At the end of the day leJOS running on an EV3 (which uses Linux at the core), is not a real time system. leJOS on the NXT was much closer to being real time. So for instance we could code our motor control mechanism in Java on the NXT, but had to put the code into a kernel module on the EV3. But having said that the EV3 is pretty capable.

Of the above the JIT compiler can have a big input. For instance I have some code that reads data from a lidar device, when that code runs it misses up to 100 out of 360 samples, but after the loop has been running for a short while things settle down and it only misses a single sample from time to time.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 2 guests