Vex IQ controller lag

At our robotics lab, we experienced a significant amount of controller lag, about 2-3 seconds. We tested multiple things, but the only thing that worked was commenting everything except for our drive loop out.

def when_started3():

global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision

while True:

    if Fl4:

        drivetrain.drive(FORWARD)

        left_drive_smart.set_velocity((controller.axisA.position()\*0.9 + controller.axisC.position() \* 0.27), PERCENT)



        right_drive_smart.set_velocity((controller.axisA.position()\*0.9 - controller.axisC.position() \* 0.27), PERCENT)

    wait(20, MSEC)

Your wait command is outside of the if statement.

If your whole code was done like this here, then you’d literally be telling the code to wait a bunch of times in a row, which adds up. - Try moving your wait commands inside of their cooresponding if statements, so that they only trigger if that condition actually happened.

This is the only wait inside of the if statement. However, when I moved the wait command, the controller lag only applied when I was turning, no longer when I pressed the buttons or moved forward and backward. In an empty robotics lab, I tried to replace, the brain, battery, controller, motor, and cable, but nothing worked. It was sometimes half a second, but at random times, it increased to 4 or 5 seconds.

If commenting out everything except the drive loop helped before, try commenting it all out again, then slowly bringing portions back. When you find the one where it begins to noticeably slow down, try isolating just that code and adding checkpoints, such that, at each critical point in that section, it prints the time since the previous checkpoint and what the cooresponding checkpoint name is on the brain.

The goal of this is to get a better idea of what exactly gets the code stuck, so if you can’t figure out why that portion of code slows down, post that chunk here.

After testing that, all parts of the code that I let back in let in some sort of controller lag, however, some parts let in more, such as lifting the claw made the controller lag go to about 4 seconds, but letting just opening and closing the claw was about 0.5 seconds of controller lag.

Yeah, this is definitely a problem caused by the code then.

Are you using run for / spin for commands that specify a time or number of rotations to be spun for?

Is your code structured in a way that there are wait commands triggered without a button being pressed (like in else statements)?

Realistically though, i can’t guess it unless you provide a snipet of the code (ideally one that increased lag significantly).

…So take a piece of the code that introduced noticeable lag and paste it here, it’ll be much easier to troubleshoot that way.

I don’t think you are going to get good feedback unless you just post the whole code.

from vex import *
import urandom
import math

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

# Robot configuration code
brain_inertial = Inertial()
LeftDiff = Motor(Ports.PORT4, False)
RightDiff = Motor(Ports.PORT10, False)
controller = Controller()
Pneumatic3 = Pneumatic(Ports.PORT12)
left_motor_a = Motor(Ports.PORT1, 1.0, False)
left_motor_b = Motor(Ports.PORT3, 1.0, False)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)

right_motor_a = Motor(Ports.PORT7, 1.0, True)
right_motor_b = Motor(Ports.PORT9, 1.0, True)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 200, 173, 76, MM, 1)
bumper_2 = Bumper(Ports.PORT2)
touchled_8 = Touchled(Ports.PORT8)



# generating and setting random seed
def initializeRandomSeed():
    wait(100, MSEC)
    xaxis = brain_inertial.acceleration(XAXIS) * 1000
    yaxis = brain_inertial.acceleration(YAXIS) * 1000
    zaxis = brain_inertial.acceleration(ZAXIS) * 1000
    systemTime = brain.timer.system() * 100
    urandom.seed(int(xaxis + yaxis + zaxis + systemTime)) 
    
# Initialize random seed 
initializeRandomSeed()

#endregion VEXcode Generated Robot Configuration

screen_precision = 0
console_precision = 0
myVariable = 0
my_event = Event()
claw = False
console_thingy = False
Fl4 = False
l3l4 = False

def Wind_up():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    LeftDiff.spin(REVERSE)
    RightDiff.spin(REVERSE)
    while not bumper_2.pressing():
        wait(20, MSEC)
    LeftDiff.stop()
    RightDiff.stop()

def shoot():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    LeftDiff.set_velocity(100, PERCENT)
    RightDiff.set_velocity(100, PERCENT)
    LeftDiff.spin_for(FORWARD, 50, DEGREES, wait=False)
    RightDiff.spin_for(FORWARD, 50, DEGREES)
    wait(0.2, SECONDS)
    Wind_up()

def controllerbuttonLUp_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    Wind_up()

def controllerbuttonRDown_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    shoot()

def controllerbuttonRUp_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    if l3l4:
        touchled_8.set_color(Color.RED)
        Pneumatic3.retract(CYLINDER2)
        l3l4 = False
    else:
        touchled_8.set_color(Color.BLUE)
        Pneumatic3.extend(CYLINDER2)
        l3l4 = True

