0
0
SimulinkHow-ToBeginner · 4 min read

How to Simulate Robot in Simulink: Step-by-Step Guide

To simulate a robot in Simulink, use the Robotics System Toolbox to model the robot's kinematics and dynamics, then build a Simulink model with blocks like Rigid Body Tree and Robot Simulator. Run the simulation to visualize robot motion and test control algorithms.
📐

Syntax

Simulating a robot in Simulink involves these main steps:

  • Load or define robot model: Use importrobot or create a rigidBodyTree object.
  • Set up Simulink model: Use blocks like Robot Simulator or Rigid Body Tree to represent the robot.
  • Define inputs: Provide joint commands or trajectories as inputs.
  • Run simulation: Use sim command or Simulink interface to start.
matlab
robot = importrobot('exampleRobot.urdf');
robot.DataFormat = 'row';
robot.Gravity = [0 0 -9.81];

simModel = 'robot_simulation_model';
open_system(simModel);
sim(simModel);
💻

Example

This example shows how to simulate a simple 2-joint robot arm in Simulink using Robotics System Toolbox.

It loads a robot model, sets up joint angle inputs, and runs the simulation to visualize the robot motion.

matlab
% Load robot model
robot = importrobot('twoLinkRobot.urdf');
robot.DataFormat = 'row';
robot.Gravity = [0 0 -9.81];

% Create joint trajectory (sinusoidal motion)
time = linspace(0,10,100)';
joint1 = 0.5*sin(time);
joint2 = 0.5*cos(time);

% Prepare input timeseries for Simulink
jointAngles = timeseries([joint1 joint2], time);

% Open Simulink model
open_system('twoLinkRobotSimulink');

% Set input in base workspace
assignin('base','jointAngles',jointAngles);

% Run simulation
simOut = sim('twoLinkRobotSimulink');
⚠️

Common Pitfalls

  • Not setting DataFormat: Forgetting to set robot.DataFormat to 'row' or 'column' causes input dimension errors.
  • Missing gravity vector: Not defining gravity leads to unrealistic dynamics.
  • Incorrect input format: Joint inputs must be timeseries or proper arrays matching robot joints.
  • Not linking robot model in Simulink: The Simulink blocks must reference the correct rigidBodyTree object.
matlab
% Wrong: Missing DataFormat
robot = importrobot('twoLinkRobot.urdf');
% This causes errors when simulating

% Correct:
robot.DataFormat = 'row';
📊

Quick Reference

Tips for smooth robot simulation in Simulink:

  • Always use importrobot to load URDF models.
  • Set DataFormat to 'row' for compatibility.
  • Use timeseries objects for joint trajectories.
  • Use Robot Simulator block for visualization.
  • Check gravity vector to simulate realistic physics.

Key Takeaways

Use Robotics System Toolbox to import and model robots in Simulink.
Set robot.DataFormat to 'row' to avoid input dimension errors.
Provide joint commands as timeseries inputs for smooth simulation.
Use Robot Simulator block to visualize robot motion in Simulink.
Always define gravity vector for realistic robot dynamics.