Drake Tutorial
  • Drake Tutorial
  • To get started
  • Introduction
    • Drake Concepts
    • Drake Multibody
    • Drake Simulation
    • Drake Notation
    • Bazel basics
  • Things to do in drake
    • Hello, Drake!
    • Create a URDF/SDF robot
    • Visualize data in LCM
    • Visualize trajectory
    • [WIP] Visualize data
    • [WIP] Control Real KUKA arm
  • Drake Controllers
    • PID control of double pendulum
    • LQR on Cart Pole
    • Inverse Dynamics Control on fully-actuated KUKA arm
    • [WIP] Control a underactuated robot using Inverse Dynamics Controller
  • Drake Optimization
    • [WIP] Trajectory Optimization 1 - Direct Transcription
    • [WIP] Trajectory Optimization 2 - Direct Collocation
    • [WIP] Trajectory Optimization 3 - Shooting method
    • [WIP] Optimal Control 1 - Quadratic Programming
    • [WIP] Optimal Control 2 - Sequential Quadratic Programming
  • Q & A
    • FAQ
    • Use Drake as part of your project
  • Deprecated
    • Create a simple system
Powered by GitBook
On this page
  • Quick access to the tutorial demos
  • Create workspace
  • Add files to the workspace
  • Compile and execute
  • The Code Explained

Was this helpful?

  1. Things to do in drake

Hello, Drake!

PreviousBazel basicsNextCreate a URDF/SDF robot

Last updated 5 years ago

Was this helpful?

From now on, get your hands dirty. Follow the steps to start building your own code.

Quick access to the tutorial demos

If you just want to see the result really quick, you could always skip the tutorial and use code on . Here's how.

cd drake
git remote add gzy https://github.com/guzhaoyuan/drake.git
git pull gzy tutorial
git checkout tutorial
bazel run //examples/hello:hello_exe

Same process applies to all the other examples.

Create workspace

To run a simple example, we need a workspace that hosts C++ source files and BUILD.bazel file.

cd drake
mkdir -p examples/hello

Add files to the workspace

Create files named BUILD.bazel and hello.cc under examples/hello.

The code is .

Add the following contents to BUILD.bazel.

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

drake_cc_binary(
    name = "hello_exe",
    srcs = ["hello.cc"],
    data = [
    ],
    deps = [
        "//common:text_logging_gflags",
        "@gflags",
    ],
)

Add the following contents to hello.cc.

hello.cc
/// @file
///
/// This example shows how to compile and run drake files

#include <gflags/gflags.h>

#include "drake/common/text_logging_gflags.h"

namespace drake {
namespace examples {
namespace hello {

DEFINE_string(your_name, "Zion",
              "Putting your name here so Drake recognize you.");

void DoMain() {
  drake::log()->info("Hello " + FLAGS_your_name + " from Drake!");
}

}  // namespace hello
}  // namespace examples
}  // namespace drake

int main(int argc, char* argv[]) {
  gflags::SetUsageMessage(
      "A simple hello Drake example.");
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  drake::examples::hello::DoMain();
  return 0;
}

Compile and execute

Use the following command to build and run the code.

bazel run //examples/hello:hello_exe
[2019-06-14 17:23:39.190] [console] [info] Hello Zion from Drake!

The BUILD.bazel tells bazel what and how to compile. So when we run the code, bazel will find and build the code under that folder.

Now let's try add argument to the executable. Try the following and see what you get.

bazel run //examples/hello:hello_exe -- --your_name=Russ
[2019-06-14 17:28:21.056] [console] [info] Hello Russ from Drake!

Hooray, you get your first Drake program running! Now you are ready to digest some real meat.

The Code Explained

BUILD.bazel

bazel will search the whole directory recursively to find BUILD.bazel. BUILD.bazel specifies the sources and headers that should be built, and tells bazel the target libraries and executables that should be generated. We need a BUILD.bazel under the work space folder.

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

Load pre-defined bazel rule. We need to build Drake binary, so we load drake_cc_binary at line 3.

BUILD.bazel
drake_cc_binary(
    name = "hello_exe",
    srcs = ["hello.cc"],
    data = [
    ],
    deps = [
        "//common:text_logging_gflags",
        "@gflags",
    ],
)

Use drake_cc_binary to define how the binary is going to be built. drake_cc_binary specifies: 1. target executable name; 2. the files to be build; 3. all the required data (for example the robot mesh files); 4. dependencies.

Use //common:text_logging_gflags and @gflags as dependency. //common:text_logging_gflags is a information logging tool. gflags allows us to specify variables at run-time by passing arguments in command line, makes it easy to tune parameters without recompile.

hello.cc

hello.cc
#include <gflags/gflags.h>

#include "drake/common/text_logging_gflags.h"

Include gflags header file. gflags parses the command line parameters at run-time.

Include drake logging header file which contains function drake::log().

hello.cc
DEFINE_string(your_name, "Zion",
              "Putting your name here so Drake recognize you.");

Define a variable FLAGS_your_name with default value and explanation text. We could change the value before executing the binary by adding -- --your_name=Russ.

hello.cc
void DoMain() {
  drake::log()->info("Hello " + FLAGS_your_name + " from Drake!");
}

Define the DoMain function, which serves as our main process. This program plots a string to console.

hello.cc
int main(int argc, char* argv[]) {
  gflags::SetUsageMessage(
      "A simple hello Drake example.");
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  drake::examples::hello::DoMain();
  return 0;
}

Entrance to the program. The program starts by parsing the argument specified by user. And then it executes the DoMain() function which plot the string with your name!

my github branch
available here