how to make constant variables being used in s-functions as tunable in matlab - matlab

i have written a simple s-function which calls a function inside a c code which models the single track model. i am using constant variables for storing mass,yaw moment of inertia,steering ratio etc. I wanted to know how to make these variables tunable. i want to create a subsystem out of my s-function and then use realtime workshop->generate s-function and select the tunable parameters from the list. but for now am not able to find any tunable parameters since i have not specified anything as tunable
this is my s-function code
#define S_FUNCTION_NAME single_track
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "single_track_func.c"
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
initialization();
}
#endif
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);
if (!ssSetNumOutputPorts(S,3)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortWidth(S, 1, 1);
ssSetOutputPortWidth(S, 2, 1);
ssSetNumSampleTimes(S, 1);
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S,
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
retvale obj_b;
InputRealPtrsType v_1 = ssGetInputPortRealSignalPtrs(S,0); //velocity
InputRealPtrsType delta_1 = ssGetInputPortRealSignalPtrs(S,1); //steering angle
real_T *a_y_1 = ssGetOutputPortRealSignal(S,0); //lateral acceleration
real_T *psi_dot_1 = ssGetOutputPortRealSignal(S,1); //yaw velocity
real_T *beta_1 = ssGetOutputPortRealSignal(S,2); //attitude angle
obj_b=singletrack((double)*(*v_1),(double)*(*delta_1));
*a_y_1 = obj_b.a_y; //lateral acceleration
*psi_dot_1 =obj_b.psi_dot; //yaw velocity
*beta_1 =obj_b.beta;
}
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
and this is the logic file which has the function singletrack()
#include "single_track_func.h"
float a_1_1,a_1_2,a_2_1,a_2_2,b_1_1,b_2_1,psi_dot_prev,beta_prev;
int count;
const int cv=75000; //cornering stiffness front
const int ch=150000; //cornering stiffness rear axle
const int m=1550; //mass of the vehicle kg
const int lv=1.344; //distance from center of gravity to front wheel
const int lh=1.456; //distance from center of gravity to rear wheel
const int theta=2800; //yaw moment of inertia
const int I_s=16; //overall steering ratio
const float dt=0.001;
retvale singletrack(double a,double b)
{
retvale my_obj;
static float beta_dot=0;
static float psi_double_dot=0;
static float beta_previous=0;
static float psi_dot_previous=0;
beta_previous = beta_prev;
psi_dot_previous = psi_dot_prev;
a_1_1 = ((-cv-ch)/((m)*(a)));
a_1_2 = ((m*(a)*(a))-((ch*lh)-(cv*lv)))/(m*(a)*(a));
a_2_1 = (-(ch*lh)+(cv*lv))/theta;
a_2_2 = ((-ch*lh*lh)-(cv*lv*lv))/(theta*(a));
b_1_1 = -cv/(m*(a));
b_2_1 = (cv*lv)/(theta);
beta_dot = a_1_1 * beta_previous + a_1_2 * psi_dot_previous - b_1_1*((b)/I_s);
psi_double_dot = a_2_1 * beta_previous + a_2_2 * psi_dot_previous + b_2_1*((b)/I_s);
my_obj.beta = beta_dot * dt + beta_previous;
my_obj.psi_dot = psi_double_dot * dt + psi_dot_previous;
my_obj.a_y = (a*((my_obj.psi_dot)-(beta_dot)));
beta_prev=my_obj.beta;
psi_dot_prev=my_obj.psi_dot;
return my_obj;
}
void initialization()
{
psi_dot_prev=0;
beta_prev=0;
}
and the corresponding .h file
#ifndef _SINGLE_TRACK_FUNC_
#define _SINGLE_TRACK_FUNC_
typedef struct retvale
{
double a_y;
double psi_dot;
double beta;
} retvale;
extern struct retvale singletrack(double a,double b);
extern void initialization();
#endif
I know i have to use ssSetSFcnParamTunable() but even after looking at examples have no idea how to do it!!
update:
i declared the variables as global real_T
real_T *m_s,*cv_s,*ch_s,*lv_s,*lh_s,*theta_s,*I_s_s,*dt_s;
and added these lines of codes in my mdlInitializeSizes(). i mex it and everything is fine. but when i use the s-function block and just change the name of the s-function to the mex file the matlab crashes. also i pass these arguments as pointers to my single_track () function
ssSetNumSFcnParams(S, 8);
m_s=mxGetPr(ssGetSFcnParam(S,0));
cv_s=mxGetPr(ssGetSFcnParam(S,1));
ch_s=mxGetPr(ssGetSFcnParam(S,2));
lv_s=mxGetPr(ssGetSFcnParam(S,3));
lh_s=mxGetPr(ssGetSFcnParam(S,4));
theta_s=mxGetPr(ssGetSFcnParam(S,5));
I_s_s=mxGetPr(ssGetSFcnParam(S,6));
dt_s=mxGetPr(ssGetSFcnParam(S,7));
ssSetSFcnParamTunable(S,0,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,1,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,2,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,3,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,4,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,5,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,6,SS_PRM_SIM_ONLY_TUNABLE);
ssSetSFcnParamTunable(S,7,SS_PRM_SIM_ONLY_TUNABLE);
any idea why there is a crash?

