From RIVeR
Jump to: navigation, search

SimMechanics is a powerful extension to Simulink that allows you to create or import mechanisms and develop dynamic simulations with them. The following tutorial will go over the necessary steps to import a SolidWorks Assembly into SimMechanics and develop a basic dynamic simulation for the assembly.

Installing SimMechanics

Install MATLAB with Simulink

SimMechanics is already included with WPI's distribution of MATLAB. Install MATLAB via the instructions found here:

Install SimMechanics Link

SimMechanics Link is the utility used to translate your SolidWorks Assembly into a SimMechanics .xml file. Download the files appropriate for your MATLAB installation from this website:

and then follow the instructions at the link below to add SimMechanics Link to MATLAB.

Link SimMechanics Link to SolidWorks

SimMechanics Link must be added to SolidWorks in order to enable exporting an assembly to SimMechanics. Follow the instructions here:

Exporting an Assembly to SimMechanics from SolidWorks

SimMechanics Link translates your SolidWorks Assembly into a SimMechanics Simulation. It does this by translating certain mates in your assembly to joints in the SimMechanics model.

  • Open your SolidWorks Assembly.
  • Make sure all of the joints in your assembly are at their zero-state. When you create the model in SimMechanics, the angle (for revolute joints) and displacement (for prismatic joints) of your joints when they are imported will be used as the zero-point.
  • Go to File->Save As and make sure the file format you use is
    SimMechanics Link (*.xml)
  • A MATLAB command window will open and SimMechanics Link will begin opening all parts from the assembly and saving them as .stl files. These will be used as geometry information for SimMechanics. NOTE: It may take some time for the command window to open. Be patient.
  • When the process completes, you may receive an error message explaining that certain mates could not be translated into SimMechanics Joints and that a weld joint had been placed in their stead. If a weld joint (unmoving joint) is acceptable, you can ignore this message. If not, you may have to change the mate to a mate that is functionally identical. For example, a "width" mate cannot be translated to a SimMechanics joint by SimMechanics Link. Instead, use a distance mate.
  • Close the MATLAB command window.

Importing a SimMechanics Link File to SimMechanics

After successfully exporting your assembly from SolidWorks, you'll have several files in the directory that you saved your SimMechanics Link File. Here, you'll be shown how to import those files into a SimMechanics model.

  • Open MATLAB and change your directory to the directory that contains the files created by SimMechanics Link.
  • Enter the following command:
  • A dialog box will open.

The mech_import dialog box.

  • Navigate to, and select the .xml file that you just exported using SimMechanics Link.
  • Choose "Import to New Model".
  • Name the model you would like to create. Make sure that the name is also a valid MATLAB function name.
  • Press OK.
  • MATLAB will import the information from the .stl and .xml files and open a SimMechanics model of your assembly.
  • Save the file.
  • MATLAB will create a .mdl file with the name you chose in the same directory as the .xml you imported.
 IMPORTANT: Do not move the .mdl file from the directory as it uses the .stl files to define the geometry for your model.

Running Your Simulation for the First Time

  • If the simulation isn't already open from the previous step, double click the .mdl file to open the model.
  • Choose Edit->Update Diagram (or press CTRL+D) and a window will open showing your assembly in its zero-state. This window will be referred to as the Simulation Diagram throughout the rest of this tutorial.
  • Press the play button. The model will simulate the affects of gravity on your model's frictionless, non-damped, non-driven joints.

Adding Joint Sensors to Your Simulation

  • Open your SimMechanics Model.
  • Choose the joint you would like to add a sensor to and double click it. A dialog box opens:


  • Increase the "Number of sensor / actuator ports" value by one.
  • Your joint will now have an additional port to allow you to connect a sensor.
  • Open the Simulink Library

Simulink Library.JPG

  • Joint sensors are located under Simscape -> SimMechanics -> First Generation -> Sensors & Actuators
  • Click and drag the Joint Sensor block into your model and connect its input to the joint you added the additional port to.
  • Double clicking the Joint Sensor will enable you to change what the Joint Sensor monitors.
  • Add a scope to the output of the Joint Sensor, double click the scope to view it, and run your simulation.

Adding Joint Actuators to Your Simulation

  • Add a port to your joint per the instructions in the above section.
  • Open the Simulink Library.
  • Joint actuators are located under Simscape -> SimMechanics -> First Generation -> Sensors & Actuators
  • Click and drag the Joint Actuator block into your model and connect its output to the joint you added the additional port to.
  • The input for joint actuators can be either a motion or a force. Double click the actuator to change this.

Joint Actuator.JPG

  • If using a force to control the joint actuator, the input will be a scalar. The units of your input will be determined by the drop down menus in the actuator dialog box.
  • If using a motion to control the joint actuator, the input will be an array in the form of [position velocity acceleration]. This can be manually provided through dynamic calculations if acceleration and velocity profiles are desired. If not, the following block can be downloaded and used to automatically calculate velocity and acceleration terms from a desired position. However, please note that you will have no control over the velocity and acceleration profiles:

Adding Additional Features to Your Simulation

Additional features for your simulation can be found in Simulink Library under Simscape -> SimMechanics -> First Generation

Helpful Simulation Settings

  • Change the color of a body in your simulation by double clicking the body block, clicking on the "Visualization" tab, and then changing "Body Color".
  • Change the length of time that your simulation will run by going to Simulation -> Configuration Parameters -> Solver and changing "Start time:" and "Stop time:".
  • Change the speed at which the speed of your simulation animates via changing the size of the time steps by going to Simulation -> Configuration Parameters -> Solver -> Solver options.
  • To save your animation as an .avi file for use in presentations:
    • In the Simulation Diagram, choose Simulation -> Store in AVI File, choose the file location, and press the play button. The file will automatically be saved when the simulation completes.