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,