Python Code Crashes Gen 2 IQ

Hello –

I have a Gen2 brain and the following code runs all the way to the last line and then the brain freezes, with the last command running until I pull the battery. I can’t see any specific problem with it. I did some various testing, and the only thing that caused it to stop failing was to get rid of the Brain Inertial. However, we’d like to use that as it is pretty accurate. Can someone set up a Gen 2 brain and verify if they are also seeing the crash, or do I have a bad brain?

Thank you!
Corey

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

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

# Robot configuration code
brain_inertial = Inertial()
left_drive_smart = Motor(Ports.PORT12, 1, False)
right_drive_smart = Motor(Ports.PORT6, 1, True)

drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)
Touch_Btn = Touchled(Ports.PORT8)
ArmSensor = Optical(Ports.PORT2)
ChooChoo_motor_a = Motor(Ports.PORT11, False)
ChooChoo_motor_b = Motor(Ports.PORT5, True)
ChooChoo = MotorGroup(ChooChoo_motor_a, ChooChoo_motor_b)
Sweeper_motor_a = Motor(Ports.PORT7, False)
Sweeper_motor_b = Motor(Ports.PORT1, True)
Sweeper = MotorGroup(Sweeper_motor_a, Sweeper_motor_b)


def calibrate_drivetrain():
    # Calibrate the Drivetrain Inertial
    sleep(200, MSEC)
    brain.screen.print("Calibrating")
    brain.screen.next_row()
    brain.screen.print("Inertial")
    brain_inertial.calibrate()
    while brain_inertial.is_calibrating():
        sleep(25, MSEC)
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)


#endregion VEXcode Generated Robot Configuration

# Define events
def onevent_ArmSensor_detects_object_0():
    Touch_Btn.set_color(Color.GREEN)
    ChooChoo.stop()

def onevent_ArmSensor_loses_object_0():
    Touch_Btn.set_color(Color.YELLOW)

# Other methods
def init_gutenberg():

    # Set the speed for the motor groups
    ChooChoo.set_velocity(100, PERCENT)
    Sweeper.set_velocity(100, PERCENT)

    # Set the speed for the drivetrain
    drivetrain.set_drive_velocity(100, PERCENT)


    # Arm the catapult
    if not ArmSensor.is_near_object():
        while not ArmSensor.is_near_object():
            ChooChoo.spin(FORWARD)

    # # Done? Set the blinker to blink
    # Thread(blinker_thread)


def blinker_thread():
    state = True
    while True:
        wait(500, MSEC)
        if state:
            Touch_Btn.set_brightness(10)
        else:
            Touch_Btn.set_brightness(100)
        state = not state

# Pretty colors
Touch_Btn.set_color(Color.RED)
Touch_Btn.set_brightness(100)


# Calibrate the Drivetrain Gyro
calibrate_drivetrain()

# system event handlers
ArmSensor.object_detected(onevent_ArmSensor_detects_object_0)
ArmSensor.object_lost(onevent_ArmSensor_loses_object_0)
# add 15ms delay to make sure events are registered correctly.
wait(15, MSEC)

## Runnable code here

init_gutenberg()


# bt = Thread(blinker_thread)


ChooChoo.spin(FORWARD)
wait(2, SECONDS)


# Some dummy code to drive around for testing
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 6, INCHES)

Sweeper.spin_for(FORWARD, 600)

drivetrain.drive_for(REVERSE, 6, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)

drivetrain.drive_for(FORWARD, 12, INCHES)
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 6, INCHES)

Sweeper.spin_for(FORWARD, 600)

drivetrain.drive_for(REVERSE, 10, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 10, INCHES)

drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(REVERSE, 10, INCHES)

ChooChoo.spin(FORWARD)

I’ll take a look either later tonight or tomorrow.

2 Likes

ok, you re experiencing an issue that’s already been fixed but not released as yet. There’s a problem in the VM related to events that we missed during testing. The workaround for now is to assign the two event handler to variables in this way.

# system event handlers
e1 = ArmSensor.object_detected(onevent_ArmSensor_detects_object_0)
e2 = ArmSensor.object_lost(onevent_ArmSensor_loses_object_0)

I’ll probably post a topic later this week explaining the issue in more detail, but the short version is that when objects that have been created by the Python code are released (so called garbage collection) it is also releasing some of the event handling objects.

5 Likes

The workaround fixed the issue. I’ll be on the lookout for a firmware update.

Thank you for such a QUICK response!
Corey

There will be an update to VEXcode IQ in the next few days with updated Python VM.

2 Likes