Reputation:
I'm writing a simulink device driver block for Arduino Due with the Matlab Function Approach as explained in the Simulink Device Driver guide. When I want to pass a double from the C++ code to the matlab function code with coder.ceval() I get a wrong number. Here is an example:
C++ function:
#include <Arduino.h>
extern "C" double dout_output()
{
return 50.5;
}
Matlab function in Simulink:
function x = example()
x = 0.0;
if strcmp(coder.target,'rtw'),
x = coder.ceval('dout_output');
end
When I run this code in external mode on an adruino due I don't get the 50.5 but a large number like 1113794816. Any sugestions?
Upvotes: 0
Views: 252
Reputation: 1928
Is the header file containing the declaration of dout_output
being included in the C code generated from the MATLAB Function code? If not, you'll likely see compiler warnings that say something about implicit int
return type.
When C compilers don't have a declaration for a function, they may assume that the return type is int
. Typically sizeof(int) != sizeof(double)
. So this mismatch can cause surprising results. This answer discusses that more.
Try to add:
coder.cinclude('dout_output.h');
to your MATLAB code where dout_output.h
is replaced with the name of the header containing the declaration of dout_output
.
You may also need to add an include directory to the custom code settings in:
"Configuration Parameters->Simulation Target->Custom Code->Include Directories"
and possibly:
"Configuration Parameters->Code Generation->Custom Code->Include Directories"
Alternatively, you can use the coder.ExternalDependency
approach to encapsulate external code dependencies for your MATLAB code.
Upvotes: 2