PID will not work and I have a tournament this weekend

My PID will not work no matter what I do. I have tried to tune these two PID’s multiple times and I get the same result everytime. The driving PID will overshoot everytime by a lot. I have been able to make it less but it is just too much. It will overshoot by two feet. The turning PID is really weird though because even though its the exact same code as the driving PID it will barely move. My theory on it is that the velocity is somehow not high enough to even have the power to move the robot and I did test it by lifing the robot and it seems true. And yes I did try to change kp and kd and ki none of it works I am truly lost.

anyway here is the code

Drive_Gear_Ratio = (36/48)

max_speed = 100
min_speed = 50

error = 0
integral = 0
derivative = 0
previous_error = 0

kp = .5
ki = 0
kd = .01
kf = .3

kp2 = 500
ki2 = 0
kd2 = 0
kf2 = 500

def drive_distance(target_distance):
    global error, integral, derivative, previous_error
    
    # Reset PID variables
    error = 0
    integral = 0
    derivative = 0
    previous_error = 0
    
    # Main PID loop
    while True:
        current_distance = ((3.14159265 * Wheel_Diameter) / 360) * (LeftDrive1111_motor_a.position(DEGREES)) * (Drive_Gear_Ratio)
        error = target_distance - current_distance
        integral = integral + error
        derivative = error - previous_error
        output = kp * error + ki * integral + kd * derivative
        feedforward = kf * target_distance
        speed = max(min_speed, min(max_speed, feedforward + max_speed - abs(output)))
        left_speed = speed
        right_speed = speed
        if error < 0:
            left_speed *= -1
            right_speed *= -1
        LeftDrive1111.set_velocity(left_speed, PERCENT)
        LeftDrive1155.set_velocity(left_speed, PERCENT)
        RightDrive1111.set_velocity(right_speed, PERCENT)
        RightDrive1155.set_velocity(right_speed, PERCENT)
        previous_error = error
        wait(20, MSEC)
        # Break the loop when close enough to the target
        if abs(error) < 1:
            LeftDrive1111_motor_a.set_velocity(0, PERCENT)
            LeftDrive1155_motor_a.set_velocity(0, PERCENT)
            RightDrive1111_motor_a.set_velocity(0, PERCENT)
            RightDrive1155_motor_a.set_velocity(0, PERCENT)
            break

def turn_to_angle(target_angle):
    global error, integral, derivative, previous_error

    # Reset PID variables
    error = 0
    integral = 0
    derivative = 0
    previous_error = 0

    # Main PID loop for turning
    while True:
        current_angle = inertial_10.heading(DEGREES)
        error = target_angle - current_angle
        integral = integral + error
        derivative = error - previous_error
        output = kp2 * error + ki2 * integral + kd2 * derivative
        feedforward = kf2 * target_angle
        speed = max(min_speed, min(max_speed, feedforward + max_speed - abs(output)))
        left_speed = -1 * speed
        right_speed = speed
        LeftDrive1111_motor_a.set_velocity(left_speed, PERCENT)
        LeftDrive1155_motor_a.set_velocity(left_speed, PERCENT)
        RightDrive1111_motor_a.set_velocity(right_speed, PERCENT)
        RightDrive1155_motor_a.set_velocity(right_speed, PERCENT)

        # Update previous error
        previous_error = error

        # Sleep for a short time to avoid excessive loop execution
        wait(20, MSEC)

        # Break the loop when close enough to the target angle
        if abs(error) < 1:
            LeftDrive1111_motor_a.set_velocity(0, PERCENT)
            LeftDrive1155_motor_a.set_velocity(0, PERCENT)
            RightDrive1111_motor_a.set_velocity(0, PERCENT)
            RightDrive1155_motor_a.set_velocity(0, PERCENT)
            break

Sorry no one has responded for you…
I personally haven’t done a PID, but it sounds like its a problem with one of your functions rather than your variables
Pro forumers might be able to help you better

Hey! Sorry this is late. There are a few things that might cause that overshoot. Looking at your code, and judging by your description, my best guess is the way you calculate your error/distance. You could try printing the output speed to the brain screen, and see what it thinks it should be. As you said, tuning your PID can also cause/solve a lot of accuracy problems, here’s an article I like to use for tuning.
Also, I believe it’s generally not great practice to use the motor encoders, and instead you might be interested in using a tracking wheel with a rotation sensor.

Let me know if you have any questions! : )

1 Like

I’m sorry I meant to post that I have already fixed the issue. Also, I don’t need to use tracking wheels they add extra weight and take up a lot of space and with this year’s game, I haven’t needed tracking wheels. I had them early in the season and they were only marginally more accurate than encoders because of how easy the skills autonomous is this year.

Thank you for your help though the reason the PID wasn’t working is because my feed forward code was screwing us and I forgot to add the negative values in the turning pid so instead of oscillating it would spin. Same for the drive PID.

3 Likes

Oh gotcha. Sounds good.

this season were using JAR template for vexcode and the motor encoders have been working very well for us so far, id reccomend installing tracking wheels if you want that extra accuracy for skills but i wouldnt reccoment it for a match

1 Like

I would prefer to use something custom as it gives me more flexibility. Plus I don’t even have access to jar template because my school only has Chromebooks.

I don’t see how you wouldn’t be able to access Jar as Chromebooks can use Jar as well to my knowledge.