Chapter 8: Control Systems for Humanoids
Overview
What You'll Learn: Control theory basics, PID controllers, ROS 2 control framework, trajectory generation
Estimated Time: 7-9 hours
Core Concepts
PID Control
class PIDController:
def __init__(self, kp, ki, kd):
self.kp = kp # Proportional gain
self.ki = ki # Integral gain
self.kd = kd # Derivative gain
self.prev_error = 0.0
self.integral = 0.0
def update(self, setpoint, measurement, dt):
error = setpoint - measurement
# P term
p_term = self.kp * error
# I term
self.integral += error * dt
i_term = self.ki * self.integral
# D term
derivative = (error - self.prev_error) / dt
d_term = self.kd * derivative
# Update
self.prev_error = error
# Control output
output = p_term + i_term + d_term
return output
ROS 2 Control Architecture
Trajectory Generation
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
def generate_trajectory(joint_names, positions, duration):
traj = JointTrajectory()
traj.joint_names = joint_names
point = JointTrajectoryPoint()
point.positions = positions
point.time_from_start = Duration(seconds=duration)
traj.points.append(point)
return traj
Lab: PID Tuning
Objective: Implement and tune PID controller for joint position control
Tasks:
- Implement PID class in Python
- Create test node that commands joint positions
- Tune Kp, Ki, Kd gains for stable control
- Plot response (setpoint vs. actual)
Summary
Key Takeaways:
- PID provides feedback control for position/velocity tracking
- ROS 2 Control standardizes controller interfaces
- Trajectory generation creates smooth motion plans
- Controller tuning critical for stability and performance
Next: Part III: Gazebo & Unity