CodeTheRobot

ROS 2 Lyrical Luth · Lesson 36 of 86

Kinematic chains

Links and joints as a chain, degrees of freedom, the base and the tool frame.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain kinematic chains 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

A kinematic chain is a series of rigid links joined by joints. The joint values (angles for revolute joints, distances for prismatic ones) fully determine where every link is. The number of independent joint values is the degrees of freedom (DOF); a planar arm with two revolute joints has 2 DOF.

Describe a chain in code as an ordered list of joints, each with a link length and a current value. The frame at the fixed end is the base; the frame at the free end is the tool (end effector). Everything in kinematics asks: given the joints, where is the tool, or the reverse.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the kinematic chains 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

struct Joint {
// One joint plus the link that follows it.
  double length;
// Length of the link after the joint, in metres.
  double angle;
// Current joint angle in radians.
};
// End of the struct.
std::vector<Joint> arm{{1.0, 0.0}, {1.0, 0.0}};
// A planar arm with two 1 m links (2 DOF), both at angle 0.
std::size_t dof = arm.size();
// For a serial chain of single-axis joints, DOF equals the number of joints.
double reach = 0.0;
// Sum of link lengths.
for (const auto& j : arm) reach += j.length;
// Fully stretched, the tool is this far from the base: the maximum reach.

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 Kinematic chains. Expected result: dof=2 reach=2

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 <iostream>
#include <vector>

struct Joint {
  double length;
  double angle;
};

int main() {
  std::vector<Joint> arm{{1.0, 0.0}, {1.0, 0.0}};
  // TODO: compute dof (number of joints) and reach (sum of the link lengths)
  std::cout << "dof=" << dof << " reach=" << reach << '\n';
  return 0;
}

Expected output

dof=2 reach=2
Stuck? Show the full solution (try it yourself first)
#include <iostream>
#include <vector>

struct Joint {
  double length;
  double angle;
};

int main() {
  std::vector<Joint> arm{{1.0, 0.0}, {1.0, 0.0}};
  const auto dof = arm.size();
  double reach = 0.0;
  for (const auto& j : arm) reach += j.length;
  std::cout << "dof=" << dof << " reach=" << reach << '\n';
  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

Describe a 3-link planar arm as a chain of joints, compute its maximum reach and minimum reach, and sketch which points on a plane the tool can and cannot reach.

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.