from controller import Robot

robot = Robot()
timestep = int(robot.getBasicTimeStep())

# Sensors
ir = robot.getDevice("ir_sensor")
ir.enable(timestep)

compass = robot.getDevice("compass")
compass.enable(timestep)

gps = robot.getDevice("gps")
gps.enable(timestep)

sonar = robot.getDevice("sonar")
sonar.enable(timestep)

left_motor = robot.getDevice("left_motor")
right_motor = robot.getDevice("right_motor")
left_motor.setPosition(float('inf'))
right_motor.setPosition(float('inf'))
left_motor.setVelocity(0.0)
right_motor.setVelocity(0.0)

def get_heading():
    north = compass.getValues()
    rad = math.atan2(north[0], north[2])
    return rad

def ball_direction():
    return ir.getValue()  # Simplified; use actual IR logic

while robot.step(timestep) != -1:
    heading = get_heading()
    ball_dir = ball_direction()
    position = gps.getValues()
    obstacle = sonar.getValue()

    # Basic ball tracking
    if ball_dir < threshold:
        left_motor.setVelocity(3.0)
        right_motor.setVelocity(3.0)
    else:
        left_motor.setVelocity(-2.0)
        right_motor.setVelocity(2.0)

    # Obstacle avoidance
    if obstacle < safe_distance:
        left_motor.setVelocity(-2.0)
        right_motor.setVelocity(-2.0)