mex codegen issue: Class insfilterNonholonomic is not supported by coder.Type as it is a handle class.

15 views (last 30 days)
Dear Matlab community,
I have a function (see attachment) that I would like to compile to c++ to increase performance and I am following this tutorial. When I run
load('KFmex_data.mat');
codegen -report KFmex.m -args {KF,t_imu,Accel,Gyro,t_gnss,Pos,Vel,Rpos,Rvel,GNSS_step} -test -test;
I get the following error:
Function input at args{1} does not have a valid type.
Caused by:
Class insfilterNonholonomic is not supported by coder.Type as it is a handle class.
Use help codegen for more information on using this command.
Error using codegen
However, according to the documentation of insfilterNonholonomic, this should be fully supported. What am I doing wrong here? Is this a bug?
Any kind of help is much appreciated!

Accepted Answer

Gargi Patil
Gargi Patil on 20 Dec 2021
Hi,
Code generation does not support handle class objects as entry point arguments. You can refer to the linked documentation for more information. Therefore, insfilterNonholonomic objects can not be passed as inputs to a code generation function.
However, the code generation support indicated in the documentation of insfilterNonholonomic allows usage of its objects within the function as follows:
function [state,covariance,stddev] = KFmex(t_imu,Accel,Gyro,t_gnss,Pos,Vel,Rpos,Rvel,GNSS_step) %#codegen
%%KF as an arugment has been removed
KF = insfilterNonholonomic; %Example
%%Code generation will not throw an error
j = 1;
k = 1;
...
end
Kindly note that loading the variable KF within the function using the load command is not viiable for the same reason as above.
  2 Comments
fixusc
fixusc on 20 Dec 2021
Generally, I can get this to work now, thanks!
I have still a small issue I don't know how to deal with properly:
I want to set the property 'ReferenceFrame' of the insfilterNonholonomic to 'ENU' (the default is 'NED'). However, Matlab coder tells me that this (nontunable) property needs to be set to a constant value. What even is a constant value in Matlab and how can I achieve this? I have tried to define a class with a constant property with the value 'ENU'
classdef ENU
properties (Constant)
ReferenceFrame = 'ENU';
end
end
and assigning the value to the filter object by using
KF.ReferenceFrame = ENU.ReferenceFrame;
but it doesn't work and gives the same error as when I specify it as
KF.ReferenceFrame = 'ENU';
This is the full error message:
??? Failed to compute constant value for nontunable property 'ReferenceFrame'. In code generation, nontunable properties can only be assigned constant values.
fixusc
fixusc on 22 Dec 2021
I got it to work by passing the variable as a constat when calling Matlab coder:
codegen -report KFmex.m -args {t_imu,Accel,Gyro,t_gnss,Pos,Vel,Rpos,Rvel,GNSS_step,IMUSampleRate,coder.Constant(ReferenceFrame),ReferenceLocation,DecimationFactor,State,StateCovariance,GyroscopeNoise,AccelerometerNoise,GyroscopeBiasNoise,GyroscopeBiasDecayFactor,AccelerometerBiasNoise,AccelerometerBiasDecayFactor,ZeroVelocityConstraintNoise}
The relevant part of the command above is:
... { ... coder.Constant(ReferenceFrame) ... } ...

Sign in to comment.

More Answers (0)

Categories

Find more on Generating Code in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!