Lab 2: Forward Kinematics

Part 0: Update Your Pupper (Do This First)

Before anything else, bring your Pupper’s software up to the current CS 123 build. The update script runs on the Pupper itself: it updates the Pupper code, installs the packages this lab’s 3D viewer needs, and checks that everything worked. The Pupper only needs to be powered on and on the internet, so start it before the hardware build.

  1. Open a terminal on the Pupper, over SSH or with the monitor setup from Lab 1.

    This room has no private network, so pupper[YOUR_GROUP_NUMBER].local won’t resolve. Connect both your laptop and the Pupper to the Stanford Visitor WiFi, find the Pupper’s IP address with ip a, and SSH by IP address with password rhea123. The < SSH into Pupper over WiFi Guide guide walks through it:

    ssh pi@<pupper_ip_address>
    

    Tip

    Typing the IP address and password every time gets old. Later, in Gates B08, you can set up passwordless SSH by running ssh-copy-id pi@pupper[YOUR_GROUP_NUMBER].local once from your laptop.

  2. Clone the update repository and run the script. Try the dry run first. It checks the Pupper and prints every command without changing anything:

    cd ~
    git clone https://github.com/cs123-stanford/pupper_rebase.git
    cd pupper_rebase
    ./rebase_local.sh --dry-run
    ./rebase_local.sh
    
  3. The script ends with a list of PASS/FAIL checks and then REBASE OK. If you see REBASE FAILED or ABORT, ask a TA before moving on.

Note

Near the end, the WiFi pairing tool installs and your SSH connection may drop for a moment. That’s expected: the update keeps running in the background. Reconnect and run tail -f ~/cs123-rebase.log to watch it finish.

Goal

Implement forward kinematics for all four legs of the Pupper robot using ROS2 and Python, and watch the computed foot positions follow the real legs in a 3D viewer in your browser.

Here’s what your implementation should look like when complete (all four legs, watched in the viser web viewer, see Part 5):

Fill out the lab document as you go. Make a copy and add your responses.

AI Use Suggestion

Coding agents likely won’t be helpful in this lab, and we suggest against using Claude Code or Codex for it. The forward kinematics you implement here is exactly the kind of content that will be tested on the closed-book, pen-and-paper quiz in Week 4. Even if you fluke your way through the lab by leaning on AI, you will have a bad time on the quiz. Work through the derivations and code yourself; AI tools are still fine for understanding the material better.

Part 1: Hardware Build

In Lab 1 you built the brain, the body, and one leg. In this lab you will build the full rest of the robot: the three remaining legs, following the same leg build you have already done once. Also put the lower leg back on the front-left knee in place of the spinning knob from Lab 1.

🤖 Open the Raise a Robot build videos in new tab 🤖

Note

The guide is password protected. The password is posted on Ed.

Begin by checking that your kit contains all the pieces; if not, please ask a TA. All the pieces for each leg are labeled — each right leg piece has an R on it, and each left leg piece has an L on it.

📝 Optional: prefer slides? Click to show the slide deck instead

These slides cover the same leg build. They are optional — the videos above are the primary instructions.

📝 Open build instructions in new tab 📝

