PID on Claw

int clawpid(){
clawPIDRunning = true;
 Claw.setStopping(hold);
  direction =1;
  target = 66;
 double kp = 0.1; // Proportional gain (adjust as needed)
  double ki = 0.01;  // Integral gain (adjust as needed)
  double kd = 0.05;  // Derivative gain (if needed)
  double min_speed = 10; // Minimum speed to maintain movement
  double max_speed = 10; // Maximum speed limit
  double current = Rotation17.position(deg); // Current position

  Rotation17.resetPosition(); // Reset encoder position

  double integral = 0.0; // Initialize integral term
  double previous_error = target - current; // Initialize previous error
  while (clawPIDRunning && fabs(target - current) > 1) {
    current = Rotation17.position(deg); // Read current position
    double error = target - current; // Calculate error

    // Update integral term with anti-windup
    if (fabs(integral) < max_speed / ki) {
      integral += error;
    }

    // Calculate speed based on PID control
    double speed = kp * error + ki * integral + kd * (error - previous_error);

    // Update previous error for derivative term
    previous_error = error;

    // Ensure speed is within limits
    if (speed > max_speed) {
      speed = max_speed;
    } else if (speed < -max_speed) {
      speed = -max_speed;
    } else if (fabs(speed) < min_speed) {
      speed = min_speed * direction; // Maintain minimum speed for smooth movement
    }

    // Apply speed to the motor
    Claw.spin(direction > 0 ? forward : reverse, fabs(speed), percent);
    task::sleep(20); // Delay for stability
  }

  Claw.stop(hold); // Stop the motor when target is reached
  clawPIDRunning = false;
  return 1;
}

this exact code works for my lift and yet when i run it on the claw it never stops rotating.

edit: code tags added by moderators, please remember to use them. This is strike 2, next time the post will be rejected,

I think the issue here is that you never update direction, so if the claw slightly overshoots, it will continue moving in the wrong direction. Instead you could use std::signbit() under the cmath header to calculate the direction based on the sign of your error.

I’m sorry, I’m not entirely sure how I would do this. Do you have an example of some kind?

I personally feel like PID and a claw are not synonymous. Personally, a claw typically works best with just a P loop. Assume you have a claw gripping on an object. Because the claw will never reach its desired position (since the error needs to be beyond the position of the object to apply force). That would result in integral continuing to wind up and eventually you’ll find the claw al ways using full power which will surely burn out the motor.

i’m simply saying I highly advise against a PID loop unless your goal is to maintain a consistent open state. But a P loop, excluding I (maybe excluding D based upon mechanical implementation) should be used for a claw.