Do functions listen to global variables under while loops (Stopping functions)
이전 댓글 표시
If I have a function running within a while loop, where the condition of the loop is a live variable (ex. a pressure sensor), will the function be stopped if the while loop condition is met while the function runs?
In this application. I have a function programmed to move motors to a certain orientation, the while loop in the main code file where the function is called is sensing if pressure = 0, if the pressure changes (~=0) before the motors have reached their orientations will the function be stopped?
댓글 수: 10
Rik
2019년 9월 18일
I'm not sure what you mean. In principle all Matlab functions run sequentially, so the condition must be true when the while statement is evaluated. If it becomes false while the code inside the loop is running that is not relevant.
rough93
2019년 9월 18일
Walter Roberson
2019년 9월 18일
MATLAB does not have "live variables". It does have global variables, which only live in one place so any change to a global through any means is known to all uses of the global.
A global can be updated inside a callback for a timer or serial port or network connection or data acquisition object or any of a number of other background callbacks.
The one issue to watch out for is that graphic object callbacks such keypressfcn are typically queued and cannot interrupt other code until the code does something that permits the event queue to be examined, such as drawnow()
rough93
2019년 9월 18일
Walter Roberson
2019년 9월 18일
You can use nested functions and shared variables.
You could also potentially move to an event driven approach where you add listeners to an event and broadcast an event when you receive a new value. However this can have more overhead and so can be slower.
rough93
2019년 9월 18일
Walter Roberson
2019년 9월 19일
In MATLAB, two functions cannot execute at the same time except when you use the Parallel Computing Toolbox (in which case each of the executing functions is in a different process.)
However:
- There are internal routines that somehow fill or empty I/O buffers in the background. It is not specified as to how they work or when they can interrupt. They do not operate at the MATLAB level, but they do alter some information that can be queried at the MATLAB level, such as number of bytes currently in the buffer. To prevent inconsistencies, there is probably some locking of the appropriate variables, but this is not certain. It is possible that they operate on a different thread at the C++ level that underlies MATLAB.
- There are MATLAB-level callbacks that can be triggered in response to I/O, such as when end-of-line is detected on a serial port, or end of a camera frame loaded. It is not clearly documented as to when these callbacks can be triggered, but probably it is between any two physical lines of MATLAB code (not at the end of each part of a compound command such as one separated with semi-colons -- but if those compound commands happen to invoke MATLAB code, then the interruption can happen between lines of that MATLAB code.)
- There are MATLAB-level callbacks that can be triggered in response to timers. It is documented that these can be triggered between any two physical lines of MATLAB code (not at the end of each part of a compound command such as one separated with semi-colons -- but if those compound commands happen to invoke MATLAB code, then the interruption can happen between lines of that MATLAB code.)
- There are MATLAB-level event listeners. At the moment I do not know what the rules are for when an event listener can be triggerred.
- There are MATLAB-level GUI callbacks such as KeyPressFcn or uicontrol('style', 'button') 'Callback' function. When these requested, MATLAB checks to see if a GUI callback is already executing, and if so then it checks the GUI object's Interruptible property to determine whether an interruption can occur, or if instead the callback can be queued. You should assume that callbacks will be queued unless you deliberately changed the object properties. https://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html . Queued callbacks are not given a chance to run until drawnow() or pause() or uiwait() or waitfor() is executed, or until figure() is asked to either create a new figure or activate an existing figure. If you have a loop of code that depends upon a variable that is changed in a callback, then you are much better to assume you need to call drawnow() to give the callback a chance to execute to change the variable.
Walter Roberson
2019년 9월 19일
I wonder about your motor control function. Is it a MATLAB level routine that is operating through the Data Acquisition or Instrument Control toolbox, continually asking for position updates and sending individual motor control commands? Or are you talking to an arduino or raspberry PI to do the motor control for you? If you are talking to an arduino or Raspberry Pi then it would seem to make the most sense to provide the pressure information directly to it and for it to make the decisions about stopping if pressure readings suggest that.
rough93
2019년 9월 20일
Walter Roberson
2019년 9월 20일
No, the arduino is looping around in its control loop. You would have it check to see if a command has come in from the host to set the goals. If not, then it should check the pressure sensors to see whether it should stop moving towards its current goal. If the sensor is okay, then it should figure out the next motor command to send and send it; if the sensor is exceeded, then it should send any needed motor stop commands and should mark itself as idle. If it has not been given a goal or has stopped moving towards a goal because of sensor, or has reached the goal, then it would mark the current goal as idling for which it would not bother to check pressure or send a command to the motor. But no matter what, it loops back checking for commands again. There is never a point where it stops running all code (unless you send a shutdown command), but there are points where it does not need to issue motor commands this cycle.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!