ROS2 PID Controller for Velocity

Medium Control 0% pass rate

Practice the ROS2 PID Controller for Velocity coding problem in Control. Browser-based execution with automated grading — no local ROS install required. 0% of engineers pass this challenge.

Problem Statement

Implement a PID velocity controller as a ROS2 node that drives a robot's actual velocity to a configurable target.

Requirements

Your node must:

  • Declare a parameter target_vel (default 0.5) — the desired linear velocity in m/s
  • Subscribe to /current_velocity (std_msgs/Float32) for the measured velocity feedback
  • Implement a PIDController class with method compute(setpoint, measurement, dt) → float
    • P term: kp * error
    • I term: accumulated ki * error * dtclamp to [-1.0, 1.0] (anti-windup)
    • D term: kd * (error - prev_error) / dt
  • Use a create_timer(0.05, ...) (20 Hz) control loop that:
    • Reads target_vel parameter each cycle (allows runtime updates via ros2 param set)
    • Calls pid.compute(target, measured, 0.05)
    • Clamps output to [-1.0, 1.0]
    • Publishes geometry_msgs/Twist to /cmd_vel with linear.x = clamped_output
  • Log "PID Controller Ready" on startup

Input / Output

Input: std_msgs/Float32 on /current_velocity (measured speed) Output: geometry_msgs/Twist on /cmd_vel

🤖 Why This Matters in Real Robots

The ros2_control stack internally runs PID controllers for every hardware joint. Understanding how to implement one from scratch is essential before using the framework. Tune kp=1.0, ki=0.1, kd=0.05 and observe the step response — underdamped (oscillates), overdamped (slow), or critically damped (fast + smooth).

Input/Output Format

Input: /target_vel, /current_vel | Output: /cmd_vel

⚠️ Common Pitfalls

  • Integral windup
  • Derivative noise spikes

📚 Helpful Resources

Frequently asked questions

What is the ROS2 PID Controller for Velocity practice problem?

It is a hands-on Control challenge on SimuCode where you implement and run ROS2 code in the browser with runtime-verified tests.

How do I practice ROS2 PID Controller for Velocity online?

Open this page, sign in, and solve the problem in the built-in IDE. Your solution is graded against real ROS2 execution checks.

What skills does ROS2 PID Controller for Velocity test?

This Medium problem focuses on Control skills used in robotics interviews and production systems.