Main Content

tunerCostFcnParam

First parameter example for tuning cost function

Description

example

tunerCostFcnParam(filter) creates a structure that has the fields required for tuning an insEKF filter with a custom cost function. The structure is useful when generating C code for a cost function using MATLAB® Coder™.

Examples

collapse all

Create an insEKF filter object. Then create a cost function using the createTunerCostTemplate object function.

filter = insEKF;
createTunerCostTemplate(filter);
doc = matlab.desktop.editor.getActive;
doc.saveAs(fullfile(pwd,"tunercost.m"));

Load prerecorded sensor data and ground truth data.

load("accelGyroINSEKFData.mat");

Create a MEX cost function using MATLAB Coder.

p = tunerCostFcnParam(filter);
disp("Generating MEX-accelerated cost function");
Generating MEX-accelerated cost function
codegen tunercost.m -args {p,ld.sensorData,ld.groundTruth};
Code generation successful.

Specify an initial orientation state and its covariance.

stateparts(filter,"Orientation",compact(ld.initOrient));
statecovparts(filter,"Orientation",1e-2);

Create a measurement noise structure using the tunernoise function.

mnoise = tunernoise(filter);

Create a tunerconfig object using the created MEX cost function.

cfg = tunerconfig(filter, MaxIterations=1, ...
    ObjectiveLimit=1e-4, ...
    Cost="custom", ...
    CustomCostFcn=@tunercost_mex);

Tune the filter. Show the tuned measurement noise and process noise in the filter.

tunedmn = tune(filter,mnoise,ld.sensorData, ...
    ld.groundTruth,cfg)
    Iteration    Parameter                    Metric
    _________    _________                    ______
    1            AdditiveProcessNoise(1)      0.3413
    1            AdditiveProcessNoise(15)     0.3381
    1            AdditiveProcessNoise(29)     0.3353
    1            AdditiveProcessNoise(43)     0.3334
    1            AdditiveProcessNoise(57)     0.3214
    1            AdditiveProcessNoise(71)     0.3121
    1            AdditiveProcessNoise(85)     0.3110
    1            AdditiveProcessNoise(99)     0.3107
    1            AdditiveProcessNoise(113)    0.3106
    1            AdditiveProcessNoise(127)    0.3105
    1            AdditiveProcessNoise(141)    0.2972
    1            AdditiveProcessNoise(155)    0.2872
    1            AdditiveProcessNoise(169)    0.2855
    1            AccelerometerNoise           0.2852
    1            GyroscopeNoise               0.2851
tunedmn = struct with fields:
    AccelerometerNoise: 0.9000
        GyroscopeNoise: 0.9000

orientationNoise = statecovparts(filter,"Orientation")
orientationNoise = 4×4

    0.0100         0         0         0
         0    0.0100         0         0
         0         0    0.0100         0
         0         0         0    0.0100

Input Arguments

collapse all

INS filter, specified as an insEKF object.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2022a