Main Content

Simulate Architectural Behavior

To simulate the mobile robot logical architecture, link Simulink® models to the components. These linked models act as Simulink behaviors and can be simulated in System Composer™ by selecting Run. To access the models and supporting files used in this example, see Simulate Mobile Robot with System Composer Workflow.

Add Simulink Behavior to Architecture Models with Bus Ports

The initial logical architecture model describes the behavior of the mobile robot system — trajectory generator, trajectory follower, motor controller, and robot and environment — for simulation. The connections represent the interactions in the system. To open the initial logical architecture model, double-click the file or enter this command in the MATLAB® Command Window.

% Open the initial logical architecture model for the mobile robot
systemcomposer.openModel('scMobileRobotLogicalArchitectureInitial');

The initial logical architecture of the mobile robot.

The structure of the logical architecture is similar to that of a Simulink model because simulation models are designed based on the flow of information. The components of the logical architecture model are linked to behavior models so that the architecture model can be simulated.

Each component is responsible for one or more functions defined in the functional architecture model. The Trajectory Follower component is responsible for calculating the wheel speed of the robot based on the path the generator created. The lower-level Motor Controller component controls the speed of each actuator motor according to the output from the Trajectory Follower component.

Note that some components are omitted from this example model. For example, sensor models like Lidar Sensor and RGB Camera are not required in this model because the true value from simulation gets the x-y position and orientation of the robot. For more complex simulations, you can add sensor models like RGB Camera to test different algorithms, such as object recognition. If you were to add such a sensor model, Lidar Sensor, another behavior component, would be required to decipher the sensor data in the Scan Matching Algorithm component.

Add Sensor Algorithm Component with Simulink Behavior

Simulate the logical architecture model by adding Simulink behavior to a Sensor Algorithm component.

  1. Create a Sensor Algorithm component. Add two input ports on the right side called Sensor Reading 1 and Sensor Reading 2. Add two output ports on the left side called xy for x-y position and q for quaternion.

    A new sensor algorithm component is highlighted in blue to show it has been added to the initial logical architecture.

  2. To create a new Simulink behavior, right-click the Sensor Algorithm component and select Create Simulink Behavior. From the Type list, select Model Reference. Choose a new model name. In this example, it is SensorAlgorithm.

    The create Simulink behavior dialog for the Sensor Algorithm component.

    Click OK. The new Simulink model is saved in the current folder. The component is converted to a reference component.

    The mobile robot logical architecture with sensor algorithm behavior.

  3. To edit the behavioral model, double-click the Sensor Algorithm component. Observe that bus element ports are created during the conversion process. For more information on setting bus ports, see Explore Simulink Bus Capabilities.

    Bus ports in the sensor algorithm component.

  4. Any port block can be used to connect different components. Convert the Sensor Reading 2 bus port and the q bus port into regular Inport and Outport blocks by deleting them and recreating them as Inport and Outport.

    Two new inport and outport blocks converted from bus ports.

  5. Double-click the Sensor Reading 1 bus port to view its properties, then pause on the name Sensor Reading 1 and click the pencil icon to open Attributes. Set the Dimensions to 2.

    The properties of the input port sensor reading 1. The dimensions attribute is set to 2.

  6. Double-click the Sensor Reading 2 port to open Block Parameters, then switch to the Signal Attributes tab. Set the Sensor Reading 2 port Port dimensions to 4.

    The sensor reading 2 block parameters. The port dimensions is set to 4.

  7. Return to the logical architecture and connect the components. The result should look like scMobileRobotLogicalArchitecture.slx in the next section.

Logical Architecture Model for Mobile Robot

The logical architecture model describes the behavior of the mobile robot system — trajectory generator, trajectory follower, motor controller, sensor algorithm, and robot and environment — for simulation. The connections represent the interactions in the system. To open the logical architecture model, double-click the file or enter this command in the MATLAB Command Window.

% Open the logical architecture model for the mobile robot
systemcomposer.openModel('scMobileRobotLogicalArchitecture');

The mobile robot example logical architecture model.

A behavior algorithm is created based on port information only. When designing a logical architecture, you can set the interface of the port to define the information in more detail. For example, if you know that 800 x 600 RGB images captured at 24 frames per second are transferred from the camera sensor, then you can set the corresponding port interfaces accordingly to ensure efficient data transfer. For more information about setting interfaces, see Define Port Interfaces Between Components.

Running Simulation Using Logical Architecture

Once behavior models are linked, you can simulate the architecture model just like any other Simulink model by clicking Run. Simulation verifies requirements such as Transportation, Collision Avoidance, and Path Generation.

The scope from the MotorController component behavior shows how well a simple P-gain controller performs to follow the reference velocity for one of the wheels on the robot.

The scope for the P-gain controller for the robot wheels

Run this script to observe how well the robot follows the waypoints.

out = sim('scMobileRobotLogicalArchitecture.slx');
% waypoints are manually defined in Constant block
waypoints = eval(get_param('TrajectoryGenerator/Manual Waypoints','Value'));

figure
hold on
plot(out.pose.Data(:,1),out.pose.Data(:,2))
plot(waypoints(:,1),waypoints(:,2))
hold off
xlabel('X Position (m)')
ylabel('Y Position (m)')
legend('Actual Trajectory','Commanded Trajectory')

The simulation plot output with the trajectory of the mobile robot

See Also

Related Topics