Error with Goal sensing code - NEED HELP ASAP

Hello all. I am trying to code something that guides my robot to face the goal when I press a button. The code gives an attribute error, but I am not sure what that means. My code is as follow, and the error is apparently on line 64. My controller is named controller_1, my motors are left_motor and right_motor, and my vision sensor is named vision_sensor. It is coded in Vexcode V5 Python.

#region VEXcode Generated Robot Configuration
from vex import *
import urandom

# Brain should be defined by default
brain=Brain()

# Robot configuration code
# vex-vision-config:begin
vision_sensor__SIG_1 = Signature(1, 835, 1877, 1356,-6023, -5067, -5545,3.7, 0)
vision_sensor = Vision(Ports.PORT10, 50, vision_sensor__SIG_1)
# vex-vision-config:end
controller_1 = Controller(PRIMARY)
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT3, GearSetting.RATIO_18_1, False)


# wait for rotation sensor to fully initialize
wait(30, MSEC)
#endregion VEXcode Generated Robot Configuration

# ------------------------------------------
# 
# 	Project:      VEXcode Project
#	Author:       VEX
#	Created:
#	Description:  VEXcode V5 Python Project
# 
# ------------------------------------------

# Library imports
from vex import *

# Begin project code

TARGET_CENTER_X = 160  # The desired center x position of the target
KP = 0.08  # The proportional gain for the PID control
KD = 0.12  # The derivative gain for the PID control

# Initialize error variables
last_error = 0  # The previous error for calculating the derivative term

# Initialize drive and turn variables
drive = 0
turn = 0
speed = 0
# Initialize loop flag
run_loop = False

while True:
    if controller_1.buttonX.pressing():  # If button X is pressed
        if not run_loop:  # If the loop is not already running
            run_loop = True  # Start the loop
    else:  # If button X is not pressed
        if run_loop:  # If the loop is currently running
            run_loop = False  # Stop the loop

    if run_loop:  # If the loop is running
        # Take snapshot and find largest object
        vision_sensor.take_snapshot(vision_sensor__SIG_1)  # Take a snapshot with the vision sensor

        # Calculate error and PID output
    if vision_sensor is not None:  #If a target is detected by the vision sensor
        error = TARGET_CENTER_X - vision_sensor.largest_object().centerX # Calculate the error between the target's center x position and the largest object's center x position
        speed = error - last_error  # Calculate the speed of change of the error
        pid_output = error * KP + speed * KD  # Calculate the PID output using the proportional and derivative gains and the error and speed
        last_error = error  # Store the current error as the previous error for the next iteration
    else:  # If no target is detected by the vision sensor
        pid_output = 0  # Set the PID output to zero

        # Get joystick inputs and calculate drive and turn values
        drive = controller_1.axis3.position() * 0.12  # Get the forward/backward joystick input from the controller and scale it by 0.12
        turn = controller_1.axis1.position() * 0.12 - pid_output  # Get the left/right joystick input from the controller and subtract the PID output from it

        # Drive robot
        left_motor.spin(FORWARD, drive - turn, VOLT)
        right_motor.spin(FORWARD, drive + turn, VOLT)

I am grateful for any help. A competition is coming up, and thats why I’m so urgent.

if vision_sensor is not None:

vision_sensor is an instance of the Vision class, so this statement will always be true

error = TARGET_CENTER_X - vision_sensor.largest_object().centerX # Calculate the error between the target's center x position and the largest object's center x position

so this will have an error if there are no objects, one solution could be

    if run_loop:  # If the loop is running
        # Take snapshot and find largest object
        objects = vision_sensor.take_snapshot(vision_sensor__SIG_1)  # Take a snapshot with the vision sensor
    else:
        objects = None
        
        # Calculate error and PID output
    if objects is not None:  #If a target is detected by the vision sensor
        error = TARGET_CENTER_X - vision_sensor.largest_object().centerX # Calculate the error between the target's center x position and the largest object's center x position
        speed = error - last_error  # Calculate the speed of change of the error
7 Likes