Realtime streaming in Simulink?

38 visualizaciones (últimos 30 días)
Clayton Allen
Clayton Allen el 15 de Nov. de 2024 a las 2:33
Comentada: Walter Roberson el 18 de Nov. de 2024 a las 22:12
Greetings,
I have this project I am tinkering with that I need to understand what are the requirments in terms of software from MathWorks.
Tonight, I was attempting to stream mobile sensor data (via the MathWorks App) into Simulink. I wrote a script in MatLab that I think might do the trick. The goal was to "subscribe" to the Azimuth value for further play using a servo. The goal would be that the servo would ultimatley keep the same heading/position as I rotated the phone on my desk.
To start, I made a simple model with constant block the would grab the variable from my workspace and then I was trying to use a display block to see the values as I rotate the phone. But it never updates?
Do I need to buy the Simulink Realtime Desktop?
function streamAzimuth()
m = mobiledev;
m.OrientationSensorEnabled = true;
m.Logging = true;
fprintf("Streaming azimuth data...\n");
while true
orientationData = orientlog(m);
if ~isempty(orientationData)
azimuth = orientationData(end, 2);
assignin("base", "currentAzimuth", azimuth(end));
else
assignin("base", "currentAzimuth", NaN);
end
pause(0.1);
end
end
  2 comentarios
Walter Roberson
Walter Roberson el 15 de Nov. de 2024 a las 3:04
How exactly are you using the currentAzimuth base workspace variable in Simulink ?
Clayton Allen
Clayton Allen el 15 de Nov. de 2024 a las 11:49
In Simulink, I have a constant block with the value set to currentAzimuth. I realized last night that value remains unchanged during the simulation hence the definition of constant. I wonder what other source block can import a changing value?

Iniciar sesión para comentar.

Respuesta aceptada

Mark McBroom
Mark McBroom el 16 de Nov. de 2024 a las 22:16
Editada: Mark McBroom el 16 de Nov. de 2024 a las 22:18
Constant blocks are only updated with new workspace vaues if the Simulink model is: paused, Model Update, and then resumed. A better approach might be to replace the constant block with an interpreted MATLAB block and place your MATLAB code for reading the sensor inside this block. The block would output the new heading/position.
  3 comentarios
Clayton Allen
Clayton Allen el 18 de Nov. de 2024 a las 21:39
Thank you to each of you! I will try the Matlab Function block once more. Perhaps my first endeavor was not done correctly. I also used the FromWorkspace block but I kept gettting this wildly fast incrementing value rather than a heading. Perhaps that was an error in Sample Time?
Walter Roberson
Walter Roberson el 18 de Nov. de 2024 a las 22:12
From Workspace needs a timeseries() object or a timetable() object, or else needs a 2D array of data in which the first column is the time and the other columns are the values of the signal at the given time.
From Workspace block is processed once (well, I guess once per reset), and so is not suitable for changing values being updated during run-time.
The key to using MATLAB Function block is to use persistent data,
persistent m
if isempty(m)
m = mobiledev;
m.OrientationSensorEnabled = true;
m.Logging = true;
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by