For a parameter to be tunable it must be passed as an input from Simulink to your S-Function.
This is done by specifying them in the Parameters part of the S-Function block's dialog (as a comma separated list), and using the ssSetNumSFcnParams method within the S-Function itself to tell the S-function how many parameters to expect (you currently have that set to 0).
Also, within the S-Function you'll need to
Use the ssGetSFcnParam method to get the data (from the dialog into the code).
Use the ssSetSFcnParamTunable method to say that the parameter(s) is tunable.
Then you'll need to rewrite your singletrack function so that all of the parameters are passed into it as inputs rather than being hard coded into the file.
However, looking at the code you've given, it would be far easier to do this all as a MATLAB Function than it would as an S-Function.

Related

Generating a DLL from a Simulink model with S-Function and using it in Unity 3D

I'm trying to generate a DLL for a Simulink model with an S function and use that in Unity 3D. To try a simple example, I just made a model with an S-function that multiplies the input value by 2.
I've generated the DLL of this using the Embedded Coder feature. The code generated contains the essential function as follows:
void S_function_tlc_try_step(RT_MODEL_S_function_tlc_try_T *const
S_function_tlc_try_M, real_T S_function_tlc_try_U_x, real_T
*S_function_tlc_try_Y_y)
{
/* local block i/o variables */
real_T rtb_Level2MATLABSFunction1;
/* M-S-Function: '<Root>/Level-2 MATLAB S-Function1' incorporates:
* Inport: '<Root>/x'
*/
/* Multiply input by two */
rtb_Level2MATLABSFunction1 = S_function_tlc_try_U_x * 2.0;
/* Outport: '<Root>/y' */
*S_function_tlc_try_Y_y = rtb_Level2MATLABSFunction1;
/* Update absolute time for base rate */
/* The "clockTick0" counts the number of times the code of this task has
* been executed. The absolute time is the multiplication of "clockTick0"
* and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
* overflow during the application lifespan selected.
*/
S_function_tlc_try_M->Timing.t[0] =
((time_T)(++S_function_tlc_try_M->Timing.clockTick0)) *
S_function_tlc_try_M->Timing.stepSize0;
UNUSED_PARAMETER(S_function_tlc_try_M);
}
The script in Unity is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class Sfunctiontry : MonoBehaviour
{
public double input, output;
double constant;
[DllImport("S_function_tlc_try_win64")]
static extern void S_function_tlc_try_step(double constant, double inp, ref double outp);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
S_function_tlc_try_step(constant, input, ref output);
Debug.Log("Multiplying by 2");
print(output);
}
}
However whenever I attach this to an object and execute, Unity crashes. It works fine when I'm using the DLL of a model without any S-function.
Is there a solution for this? Any leads would be great.
Thanks in advance.

Simulink S-Function Builder with External Library and Runtime Binary

