Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Importing Domain and Component Classes

You must store Simscape™ model files (domains and components) in package directories, as described in Organizing Your Simscape Files. Like the MATLAB® class system, each package defines a scope (or namespace). You can uniquely identify a model class name and access it using a fully qualified reference. For example, you can access the domain model class electrical using foundation.electrical.electrical.

In composite components, class member declarations include user-defined types, that is, component classes. If you do not use import statements, accessing component class names from a different scope always requires a fully qualified reference. For example, the Foundation library Resistor block is:

foundation.electrical.elements.resistor

An import mechanism provides a convenient means to accessing classes defined in different scopes, with the following benefits:

  • Allows access to model class names defined in other scopes without a fully qualified reference

  • Provides a simple and explicit view of dependencies on other packages

There are two types of syntax for the import statement. One is a qualified import, which imports a specific package or class:

import package_or_class;

The other one is an unqualified import, which imports all subpackages and classes under the specified package:

import package.*;

The package or class name must be a full path name starting from the library root (the top-level package directory name) and containing subpackage names as necessary.

You must place import statements at the beginning of a Simscape file. The scope of imported names is the entire Simscape file, except the setup section. For example, if you use the following import statement:

import foundation.electrical.elements.*;

at the beginning of your component file, you can refer to the Foundation library Resistor block elsewhere in this component file directly by name:

rotorResistor = resistor(R = rotor_resistance);

See the import reference page for syntax specifics and more examples.

Composite Component Using import Statements

This example shows how you can use import statements to implement a composite component equivalent to the one described in Composite Component — DC Motor . The two components are identical, but, because of the use of the import statements, the amount of typing in the nodes and components sections is significantly reduced.

import foundation.electrical.electrical;   % electrical domain class definition
import foundation.electrical.elements.*;   % electrical elements
import foundation.mechanical.rotational.*; % mechanical rotational domain and elements
component DC_Motor1
% DC Motor1
% This block models a DC motor with an equivalent circuit comprising a
% series connection of a resistor, inductor and electromechanical converter.
% Default values are as for the DC Motor Simscape example, ssc_dcmotor.

nodes
    p = electrical;               % +:left
    n = electrical;               % -:left
    R = rotational;               % R:right
    C = rotational;               % C:right
end

parameters
    rotor_resistance    = { 3.9, 'Ohm' };           % Rotor Resistance
    rotor_inductance    = { 12e-6, 'H' };           % Rotor Inductance
    motor_inertia       = { 0.01, 'g*cm^2' };       % Inertia
    breakaway_torque    = { 0.02e-3, 'N*m' };       % Breakaway friction torque
    coulomb_torque      = { 0.02e-3, 'N*m' };       % Coulomb friction torque
    viscous_coeff       = { 0, 'N*m*s/rad' };       % Viscous friction coefficient
    velocity_threshold  = { 0.1, 'rad/s' };         % Linear region velocity threshold
    back_emf_constant   = { 0.072e-3, 'V/rpm' };    % Back EMF constant
end

components(Hidden=true)
    rotorResistor                  = resistor(R = rotor_resistance);
    rotorInductor                  = inductor(l = rotor_inductance);
    rotationalElectroMechConverter = rotational_converter(K = back_emf_constant);
    friction                       = friction(brkwy_trq = breakaway_torque, Col_trq = coulomb_torque,
                                        visc_coef = viscous_coeff, vel_thr = velocity_threshold);
    motorInertia                   = inertia(inertia = motor_inertia);
end

connections
    connect(p, rotorResistor.p);
    connect(rotorResistor.n, rotorInductor.p);
    connect(rotorInductor.n, rotationalElectroMechConverter.p);
    connect(rotationalElectroMechConverter.n, n);
    connect(rotationalElectroMechConverter.R, friction.R, motorInertia.I, R);
    connect(rotationalElectroMechConverter.C, friction.C, C);
end

end

Consider the three import statements at the beginning of the file. The first one:

import foundation.electrical.electrical;

is a qualified import of the Foundation electrical domain class. Therefore, in the nodes section, you can define the p and n nodes simply as electrical.

The second statement:

import foundation.electrical.elements.*;

is an unqualified import, which imports all subpackages and classes under the foundation.electrical.elements subpackage and therefore gives you direct access to all the Foundation electrical components in the Elements sublibrary, such as inductor, resistor, and rotational_converter.

The third statement:

import foundation.mechanical.rotational.*;

is an unqualified import, which imports all subpackages and classes under the foundation.mechanical.rotational subpackage and therefore gives you direct access to the Foundation mechanical rotational domain definition (rotational) and components (such as friction and inertia).

The nodes block declares two electrical nodes, p and n, and two mechanical rotational nodes, R and C.

The components block declares all the member (constituent) components, using the following components from the Simscape Foundation library:

  • Resistor

  • Inductor

  • Rotational Electromechanical Converter

  • Rotational Friction

  • Inertia

Because of the import statements at the top of the file, these classes already exist in the scope of the file, and you do not have to specify their complete names starting from the top-level package directory.

Was this topic helpful?