How to read every n miliseconds a double value from .txt file in Simulink for a real-time application? - matlab

What I'm doing is a real-time application for visual servoing that needs to read an X and Y values of a coordinate during running time. As the simulation runs, these values are changed in time by a Python Script and then, I need to read every n milliseconds (say 20ms) this changing values in Simulink to move some DC motors based on the said (X,Y) coordinates.
To sum up, what I need is how to read in run-time values from an external file in Simulink. I could be a .txt, or any other kind of file but it has to read the changing values in real-time.
I'd totally appreciate your help!
I've tried to read a .txt file from a user-defined simulink block, but the functions I used are not supported in simulink, like fscanf.

Related

Simulink: Simulating using from file block. Choosing a variable start point

I have a control model in Simulink which consists of two blocks. One which takes some inputs and generates three signals, x,y,z as arrays (trajectory) and feeds them to the second block as reference for the control.
I would like to be able to run this using a recorded trajectory. I have simulated the trajectory (by running the simulation once) and written the data to a mat file (signals plus timestamp). I can remove the first block and feed the mat file to the second control block and it works fine.
The trajectory is a loop. My question is, I would like to be able to start the simulation at any point in the file and I am not familiar with how Simulink manages time. If I want to start from a different point what do I need to do and can I make it continuous so that if I start from point N-1 in the file it will proceed through N and back to 1,2,3 etc.
Thanks,
Bryan
If you want to start at a different time point you won't be able to directly use the .mat file that you have created. You'll need to load the data into MATLAB and change the time vector so that t=0 corresponds to the data that you do want to start with.
Since you want to repeat the sequence, you most likely want to use the Repeating Sequence block. This would require you to load the data into MATLAB (and do the time alignment) anyway.

Recording Data from Behaviourspace

I wish to store record the output of a NetLogo simulation run in two ways:
Output of some functions at the end of the simulation creating a single .csv for all simulation runs.(This is fairly easy to accomplish)
Output of some variables of an agentset at the end of each tick and store the values in a separate .csv for each simulation.
I don't know how to create a separate .csv for each simulation run and also accomplish the two at the same time.
In terms of the efficiency, while recording the data at each tick, does NetLogo write to .csv directly or stores in memory and writes once in the end?
Note: I run the simulations on HPC using behavior space.
Tries:
Using RNetLogo I can accomplish this, but it is too slow due to the inherent time in data transfer between R and NetLogo. Also, running NetLogo in parallel from R seems difficult to do
Tried writing an if condition in reporter tab in behavior space GUI, doesn't work. It seems allow only reporters.
EDIT:
Link same question asked at d-level

Simulink From Workspace: can't use timestamps from matrix

I'm using the simulink block From Workspace to read in some audio data provided by a script. I have formatted the data in a matrix with 2 columns, the first is the timestamp and the second is the data.
In the configuration paramaters, I have specified Fixed-Step and Discrete solver. The Start time and Stop also need to be configured manually and don't seem to come from the data.
Also, in the From Workspace block configuration, I need to specify the sample time (1/44100) or I get a warning if I specify -1, to inherit from the data and then get strange sample times.
So, how can I get simulink to use only the sample times in the matrix and use the first and last timestamps as the start and stop time of the simulation?
You should be able to do what you want by doing the following:
Firstly note that your problem is by definition not fixed step, hence you cannot use a fixed-step solver, which by definition is ... fixed-step.
You must use a variable step solver.
Assuming your (2 column) input data is called simin then set the start and stop times to be simin(1,1) and simin(end,1) respectively.
In your From Workspace block set the sample time to be 0 (which should have been the default).
Also de-select the Interpolate data option; and set "Form the output after final data value by:" to zero (you won't be using anything past the end of your data set so this should be OK.
Then you need to tell the solver to take additional steps to those that it would naturally want to take.
Do this on the Data Import/Export pane of the Model Configuration Parameters.
Near the bottom of the pane there is a selection box and an edit box for doing this.
Note however that this does not prevent the solver from taking steps at other time points, it just forces it to take additional steps at the times you specify.
But because you have your From WOrkspace block to not interpolate this shouldn't be a problem either. You should put simin(:,1) in here so that the solver is guaranteed to take steps at the time points in your input data.
Note that if you want an input block that only samples at the time points in the simin time vector then the only way to do this is to write an S-function that uses the mdlGetTimeOfNextVarHit method to tell the solver what the next sample time (for this block) should be.

Extract correct data of a file in Matlab

During an integration process with variable stepsize (in concrete, ode113 integrator is used) the position of a body is determined through its acceleration which is stored in a file along with time (i.e. two columns, one for time and the remaining for the acceleration). However, because of there are failed integration steps through the mentioned process, the file for the acceleration data has more rows than the corresponding one for the position data. How I could to extract the correct data of the acceleration data and create a new file for it with the same rows as the position data file?
Is the incorrect data always distributed in a logical way? You can create a search function, looking at the indices of the matrix. Thus cut out the correct data by copying them to a new variable of the now correct length. possibly expensive interms of run time and data storage but definately surefire.

Combined XY realtime plots

Ok so there was confusion on what i was asking. I need help adjusting the algorithm I already have here. First part I need the X axis to be time and Y axis to be data read from file the user specifies the directory. This data file will be written every second so the graph should pause or graph 0 if there is no data and time is still running. The purpose of this program is that a script in linux will write to a file cpu, memory and power consumption and I have to graph that.
You can add multiple TimeSeries to a TimeSeriesCollection, as shown here.