Matlab coder and extendedKalmanFilter object
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I aim to implement an extended Kalman filter and generate ros node using matlab code generation tool for my specific case, which requires custom C++ code to implement the scatter interpolate functionality. The reason for using custom C++ code is that MATLAB's scatteredInterpolant is not compatible with code generation. My main question revolves around passing extra parameters to the dynamic model and measurement model used in the extendedKalmanFilter object. Below is the code where I call my custom interpolate function:
coder.cinclude("/home/mpc/Desktop/mba/mba/mba.hpp");
coder.cinclude("myInterpolator.h");
coder.cinclude("myInterpolatorwrapper.h");
coder.updateBuildInfo("addSourceFiles", "myInterpolator.cpp");
coder.updateBuildInfo("addSourceFiles", "myInterpolatorwrapper.cpp");
myinterpolatorptr = coder.opaque('interpolatortype','NULL','HeaderFile','"myInterpolatorwrapper.h"');
if coder.target('MATLAB')
% Implement if matlab is used
else
myinterpolatorptr = coder.ceval('interpolator_constructor');
res = coder.ceval('interpolateat_wrapper',myinterpolatorptr,354489.0, 6704413.0);
end
Example that I was following when implementing this is: https://se.mathworks.com/matlabcentral/answers/254524-how-can-i-use-c-classes-in-my-matlab-functions-and-generate-code-using-matlab-coder. The idea is that in dynamic or measurement model related to EKF I would only need to call the following line
res = coder.ceval('interpolateat_wrapper',myinterpolatorptr,354489.0, 6704413.0);
where 354489.0, 6704413.0 are example x and y, coordinates. One possible solution that come to my mind was that I would declare myinterpolatorptr as a global variable, however that is not supported for code generation.
This is how I initialize the EKF:
disp('Initialzing myFilter Object');
myFilter = extendedKalmanFilter( "StateTransitionFcn",@aug_dynamic_model,...
"MeasurementFcn",@measurement_model);
Also if I try to pass it like this:
%global myinterpolatorptr
if coder.target('MATLAB')
const = [0];
else
myinterpolatorptr = coder.opaque('interpolatortype','HeaderFile','"myInterpolatorwrapper.h"');
myinterpolatorptr = coder.ceval('interpolator_constructor');
res = coder.ceval('interpolateat_wrapper',myinterpolatorptr, 354489.0, 6704413.0);
const = [myinterpolatorptr];
end
fprintf('map value: %f.\n',res);
% initialize the EKF Object
disp('Initialzing myFilter Object');
myFilter = extendedKalmanFilter( "StateTransitionFcn",@(x,u)aug_dynamic_model(x,u,const),...
"MeasurementFcn",@measurement_model);
I get error:
"Failed to compute constant value for nontunable property 'StateTransitionFcn'. In code generation, nontunable properties can only be assigned constant values."
Here are the custom myInterpolatorwrapper.h
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <memory>
#include <mba.hpp>
#include <iostream>
#include <fstream>
#if defined __cplusplus
extern "C" {
#endif
typedef void * interpolatortype;
interpolatortype interpolator_constructor( void );
double interpolateat_wrapper( interpolatortype a_interpolator_type, double x, double y );
#if defined __cplusplus
}
#endif
#endif
and here custom myInterpolatorwrapper.cpp
#include "myInterpolator.h"
#include "myInterpolatorwrapper.h"
interpolatortype interpolator_constructor( void )
{
MyInterpolator * a_myInterpolator = new MyInterpolator();
interpolatortype interpolatorpointer = (interpolatortype)a_myInterpolator;
return interpolatorpointer;
}
double interpolateat_wrapper( interpolatortype a_interpolator_type, double x, double y )
{
MyInterpolator* a_myInterpolator = (MyInterpolator*)a_interpolator_type;
return a_myInterpolator->interpolateAt(x,y);
}
1 comentario
Shashank Mahabala Sharma
el 8 de Nov. de 2023
Editada: Shashank Mahabala Sharma
el 8 de Nov. de 2023
Hi Jere,
Unfortunately, MATLAB Coder doesn't support anonymous functions. A possible workaround is to model the anonymous function's parameters using a sub-function and persistent variables. The idea is to make aug_dynamic_model(x,y,c) a sub-function and 'c' a persistent variable in that function. Then you use nargin of aug_dynamic_model to update the value of parameter 'c' when called with more than two arguments.
For an exmple of such an implementation take a look at,
http://www.mathworks.com/matlabcentral/answers/116449-use-quadgk-with-multiple-inputs-with-matlab-coder#answer_124990
Respuestas (0)
Ver también
Categorías
Más información sobre Simulink Coder en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!