I have a subsystem and generate a S-Function by right clicking, selecting C/C++ -> generate S-Function. I then choose the mass variable in the Body block to be tunable. Once the S-Function is generated, I click on the mask of the S-Function but the mass does not appear.
MATLAB: How to make the mass of a Body block become tunable when generating a S-Function for a subsystem in SimMechanics 4.1 (R2012b)
Simscape Multibody
Related Solutions
The ability to remove a body from simulation is not directly available in SimMechanics 4.1 (R2012b).
However, there are some workarounds that can model this to some extent.
1. SimMechanics First Generation
With First Generation blocks, the mass and inertia of a body can be dynamically altered during simulation using a Variable Mass & Inertia Actuator block. Therefore, the mass and inertia can be set to a very small number (but not zero), at the point that the object is removed.
2. SimMechanics Second Generation
Second Generation technology in SimMechanics 4.1 (R2012b) does not have a Variable Mass & Inertia Actuator block (see solution 1-ICUMET). However, the effects of a body can be modeled using a dynamic load.
Please refer to the attached model of a robot arm holding up a load for 10 seconds and then dropping it. This load is modeled as follows:
a. Currently, in SimMechanics Second Generation, bodies can only be actuated with force and torque (and not motion). Therefore, you can use a simple Proportional Controller to control the arm's position and to hold up the load at the end of the arm for the first 10 seconds of simulation.
b. There is a Signal Viewer inside the "Arm Controller" block in the attached model. Please look at the torque on the arm here. When the load is engaged, the torque is larger and when the load is dropped, the torque value decreases to account for the lost mass.
The ability to retain a custom mask for a generated s-function block is not available in Simulink Coder. When generating an S-function from a subsystem a new mask is generated automatically. You have to define appropriate parameters as tunable, if you want to change them.
To work around this issue and use your original mask instead of the one automatically created, some adaptions are necessary. This is because when generating an S-function new variable names are generated for the tunable parameters.
To generate an S-function from your subsystem you can open its context menu and choose Code Generation -> Generate S-Function. A GUI will show up, where you can pick tunable parameters. In this list you have to define all your mask parameters, to ensure you will be able to change these values for your S-function block as well. When clicking the Build button, the S-function is generated with a new mask.
You now can do the following:
1) Make a copy of the original subsystem
2) Delete its contents
3) Paste the generated S-function (as new content)
4) Now in the original mask all variable names have to be replaced by its corresponding S-function parameter names (sfcnParam1, sfcnParam2, ...).
As long as you do not change the mask of the original subsystem and do not change the list of tunable parameters for you subsystem/S-function, you will not need to repeat this process when updating the subsystem and generating a new S-function from it.
Best Answer