CodeTheRobot

ROS 2 Lyrical Luth · Lesson 39 of 86

DH parameters

The four Denavit–Hartenberg parameters, transform matrices and chaining them.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain dh parameters in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

The Denavit–Hartenberg (DH) convention describes each joint with four numbers: a (link length), alpha (link twist), d (link offset) and theta (joint angle). Each row gives one 4×4 homogeneous transform; multiplying the transforms in order gives the tool pose relative to the base.

DH is compact and used in many robot datasheets, but URDF does not use it: URDF gives an origin and axis per joint instead. Converting a DH table to a URDF (or checking a URDF against a DH table) is a common task. Always state which DH variant (standard or modified) the table uses.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the dh parameters example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

using Mat = std::array<double, 16>;
// A 4×4 matrix stored row by row.
Mat dh(double a, double alpha, double d, double theta) {
// The transform for one row of the DH table.
  double ct = std::cos(theta), st = std::sin(theta), ca = std::cos(alpha), sa = std::sin(alpha);
// Precompute the sines and cosines.
  return {ct, -st * ca, st * sa, a * ct,
// First row of the standard DH matrix.
          st, ct * ca, -ct * sa, a * st,
// Second row.
          0, sa, ca, d,
// Third row.
          0, 0, 0, 1};
// Last row of a homogeneous transform.
}
// End of dh().
Mat T = mul(dh(1, 0, 0, q1), dh(1, 0, 0, q2));
// Chain two joints by multiplying their matrices; T[3] and T[7] hold the tool x and y.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Robot arm

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

tip x110 y58ROBOT ARM

Demo for DH parameters. Expected result: x=1.000 y=1.000

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <array>
#include <cmath>
#include <cstdio>

using Mat = std::array<double, 16>;

Mat dh(double a, double alpha, double d, double theta) {
  const double ct = std::cos(theta), st = std::sin(theta), ca = std::cos(alpha), sa = std::sin(alpha);
  return {ct, -st * ca, st * sa, a * ct, st, ct * ca, -ct * sa, a * st, 0, sa, ca, d, 0, 0, 0, 1};
}

Mat mul(const Mat& A, const Mat& B) {
  Mat C{};
  // TODO: multiply two 4x4 row-major matrices: C[r*4+c] = sum over k of A[r*4+k] * B[k*4+c]
  return C;
}

int main() {
  const Mat T = mul(dh(1, 0, 0, M_PI / 2), dh(1, 0, 0, -M_PI / 2));
  std::printf("x=%.3f y=%.3f\n", T[3] + 0.0, T[7] + 0.0);
  return 0;
}

Expected output

x=1.000 y=1.000
Stuck? Show the full solution (try it yourself first)
#include <array>
#include <cmath>
#include <cstdio>

using Mat = std::array<double, 16>;

Mat dh(double a, double alpha, double d, double theta) {
  const double ct = std::cos(theta), st = std::sin(theta), ca = std::cos(alpha), sa = std::sin(alpha);
  return {ct, -st * ca, st * sa, a * ct, st, ct * ca, -ct * sa, a * st, 0, sa, ca, d, 0, 0, 0, 1};
}

Mat mul(const Mat& A, const Mat& B) {
  Mat C{};
  for (int r = 0; r < 4; ++r)
    for (int c = 0; c < 4; ++c)
      for (int k = 0; k < 4; ++k) C[r * 4 + c] += A[r * 4 + k] * B[k * 4 + c];
  return C;
}

int main() {
  const Mat T = mul(dh(1, 0, 0, M_PI / 2), dh(1, 0, 0, -M_PI / 2));
  std::printf("x=%.3f y=%.3f\n", T[3] + 0.0, T[7] + 0.0);
  return 0;
}

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Write the DH table for a 2-link planar arm (a = 1, alpha = 0, d = 0), multiply the two transforms, and compare the tool position with your forward kinematics result.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.