Running Drake code from Julia's REPL

Twan Koolen edited this page Sep 21, 2016 · 3 revisions

Install Julia

Get Julia 0.5 from http://julialang.org/downloads/ and install it.

Install Cxx.jl

Start the Julia REPL by running the julia program in the bin folder of your Julia distribution from the command line. You may want to add that bin folder to your PATH (add export PATH="$PATH:/wherever/julia/is/bin" to your .bashrc).

Run:

Pkg.init()
Pkg.add("Cxx")

This will get the latest released version of the Cxx.jl package and build it. It may take a couple of minutes.

Create a file that sets up the Cxx.jl environment

Create a file called drake.jl with the following contents:

using Cxx
const drake_install_dir = "/Users/twan/code/drake-distro/build/install" # adapt this to your setup
addHeaderDir(drake_install_dir * "/include/eigen3", kind = C_System)
addHeaderDir(drake_install_dir * "/include", kind = C_System)
Libdl.dlopen(drake_install_dir * "/lib/libdrakeRBM.dylib")
cxxinclude("drake/systems/plants/RigidBodyTree.h")

Run Drake code from the Julia REPL

Run julia from the command line in the directory that contains drake.jl. Enter:

include("drake.jl")

Now type "<" in the REPL to enter C++ mode. You can then try the following examples.

Example 1: creating a RigidBodyTree and transforming a point to world frame

Enter the following code line by line (making sure that the REPL is in C++ mode):

RigidBodyTree tree("/Users/twan/code/drake-distro/drake/examples/Atlas/urdf/atlas_minimal_contact.urdf", drake::systems::plants::joints::kQuaternion); // adapt to your setup
auto q = tree.getZeroConfiguration();
auto cache = tree.doKinematics(q);
Eigen::Vector3d p;
p.setRandom();
std::cout << p << std::endl;
auto p_world = tree.transformPoints(cache, p, 5, 0);
std::cout << p_world << std::endl;

Example 2: computing the Jacobian of a function

Enter the following code line by line (making sure that the REPL is in C++ mode):

auto f = [](const auto& x) { using Scalar = typename std::remove_reference<decltype(x)>::type::Scalar; return Eigen::Matrix<Scalar, 1, 1>(x.dot(x)).eval(); };
auto x = Eigen::VectorXd::Random(13).eval();
std::cout << f(x) << std::endl;
#include "drake/math/autodiff.h"
#include "drake/math/autodiff_gradient.h"
using namespace drake::math;
auto x_autodiff = jacobian(f, x);
auto dx = autoDiffToGradientMatrix(x_autodiff);
std::cout << dx << std::endl;

Following steps

You can learn a bit more about Cxx.jl (e.g., their non-REPL interface to C++ code) from https://github.com/Keno/Cxx.jl.