Get values from c++ using S-function builder

6 views (last 30 days)
Hi, I'm new to coding and I was asked to study the s-function builder for a project and I can't seem to find a way to assign the pointers to the values I want for the input and the output of the block. Or maybe I'm just lacking some fundamental logical knowledge on this?
The project I'm working on is to study Speeduino, an open source code. This is a very small portion of the project that I'm studying currently.
inline uint32_t div360(uint32_t n) {
#ifdef USE_LIBDIVIDE
return libdivide::libdivide_u32_do(n, &libdiv_u32_360);
#else
return n / 360U;
#endif
}
unsigned long angleToTime(int16_t angle, byte method)
{
unsigned long returnTime = 0;
if( (method == CRANKMATH_METHOD_INTERVAL_REV) || (method == CRANKMATH_METHOD_INTERVAL_DEFAULT) )
{
returnTime = div360(angle * revolutionTime);
}
else if (method == CRANKMATH_METHOD_INTERVAL_TOOTH)
{
if(triggerToothAngleIsCorrect == true)
{
unsigned long toothTime = (toothLastToothTime - toothLastMinusOneToothTime);
uint16_t tempTriggerToothAngle = triggerToothAngle; // triggerToothAngle is set by interrupts
returnTime = ( (toothTime * angle) / tempTriggerToothAngle );
}
else { returnTime = angleToTime(angle, CRANKMATH_METHOD_INTERVAL_REV); } //Safety check. This can occur if the last tooth seen was outside the normal pattern etc
}
return returnTime;
}
I put this code in the wrapper in the output section along with the appropriate headers
This is the block that I created
I couldn't mex
How will I be able to assign the variables in the codes to the inputs and the outputs of the block so I can run it?
Thank you

Accepted Answer

Shaik
Shaik on 14 May 2023
Hello,
Hope you are doing good.
To create an S-function using the S-function Builder, you need to define the inputs, outputs, and other parameters of your block. The S-function Builder provides an interface for specifying these details and writing the necessary code to implement the behavior of your block.
Here's a step-by-step guide on how to assign variables in your code to the inputs and outputs of the S-function:
  1. Open the S-function Builder: In MATLAB, open the S-function Builder by typing sfunctionbuilder in the command window.
  2. Specify the number and types of inputs and outputs: In the S-function Builder window, go to the "Ports" tab. Enter the number of inputs and outputs you need for your block and specify their types (e.g., double, int16, etc.). You can also give them names.
  3. Define the S-function methods: In the S-function Builder window, go to the "Methods" tab. You will see a list of methods such as Start, Outputs, Update, etc. These methods define the behavior of your block.
  4. Implement the Outputs method: Select the Outputs method and click on the "Edit" button. This will open a MATLAB Editor window where you can write the code for the Outputs method.
  5. Assign variables to inputs and outputs: In the Outputs method code, you can assign the values of your variables to the outputs of the block. You can access the inputs and outputs using the u and y pointers, respectively.
For example, assuming you have two inputs and one output defined in the S-function Builder, you can assign the values in your code as follows:
void Outputs(SimStruct *S, int_T tid)
{
// Get the pointers to inputs and outputs
InputRealPtrsType u = ssGetInputPortRealSignalPtrs(S, 0);
InputRealPtrsType v = ssGetInputPortRealSignalPtrs(S, 1);
OutputRealPtrsType y = ssGetOutputPortRealSignalPtrs(S, 0);
// Assign values to outputs
for (int_T i = 0; i < ssGetNumSamples(S); ++i)
{
// Assign the values from your code to the outputs
y[i] = div360(u[i] * v[i]);
}
}
In this example, u and v are pointers to the input signals, and y is a pointer to the output signal. The values from your code are assigned to the output signal within the for loop.
  1. Generate the S-function: Once you have implemented the necessary methods, click on the "Generate S-Function" button in the S-function Builder window. This will generate the S-function file (.c and .tlc) that you can use in Simulink.
After generating the S-function, you can use it in your Simulink model by adding the S-function block and specifying the appropriate inputs and outputs.
  2 Comments
Jeremiah Pe
Jeremiah Pe on 14 May 2023
Thank you for the reply,
So with the S-function Builder I created above with 4 inputs and 2 outputs, a wrapper is created like so
void sfunc_att_Outputs_wrapper(const int16_T *angle,
const uint8_T *method,
const uint32_T *revolutionTime,
const uint16_T *tempTriggerToothAngle,
uint32_T *returnTime,
uint32_T *div360)
With this wrapper, is there a way to get values for the outputs with the inputs from this S-function Builder that was created? Can I link the variables from the code to their respective pointers and not create a new command or so?
I'm sorry if I'm slow, please bear with me.
Shaik
Shaik on 15 May 2023
Hi,
From the code snippet you provided, it appears that you have an S-function wrapper generated by the S-function Builder. This wrapper takes input pointers (angle, method, revolutionTime, and tempTriggerToothAngle) and output pointers (returnTime and div360), which allow you to access and modify the corresponding variables outside the S-function.
To use this wrapper and obtain values for the outputs, you need to follow these steps:
  1. Define the input variables (angle, method, revolutionTime, and tempTriggerToothAngle) and output variables (returnTime and div360) in your MATLAB code or any other programming language you're using.
  2. Assign values to the input variables according to your requirements.
  3. Call the sfunc_att_Outputs_wrapper function, passing the input variable pointers as arguments. This will execute the S-function and calculate the output values.
  4. After the function call, you can access the calculated output values from the output variables you defined earlier.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!