Do functions listen to global variables under while loops (Stopping functions)

3 vues (au cours des 30 derniers jours)
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 commentaires
rough93
rough93 le 20 Sep 2019
The code is talking to a arduino in order to send commands and receieve position feedback. I've implemented the Arduino hardware control toolbox in order to communicate through the arduino (which only acts as a middleman). It sounds like your suggestion would cause the arduino to stop all running code however, which I don't want to neccessarily do, just the current loop.
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 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 commentaires
rough93
rough93 le 18 Sep 2019
Truly continuously monitoring would have to be inside the function still though right? Otherwise this function will complete and then re-read the pressure. I need to find a way to give the motor a move-to command and read pressure simultaneously while the motor is in motion so it can stop if hits something.
dpb
dpb le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Acquisition Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by