# Model Engine Timing Using Triggered Subsystems

This example shows how to model a four-cylinder spark ignition internal combustion engine from the throttle to the crankshaft output using triggered subsystems. In this example, the `sldemo_engine`

model is based on [1].

The `sldemo_engine`

model uses the Triggered Subsystem blocks in the `Valve Timing`

and `Combustion`

subsystems. In this example, the tasks of all four cylinders are implemented with one set of blocks instead of four sets of blocks, one set per cylinder.

Open the model.

`open_system('sldemo_engine')`

Warning: Unrecognized function or variable 'CloneDetectionUI.internal.CloneDetectionPerspective.register'.

In an inline four-cylinder four-stroke engine, 180 degrees of crankshaft rotation separates the ignition of each successive cylinder. This separation means that each cylinder fires on every other crank revolution. In this model, the intake, compression, combustion, and exhaust strokes occur simultaneously (at any given time, one cylinder is in each phase). To account for compression, the combustion of each intake charge is delayed by 180 degrees of crank rotation from the end of the intake stroke.

In this model, an Integrator block accumulates the cylinder mass air flow in the `Intake Manifold`

block located inside the `Throttle and Manifold`

subsystem. The `Valve Timing`

subsystem generates pulses that correspond to specific rotational positions to manage the crank angle, air-fuel mass and triggers execution of `Compression`

subsystem. The `Compression`

subsystem uses a Unit Delay block to insert 180 degree (one event period) of delay between the intake and combustion of each air-fuel charge.

Consider a complete four-stroke cycle for one cylinder. The following describes the functionality of each stroke.

**Intake stroke**$$-$$ During this stroke, the intake manifold opens and takes in the air-fuel mixture. The piston moves from the top dead center (TDC) to the bottom dead center (BDC) and the crank shaft completes 180 degrees rotation. In this model, the`Intake Manifold`

subsystem inside the`Throttle and Manifold`

subsystem integrates the mass flow rate from the manifold.**Compression stroke**$$-$$ During this stroke, the intake manifold closes and the air-fuel charge is compressed by 180 degrees rotation of the crank shaft. The piston moves from BDC to TDC. Due to compression, the temperature of the air-fuel mixture increases which assists the combustion process.**Combustion stroke**$$-$$ The spark plug ignites the air-fuel mixture. Combustion of the mixture inside the cylinder generates pressure that forces the piston to move from TDC to BDC. During this stroke, the crank shaft rotates by 180 degrees and generates torque.**Exhaust stroke**$$-$$ During the exhaust stroke, the piston moves from BDC to TDC by 180 degrees rotation of the crank shaft. When the piston head reaches TDC, the exhaust manifold opens and let the exhaust out of the cylinder. In this model, the Integrator block inside the`Throttle and Manifold`

subsystem is reset and prepared for the next cycle of four strokes (720 degrees) of this cylinder.

### Valve Timing

The `Valve Timing`

subsystem controls valve timing (opening and closing), triggers ignition, and resets the crank shaft angle based on the angular position of the engine crank shaft. The `Valve Timing `

subsystem contains two Triggered subsystem blocks: `Reset Crank Angle on Rising Edge`

and `Reset Piston Air Mass on Falling Edge`

.

`Reset Piston Air Mass on Falling Edge`

is triggered when the crankshaft moves from TDC to BDC, and transfers the air-fuel mixture from the intake manifold to the cylinders via discrete valve events. The transfer process takes place concurrently with the continuous-time processes of intake flow, torque generation, and acceleration.

`Reset Crank Angle on Rising Edge`

is triggered once the crankshaft angle exceeds 180 degrees, and resets the angle to 0 degrees.

### Compression

The `Compression`

subsystem represents the engine dynamics during the compression stroke. This subsystem is triggered when it receives a positive trigger value at the Trigger port of the block. The Unit Delay block samples the integrator state. This value, the accumulated mass charge, is available at the output of the `Compression`

subsystem for use during combustion.

### Throttle and Manifold

The throttle body of the engine is modeled by the `Throttle and Manifold`

subsystem.

#### Throttle

The `Throttle`

