PID control of double pendulum

We use a double pendulum URDF as model. The task of the PID controller is to do state feedback control and keep the double pendulum at the top. To get a sense of how the model look like, try the command:

bazel run //examples/double_pendulum:double_pendulum_demo

The code in the tutorial is also available on my github, so you could also checkout my code directly if you get stuck.

In this tutorial, we will go through:

  1. Create workspace and compile.

  2. Import the double pendulum URDF as a MultibodyPlant.

  3. Create a PID controller, and connect it to MultibodyPlant.

  4. Simulate and visualize.

Run the demo

Create workspace

Create workspace and files.

cd drake
mkdir -p examples/double_pendulum_pid

Add BUILD.bazel

To make the work space compile, we add the following to BUILD.bazel

BUILD.bazel
load(
    "@drake//tools/skylark:drake_cc.bzl",
    "drake_cc_binary",
)

load("//tools/install:install_data.bzl", "install_data")

drake_cc_binary(
    name = "run_double_pendulum_pid_exe",
    srcs = [
        "run_double_pendulum_pid.cc",
    ],
    data = [
        ":models",
        "//tools:drake_visualizer",
    ],
    deps = [
        "//common:essential",
        "//common:find_resource",
        "//common:text_logging_gflags",
        "//geometry:geometry_visualization",
        "//lcm",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/analysis",
        "//systems/controllers",
        "//systems/framework",
        "@sdformat",
    ],
)

install_data()

Add source file

And add the run_double_pendulum_pid.cc file

Simulate and visualize

The Code Explained

Try with your own model

You could bring your own simple URDF/SDF (with 1 or 2 degree of freedom) and try doing a position control with the PID controller.

Last updated

Was this helpful?