def when_started1():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    claw = True
    console_precision = 6
    drivetrain.set_stopping(HOLD)
    left_drive_smart.set_max_torque(100,PERCENT)

    right_drive_smart.set_max_torque(100,PERCENT)
def dol4forsomereason():
    global Fl4
    touchled_8.set_color(Color.BLUE)
    Pneumatic3.extend(CYLINDER2)
    l3l4 = True
    drivetrain.set_drive_velocity(100, PERCENT)
    drivetrain.set_turn_velocity(50, PERCENT)
    drivetrain.drive_for(REVERSE, 90, MM)
    drivetrain.turn_for(LEFT, 17, DEGREES)
    drivetrain.set_timeout(0.6, SECONDS)
    drivetrain.drive_for(REVERSE, 100, MM)
    wait(0.1, SECONDS)
    my_event.broadcast()
    Fl4 = True

def when_started2():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    print("\033[2J")
    Fl4 = False
    l3l4 = True
    LeftDiff.set_max_torque(100, PERCENT)
    RightDiff.set_max_torque(100, PERCENT)
    LeftDiff.set_velocity(100, PERCENT)
    RightDiff.set_velocity(100, PERCENT)
    Wind_up()

def controllerbuttonEUp_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    brain.program_stop()

def controllerbuttonFDown_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    Pneumatic3.retract(CYLINDER1)
    claw = True
    LeftDiff.set_timeout(2, SECONDS)
    RightDiff.set_timeout(2, SECONDS)
    RightDiff.spin_for(FORWARD, 235, DEGREES, wait=False)
    LeftDiff.spin_for(REVERSE, 235, DEGREES)
    Pneumatic3.extend(CYLINDER1)
    wait(0.05, SECONDS)
    claw = False
    LeftDiff.set_timeout(0, SECONDS)
    RightDiff.set_timeout(0, SECONDS)
    LeftDiff.set_velocity(50, PERCENT)
    RightDiff.set_velocity(50, PERCENT)
    RightDiff.spin_for(REVERSE, 235, DEGREES, wait=False)
    LeftDiff.spin_for(FORWARD, 235, DEGREES)
    LeftDiff.set_velocity(100, PERCENT)
    RightDiff.set_velocity(100, PERCENT)

def controllerbuttonFUp_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    if claw:
        Pneumatic3.extend(CYLINDER1)
        claw = False
    else:
        Pneumatic3.retract(CYLINDER1)
        claw = True

def controllerbuttonLUp_pressed_callback_1():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    Fl4 = True

def when_started3():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    while True:
        if Fl4:
            drivetrain.drive(FORWARD)
            left_drive_smart.set_velocity((controller.axisA.position()*0.9 + controller.axisC.position() * 0.27), PERCENT)

            right_drive_smart.set_velocity((controller.axisA.position()*0.9 - controller.axisC.position() * 0.27), PERCENT)
            wait(20, MSEC)

def controllerbuttonEDown_pressed_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    if not Fl4:
        dol4forsomereason()

def my_event_callback_0():
    global myVariable, my_event, claw, console_thingy, Fl4, l3l4, screen_precision, console_precision
    shoot()

# system event handlers
controller.buttonLUp.pressed(controllerbuttonLUp_pressed_callback_0)
controller.buttonLUp.pressed(controllerbuttonLUp_pressed_callback_1)
controller.buttonRDown.pressed(controllerbuttonRDown_pressed_callback_0)
controller.buttonRUp.pressed(controllerbuttonRUp_pressed_callback_0)
controller.buttonEUp.pressed(controllerbuttonEUp_pressed_callback_0)
controller.buttonFDown.pressed(controllerbuttonFDown_pressed_callback_0)
controller.buttonFUp.pressed(controllerbuttonFUp_pressed_callback_0)
controller.buttonEDown.pressed(controllerbuttonEDown_pressed_callback_0)
my_event(my_event_callback_0)
# add 15ms delay to make sure events are registered correctly.
wait(15, MSEC)

ws2 = Thread( when_started2 )
ws3 = Thread( when_started3 )
when_started1()

Disclaimer: I’ve been doing work with other programming languages recently, so some of the things i pick up on might not necessarily be relevant.

The thing I found is this:

When that “while true” loop is running and Fl4 is false, you’d have an empty loop, since the “wait” command is inside of the if statement.

Also, when you said that there was lag, is it a delay between controller input and robot reaction, or is it delaying robot response to until the previous action of the robot is done?

It’s delay between controller input and robot reaction.

It’s been a little while, have you tried fixing thr above by moving the wait command from the if statement to the while loop?

Also, is every input lagging, or are some still able to operate normally?