subsystem uses throttle plate angle ($$\theta $$), manifold pressure ($${P}_{m}$$), and atmospheric pressure ($${P}_{amb}$$) as inputs. The air-fuel mixture intake rate($${\underset{}{\overset{\dot{}}{m}}}_{ai}$$) at the intake manifold can be expressed as the product of two functions:

Empirical function of the throttle plate angle

Function of the atmospheric and manifold pressures

In cases of lower manifold pressure (greater vacuum), the flow rate through the throttle body is sonic and is only a function of the throttle angle. This model accounts for this low pressure behavior with a switching condition in the following compressibility equations.

**Equation 1**

$${\underset{}{\overset{\dot{}}{m}}}_{ai}=f(\theta )\cdot g({P}_{m})=\text{mass}\text{}\text{flow}\text{}\text{rate}\text{}\text{into}\text{}\text{manifold}\text{}\text{(g/s)}$$

$$f(\theta )=2.821-0.05231\cdot \theta +0.10299\cdot {\theta}^{2}-0.00063\cdot {\theta}^{3}$$

$$g({P}_{m})=1;\text{}\text{if}\text{}{P}_{m}\le {P}_{amb}/2$$

$$g({P}_{m})=\frac{2}{{P}_{amb}}\sqrt{{P}_{m}{P}_{amb}-{P}_{m}^{2}};\text{}\text{if}\text{}{P}_{amb}/2\le {P}_{m}\le {P}_{amb}$$

$$g({P}_{m})=-\frac{2}{{P}_{m}}\sqrt{{P}_{m}{P}_{amb}-{P}_{amb}^{2}};\text{}\text{if}\text{}{P}_{amb}\le {P}_{m}\le 2{P}_{amb}$$

$$g({P}_{m})=-1;\text{}\text{if}\text{}{P}_{m}\ge 2{P}_{amb}$$

$${\underset{}{\overset{\dot{}}{m}}}_{ai}\to \text{mass}\text{}\text{flow}\text{}\text{rate}\text{}\text{into}\text{}\text{manifold}\text{}\text{(g/s);}\text{}$$

$$\theta \to \text{throttle}\text{}\text{angle}\text{}\text{(deg);}$$

$${P}_{m}\to \text{manifold}\text{}\text{pressure}\text{}\text{(bar);}\text{}$$

$${P}_{amb}\to \text{ambient}\text{}\text{(atmospheric)}\text{}\text{pressure}\text{}\text{(bar);}$$

The individual equations, mentioned above, are incorporated using Fcn blocks. A Switch block determines whether the flow is sonic by comparing the pressure ratio to its switch threshold. In the sonic regime, the flow rate is a function of the throttle position only. The direction of flow is from the higher to lower pressure, as determined by the Sign block. The MinMax block ensures that the pressure ratio is always unity or less.

#### Intake Manifold

The `Throttle and Manifold`

subsystem contains the `Intake Manifold`

subsystem. This subsystem uses the throttle flow input and engine speed ($$N$$) as inputs. The intake manifold is modeled as a differential equation for the manifold pressure ($${\underset{}{\overset{\dot{}}{P}}}_{m}$$). The difference in the incoming and outgoing mass flow rates represents the net rate of change of air mass with respect to time. This quantity, according to the ideal gas law, is proportional to the time derivative of the manifold pressure.

However, unlike the model in [1], this model does not incorporate exhaust gas recirculation (EGR).

**Equation 2**

$${\underset{}{\overset{\dot{}}{P}}}_{m}=\frac{RT}{{V}_{m}}({\underset{}{\overset{\dot{}}{m}}}_{ai}-{\underset{}{\overset{\dot{}}{m}}}_{ao})$$

$$R\to \text{specific}\text{}\text{gas}\text{}\text{constant;}\text{}$$

$$T\to \text{temperature}\text{}\text{(K);}\text{}$$

$${V}_{m}\to \text{manifold}\text{}\text{volume}\text{}({m}^{3})\text{;}\text{}$$

