Model and Validate a System - MATLAB & Simulink (original) (raw)

Model each component in the robot system to reflect its physical or functional behavior. Then, verify the model by simulating with test data. A Simulink® model of a component is based on several starting points:

This tutorial models physical and functional components with explicit input/output relationships. In this tutorial, you will:

  1. Use the system equations to create a Simulink model.
  2. Add and connect Simulink blocks in the Simulink Editor. Blocks represent coefficients and variables from the equations.
  3. Build the model for each component separately. The most effective way to build a model of a system is to first consider components independently.
  4. Start by building simple models using approximations of the system. Identify assumptions that can affect the accuracy of your model. Iteratively add detail until the level of complexity satisfies the modeling and accuracy requirements.
  5. Validate components by supplying an input and observing the output.

Open System Layout

A big-picture view of the whole system layout is useful when modeling individual components. This model shows how the individual components are related to each other. Start by loading the system_layout model.

The system_layout model.

Model Physical Components

Describe the relationships between components, for example, data, energy, and force transfer. Use the system equations to build a graphical model of the robot system in Simulink.

Some questions to ask before you begin to model a component:

Derive the equations for each component using scientific principles. Many system equations fall into three categories:

Model Linear Motion of Wheels

Two forces act on a wheel:

Acceleration is proportional to the sum of these forces:

where kdrag is the drag coefficient and_m_ is the mass of the robot. Each wheel carries half of this mass.

Build the wheel model:

  1. In the system_layout model, double-click the subsystem named Right Wheel to display the empty subsystem.
  2. To model velocity and acceleration, add an Integrator block. Leave the initial condition set to 0. The input of this block is the acceleration Vdot and the output is the velocity V.
    The Integrator block has an unconnected input signal named Vdot and an output signal labeled V that is connected to an Outport block named Speed. An unconnected Inport block named Force is to the left of the unconnected signal line for the signal Vdot.
  3. To model the drag force, add a MATLAB Function block from the User-Defined Functions library. The MATLAB Function block provides a quick way to implement mathematical expressions in your model. To edit the function, double-click the block to open the MATLAB Function Block Editor.
  4. In the MATLAB Function Block Editor, enter the MATLAB® code to calculate the drag force.
    function Fdrag=get_fdrag(V,k_drag)
    Fdrag=k_dragVabs(V);
    The MATLAB Function Block Editor shows the code for a MATLAB function named Fdrag that calculates the drag force.
  5. Define arguments for the MATLAB Function block. In theMATLAB Function Block Editor, click Edit Data. Click k_drag, and then set Type to .
  6. Subtract the drag force from the motor force using theSubtract block. Complete the force-acceleration equation using a Gain block with the Gain parameter set to 1/(m/2).
  7. To reverse the direction of the MATLAB Function block, select the block. Then, in the Simulink Toolstrip, on the Format tab, click Flip left-right . Connect the blocks.
    In the wheel model, the MATLAB Function block is in a feedback loop between the speed output signal. The Subtract block subtracts the drag force calculated by the MATLAB Function block from the force input to the subsystem. The Subtract block and Gain block are between the force input and the input to the Integrator block.
  8. The dynamics of the two wheels are the same. Make a copy of the subsystem named Right Wheel that you just created and paste it in the subsystem named Left Wheel.

To view the parent model, click Up To Parent .

Model Rotational Motion of Wheels

When the two wheels turn in opposite directions, they move in a circle of radius_r_, causing rotational motion of the robot. When the wheels turn in the same direction, there is no rotation. Assuming that the wheel velocities are always equal in magnitude, you can model rotational motion as dependent on the difference of the two wheel velocities VR and VL:

Build the rotation dynamics model:

  1. In the top level of the system_layout model, double-click the subsystem named Rotation to display the empty subsystem. Delete the connection between the Inport and the Outport blocks.
  2. To model angular speed and position, add an Integrator block. Leave the initial condition set to 0. The output of this block is the rotational position θ and the input is the angular speed θ˙.
  3. Compute angular speed from tangential speed. Add a Gain block and set the Gain parameter for the block to1/(2*r).
  4. Connect the blocks.
    An Inport block named Speed difference connects to the Gain block, which connects to an Integrator block, which connects to an Outport block named Angle

To view the parent model, click Up To Parent .

Model Functional Components

Describe the function from the input of a function to its output. This description can include algebraic equations and logical constructs, which you can use to build a graphical model of the system in Simulink.

Model Coordinate Transformation

The velocity of the robot in the x and y coordinates, VX and_VY_, is related to the linear speed_VN_ and the angle θ:

Build the coordinate transformation model:

  1. In the top level of the system_layout model, double-click the subsystem named Coordinate Transform to display the empty subsystem.
  2. To model trigonometric functions, add a SinCos block from the Math Operations library.
  3. To model multiplication, add two Product blocks from the Math Operations library.
  4. Connect the blocks.
    The coordinate transformation model has two inputs named Linear speed and Angle and two outputs named Y speed and X speed. The X and Y speeds are calculated by multiplying the sine and cosine of the angular position by the linear speed input.

To view the parent model, click Up To Parent .

Set Model Parameters

