TBH Controller Question

How’s this? I tried doing what you said and it still crashes.
Here is the code:

# VEX V5 Python Project with Competition Template
import sys
import vex
from vex import *
import motor_group
import drivetrain
import smartdrive

#region config
brain                = vex.Brain()
motor_5              = vex.Motor(vex.Ports.PORT5, vex.GearSetting.RATIO6_1, False)
motor_6              = vex.Motor(vex.Ports.PORT6, vex.GearSetting.RATIO6_1, False)
Flywheel_group       = motor_group(Flywheel1,Flywheel2)
controller           = vex.Controller(vex.ControllerType.PRIMARY)
#endregion config

#################################Variables#######################################

max_voltage = 12
min_voltage = 0
tbh = 0
error= 0
target_rpm = 3000
tbh = .5
output_voltage = 3
prev_error=0
gain = .00025
output = 8.7

################################End_Variables##########################################

# Creates a competition object that allows access to Competition methods.
competition = vex.Competition()

################################Pre_Auton#############################################

def pre_auton():
    # Calibrate Inertial
    pass

#################################TBH LOOP##################################################

def tbh_loop():
    global tbh, error, output_voltage, current_rpm, target_rpm, gain, prev_error, gain, output
    while True:
    # Get the current RPM of the flywheel
     current_rpm = Flywheel_Sensor.velocity(VelocityUnits.RPM)
    
    # Calculate the error between the current RPM and the target RPM
    error = target_rpm - current_rpm

    output += gain * error

    if (error > 0 and prev_error < 0) or (error < 0 and prev_error > 0):
        output = 0.5 * (output + tbh)
        tbh = output
        prev_error = error
    # Set the flywheel motor speeds based on the output voltage    
    Flywheel_group.spin(FORWARD, output, VOLT)    


##################################END TBH LOOP#############################################

def autonomous():
    # Place autonomous code here
    pass

############################################################################################

def drivercontrol():
    # Place drive control code here, inside the loop
    while True:
        sys.run_in_thread(tbh_loop)
        if controller.buttonL1.pressing():
            target_rpm = 3000
            sys.sleep(.05)
        elif controller.buttonL.pressing():
            target_rpm = 0
            sys.sleep(.05)
             
            

    pass

###########################################################################################

# Set up (but don't start) callbacks for autonomous and driver control periods.
competition.autonomous(autonomous)
competition.drivercontrol(drivercontrol)

# Run the pre-autonomous function.
pre_auton()

# Robot Mesh Studio runtime continues to run until all threads and
# competition callbacks are finished.

Hey I don’t know if this has been said here or if it is just a problem with the way we coded the flywheel. But sometimes the field will disconnect and the TBH loop completely stops working and the flywheel doesn’t spin. This happened a couple times during last week’s competition, and we coded a quick solution for it. We added a function on button press that sets all of the variables in tbh to that were defined in initialization to their defaults. This started it back off fine and it worked. This may be completely useless knowledge but on the off chance that something is happening to someone else like this, here is a solution