# Drake Simulation

This page illustrates how the Drake Simulation works. We will introduce 3 components that drake uses. They are **Simulator**, **SceneGraph**, and **Diagram**.

### Diagram

To explain what a `diagram` is, we could look at how Matlab Simulink works. Matlab Simulink is a simulation tool. Simulink's main graph is composed of connected systems. A complex system can host multiple subsystems. All these subsystems connect in some way to form the entire system.

`diagram` is the main graph of Drake. `diagram` is composed of systems like `MultibodyPlant`, controllers and other useful blocks. Like Simulink, the `diagram` determines how the system is constructed, what each block is, and how they are connected. Drake has a`DiagramBuilder` class to help glue the system blocks together. It adds system blocks into the `diagram` and connects input and output ports of blocks together.

*Thinking: what are the information and data format that's being transmitted between the ports?*

For a robotic system, there is a special system block that represents  all the robots in `diagram`. This system is called `MultibodyPlant`. The `MultibodyPlant` is a huge class containing all the parameters and data related to the robots.

{% content-ref url="drake-multibody" %}
[drake-multibody](https://drake.guzhaoyuan.com/introduction/drake-multibody)
{% endcontent-ref %}

### SceneGraph

`SceneGraph` is the visualization and collision checking tool.&#x20;

#### Collision Checking

Before simulation, `SceneGraph` is initiated and connected to `MultibodyPlant`.&#x20;

During simulation, `SceneGraph` would give the information of whether two objects collide and what is the distance between two objects, given the state input from `MultibodyPlant`. Then the `MultibodyPlant` decides whether the collision is a soft contact or a fierce crash, how much force is generated in between objects given the collision information.

#### Visualization

To visualize the robot, `MultibodyPlant` should be registered to the `SceneGraph`. The `SceneGraph` would then send rendering message to another process called `drake_visualizer` using LCM.

`drake_visualizer` would handle the rendering job. It would draw the robot, frames, arrows per request.

### Simulator

The `Simulator` takes the whole system `diagram` and runs the simulation. Using the robot dynamics equation of motion and environment forces, the `Simulator` computes the state change. It then runs numerical integration for continuous system or state update for discrete system, to calculate the next system state, and write the states back to the diagram's corresponding `context`. It keeps updating the states until the simulation finishes.

## Steps: from URDF to a moving robot

1. Import URDF or SDF file to create the robot `MultibodyPlant` in `diagram`.
2. Connect the `MultibodyPlant` input with torque input block, which could be controller block or signal source block.
3. Register the robot into `SceneGraph` for visualization, use `builder` to connect the `SceneGraph` and `MultibodyPlant` for collision checking.
4. Create `Simulator` to simulate the `diagram`.
5. Compile and run. Open `drake_visualizer` to see the result.

A complete example of this process could be found below.

{% content-ref url="../drake-controllers/try-out-pid-controller" %}
[try-out-pid-controller](https://drake.guzhaoyuan.com/drake-controllers/try-out-pid-controller)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://drake.guzhaoyuan.com/introduction/drake-simulation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
