How do I create a stopwatch in Simulink that stores up to 4 split values?
20 views (last 30 days)
Show older comments
I'm trying to create a single button operated stopwatch that runs a new timer after 5 presses, and works like this:
1st button press: start [new] timer; 2nd press: save split time #1; 3rd press: save split time #2; 4th: save split time #3; 5th: save split time #4;
These four split times then go into an algorithm - comprised mostly of MATLAB function blocks because I am a Simulink novice - which means I want it to execute once I have all 4 split times from each timer.
To solve my problem, I tried to connect a Raspberry Pi GPIO Read block to a MATLAB function block at the beginning of the Simulink model. My code attempted to save the split times as an array of 4 elements in a row, and create the timer with tic/toc:
function t = button(input)
t = [1 1 1 1]; % initialise using arbitrary nonzero values, split times saved here
for i = 1:100
if input == 0 % button pressed
% [0 0 0 0] -> [t 0 0 0] -> [t t 0 0] -> [t t t t]
if all(t) == 1
stclk = tic;
t = [0 0 0 0]; % clear split times
elseif t(1) == 0
t(1) = toc(stclk);
elseif t(2) == 0
t(2) = toc(stclk);
elseif t(3) == 0
t(3) = toc(stclk);
elseif t(4) == 0
t(4) = toc(stclk);
end
end
pause(0.1);
end % I wouldn't be surprised if this code is not perfect either, but that's no longer the point
Unfortunately tic/toc functions are "not supported for code generation", which is a problem as I want the algorithm stored onto my Raspberry Pi and running without connection to MATLAB. I read about creating a stopwatch using a triggered subsystem block. I don't understand that fully as it is, so I certainly don't know how I can create it so that the first signal drop (pressing the button) executes a different command to the subsequent 4 signal drops.
I'm still new-ish to Simulink, so I would greatly appreciate if you were able to explain it like I'm 5 years old and describe any step-by-step process on Simulink. Many thanks
Lucus
0 Comments
Answers (1)
Vishal Neelagiri
on 5 Dec 2016
You can use the 'clock' block along with the 'Triggered subsystem' block in this case. You can extract the Simulation time using the clock block. The triggered subsystem block can have the input specified via the clock block and the trigger specified via the Raspberry Pi GPIO block. Then, you can use the MATLAB Function block inside this triggered subsystem to implement your algorithm depending on the trigger from the external hardware. These documentation links might be useful:
https://www.mathworks.com/help/simulink/slref/clock.html http://www.mathworks.com/help/releases/R2016a/simulink/ug/triggered-subsystems.html
Note: The 'clock' block is supported for code generation
1 Comment
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!