Part 2: Setup

  1. Make sure you have completed Lab 1 and are familiar with the ROS2 environment on your Raspberry Pi 5.

  2. Open the forward kinematics lab code repository (https://github.com/cs123-stanford/forward_kinematics_lab) on your GitHub account. Then, fork the repository to your own GitHub account following the instructions in 🍴 Forking Repositories Guide.

  3. Open the forward kinematics lab folder in VSCode

    cd ~/forward_kinematics_lab
    code .
    

Part 3: Understanding the Code Structure

Before we start implementing the TODOs, let’s understand the structure of the forward_kinematics.py file:

  1. The code defines a ForwardKinematics class that inherits from rclpy.node.Node.

  2. It subscribes to the joint_states topic and publishes one end-effector position topic per leg (leg_front_l_end_effector_position, leg_front_r_end_effector_position, …) plus one colored sphere per leg on the marker topic.

  3. The rotation_x, rotation_y, rotation_z and translation helpers build the homogeneous transforms, and the four methods fk_front_left, fk_front_right, fk_back_left and fk_back_right are where we’ll implement the forward kinematics of each leg.

  4. A leg whose method still returns None is simply skipped, so you can implement and check the legs one at a time.

  5. The code uses NumPy for matrix operations.

  6. Note that it is convention to orient the coordinate frame so that the rotation about each motor is the z axis.

Part 4: Implementing Forward Kinematics

For the following steps, you can view the Pupper CAD to help you understand the kinematic chain CAD

Step 1: Implement Rotation Matrices

  1. Open forward_kinematics.py and locate the rotation_y, rotation_z and translation methods (rotation_x is done for you).

  2. Implement the rotation matrices about the x, y, and z axes. Follow the homogeneous coordinates representation as presented in lecture.

DELIVERABLE: Which axis is typically used as the default axis for rotations in robotic systems? What angles are we rotating along the default axis? Why?

Step 2: Implement Transformation Matrices (Front-Left Leg)

Start with the front-left leg in fk_front_left.

Note

In the following steps, \(\theta\) (theta) represents the motor angle. Figuring out the sign of \(\theta\) will be trickier than you might expect!

  1. The transformation matrix from the base link to leg_front_l_1 has been implemented for you in T_0_1. This involves a translation and two rotations. We include a visualization of this transformation below to facilitate your understanding (keeping all these in mind can be tricky!). Understanding this transformation will help you complete the remainder of the transformations.

    ../../../_images/base_l1_kinematics.png

    Transformation from base link to leg_front_l_1

DELIVERABLE: Explain the reasoning behind this implementation. What does the translation and each of the rotations do in T_0_1?

  1. Implement the transformation matrix from leg_front_l_1 to leg_front_l_2 in T_1_2. Follow the same thought process as with T_0_1. Check out the figure below for visual reference.

    ../../../_images/l1_l2_kinematics.png

    Transformation from leg_front_l_1 to leg_front_l_2

  2. Implement the transformation matrix from leg_front_l_2 to leg_front_l_3 in T_2_3. Check out the figure below for visual reference.

    ../../../_images/l2_l3_kinematics.png

    Transformation from leg_front_l_2 to leg_front_l_3

  3. Implement the transformation matrix from leg_front_l_3 to the end effector in T_3_ee. Check out the figure below for visual reference.

    ../../../_images/l3_ee_kinematics.png

    Transformation from leg_front_l_3 to the end effector

  4. Compute the final transformation matrix following the described process from lecture in T_0_ee. Remember that the end effector position is not in homogeneous coordinates. Calculate end_effector_position from T_0_ee.

We recommend jumping to Part 5 now to check the front-left leg in the viewer before moving on to the other three legs.

Step 3: Extend to the Other Three Legs

Now implement fk_front_right, fk_back_left and fk_back_right. Each leg is the same chain as the front-left leg (T_0_1, T_1_2, T_2_3, T_3_ee, then T_0_ee), with two differences:

  • T_0_1 starts from a different hip position on the body (we provide the numbers you need as comments in the code).

  • The right legs are mirror images of the left legs, and mirroring changes the sign of some of the terms.

We provide the base link to leg_back_r_1 transformation in the diagram below. The rest of the back-right chain follows the same pattern as the front leg:

../../../_images/base_back_kinematics.png

Base to back right leg transformation diagram

For the front-right and back-left legs, no diagram is given on purpose: work them out from the front-left and back-right diagrams and the CAD. The viewer in Part 5 tells you immediately whether a leg is right: move one joint at a time by hand and check that the sphere stays on the foot. If you are truly stuck after trying, ask a TA.

DELIVERABLE: For each of the three additional legs, state which terms change compared to the front-left leg and why.

Part 5: Checking Your Implementation in the Viewer

  1. Save your changes to forward_kinematics.py.

  2. Run the ROS2 nodes. This also starts the 3D web viewer (viser) on the Pupper:

    ros2 launch forward_kinematics.launch.py
    
  3. In another terminal, use the following command to run the main code:

    python forward_kinematics.py
    
  4. Open the viewer in your browser. Both commands above print the exact link when they start. If your laptop is on the same Wi-Fi as the Pupper, browse to http://<pupper-ip>:8080 (several people can have it open at once). If you can only reach the Pupper over SSH, forward the port from your laptop first and browse to http://localhost:8080:

    ssh -N -L 8080:localhost:8080 pi@pupper[YOUR_GROUP_NUMBER].local
    

    If you have trouble reaching the Pupper, see < SSH into Pupper over WiFi Guide.

  5. The viewer shows the Pupper model following the real joint angles, plus one sphere per leg at the position your forward kinematics computed: front-left green, front-right red, back-left blue, back-right yellow. The side panel lists the joint angles and end-effector positions. A leg whose method still returns None has no sphere yet.

  6. Move each leg of your robot by hand (the motors are held limp for you) and check that its sphere stays on the foot. If a sphere drifts off the foot when you move one particular joint, that joint’s transform (or the sign of its angle) is the one to fix.

Note

No Wi-Fi connection? If your laptop cannot reach the Pupper’s web page (weak or no Wi-Fi in your area, or the ssh -L route above fails), fall back to RViz2, which draws the same robot model and the same four markers:

rviz2 -d forward_kinematics.rviz

The above command will load the RViz config file. If you just run rviz2, you can manually add the configuration: click “Add”, select a Robot Model type and the /robot_description topic; then “Add” again, select a Marker type and the /marker topic. RViz2 needs a display (the monitor setup from Lab 1 or X forwarding) and may fail on your robot due to heating in the Raspberry Pi. If this happens, reach out to a TA to check the implementation first, then turn off Pupper, wait a while to let it cool down, and try again.

DELIVERABLE:

  1. Take a video of the working implementation with you moving each of Pupper’s four legs and the viewer mimicking the results, and upload it to the Google Drive

  2. Write out the full equation you used to calculate the forward kinematics (in math). Please use LaTeX and take a screenshot, or use the equation functionality in Google Docs. What is the benefit of using homogeneous transformations?

  3. Why is there a 1 in the bottom-right corner of a homogeneous transformation matrix?

Part 6: Analyzing the Results

  1. Record the end-effector positions for the left front leg configurations (the side panel of the viewer shows them, or run ros2 topic echo /leg_front_l_end_effector_position).

  2. Compare these positions with the expected positions based on the physical dimensions of your robot. (Why are the numbers printed in the terminal so small?)

  3. If there are discrepancies, try to identify the source of the errors. It could be due to:

    • Incorrect transformation matrices

    • Inaccurate joint angle readings

    • Errors in the physical measurements of the robot

DELIVERABLE:

  1. Measuring the correct physical parameters of the robot (leg lengths, motor angles, etc.) is essential to compute accurate kinematics. This process is called system identification. How would your estimate of the end effector (EEF) position change if your estimate of leg link 2 (r2) is off by 0.2 cm short from the actual distance to leg link 1 (r1)? What about 0.4 cm, or 0.8 cm? Write out the numbers you computed, and how you calculated them, for both 0 degrees rotation in each of the joints, and 45 degrees rotation in each of the joints. Qualitatively, how does error in estimated EEF position change with respect to error in leg length?

  2. How does computational complexity of FK scale with respect to degree of freedom (number of motor angles)? Please use big O notation.

Additional Challenges (Optional)

If you finish early and want to explore further:

  1. While testing in the viewer, write a script that saves the sequence of your well-crafted motion, recorded as end effector positions into a file. You will have a chance to let Pupper replay this recorded motion in the next lab! You will need to use the joint_states topic to record the motor angles, and the leg_<leg>_end_effector_position topics to record the end effector positions.

  2. Keep your four-leg forward kinematics handy: lab 3 builds on it to make Pupper walk.

Friendly reminder: The first optional lab will be released next week, attempt at your own risk!

Remember, understanding forward kinematics is crucial for robot control and motion planning. Take your time to ensure you understand each step of the process!