You can determine appropriate values for parameters in your model using several sources, including:

The system_layout model uses these parameters.

Parameter Symbol Value
Mass m 2.5 kg
Rolling resistance k_drag 30 Ns2/m
Robot radius r 0.15 m

A Simulink model can access parameter values defined using variables in the MATLAB workspace. Define these variables by entering the commands in the MATLAB Command Window.

m = 2.5; k_drag = 30; r = 0.15;

Validate Components

Validate components by supplying an input and observing the output. Even such a simple validation can point out immediate ways to improve the model. This example validates these behaviors:

This example uses Outport blocks to log output data to the workspace and the Simulation Data Inspector. For more information about logging data and other logging techniques, see Save Simulation Data.

Validate Wheel Component

Create and run a test model for the wheel component:

  1. Create a new model. In the Simulation tab, clickNew . Copy the Subsystem block named Right Wheel into the new model.
  2. To create a test input, add a Step block from the Sources library and connect it to the input of the subsystem named Right Wheel. Leave the Step time parameter set to 1.
  3. To log output, connect the subsystem to an Outport block.
    A Step block connects to the input of a subsystem block named Right Wheel. An Outport block connects to the output port of the subsystem block. The input port of the subsystem is named Force, and the output port of the subsystem is named Speed.
  4. Simulate the model. In the Simulation tab, clickRun .
  5. To view the results in the Simulation Data Inspector, clickData Inspector . To plot the data, select the check box next to the Speed:1 signal.
    The Simulation Data Inspector plots the output signal of the subsystem named Right Wheel.

The simulation results show that the model exhibits the general expected behavior. No motion occurs until force is applied at the step time. When force is applied, the speed starts to increase and then settles at a constant when the applied force and the drag force reach an equilibrium. In addition to validation, this simulation also shows the maximum speed of the wheel for the given force.

Validate Rotation Component

Create and run a test model for the rotation model:

  1. To create a new model, click New . Copy the subsystem block namedRotation into the new model.
  2. To create a test input in the new model, add a Step block from the Sources library. Leave the Step time parameter set to 1. Connect the output of the Step block to the input of the subsystem named Rotation. This input represents the difference of the wheel velocities when the wheels rotate in opposite directions.
  3. To log output, connect the subsystem to an Outport block.
    A Step block connects to the input of a Subsystem block named Rotation. The input port of the subsystem is named Speed difference, and the output port of the subsystem is named Angle. The output is logged using an Outport block.
  4. Simulate the model. In the Simulation tab, clickRun .
  5. To view the results in the Simulation Data Inspector, clickData Inspector . To plot the data, select the check box next to the Angle:1 signal.
    The Simulation Data Inspector plots the output signal of the subsystem named Rotation.

This simulation shows that the angle increases steadily when the wheels are turning with the same speed in opposite directions. You can improve the model to make the angle output easier to interpret. For example, you can:

MATLAB trigonometric functions take inputs in radians.

Validate Model

After you validate individual components, you can perform a similar validation on the complete model. This example validates these behaviors:

Test Wheels Spinning in Same Direction

When the wheels are both spinning in the same direction, the robot should move in a line.

  1. In the system_layout model, double-click the subsystem named Inputs to display the empty subsystem.
  2. Create a test input by adding a Step block. Leave theStep time parameter set to 1. Connect the Step block output to both Outport blocks.
    A Step block connects to two Outport blocks, one named Left force and one named Right force
  3. At the top level of the model, connect both Integrator blocks to Outport blocks to log the data. Label the signalsX position and Y position.
    The Coordinate Transform subsystem has two output ports named X speed and Y speed connected to two Outport blocks..
  4. Simulate the model.
  5. In the Simulation Data Inspector, select the X position and Y position signals to view the results.

Since the angle is zero and does not change, the vehicle moves only in the_x_ direction, as expected.

The Simulation Data Inspector plots the x position and y position.

Test Wheels Spinning in Opposite Directions

When the wheels spin in opposite directions, the robot should rotate in place.

  1. Double-click the subsystem named Inputs. To reverse the direction for the left wheel, add a Gain block between the source and the second output and set the Gain parameter to -1.
    The Step block connects directly to the Outport block named Right Force. The Gain block with a gain of -1 inverts the Step block output. The output from the Gain block connects to the Outport block named Left force.
  2. Mark the angle output for signal logging. Right-click the output signal from the subsystem block named Rotation and clickLog Selected Signals.
  3. Simulate the model.
  4. View the signals in the Simulation Data Inspector using two vertically aligned subplots. Click Visualizations and Layouts. Then, under Basic Layouts, choose the 2×1 layout. In the upper subplot, plot the X position and Y position signals. In the lower subplot, plot the Rotation:1 signal.

The upper subplot shows that no motion occurs in the X-Y plane. The lower subplot shows steady angular motion.

The Simulation Data Inspector with two vertically aligned subplots. The upper subplot shows that the X position and Y position signals remain zero for the duration of the simulation. The lower subplot shows a linearly increasing signal indicating steady angular motion.

You can use this final model to explore different scenarios by changing the input:

See Also

Blocks

Tools

See Also

Topics