Timer vs Counter, Data Types and Solver Types in Simulink

Hello everyone. Welcome to MAE Learning. In this article, we will explore timer vs counter. We will also discuss data types used in Simulink. Lastly, we will cover solver types.

Timer Vs Counter

1. Timer in Simulink

  • A timer is used to track time.
  • Example: If you want to turn on a signal after 5 seconds.
  • In Simulink, you can use the Clock block to get the current simulation time.
  • Use it with the Compare block to check if the time is > 5 seconds.

Example

Basic Timer Logic
Output Graph

2. Counter in Simulink

  • A counter is used to count.
  • Example: Every time a pulse arrives, the counter increments by 1.
  • Simulink has a Counter block that counts on either the rising edge or the falling edge.
  • You can also set a maximum count and a reset condition.
Counter Logic
Output Graph

Examples

  • Timer: Turns on the fan 3 seconds after the engine starts.
  • Counter: Calculates distance after every 10 wheel rotations.
FeatureTimerCounter
DefinitionIt will track the timeCounts pulses or events
InputSimulation timeTrigger signal (pulse)
OutputTime based condition (For example > 5 sec)Number of counts (0,1,2,3…)
Example BlockClock + CompareCounter Free-Running / Counter Limited
Use CaseFan after 3 secondsTaking action after 10 pulses
Reset OptionTime reset occurs from simulation startThere is an option to reset the counter.
Timer Vs Counter

Data Type

Default Data Type in MATLAB

  • The default data type in MATLAB is double (double precision floating point).
  • In Matlab if you write any variable a = 5, it is stored in double by default.
Data TypeDescriptionExampleAutomobile Use Case
doubleDefault stores numeric, decimal valuesspeed = 80.5Vehicle speed, battery voltage
singleLess memory, lower precisiontemp = single(35.2)Sensor data where memory is saved
int8 / int16 / int32 / int64Signed integers (negative + positive)rpm = int16(3000)Engine RPM, wheel rotation count
uint8 / uint16 / uint32 / uint64Unsigned integers (sirf positive)soc = uint8(85)Battery SOC percentage (0 to 100)
charStores a character or stringmsg = ‘Error’DTC message (Diagnostic Trouble Code)
logicalTrue (1) Or False (0) valuesflag = trueEngine ON/OFF status, brake switch
stringText to string formatstr = “EV Mode”Drive mode display
cellMixed data types togetherc = {80, ‘kmph’}Speed with unit
structData ko structure form mecar.speed = 120ECU parameters grouping
tableTabular data storeT = table(speed, rpm)Drive cycle data analysis
Types of Data Type

Example

% Vehicle data
speed = 80.5;ย ย ย ย ย ย ย ย ย  % double, km/h
rpm = int16(3000);ย ย ย ย  % engine rpm
soc = uint8(85);ย ย ย ย ย ย  % battery SOC %
engineOn = true;ย ย ย ย ย ย  % logical
mode = "EV Mode";ย ย ย ย ย  % string

Solver Types

Solver in MATLAB/Simulink

  • The solver’s job is to solve the system equations over time.
  • Meaning, differential equations are created in your model, and the solver solves them step-by-step and gives the output.
  • Example: The change in vehicle speed depends on acceleration. The solver will calculate the speed over time.
TypeDescriptionExample SolverUse Case in Automobile
Fixed-stepEach time step is equal (e.g. 0.01 sec, 0.1 sec)ode1 (Euler), ode3, ode4 (Runge-Kutta)Real-time ECU testing, HIL bench, controller design
Variable-stepStep size changes automatically (for error control)ode45, ode15s, ode23tAccurate plant modelling, vehicle dynamics simulation, battery thermal model
Solver Types

We can change solver type from configuration parameter setting.

Configuration Parameter

Example

  • Fixed-step: If you set a 0.01 second step, the solver will solve equations every 0.01 seconds.
  • Variable-step: If the system is smooth, it will take a large step. If the system is changing rapidly, it will take a small step.

Automobile Example

  • Fixed-step: Cruise control ECU simulation, because the controller needs exact step size in real-time.
  • Variable-step: Battery thermal model or vehicle longitudinal dynamics, where accuracy is more important.