I am trying to make a S-Function (written in C and using SDL library) in Simulink to read joystick values from the device and output them to Simulink. I used before the standard aerospace library joystick block, however, this does not support compiling (which is needed for Rapid Acceleration). Hence, I decided to write my own S-Function.
I managed to make a simple C program that uses SDL (I downloaded https://www.libsdl.org/download-2.0.php) to read values of joystick and print to terminal:
#include <stdio.h>
#include <unistd.h>
#include "SDL2/SDL.h"
static SDL_Joystick *joy = NULL;
int
main(int argc, char *argv[])
{
// ITIALIZE:
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
printf("Opened Joystick 0\n");
printf("Name: %s\n", SDL_JoystickNameForIndex(0));
printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
printf("Could not open Joystick 0\n");
return 0;
}
}
SDL_JoystickEventState(SDL_IGNORE);
// READ VALUES:
for (int i = 1; i < 100; ++i){
SDL_JoystickUpdate();
for (int j = 0; j < SDL_JoystickNumAxes(joy); ++j) {
int value = (((int) SDL_JoystickGetAxis(joy, j)) + 32768);
printf("Axes %d: %d ", j, value);
}
printf("\n");
usleep(100000);
}
// CLOSE:
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
return 1;
}
I compile the C code in Code::Blocks with linkers:
-lmingw32 -lSDL2main -lSDL2
The compiled .exe requires runtime binary SDL.dll, which is simply located in the same folder as the compilation, and everything works.
The problem is, how to transfer the above to work in Simulink environment? I have transferred the code into Simulink S-Function builder:
/* Includes_BEGIN */
#include "SDL2/SDL.h"
#include <stdio.h>
/* Includes_END */
/* Externs_BEGIN */
static SDL_Joystick *joy = NULL;
/* Externs_END */
void sunf_joystick_Start_wrapper(void)
{
/* Start_BEGIN */
// ITIALIZE:
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
// Check for joystick
if (SDL_NumJoysticks() > 0) {
// Open joystick
joy = SDL_JoystickOpen(0);
if (joy) {
ssPrintf("Opened Joystick 0\n");
ssPrintf("Name: %s\n", SDL_JoystickNameForIndex(0));
ssPrintf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
ssPrintf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
ssPrintf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
} else {
ssWarning("Warning:Joystick","Could not open Joystick 0\n");
}
}
SDL_JoystickEventState(SDL_IGNORE);
/* Start_END */
}
void sunf_joystick_Outputs_wrapper(real_T *y0)
{
/* Output_BEGIN */
SDL_JoystickUpdate();
for (int j = 0; j < SDL_JoystickNumAxes(joy); ++j) {
y0[j] = (((int) SDL_JoystickGetAxis(joy, j)) + 32768);
}
/* Output_END */
}
void sunf_joystick_Terminate_wrapper(void)
{
/* Terminate_BEGIN */
if (SDL_JoystickGetAttached(joy)) {
SDL_JoystickClose(joy);
}
/* Terminate_END */
}
and added the LIB_PATH and INCL_PATH to point for the SDL library:
S-function builder Libraries tab
However, I get a lot of similar error messages when trying to build through the GUI:
C:\Users\user\AppData\Local\Temp\mex_121831936796334_22096\sunf_joystick_wrapper.obj:sunf_joystick_wrapper.c:(.text+0xe): undefined reference to `SDL_InitSubSystem'
To me it seems that the libraries are not linked correctly. An idea how to fix this? I have tried to build it also with mex through MATLAB command line, not successful, and feels also wrong way to do it.
Also, any advice where the the runtime library SDL.dll should be stored or referenced if the compilation is successful?
All files in: https://github.com/JohannesSoikkeli/Simulink_joystick
Many thanks!

(Unity) How to bake data (Vector3 and Color32) onto render textures?

With the recent introduction of VFX Graph, attribute maps are being used to 'Set Position/Color from Map'.
In order to get an attribute map, one must bake position and color data into render textures. But there is no reference to how to do this that I could find or even on the Unity docs.
Any help on how to do this will be appreciated!
Most of the time you would want to use a Compute Shader to bake a list of points into your textures. I'd suggest you check these repositories for reference:
Bake Skinned Mesh Renderer Data into textures
https://github.com/keijiro/Smrvfx
Bake Kinect data into textures
https://github.com/roelkok/Kinect-VFX-Graph
Bake pointcloud data into texture:
https://github.com/keijiro/Pcx
Personally, I'm using these scripts which work for my purpose though I'm no expert in Compute Shaders:
public class FramePositionBaker
{
ComputeShader bakerShader;
RenderTexture VFXpositionMap;
RenderTexture inputPositionTexture;
private ComputeBuffer positionBuffer;
const int texSize = 256;
public FramePositionBaker(RenderTexture _VFXPositionMap)
{
inputPositionTexture = new RenderTexture(texSize, texSize, 0, RenderTextureFormat.ARGBFloat);
inputPositionTexture.enableRandomWrite = true;
inputPositionTexture.Create();
bakerShader = (ComputeShader)Resources.Load("FramePositionBaker");
if (bakerShader == null)
{
Debug.LogError("[FramePositionBaker] baking shader not found in any Resources folder");
}
VFXpositionMap = _VFXPositionMap;
}
public void BakeFrame(ref Vector3[] vertices)
{
int pointCount = vertices.Length;
positionBuffer = new ComputeBuffer(pointCount, 3 * sizeof(float));
positionBuffer.SetData(vertices);
//Debug.Log("Length " + vertices.Length);
bakerShader.SetInt("dim", texSize);
bakerShader.SetTexture(0, "PositionTexture", inputPositionTexture);
bakerShader.SetBuffer(0, "PositionBuffer", positionBuffer);
bakerShader.Dispatch(0, (texSize / 8) + 1, (texSize / 8) + 1, 1);
Graphics.CopyTexture(inputPositionTexture, VFXpositionMap);
positionBuffer.Dispose();
}
}
The compute shader:
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> PositionTexture;
uint dim;
Buffer<float3> PositionBuffer;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
uint index = id.y * dim + id.x;
uint lastIndex = PositionBuffer.Length - 1;
// Trick for generating a pseudo-random number.
// Inspired by a similar trick in Keijiro's PCX repo (BakedPointCloud.cs).
// The points that are in excess because of the square texture, point randomly to a point in the texture.
// e.g. if (index > lastIndex) index = 0 generates excessive particles in the first position, resulting in a visible artifact.
//if (index > lastIndex) index = ( index * 132049U ) % lastIndex;
float3 pos;
if (index > lastIndex && lastIndex != 0) {
//pos = 0;
index = ( index * 132049U ) % lastIndex;
}
pos = PositionBuffer[index];
PositionTexture[id.xy] = float4 (pos.x, pos.y, pos.z, 1);
}

Timer0 as counter with Microchip PIC10F202

I developing RC switch for my RC glider.
I have microkontroller Microchip PIC10F202 and I have problem with ON/OFF LED Lights.
I want triggering input PPM signal from RC receiver, I setted GP2 as input (by T0CKI and count for every rise edge - from low to high), but LED is still ON, not reacting on input signal from RC receiver.
I post my source code in C language, I can not ASM language.
// this is header file with some macros and function prototypes
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h>
#define _XTAL_FREQ 4000000
#define KONST_ON 50
#define KONST_OFF 1000
#define STROBO_LED GP0
#define NAVI_LED GP1
#define RC_SIGNAL GP2
#define STAV_ON 1
#define STAV_OFF 0
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
void setup ();
void flashing ();
#endif
// In this function is setup for microkontroller
#include <xc.h>
#include "prototypy.h"
void setup ()
{
OSCCAL = 0b0111111;
//STATUS = 0b00111000;
OPTION = 0b11100111;
TRISGPIO = 0b0000;
};
// This is function for LED flashing
#include <xc.h>
#include "prototypy.h"
void flashing ()
{
__delay_ms (KONST_OFF);
STROBO_LED = STAV_OFF;
__delay_ms (KONST_ON);
STROBO_LED = STAV_ON;
};
// in this main function I do logical product, where I have logical state from pin GP2 and number 1
#include <xc.h>
#include "prototypy.h"
// Code protection bit
#pragma config CP = OFF
// Watchdog timer
#pragma config WDTE = OFF
// Pin function
#pragma config MCLRE = OFF
void main ()
{
setup ();
while (1)
{
if (RC_SIGNAL & 1)
{
flashing ();
}
}
}
Please, can me someone help and found error in my source code?
I'am not really familar with that devive, but I guess you Pin RC_SIGNAL is not configured as an Input. Please try
TRISGPIO = 0b0100;

Trying to write 16-bit PNG

I'm capturing images from camera, and I have two function for saving 16-bit(!) image one in PNG and one in TIFF formats.
Could you please explain why the PNG is a very noisy image? like this:
PNG function:
bool save_image_png(const char *file_name,const mono16bit& img)
{
[...]
/* write header */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during writing header");
png_set_IHDR(png_ptr, info_ptr, width, height,
bit_depth,PNG_COLOR_TYPE_GRAY , PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
/* write bytes */
if (setjmp(png_jmpbuf(png_ptr)))
abort_("[write_png_file] Error during writing bytes");
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
for (y = 0; y < height; y++)
{
row_pointers[y] = (png_bytep)img.getBuffer() + y * width*2;
}
png_write_image(png_ptr, row_pointers);
/* end write */
[...]
}
and TIFF function:
bool save_image(const char *fname,const mono16bit& img)
{
[...]
for(y=0; y<height; y++) {
if((err=TIFFWriteScanline(tif,(tdata_t)(img.getBuffer()+width*y),y,0))==-1)
break;
}
TIFFClose(tif);
if(err==-1) {
fprintf(stderr,"Error writing to %s file\n",fname);
return false;
}
return true;
//#endif //USE_LIBTIFF
}
Thank you!
png_set_swap does nothing. You have to actually flip bytes in each pixel of the image.
If you’re on a PC and have SSSE3 or newer, a good way is _mm_shuffle_epi8 instruction, make a permute vector with _mm_setr_epi8.
If you’re on ARM and have NEON, use vrev16q_u8 instruction instead.
Perhaps you have a byte-order problem.
Try adding:
png_set_swap(png_ptr);
before saving the image