Random lags while running app

29 次查看(过去 30 天)
Gavin
Gavin 2025-7-15
评论: dpb 2025-8-4,17:53
I have an App Designed application that collects data from an Arduino Pico.
The Pico runs individual trials with accurate to submillisecond timing. MATLAB starts each trial as soon as the previous one is complete. Data is saved in tables as I found that writing to disk after each trial took too long. Even so, the time between trials is longer than it should be AND it can be extremely long on occasion. I've plotted each trial duration and the timing of the end of one to the start of the next. Any ideas as to why MATLAB occasionally takes long breaks? I've tried clearing variables between each trial (doesn't seem to affect my app tables). Removed calls to get time of day (useing Pico Timer for data) various other attemps with Profiler but no luck since the error is rare. Copilot wasn't much help either.
I tried updating to version R2025a and that was much worse!
Any help much appreciated. Code for the app is here, it's big:
https://drive.google.com/file/d/1mwCtRJ-l_2fhk26JYzz2IxidDqkU3IV1/view?usp=sharing
  16 个评论
Gavin
Gavin 2025-7-23
Using tic toc I've been tracking down where the lag occurs and it looks like it's between when I send a command to the Pico and when it gets it.
Pico is a USB connected arduino module. I'm running Windows 11. Connections is as follows:
app.PicoCom = serialport(ports(1,2),57600,"Timeout",0.1);
configureTerminator(app.PicoCom,"CR/LF");
I have no problem with incoming data, the issue is when I send the go command 'G' it varies how long it takes for Windoze to get around to sending it to the Pico.
str="G"; then call proc that has
if ~isempty(str)
write(app.PicoCom, str, "string");
While typically 20-30 msec which is fine, ever few trials it can take 500ms or longer.
Is there a faster way to do this than write()? Shortening Timeout didn't help. Is that just for incoming?
dpb
dpb 2025-7-23
Good spelunking to have uncovered the "where"...can you tell whether it is a delay from the time the MATLAB line is encountered before the transmission or a delay in actually executing the code itself?
I'm not sure there is any way you can control that, at least with MATLAB-level code; you're pretty-much at the mercy of the OS there, I think.
Only a couple of ideas to try otomh; first would be to use the low-level char() array for the data instead of the string class; this is just the string of bytes without the overhead of the string wrapper to be dereferenced on sending.
str='G';
if ~isempty(str)
write(app.PicoCom, str, 'char');
Second would be writeline which sends the terminator, too.
I don't know that if you dropped into mex and C to do the communications you could ensure better performance or not; the internals are all builtin functions already, so it would only be whether that could prevent some sort of delay in the MATLAB code itself between lines of code being dispatched.
What would it take to just build a command line interface that does nothing but ping the Arduino without any other distractions and see if that behaves more reliably?

请先登录,再进行评论。

采纳的回答

Gavin
Gavin 2025-8-3,20:53
Turns out it takes a LONG time to update a graph. I moved the screen updates to happen AFTER the start of the next trial and that seems to have resolved it, or at least vastly improved the problem.
AI had some recomendataions but basically said I was on the right track. It didn't catch the screen updating issue as I couldn't give it the whoe program at once.
Thanks to all that gave this some thought and had some suggestions.
  1 个评论
dpb
dpb 2025-8-4,17:53
Ah, so! Glad you were able to resolve it...
"...it takes a LONG time to update a graph"
Are you redrawing or updating the data in place? For speed, unless it is mandtory for other things to be updated, just modifying 'X|YData' is much faster, although if rendering itself is an issue, that won't be helped.
For realtime graphics, don't forget to look into animatedline and friends...

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2025-7-24
See if the problem goes away if you set the priority of any of your program's processes to High. Type Control-Shift-Esc to bring up Task manager. On the "Details" panel (Window 11, slightly different in Windows 10) find your processes (any MATLAB processes, your executable, and anything else related to your running of your program. Then right click and go to "Set priority" and set it to High. See if that avoids the problem.
  1 个评论
dpb
dpb 2025-7-24
编辑:dpb 2025-7-24
Raising priorities was discussed early on, IA, I do not know whether Gavin actually experimented with doing so or not, however...
It would still be interesting to see if could somehow tie in what is happening with OS context switching at the same time these delays occur...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by