Racing Lounge

Best practices and teamwork for student competitions

Robot Modeling and Simulation: A Walking Robot Case Study 14

Posted by Sebastian Castro,

Today, my robotics colleague Sebastian Castro is back for another guest blog.

Before handing it over to Sebastian allow me a short note. My colleague Steve Eddins recently started a second blog on MATLAB Central, this one about Deep Learning. If you are interested in learning more about Deep Learning please visit his Deep Learning Blog and Subscribe. Enjoy!

– – –

In this post, I will discuss robot modeling and simulation with Simulink®, Simscape™, and Simscape Multibody™. To put things in context, I will walk you through a walking robot example (get it?).


First of all… why simulate? I’ve broken down the benefits into two categories.

  • Safety: Robots will fall. Prototypes will break. You can verify that controls algorithms are at a good starting point in simulation before moving to hardware. Simulation lets you test your robot and controller design under multiple scenarios without building prototypes. In simulation, you also get the benefit of intentionally generating unsafe conditions, as well as discovering unexpected issues.
  • Efficiency: Physical experiments take time and effort to set up and reset between runs. With simulation, you get a programmatic environment to automate experiments and walk away from your desk. If your robot is controlled by an embedded system, simulation lets you test algorithm changes without having to port and rebuild the code on hardware every time. This separation of algorithm and implementation can also help you determine whether new issues are due to algorithm changes or physical limitations.

Robot Simulation Components

We will now look at a typical robot simulation architecture, which consists of multiple layers. Depending on your goals, you may only need to implement a subset of these for your simulation.

Robot Mechanics

Simscape Multibody lets you model the 3D rigid body mechanics of your robot. There are two ways to do this.

  • Build from scratch: It may take some initial time to build a model from scratch. However, if set up correctly, you can easily change properties such as dimensions, cross-sections, masses, etc. If you are still in the conceptual design phase, this can be useful as you sweep through different parameters and validate your design.
  • Import from CAD: Useful if you have already created a robot model and want to simulate its dynamics using more realistic geometric and inertial properties. So long as the kinematics of the CAD model remain the same, you can make changes in CAD and reimport the parameters into your model. For more information, look at our blog post on importing CAD assemblies.


Regardless of how you create the robot model, the next step is to add dynamics to it.

  • Internal mechanics: Every Joint block (translational or rotational) in the model can be assigned mechanical stiffness, damping, and initial conditions.
  • External mechanics: First, you can set up the direction and magnitude of gravity. Equally important for legged robots, you need to model contact with the ground. We discuss this in more detail in our contact modeling blog post.

For more information on modeling the 3D rigid body mechanics of a walking robot, check out the video below.

[Video] MATLAB and Simulink Robotics Arena: Walking Robots, Part 1: Modeling and Simulation

Actuator Dynamics and Control

As shown in the simulation architecture diagram earlier, the actuator is the “glue” between the algorithm and the model (or robot). Actuator modeling consists of two parts: one on the controller side, and one on the robot side.

  • Actuator control: By prescribing motion to an actuator model in Simscape, you can first perform actuator sizing. This lets you determine the power (for example, current, torque, etc. for electric actuators) needed for your actuator to perform as desired. Once you have an actuator model, you can use Simulink to design a controller and test it in simulation before deploying it.

  • Actuator dynamics: You can use Simscape to build more detailed actuator models. This allows you to connect the 3D mechanical motion of the robot to other physical domains – for example, the electrical domain for motors or the fluid domain for piston actuators.

Different design tasks may need different model detail. Depending on this, simulation speed could range from much faster than real-time to much slower than real-time, and this is an important tradeoff. Let’s take the following example. Suppose you’re designing a robot which has both a high-level motion planning algorithm and a low-level electronic motor controller with high-frequency pulse-width modulation (PWM).

  • A motion planning mission may require minutes of simulation, whereas a motor control response may be in the order of milliseconds.
  • To test the motion planner, you can assume that the low-level actuators just work; for electronics design, you may need to dig all the way into the actuator current transients to make sure individual components will not fail.

Ideally, you’d like to have reusable and configurable model components for different scale simulations. Simulink facilitates this with modeling features such as variants, block libraries, and model referencing. To see how this was done with the walking robot actuator models, watch the video below.

[Video] MATLAB and Simulink Robotics Arena: Walking Robots, Part 2: Actuation and Control

Motion Planning

Motion planning can be an open-loop or closed-loop activity.

