Do functions listen to global variables under while loops (Stopping functions)
2 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 20 Sep 2019
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.
Accepted Answer
dpb
on 18 Sep 2019
pressure = readDevicepressure
while pressure == 0
(x, y, z) = function(move the pressure reader to x,y,z)
end
only reads the pressure before the loop starts; there's nothing that updates it (unless your function reads it again internally).
If you want to be continuously monitoring the pressure, then do so--
pressure = readDevicepressure;
while pressure == 0
(x, y, z) = function(move the pressure reader to x,y,z)
pressure = readDevicepressure;
end
2 Comments
dpb
on 18 Sep 2019
That is true, yes, this only polls the pressure sensor when the function completes.
The typical RT system control loop in polled operation ensures there's no section of code longer than some allowable time interval before inputs are polled.
Otherwise, one needs it to be interrupt-driven, not polled. In ML that would be a timer in the base product; I don't know if there are other features in some other toolboxes for such purposes or not. The data acquisition TB can control specific data acq devices but whether your device falls under it or not we don't know.
More Answers (0)
See Also
Categories
Find more on Data Acquisition Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!