Storing real-time data and use it while running

Hello all,
I'm working with some IMUs to estimate the position of them, and I'm trying to have a simple bias correction implemented.
So my idea is to start the IMU and let it at rest for a specific time, let's say 10 seconds. So, for the first 10 seconds, the IMU does not move and I collect the data from the IMU.
Then, at T=10 seconds, I want to compute the mean of all the values from the first 10 seconds, that I will call the bias.
From then (T> 10 s), I want to remove the bias on the sensors by simply substracting the value that I computed.
What I need is therefore to store the values that are coming in real-time, and being able to read in a User-Defined Function them while the simulation is still running. If I understood correctly, I cannot use the To Workspace block as the data cannot be accessed in a function while running.
Also, I want to avoid using a condition like 'if T==10', because for robustness I want to make sure that I will not miss this condition (what if the timer goes from 9.9 to 10.1 the sensors skipped a signal ?).
And finally, I want to compute the mean (the bias), only once, since I will use the data from the first 10 seconds to compute it, and it is therefore unoptimized to compute it each time the User-Defined Function is called.
Thank you very much for your help, I hope I was clear enough. Let me know if you need more information.

9 commentaires

Jonas
Jonas le 27 Sep 2021
Are you sure you want to calculate the mean of the sensor? This requires you to store each and every data sample and use it to calculate the mean after 10 seconds. If your sample time is 8kHz for example you will need to store and calculate the mean of 80 000 samples.
If you use the average (which I think suits your application just fine), you can simply maintain a counter and integrate the signal data.
Oops, not a native speaker, I'm not even sure what the difference between mean and average is.
Anyway, what you are suggesting could indeed be interesting. To do so, I just need to send the output of my function as an input of the same function right ? Because, for the counter I still need to store some data that I use during the simulation
mean and average are the same thing.
Jonas
Jonas le 27 Sep 2021
I am sorry, I was confusing mean with median, I thought you wanted to calculate the latter which would make the implementation more cumbersome.
Calculating the mean (= average) is quite easy.
I would use an Enabled Subsystem to calculate your mean in real-time. It has one Input, being your sensor signal. Inside, it adds each new sensor signal sample with the previously stored accumulated samples, by for example a Unit Delay block. In parallel, it also starts incrementing a counter starting from 0, also using a Unit Delay block. You bring the output of these parallel calculations to two Output blocks of the Enable Subsystem. You can enable the subsystem whenever you want, and then disable it after 10 seconds.
After you disable the Enabled Subsystem, you divide the signal accumulated value with the value of the counter. This is your mean. This calculation can be put inside a Triggered Subsystem, to calculate it once and then hold its outputs. The mean can now be used to compensate your sensor.
How about:
If/then according to clock compared to 10 .
Have three datastore locations (or a vector of length 3), https://www.mathworks.com/help/simulink/ug/data-store-basics.html
If branch: add the new input to storage(1), add 1 to storage(2), divide storage(1) by storage(2) to get new storage(3)
else branch: subtract storage(3) from the input value and use the modified value in whatever calculations.
Jonas
Jonas le 27 Sep 2021
That works too, based on the same principle.
My thinking with using the Enabled Subsystem, is that you can set the 'Output when disabled' of its outputs to 'held', and the 'Status when enabling' to 'reset', such that the counters initialise again to 0 when you want to do the process again. With using Data Stores, you will need something additional to reset them to initial values if you want to calculate the bias again.
I tend to think of "disabling" a subsystem as being an expensive operation; I do not know if that is true but I imagine it as expensive ;-)
Jonas
Jonas le 27 Sep 2021
In code generation at least, an Enabled Subsystem just encapsulates the subsystem in an if-statement. So I am not really that worried about it being expensive; a disabled subsystem is just skipped in execution.
The two settings I mentioned before, are handled with an additional if-statement maintaining a Boolean which records if we are enabling for the first time or not. Using this boolean, it initialises the used variables inside the subsystem.
Thanks to both of you.
I've used Jonas' way and it works fine. Did not use an Enabled Subsystem though, but I did something similar.
Thank you Walter for your solution too!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multicore Processor Targets dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by