How to store any sensor's last 1 second's values ​​in workspace as a vector (Real Time mode)?

Hey
e.g.
joy = vrjoystick(1);
x = axis(joy, 1); % This just gives me current value of x-axis of the joystick
But I need a vector which gives me values of 'x' in latest 1 second.
Why I need this? Because one of my matlabscript.m demands values of 'x' in latest 1 second. Not just the current value of x-axis of the joystick.
% So i think there should be something always running in background of Matlab, which can give me recent values of 'x'%.
Thanks

6 Comments

How are you feeding joystick data into Matlab currently?
joy = vrjoystick(1)
a = axis(joy, 1)
'a' is the joystick data
I haven't used vrjoystick but I see that it outputs an object that interfaces with your device. I'm assuming the 'joy' output contains sensor data but I'm not sure how that's organized. It's also strange to me that the 'joy' object is being fed into the axis function to produce the current value of the joystick. I'd need to see your code and the plot it produces to help you more.
Nevertheless, would a timer object help you? You could create a timer object that continuously collects joystick data in 1-second loops such that you'll always have the most recent second of data available and you could just sample that 1-second-long vector as needed.
buffer=1; % #seconds to store
dt = .01; % discrete time step i.e sample time of steering angles value taken
n = buffer/dt; % #steering values
joy=vrjoystick(1);
s = zeros(n,1); % Preallocate vector
while true % Infinite loop
s(n+1) = 25*axis(joy, 1); % axis function will give only one value ranging -1 to +1
s = s(2:end);
plot(s)
axis([0 n -30 30])
pause(dt)
end
Result is a live plot which changes as I steer of my joystick

Sign in to comment.

 Accepted Answer

What's the sampling rate? In your plot, I assume the y axis is angular velocity while the x axis is an index of samples such that x=60 is the 60th sample. If you're sampling at 200Hz that's a sample per 5ms so x=60 is 300ms; if you're sampling at 60Hz, x=60 is 1000ms.
I assume the variable 's' is a vector whose length continually grows. Say your sampling rate is 200Hz, all you need is the last 200 samples of 's' to isolate the last 1-second of data. Within your while loop, that would be ...
sampleRate = 200 %hz
while ...
lastSecond = s(end-sampleRate+1 : end);
end
Variable 'lastSecond' will always contain the most recent (hense last) second of data. However you'll likely run into memory problems if you allow s to grow to enormous lengths.
If you're interested in collecting only the last second of data continuously and tossing any data prior to 1 second,
sampleRate = 200 %hz
s = zeros(1, sampleRate);
while ...
d = 25*axis(joy, 1);
s(1) = []; %get rid of oldest value
s(sampleRate) = d; %add newest value
end
Of course none of this is tested because I don't have a cool steering wheel interface.

3 Comments

More efficient would be
s = [s(2:end), d];
The code
s(1) = [];
s(sampleRate) = d;
can be broken down to
find size of s(2:end) and allocate enough memory to hold it. Call this new location s1
copy s(2:end) into the new location, s1
delete the old s and rename s1 to s
find size of s1 together with new element, and allocate enough memory to hold it. Call this new location s2
copy s1(1:end) into the new location s2(1:end-1) and copy d into s2(end)
delete the old s1 and rename s2 to s
This is two memory copies of most of the information.
The code I suggest can be broken down to
find size of s(2:end) together with new element, and allocate enough memory to hold it. Call this new location s1
copy s(2:end) into the new location s1(1:end-1) and copy d into s1(end)
delete the old s and rename s1 to s
This is one memory copy of most of the information.
However, it would probably be even more efficient to use a circular queue implementation, as those do not need to reallocate the memory.
@ Adam Danz Thanks for your effort & Time.
But my interest is doing all this in a parallel i.e. in background. Please read last two lines of my initial question.
please refer this ongoing question.. How can I run 2 tasks in parallel?
If you create a timer object, which was mentioned in this thread and the thread in your link, you'll still need code that continually collects the most recent 1-second of joystick data and the code offered here should help you with that.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 30 Jun 2018

Commented:

on 3 Jul 2018

Community Treasure Hunt

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

Start Hunting!