Table of Contents

Lab 6: Inverse Kinematics and Trajectory Tracking

Goal

We want to build upon last time's lab, but this time instead of finding out what is the final position as a function of motor values, this time we will be making (or trying at least) a function that takes as input the position and outputs the motor values. We will also play with this function to make the pupper's leg to move in a triangular shape

Part 0: Setup

cd ~/lab_3_fall_2025
code .

For docker users, you may try to simulate with this setup (If you already did this for lab 2, it is the same archive, no need to redownload or rebuild the image): Download this and then do these following commands:

tar -xvf pupper_viz.tar.gz
cd pupper_viz/
docker build -t pupper_viz .

Once done, you may paste your lab 3 solution into the lab_3.py and then

./run.sh 3

Part 1: Forward Kinematics on Right Front Leg

  1. Open lab_3.py and locate the forward_kinematics method in the InverseKinematics class
  2. In this lab, we will instead use the right front leg of Pupper. Implement the forward_kinematics method for the right front leg. This should be very similar to your implementation of the front left leg from lab 2.

Part 2: Implement Inverse Kinematics

TODO 1: Implement the cost_function(theta) for inverse kinematics. This function returns cost, a scalar, and l1, a vector of size 3. Use the forward_kinematics method to get the current end-effector position. Calculate the L1 distance between the current and target end-effector positions. Return the sum of squared L1 distances as the cost (AKA the squared L2 norm of the error vector). TODO 2: Implement the gradient(theta, epsilon) function to calculate the numerical gradient for inverse kinematics. TODO 3: Implement the gradient descent algorithm for inverse kinematics.

What happens if the learning rate is too small… what if the learning rate gets too big? (Note: for Pupper’s safety, don’t change the learning rate in the code)

Part 3: Implement Trajectory Generation

TODO 4: Implement the interpolation for the triangular trajectory. You need to create a function that performs linear interpolation between the triangle’s vertices. The trajectory should loop smoothly from vertex 1 to 2, vertex 2 to 3, and then from vertex 3 back to vertex 1 based on the time variable. The input to the function is a time variable t that dictates where along the triangle’s edges the point currently lies for a given 3-second period. Each vertex transition (e.g., from vertex 1 to vertex 2) should last approximately 1 second. For example, 0 ⇐ t < 1 should interpolate between vertex 1 and vertex 2.

Part 4: Run and Test your implementation

ros2 launch lab_3.launch.py
python3 lab_3.py

Part 5: Analyze and Improve Performance