$${\underset{}{\overset{\dot{}}{m}}}_{ao}\to \text{mass}\text{}\text{flow}\text{}\text{rate}\text{}\text{of}\text{}\text{air}\text{}\text{out}\text{}\text{of}\text{}\text{the}\text{}\text{manifold}\text{}\text{(g/s);}\text{}$$

$${\underset{}{\overset{\dot{}}{P}}}_{m}\to \text{rate}\text{}\text{of}\text{}\text{change}\text{}\text{of}\text{}\text{manifold}\text{}\text{pressure}\text{}\text{(bar/s);}$$

The mass flow rate of air-fuel mixture that the model pumps into the cylinders from the manifold is described in the following empirical equation. This mass rate ($${\underset{}{\overset{\dot{}}{m}}}_{ao}$$) is a function of the manifold pressure ($${\underset{}{\overset{\dot{}}{P}}}_{m}$$) and the engine speed ($$N$$).

**Equation 3**

$${\underset{}{\overset{\dot{}}{m}}}_{ao}=-0.366+0.08979\cdot N\cdot {P}_{m}-0.0337\cdot N\cdot {P}_{m}^{2}+0.0001\cdot {N}^{2}\cdot {P}_{m}$$

$$N\to \text{engine}\text{}\text{angular}\text{}\text{speed}\text{}\text{(rad/s);}\text{}$$

$${P}_{m}\to \text{manifold}\text{}\text{pressure}\text{}\text{(bar);}\text{}$$

To determine the total air-fuel mixture charge pumped into the cylinders, the simulation integrates the mass flow rate from the intake manifold and samples it at the end of each intake stroke event. This sampling determines the total air mass that is present in each cylinder after the intake stroke and before compression.

The `Intake Manifold`

subsystem is used to model the throttle dynamics expressed in Equation 2. The differential equation from Equation 2 models the intake manifold pressure. A Simulink Function block computes the mass flow rate into the cylinder, a function of manifold pressure and engine speed, expressed in Equation 3.

### Combustion

Torque is generated during the combustion stroke. The `Combustio`

n subsystem models the combustion process and torque generation using four variables, expressed in Equation 4. The model uses a Mux block to combine these variables into a vector that provides input to the Fcn block that incorporates the $$Torqu{e}_{eng}$$ equation.

**Equation 4**

$$\underset{}{\overset{\dot{}}{N}}\to \text{engine}\text{}\text{angular}\text{}\text{acceleration}\text{}(rad/{s}^{2})\text{;}\text{}$$

$${m}_{a}\to \text{mass}\text{}\text{of}\text{}\text{air}\text{}\text{in}\text{}\text{cylinder}\text{}\text{for}\text{}\text{combustion}\text{}\text{(g);}\text{}$$

$$\left(\frac{A}{F}\right)\to \text{air}\text{}\text{to}\text{}\text{fuel}\text{}\text{ratio;}\text{}$$

$$\sigma \to \text{spark}\text{}\text{advance}\text{}\text{(degrees}\text{}\text{before}\text{}\text{top}\text{}\text{-}\text{}\text{dead}\text{}\text{-}\text{}\text{center);}\text{}$$

$$Torqu{e}_{eng}\to \text{torque}\text{}\text{produced}\text{}\text{by}\text{}\text{the}\text{}\text{engine}\text{}\text{(Nm);}\text{}$$

$$Torqu{e}_{eng}=-181.3+379.36\cdot {m}_{a}+21.91\cdot \left(\frac{A}{F}\right)-0.85\cdot {\left(\frac{A}{F}\right)}^{2}+0.26\cdot \sigma -0.0028\cdot {\sigma}^{2}+$$

$$+0.027\cdot N-0.000107\cdot {N}^{2}+0.00048\cdot N\cdot \sigma +2.55\cdot \sigma \cdot {m}_{a}-0.05\cdot {\sigma}^{2}\cdot {m}_{a}$$

### Engine Dynamics

The `Engine Dynamics`

subsystem computes the engine speed ($$N$$) and crank angle using the load torque $$Tload$$ and the generated torque $$Teng$$. $$Tload$$ is computed using step functions in the `Drag Torque`

