主要内容

生成用于在线状态估计的代码MATLAB

You can generate C/C++ code from MATLAB®code that usesextendedKalmanFilter,unscentedKalmanFilterparticleFilterobjects for online state estimation. C/C++ code is generated using thecodegen(MATLAB Coder)command fromMATLAB Coder™software. Use the generated code to deploy online estimation algorithms to an embedded target. You can also deploy online estimation code by creating a standalone application usingMATLAB Compiler™software.

To generate C/C++ code for online state estimation:

  1. Create a function to declare your filter object as persistent, and initialize the object. You define the object as persistent to maintain the object states between calls.

    功能[CorrectedX] = ukfcodegen(output)%将对象声明为持续。persistentobj;ifisempty(obj)% Initialize the object.obj = unscentedKalmanFilter(@vdpStateFcn,@vdpMeasurementFcn,[2;0]); obj.MeasurementNoise = 0.01;end%估计国家。更正=正确(OBJ,输出);预测(obj);end

    该函数创建一个无声的Kalman滤波器对象,用于在线状态估算具有两个状态和一个输出的范德波尔振荡器。您使用先前编写和保存的状态过渡和测量功能,vdpstatefcn.mvdpMeasurementFcn.m,并指定两个状态的初始状态值(2, 0). Hereoutputis the measured output data. Save theukfcodegen.m功能on the MATLAB path. Alternatively, you can specify the full path name for this function.

    在里面ukfcodegen.m功能, the persistent object is initialized with condition如果Isempty(OBJ)to ensure that the object is initialized only once, when the function is called the first time. Subsequent calls to the function only execute the预测correct命令更新状态估计。在初始化期间,您指定对象的非义属性,例如StateTransitionFcn(在ukfcodegen.masvdpstatefcn.m) andMeasurementFcn(在ukfcodegen.masvdpMeasurementFcn.m). After that, you can specify only the tunable properties. For more information, seeTunable and Nontunable Object Properties.

    在状态过渡和测量功能中,您必须仅使用支持代码生成的命令。金宝app有关这些命令的列表,请参阅支持C/C ++代码生成的功能和对象金宝app(MATLAB Coder). Include the compilation directive%#codegen在里面se functions to indicate that you intend to generate code for the function. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in errors during code generation. For an example, typevdpstatefcn.mat the command line.

  2. Generate C/C++ code and MEX-files using thecodegen(MATLAB Coder)command fromMATLAB Codersoftware.

    codegenukfcodegen-args{1}

    The syntax-args {1}为您的函数指定参数的示例。该参数设置函数参数的维度和数据类型output作为双重标量。

    Note

    If you want a filter with single-precision floating-point variables, you must specify the initial value of the states as single-precision during object construction.

    obj = unscentedKalmanFilter(@vdpStateFcn,@vdpMeasurementFcn,single([2;0]))

    Then to generate code, use the following syntax.

    codegenukfcodegen-args{{{单(1)}
  3. Use the generated code.

    • Use the generated C/C++ code to deploy online state estimation to an embedded target.

    • Use the generated MEX-file for testing the compiled C/C++ code in MATLAB. The generated MEX-file is also useful for accelerating simulations of state estimation algorithms in MATLAB.

      Load the estimation data. Suppose that your output data is stored in themeadured_data.mat文件。

      loadmeadured_data.matoutput

      Estimate the states by calling the generated MEX-file.

      fori = 1:numel(output) XCorrected = ukfcodegen_mex(output(i));end

    此示例生成用于编译MEX文件的C/C ++代码。要为其他目标生成代码,请参见codegen(MATLAB Coder)在里面MATLAB Coder文档。

Tunable and Nontunable Object Properties

财产种类 Extended Kalman Filter Object 无味的卡尔曼过滤对象 粒子过滤对象
Tunable properties that you can specify multiple times either during object construction, or afterward using dot notation State,StateCovariance,ProcessNoise, 和suberementNoise State,StateCovariance,ProcessNoise,suberementNoise,Alpha,Beta, 和卡帕 ParticlesWeights
您只能在对象构造期间或之后使用DOT表示法,但在使用之前,您只能指定一次的属性预测或者correctcommands StateTransitionFcn,MeasurementFcn,StateTransitionJacobianFcn, 和测量jacobianfcn StateTransitionFcnMeasurementFcn StateTransitionFcn,MeasurementLikelihoodFcn,StateEstimationMethod,StateOrientation,重新采样板ResamplingMethod
您必须在对象构建过程中必须指定的非荷属性 HasAdditiveProcessNoisehasadDitiveMeasurementNoise HasAdditiveProcessNoisehasadDitiveMeasurementNoise

See Also

||

相关话题