This is an old revision of the document!
Lab 5: Forward Kinematics
Part 1: Intro
In order to be able to control the pupper, the first thing we would need is to get the position of the legs. What we are given through the power of ROS magic as an input is the angles of each joint, and by knowing the topology of the robot we can figure out the end position of each leg through the power of linear algebra.
Recall from computer graphics that points in space are represented with vectors, and that any transformations can be represented with a matrix
Part 2: Setup
cd ~/lab_2_fall_2025
code .
Step 3. Understanding the Code Structure
Before we start implementing the TODOs, let's understand the structure of the lab_2.py file:
The code defines a ForwardKinematics class that inherits from rclpy.node.Node
It subscribes to the joint_states topic and publishes to the leg_front_1_end_effector_position and marker topics.
The forward_kinematics method is where we'll implement the forward kinematics calculations
The code uses NumPy for matrix operations.
Note that it is the convention to orient the coordinate frame so that the rotation about each motor is the z axis.
Step 4: Implementing Forward Kinematics
Step 1: Implement Rotation Matrices
Open lab_2.py and locate the forward_kinematics method.
Implement the rotation matrices about the x, y, and z axes.
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
In the following steps, (theta) represents the motor angle. Figuring out the sign of will be trickier than you might expect!
Part 5: Debugging Your Implementation With RVIZ2
Save your changes to lab_2.py
Run the ROS2 nodes:
ros2 launch lab_2.launch.py
In another terminal, use the following command to run the main code:
python lab_2.py
Move the left front leg of your robot and observe the changes in the published positions
To test your code in simulation to make sure that the code works as expected, you can use RVIZ2. RVIZ2 will show the Pupper model as well as a marker that shows the output from the forward kinematics.
rviz2 -d lab_2.rviz
The above command will load the RVIZ config file. If you just run rviz2, you can manually add the configuration. After running rviz, click the “Add” button, and then select a Robot Model type. Select the /robot_description topic. Next, add the marker by selecting “Add” again, and select a Marker type. Select the topic /marker.
While we’ve tested this pipeline on a Pupper and it works as expected, rviz2 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.
Part 6: Analyzing the Results
Record the end-effector positions for the left front leg configurations.
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?)
If there are discrepancies, try to identify the source of the errors. It could be due to: incorrect transformation matrices, innacurate joint angle readings, errors in the physical measurements of the robot
Additional challenges (optional
If you finish early and want to explore further:
Extend your implementation to calculate forward kinematics for all four legs of the Pupper robot. Save your calculations for these other legs for lab 4, where we will need forward kinematics for all four legs.
We provide the base link to leg_back_r1 transformation in the diagram below. The rest of the transformations are identical to the front leg:
During the testing with rviz2, 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_front_l_end_effector_position topic to record the end effector positions.