subsystem. Engine speed is computed using the equation $$J\underset{}{\overset{\dot{}}{N}}=Torqu{e}_{eng}-Torqu{e}_{load}$$, where J is the engine rotational moment of inertia $(kg\cdot {m}^{2})$.

### Simulate the Model and Visualize the Results

Simulate the model and visualize the results using `plot`

function.

`sim('sldemo_engine'); `

The default input values are used for simulation:

$$Throttle=8.97\text{}\text{(deg)}\text{}\text{if}\text{}t5$$

$$Throttle=11.93\text{}\text{(deg)}\text{}\text{if}\text{}t\ge 5$$

$$Load=25\text{}\text{(Nm)}\text{}\text{if}\text{}t\le 2\text{}\text{or}\text{}t\ge 8$$

$$Load=20\text{}\text{(Nm)}\text{}\text{if}\text{}2t\le 8$$

Simulink® returns the simulated engine speed, the throttle commands that drive the simulation, and the load torque that disturbs it.

The model logs this data to the base workspace in a structure, `sldemo_engine_output`

.

PlotHandle = plot(sldemo_engine_output.get('LoadTorque').Values.Time, ... sldemo_engine_output.get('LoadTorque').Values.Data, 'r', ... sldemo_engine_output.get('ThrottleAngle').Values.Time, ... sldemo_engine_output.get('ThrottleAngle').Values.Data, 'b' ); title('Open-Loop Simulation Inputs: Load Torque and Throttle Angle vs Time'); xlabel('Time (sec)'); ylabel('Engine Speed (rad/sec)'); set(gca,'Color','w','XGrid','On','XColor',[0 0 0],... 'YGrid','On','YColor',[0 0 0]); axis([0 10 5 30]); h= legend('Load Torque (Nm)','Throttle Angle (deg)','Location','northeast'); set(h,'TextColor','k','Color','none');

PlotHandle = plot(sldemo_engine_output.get('EngineSpeed').Values.Time, ... sldemo_engine_output.get('EngineSpeed').Values.Data,'b' ); title('Open-Loop Simulation Results: Engine Speed Control'); xlabel('Time (sec)'); ylabel('Engine Speed (rad/sec)'); set(gca,'Color','w','XGrid','On','XColor',[0 0 0],... 'YGrid','On','YColor',[0 0 0]); axis([0 10 1500 3500]); h = legend('Engine Speed (rpm)','Location','northeast'); set(h,'TextColor','k','Color','none');

## See Also

Triggered Subsystem | Subsystem Reference | State Writer | State Reader | Integrator | Second-Order Integrator | Mux | Step

## Related Topics

- Enable Signal Logging for Model
- Engine Timing Model with Closed Loop Control
- Powertrain Blockset
- Specify Block Execution Order, Execution Priority and Tag

## References

[1] Crossley, P. R., and J. A.
Cook. “A Nonlinear Engine Model for Drivetrain System Development.”
*International Conference on Control 1991. Control ’91* 1991,
pp. 921–25 vol.2. *IEEE Xplore*, *DOI.org (Crossref)*, https://ieeexplore.ieee.org/abstract/document/98573.

[2] The Simulink Model. Developed by Ken Butts, Ford Motor Company. Modified by Paul Barnard, Ted Liefeld and Stan Quinn, MathWorks®, 1994 — 7."

[3] Moskwa, John J., and J. Karl
Hedrick. “Automotive Engine Modeling for Real Time Control Application.” *1987
American Control Conference*, 1987, pp. 341–46. *IEEE Xplore*, *DOIː
10.23919/ACC.1987.4789343*.

[4] Powell, B. K., and J. A. Cook.
“Nonlinear Low Frequency Phenomenological Engine Modeling and Analysis.” *1987
American control conference*, 1987, pp. 332–40. *IEEE Xplore*, *DOIː
10.23919/ACC.1987.4789342*.

[5] Weeks, Robert W., and John J.
Moskwa. “Automotive Engine Modeling for Real-Time Control Using MATLAB/SIMULINK.”
*SAE transactions*, vol. 104, 1995, pp. 295–309. *JSTOR*, https://www.jstor.org/stable/44473229.