You are on page 1of 28

MDSD using The MathWorks flow

Massimo Violante
Politecnico di Torino
Dip. Automatica e Informatica
Torino, Italy
Our goal
n To generate real-time sw starting from Simulink
models
n Assumptions:
n Hard real-time tasks à deadlines cannot be missed
n Periodic tasks à infinite sequence of identical instances
periodically released
n Deadline for the task is equal to the task period T

τ0 τ1 τ2 τn

Task period T
2
Simulink solver
n A model is defined by:
n Output function:𝑦" = 𝜆(𝑢" , 𝑚" )
n State function: 𝑠"+, = 𝛿 𝑢" , 𝑚"
n Where:
n 𝑡 =current time
𝑢" 𝑦"
n 𝑢 =input Model
n 𝑦 =output 𝑚"
n 𝑚 =state
n Δ = step size

3
Simulink solver
n The solver operates as follows:
n 1 - At time 𝑡, given input 𝑢" and state 𝑚"
n Compute output 𝑦" = 𝜆(𝑢" , 𝑚" )
n Compute next state 𝑛𝑠 = 𝑚"+, = 𝛿 𝑢" , 𝑚"
n Compute next time Δ
n 2 - Update time 𝑡 = 𝑡 + Δ, set 𝑚" = 𝑛𝑠
n 3 - Repeat from 1
n The step size Δ depends on the adopted solver

4
Solvers
n Fixed-step solvers
n Solve the model at regular time intervals à Δ is chosen a
priori
n Decreasing the step size increases the accuracy of the
results while increasing the time required to simulate the
system
n Variable-step solvers
n Solve the model choosing Δ at each solver iteration
n Reducing the step size to increase accuracy when a model's
states are changing rapidly and increasing the step size to avoid
taking unnecessary steps when the model's states are changing
slowly

5
Solvers
n Continuous solvers
n Use numerical integration to compute a model's
continuous states at the current time step based on the
states at previous time steps and the state derivatives
n Continuous solvers rely on the individual blocks to
compute the values of the model's discrete states at each
time step
n Discrete solvers
n Solve purely discrete models
n They compute the next simulation time step for a model
n They rely on each block in the model to update its
individual discrete states
6
Selecting the solver for sw
n Sw scheduling
τ0 τ1 τ2 τn

Task period T

n Solver configuration
n Discrete solver
n Fixed-step solver, with step size Δ equal to task period T

7
Example
n See 00_dummy_model example

n Solver is fixed step discrete with 10 msec step size


n Code generator is set to use:
n ert.tlc
n reusable function interface
n modular file placement 8
Code generation
n Using algorithm export, and the above
configurations, the following code is produced
Embedded coder
output

Simulink input ert_main.c Testbench for


the model

𝑢" 𝑦"
model model.c
Source code for
the model
𝑚"
model.h
Model-specific
data types

Hw-specific
rtwtypes.h
data types
9
Code generation
The model is translated into three functions:
Embedded coder § initialize(M, U, Y): to reset the model state
output M;
§ step(M, U, Y): to execute one solver step
ert_main.c for the model
§ terminate(M, U, Y): to clean-up memory
after the last execution of the model
model.c
initialize(M, U, Y)
{…}

step(M, U, Y)
model.h {…}

terminate(M, U, Y)
{…}
rtwtypes.h

10
Code generation
Embedded coder
output

ert_main.c Translates the Simulink native data types


(e.g., char) into the hw-specific data types
(e.g., int8_T)
model.c
typedef signed char int8_T;
.
.
model.h .

rtwtypes.h

11
Code generation
Embedded coder
output main()
{
ert_main.c initialize(M, U, Y);

while( stop == FALSE )


{
model.c
step(M, U, Y);
wait_for(Δ);
}
model.h terminate(M, U, Y);
}

rtwtypes.h

12
Code generation
n Full executable export takes care of:
n Creating task for model execution, and device driver for
I/O
n Sequencing operations:
n Acquisition of inputs from the plan
n Execution of the controller model
n Application of outputs to the plant
n Advantages:
n Very simple once I/O drivers are modeled
n Limitations:
n Limited expandability to unsupported I/O
n Limitations on scheduling period
13
Example 1
n HC-SR 04 Ultrasonic Sensor is not supported,
custom code must be written
n HC-SR 04 specs
n Maximum range: 400 cm à pulse duration = 23200 µsec
n Minimum range: 2 cm à pulse duration = 116 µsec
n MathWorks PSP for FRDM K64 accept 1 msec as shortest
step size
n Unsuitable for handling the HC-SR 04
n Algorithm export is needed + custom made sw
n Algorithm and custom sw must be integrated manually

14
The target system
Algorithm export Control Application

Application Framework SW

Custom SW Process Scheduler


platform Drivers
Resource Manager

Communication Actuators
interfaces
CPU Memory I/O HW
Special
Sensors interfaces

HW platform
15
Example 1
n Two GPIOs (other than 5V Vcc and GND) are
needed to manage the HCSR-04
n Trigger to generate a 10 µsec pulse
n Echo to read the pulse proportional
to the object distance
n The SW platform shall:
n Trigger the sensor
n Read the distance
n Call the step function according to the
selected time step

16
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

17
www.mbed.org
n Code for managing Trigger and Echo
Digital output assigned to the pin D2
#include "mbed.h” int main()
{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

18
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); Digital input assigned to the pin D4, it generates an
{
} pc.printf( "Reading inputs....\n\r" );
interrupt when the pin value changes
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

19
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;

void
Hardware timer to measure
start( void )
trigger = 0;

{ pulse duration while (true)


t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

20
www.mbed.org
Functions to be invoked in response to
n Code for managing Trigger
rising event (0à1)and Echoevent (1à0)
and falling
on pin D4
#include "mbed.h” int main()
{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

21
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
Generate
void 10 µsec
start( voidpulse
)
trigger = 0;

{ on D2, 0à1à0 while (true)


t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

22
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
On raising edge (0à1) on D4,
InterruptIn echo(D4);
start time measure
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

23
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float On falling edge (1à0) on
distance; D4, stop
distance = 0; time
trigger = 0;
void measure,
start( void ) read the timer and compute
{
t.start(); the distance
while (true)
{
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

24
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


Simple, serial-based I/O{using
DigitalOut
InterruptIn
the USB
trigger(D2);
echo(D4);
connector t.reset();

Timer t; echo.rise( &start );


echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

25
www.mbed.org
n Code for managing Trigger and Echo

#include "mbed.h” int main()


{
DigitalOut trigger(D2); t.reset();
InterruptIn echo(D4);
Timer t; echo.rise( &start );
echo.fall( &stop );
Serial pc(USBTX, USBRX); // tx, rx
float distance; distance = 0;
trigger = 0;
void start( void )
{ while (true)
Wait 1 sec before repeating all
t.start(); {
} pc.printf( "Reading inputs....\n\r" );
the operations again trigger = 1;
void stop( void ) wait_us( 10 );
{ trigger = 0;
t.stop(); pc.printf( "\n\rD: %.3f\n\r", distance );
distance = t.read_us()/58.0; wait( 1.0f );
t.reset(); }
} }

26
www.mbed.org
n Code for managing scheduler

#include "mbed.h” Define a free running counter


Ticker S;

void do_step( void )


{
// step the model
} Every 0.1 sec of the counter,
int main() call do_step()
{
S.attach( &do_step, 0.1 );

while (true)
{
}
}

27
Example 2
n Blink the RED led with a rate proportional to the
measured distance, the shorter the distance, the
higher then blinking rate
n If distance < 200àrate is set to distance/100
n Otherwise, it is 1 sec

28

You might also like