Interpolation with Piecewise Polynomials in Simulink

7 views (last 30 days)
I am trying to get values from a range of points, using interpolation on Simulink. Basically, I have a moving car and want to get the proprieties of a road (described in a discrete manner) for the position of the car. The simulations are large (road is 100+ Km, with breakpoints every 0.25 meters), so for the Matlab implementation I create the piecewise-polynomial interpolator function (using 'pchip') and then just use ppval to get the value at each timestep. Something along the lines:
% Pre-processing
pp = pchip([distance_values],[propriety_values]);
% On the timestep
prop_at_displacement = ppval(pp,displacement);
I would like to employ a similar structure on Simulink, but the 'pp' from the Matlab cannot be used on the code of the Simulink. As I get the following error:
"PP forms constructed in MATLAB cannot be used directly by PPVAL for code generation. Use UNMKPP to extract the data and MKPP to create a MATLAB Coder compatible PP form."
I can use UNMKPP, to extract the coefficients and breakpoints to use afterwards on the mkpp. However, currently the only way I can get it to work is to place the MKPP and PPval on the Matlab function on the Simulink, this makes the simulations slow as the MKPP evaluates each timestep. I think using a block that only runs on the initial timestep for the MkPP could work, but I would prefer to run this command even before the Simulink started running, so that even the first timestep is not impacted, as the application is time sensitive.
I tried to run the MKPP command on the INIT callback of the Simulink function, but the same error remains. Is there another way to run pre-processed commands in Simulink that are coded and not Matlab interpreted, so that the PP form created is compatible with the Simulink?
Or another method is preferred to speed up the simulation?
Thanks for the replies
Foot notes:
Inputs: 1 - Distance Travelled
Outputs: 2+
Interpolation: Pchip (preferred)
Extrapolation: Not required (simulation to stop when data is not available)
Breakpoints: equal to all the outputs
Purpose: Deploy to a RT, so blocks need to be C-Coded capable for deployment

Answers (1)

David Fink
David Fink on 8 Feb 2021
Wrapping the call to mkpp in coder.const will evalue it once during model update, and reuse that value during simulation.
This will impact model update, but runs before the first timestep.
Note that this requires that the inputs to mkpp are constant.


Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by