How do I create a stopwatch in Simulink that stores up to 4 split values?

20 views (last 30 days)
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

Answers (1)

Vishal Neelagiri
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
Lucus Yeo
Lucus Yeo on 7 Dec 2016
Edited: Lucus Yeo on 7 Dec 2016
This seems like a great idea! I'm not sure if I got it to work because I'm not certain on how blocks inside the triggered subsystem work.
Suppose I want the subsystem to output t as an array containing my split times. Right now inside the subsystem I have a MATLAB Function block with code:
function t = button(input,trigger)
t = [1 1 1 1 1];
if trigger == 0 % button pressed when input signal = 0; did I write this correctly?
if all(t) == 1
t = [input 0 0 0 0]; % clears previous saved split times
% input = reading given by clock
elseif t(2) == 0
t(2) = input - t(1); % the split times are written into t(2) to t(5)
elseif t(3) == 0
t(3) = input - t(1); % because I don't know if the clock is resettable
elseif t(4) == 0
t(4) = input - t(1);
elseif t(5) == 0
t(5) = input - t(1);
end
end
end
Inside the triggered subsystem, the trigger block is connected as one of the inputs to the MATLAB function block; and I'm not sure if that is right because I didn't see anything like that in documented examples. I don't receive any error messages when I try to run the model, but have I understood your suggestion correctly?

Sign in to comment.

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!