Legacy C code function o/p mismatch in simulink

Hi,
I am facing a unique problem here, must be some mistake of mine but I'm not able to see it.
I am using a legacy c function which is a mathematical model of an engine providing thrust as output. Now here is the problem:
When I am running the function as a c project independently, with some constant i/p's for one sample time, it is giving me correct results. In Simulink, after reading the documentation, I've made a s-function using the s-function builder as well as the legacy code tool. I am running the model with the same set of constant i/p's for one sample time(solver:fixed step), it is giving some completely different results, whereas the same function when run in a c compiler is giving correct results. I'm trying all the mentioned methods as mentioned to create the s-functions, but it is ending with the same result.I've tried with all the fixed step solvers just to check, but its the same.
I have also tried creating a mex function which I am calling from an embedded matlab function, this one is giving me correct results! Since I don't want to include an embedded matlab function in my model, s-function is my preference.
My objective is to include this legacy function in an aerospace non-real time simulation model which I have already made which uses the fixed step ODE4(R-K method) as the solver.
Will be really glad if some one sheds any light on this.
Regards, Devjit

回答(1 个)

It's hard to tell just from your description, but it looks like you might not be setting up your S-function correctly. What is your function's prototype? Have you ensured that the S-function's inputs and outputs have been configured with the right type/size? Have you examined the S-function mdlOutputs function to ensure that your function is getting called correctly?
I would also recommend trying to debug your S-function with a C/C++ debugging tool.

4 个评论

@Kaustubha: Thanks for your reply. I have checked the ip/op types, i am using a set of constants as i/p just for a start before connecting it to the bus. Also, if I use the legacy code tool to create the S-function, the i/p & o/p ports don't have to be written manually I guess? When I am using the S-function builder or writing it manually then for a constant i/p and subsequent o/p, I am keeping the port width as 1.
I'll keep on trying though :-). Another question: Suppose I have created a mex function out of my c program. How do you I include that in my simulink model? Is it mandatory to use S-functions?
A MEX-file can be treated like any other MATLAB function. You can either call it directly using an Interpreted MATLAB Function (http://www.mathworks.com/help/toolbox/simulink/slref/interpretedmatlabfunction.html) or from a C S-function using mexCallMATLAB (http://www.mathworks.com/help/techdoc/apiref/mexcallmatlab.html).
With the legacy code tool, the input/output types/sizes are parsed from the OutputFcnSpec.
Thanks for your reply! It has helped me indeed :-)

此问题已关闭。

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by