Optimization tools are useful in many aspects of robot modeling and simulation, such as

  • Robot design: Determining optimal geometry (size, position, cross-section, etc.) or dynamics (mass/stiffness/damping, or equivalents in electrical or fluid actuators). For example, see Estimating Parameters of a DC Motor.
  • Control design: Tuning control gains, thresholds, rate limits, etc. to meet performance and safety requirements. For example, see Optimizing System Performance: DC Motor.
  • Motion planning: Finding a sequence of motion inputs that satisfy overall planning goals. This approach is shown in the animation and video below, in which a genetic algorithm is used to optimize the robot walking trajectory.

Designing an open-loop motion profile through optimization can be a good start, but this may not be robust to variations in the physical parameters, terrain, or other external disturbances. In theory, you could use optimization and simulation to test against scenarios that cover all the challenges you expect in the real world. In practice, a closed-loop system — or a system that can react to the environment — is better suited to handle these challenges.

Closed-loop motion controllers require information about the environment through sensors. Common sensors for legged robots include joint position/velocity sensors, accelerometers/gyros, force/pressure sensors, and cameras. An overall control policy can then be determined using model-based methods like Internal Model Control, or with machine learning techniques like reinforcement learning.

The video below shows how you can repeatedly simulate a model and collect results to optimize open-loop trajectories for a walking robot. Running simulations in batch can similarly help you perform tasks such as tuning closed-loop controller parameters or training algorithms using machine learning.

[Video] MATLAB and Simulink Robotics Arena: Walking Robots, Part 3: Trajectory Optimization


You have now seen how simulation can help you design and control a legged robot.

For more information, make sure you watch the walking robot videos above and download the example files from the File Exchange, as well as an Onshape model you can import into Simscape Multibody. You can also find a four-legged Running Robot example on the File Exchange.

Are you working with legged robots? We’d be interested in hearing from you.

– Sebastian

14 CommentsOldest to Newest

Myrobostation replied on : 1 of 14
In theory, you can use Optimization and Simulation to test the scenes that cover all the challenges that you expect in the real world.
Guillermo replied on : 2 of 14
Definitely one of the best tutorial of this kind of robots. Thank you so much for all the information! I have been looking for something like this for a long time. Keep up with the great work!
scastro replied on : 3 of 14
@Myrobostation : Exactly, *in theory*. The main argument is that closed-loop behavior tends to be more robust to unexpected disturbances vs. a finely tuned open-loop gait. @Guillermo : Thank you for your kind comments. Please feel free to reach out to us with more ideas for content, or whenever you produce some cool simulations!
Ning Hu replied on : 4 of 14
In "[pFinal,reward] = ga(costFcn,numPoints*3,[],[],[],[], ... lowerBnd,upperBnd,[],1:numPoints*3,opts)" this command from optimizerobotMotion.m file. I wanna know what the pFinal means ? And what the relationship between joints' motion and costfun? @scastro Thank you .
James replied on : 6 of 14
where can I get the robot parameter file you are constantly using the variable from it any other source to learn Matlab and Simulink from scratch for robotics !!
saba suhail replied on : 8 of 14
Hi, can you offer me the theoretical part as pdf report, especially the mathematical realization and algorithm part. i will appreciate that. thank you
@Saba Suhail -- the mathematical model of the walking robot itself is not analytical, since it is modeled using Simscape tools. The equations are automatically derived based on the components and how they are connected. For the trajectory generation, we used these slides as reference -- specifically, the slides on Page 2: For the optimization algorithm, the references in the MATLAB "ga" function can be found here:
Qi Xu replied on : 11 of 14
In the first part ot modeling,the contact between the robot foot and the plane is modeled with 4 points(4 tiny spheres actually).Is it proper enough to imitate the real-world case?As far i know ,the friction between feet and ground is vital to walking.Does it mean we need a more detailed modeling?
Hi @Qi Xu -- you're right that using 4 points is an approximation. Since the foot is perfectly flat and rectangular, 4 points are actually sufficient as they represent the convex hull of the actual foot. As your foot has more complex shapes, curves, etc. then I would recommend implementing your own contact laws, since using a large number of points is not very scalable.
Shiny Shajahan replied on : 13 of 14
Hi, Can you offer me the design part of the PID controller and the reference paper you choosen for the motion inputs to the joints
@Shiny: If you download the files (, you will find all the parameters for the PID controllers... which work specifically for this model, so you are pretty much guaranteed to have to redesign things for your work. The reference for the cubic trajectories is a presentation also listed in the files, but for convenience the link is: