I want you to take this code and merge the leftTurn() and straight() functions so that every time neither of the line finders detect a line, it uses the straight. Be careful of the fact that you need to reset the encoderA and encoderB values every time you run the function.Make sure they’re not in the loop though since that would cause problems.
from time import sleep # Import the time library for the sleep function
from basehat import Button, LineFinder, IMUSensor
from buildhat import Motor, ColorSensor
Initialize motors and sensors
motorA = Motor('A')
motorB = Motor('B')
lineFinderL = LineFinder(24)
lineFinderR = LineFinder(26)
IMU = IMUSensor()
colorPort = "C"
color = ColorSensor(colorPort)
def control_motors(speedA, speedB):
"""Starts both motors with given speeds."""
motorA.start(speedA)
motorB.start(speedB)
def stop_motors():
"""Stops both motors."""
motorA.stop()
motorB.stop()
def rightTurn(valL, valR):
"""Performs a right turn based on line finder values."""
if valL == 0 and valR == 0:
control_motors(100, -100) # Forward
elif valL == 0 and valR == 1:
control_motors(100, 100) # Right
elif valL == 1 and valR == 0:
control_motors(-100, -100) # Left
else:
control_motors(100, 0) # Slow right
def leftTurn(valL, valR):
"""Performs a left turn based on line finder values."""
if valL == 0 and valR == 0:
control_motors(100, -100) # Forward
elif valL == 0 and valR == 1:
control_motors(100, 100) # Right
elif valL == 1 and valR == 0:
control_motors(-100, -100) # Left
else:
control_motors(0, -100) # Slow left
def straight():
"""Keeps the robot moving straight by balancing motor encoders."""
"""Essential for the hill section as line sensors aren't reliable due to calibration"""
control_motors(100, -100)
if abs(encoderA) + 100 < abs(encoderB):
control_motors(100, 100)
elif abs(encoderA) > abs(encoderB) + 100:
control_motors(-100, -100)
def place_cargo():
"""Function for placing cargo."""
print("Cargo placed.")
Initialize encoder positions
initialPositionA = motorA.get_position()
initialPositionB = motorB.get_position()
Variables for managing the robot's behavior
iteration = 1
magnet_count = 0
on_hill = False
current_function = leftTurn # Start with leftTurn
hill_started = False
downhill_detected = False # All these hill values are present to make sure that it ends the straight() function only after getting back to flat ground
cargo_delivered = 0
magnet_detected = False # To prevent multiple counts when continuously detecting the magnet
try:
while True:
encoderA = motorA.get_position()-initialPositionA #allows us to get a base encoder value for the motors
encoderB = motorB.get_position()-initialPositionB

    valL, valR = lineFinderL.value, lineFinderR.value
    print(f"Sensor Left: {valL}, Sensor Right: {valR}") #Debugging
    magx, magy, magz = IMU.getMag()
    print(" MX = %7.2f uT  MY = %7.2f uT  MZ = %7.2f uT" % (magx, magy, magz))
    gyrx,gyry, gyrz = IMU.getGyro()
    print(" GX = %7.2f dps GY = %7.2f dps GZ = %7.2f dps" % (gyrx, gyry, gyrz
        
    
    
    
    # Display encoder values for each motor
    try:
        print(f"Encoder A: {encoderA}, Encoder B: {encoderB}")
    except IOError as error:
        print("Error reading encoder:", error)

    # Small delay to reduce CPU load
    sleep(0.05)
except KeyboardInterrupt:
stop_motors()