这是我调用PID函数的地方 . 它位于ROS环路中,频率为60Hz . 我以1Hz的频率接收每秒编码器计数 . 我设定了每秒的目标计数,这是我的设定值 .

ros::Rate loop_rate(60);
      while (ros::ok())
      {
        PID_L();
        setPWM(frontLeftMotor.pwm_rail, duty_cycle_l);

        ros::spinOnce();
        loop_rate.sleep();
     }

这是我的PID功能 . 我的代码有什么问题吗?我觉得无论我调整PID多少,我都无法达到设定值(每秒滴答) .

void PID_L()
{

    const double kp = 0.45;
    const double ki = 0.002;
    const double kd = 0.05;

    int error, derivative;

  // Proportional
  // Having just a Proportional to control motors, it will cause the motors to oscillate.
  // To over come this, we add the Integral part to add the sum of all past errors.
  error = targetTicks_l - cps_left;

  std::cout << "error: " << error << "\n";
  std::cout << "target_l: " << targetTicks_l << "\n";
  std::cout << "actual_l: " << cps_left << "\n";

  integral_l = integral_l + error;

  if (error == 0)
  {
    integral_l = 0;
  }

  if (abs(error) > 20)
  {
    integral_l = 0;
  }

  // Derivative
  // If there is still some oscillating within the motors, we add the derivative part.
  // The Derivative works by calculating the change in error
  // We might not want to used this, but I will leave it for now.
  derivative     = error - previous_error_l;
  previous_error_l = error;

  duty_cycle_l = map(((kp * error) + (ki * integral_l) + (kd * derivative)), 0, 5400, 0, 99); 
  std::cout << "result of map: " << duty_cycle_l << std::endl;
  if (duty_cycle_l < 0)
    duty_cycle_l = 0;
  std::cout << "setting duty cylce to: " << duty_cycle_l << "\n";
  setPWM(frontLeftMotor.pwm_rail, duty_cycle_l);
}