Compiler and/or Brain Issues

I’m having 2 weird glitches when programming our robot:

  1. the robot keeps skipping over lines of code. For example, there is a point where I have the robot wait for 1 second before continuing, and it completely ignores that line. It isn’t exactly random because it always skips over the exact same lines every time.

  2. the GPS sensor sometimes goes haywire. sometimes, it doesn’t give any values at all, it just gives me NaN. It doesn’t happen consistently (which is why I can’t send a picture of it) and it only resets and stops when I turn the brain off and on.

If anyone has had the same issues or knows a solution (even if I need a brand new brain), I would love to know.

As always, posting you code is a must when asking programming questions.

2 Likes

the code is almost 1000 lines of junk. is there any way to post just the parts that would help solve the problem?

And it seems to be happening on all of my programs, not just this one.

There must be some logical error in the code if it repeats. Programs do not “skip” lines of code. The best way to debug is to create a minimal example of the issue you can post here, otherwise we are all just guessing at what may be wrong.

Is vexos updated to the very latest version (1.1.1, released March 2022) ?

I have seen the gps sometimes return NaN if it doesn’t boot correctly, but it’s very intermittent and usually only happens when the gps is first powered on, I saw that in test perhaps once or twice in several hours of testing. rebooting the gps (unplug/plug in smart cable) would usually fix it.

3 Likes

No. And you zeroed in on the problem. It’s not what you think is the three important lines of code, it’s going to be in one of those “junk” lines. Just post it. People are not going to steal your cool code. We are also pretty much non-judgmental about code, but you may get other tips on things to change.

2 Likes

got it. I’ll see if I could put some comments in to at least somewhat organize it.

Yes, the robot was updated as soon as the update came out.

It happens as soon as I turn it on, but it continues on until I either reboot it or turn the robot off.

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

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

# Robot configuration code
expander_7 = Triport(Ports.PORT7)
left_motor_a = Motor(Ports.PORT16, GearSetting.RATIO_6_1, True)
left_motor_b = Motor(Ports.PORT9, GearSetting.RATIO_6_1, True)
left_drive_smart = MotorGroup(left_motor_a, left_motor_b)
right_motor_a = Motor(Ports.PORT4, GearSetting.RATIO_6_1, False)
right_motor_b = Motor(Ports.PORT11, GearSetting.RATIO_6_1, False)
right_drive_smart = MotorGroup(right_motor_a, right_motor_b)
drivetrain = DriveTrain(left_drive_smart, right_drive_smart, 319.19, 381, 40, MM, 0.2857142857142857)
controller_1 = Controller(PRIMARY)
goalcarrier = Motor(Ports.PORT10, GearSetting.RATIO_36_1, False)
rearcarrier = Motor(Ports.PORT18, GearSetting.RATIO_36_1, True)
inertial = Inertial(Ports.PORT15)
claw1 = DigitalOut(brain.three_wire_port.a)
limit_switch_b = Limit(brain.three_wire_port.b)
encoder_7a = Encoder(expander_7.a)
rear_switch = Bumper(brain.three_wire_port.c)
encoder_7c = Encoder(brain.three_wire_port.e)
# vex-vision-config:begin
vision_10__NEUTRAL_GOAL = Signature(1, 1121, 1613, 1367,-3973, -3579, -3776,2.4, 0)
vision_10__SIG_2 = Signature(2, 0, 0, 0,0, 0, 0,3, 0)
vision_10__SIG_3 = Signature(3, 0, 0, 0,0, 0, 0,3, 0)
vision_10 = Vision(Ports.PORT17, 50, vision_10__NEUTRAL_GOAL, vision_10__SIG_2, vision_10__SIG_3)
# vex-vision-config:end
gps_6 = Gps(Ports.PORT1, -177.80, 0.00, MM, -90)
potentiometerc = PotentiometerV2(expander_7.c)
rings_motor_a = Motor(Ports.PORT5, GearSetting.RATIO_18_1, False)
rings_motor_b = Motor(Ports.PORT13, GearSetting.RATIO_18_1, True)
rings = MotorGroup(rings_motor_a, rings_motor_b)
platform = Sonar(brain.three_wire_port.g)
puncher = DigitalOut(expander_7.d)


# wait for rotation sensor to fully initialize
wait(30, MSEC)

# define variables used for controlling motors based on controller inputs
controller_1_left_shoulder_control_motors_stopped = True
controller_1_right_shoulder_control_motors_stopped = True
drivetrain_l_needs_to_be_stopped_controller_1 = False
drivetrain_r_needs_to_be_stopped_controller_1 = False

# define a task that will handle monitoring inputs from controller_1
def rc_auto_loop_function_controller_1():
    global drivetrain_l_needs_to_be_stopped_controller_1, drivetrain_r_needs_to_be_stopped_controller_1, controller_1_left_shoulder_control_motors_stopped, controller_1_right_shoulder_control_motors_stopped, remote_control_code_enabled
    # process the controller input every 20 milliseconds
    # update the motors based on the input values
    while True:
        if remote_control_code_enabled:
            # calculate the drivetrain motor velocities from the controller joystick axies
            # left = axis3 + axis1
            # right = axis3 - axis1
            drivetrain_left_side_speed = controller_1.axis3.position() + controller_1.axis1.position()
            drivetrain_right_side_speed = controller_1.axis3.position() - controller_1.axis1.position()
            
            # check if the value is inside of the deadband range
            if drivetrain_left_side_speed < 5 and drivetrain_left_side_speed > -5:
                # check if the left motor has already been stopped
                if drivetrain_l_needs_to_be_stopped_controller_1:
                    # stop the left drive motor
                    left_drive_smart.stop()
                    # tell the code that the left motor has been stopped
                    drivetrain_l_needs_to_be_stopped_controller_1 = False
            else:
                # reset the toggle so that the deadband code knows to stop the left motor next
                # time the input is in the deadband range
                drivetrain_l_needs_to_be_stopped_controller_1 = True
            # check if the value is inside of the deadband range
            if drivetrain_right_side_speed < 5 and drivetrain_right_side_speed > -5:
                # check if the right motor has already been stopped
                if drivetrain_r_needs_to_be_stopped_controller_1:
                    # stop the right drive motor
                    right_drive_smart.stop()
                    # tell the code that the right motor has been stopped
                    drivetrain_r_needs_to_be_stopped_controller_1 = False
            else:
                # reset the toggle so that the deadband code knows to stop the right motor next
                # time the input is in the deadband range
                drivetrain_r_needs_to_be_stopped_controller_1 = True
            
            # only tell the left drive motor to spin if the values are not in the deadband range
            if drivetrain_l_needs_to_be_stopped_controller_1:
                left_drive_smart.set_velocity(drivetrain_left_side_speed, PERCENT)
                left_drive_smart.spin(FORWARD)
            # only tell the right drive motor to spin if the values are not in the deadband range
            if drivetrain_r_needs_to_be_stopped_controller_1:
                right_drive_smart.set_velocity(drivetrain_right_side_speed, PERCENT)
                right_drive_smart.spin(FORWARD)
            # check the buttonL1/buttonL2 status
            # to control rearcarrier
            if controller_1.buttonL1.pressing():
                rearcarrier.spin(FORWARD)
                controller_1_left_shoulder_control_motors_stopped = False
            elif controller_1.buttonL2.pressing():
                rearcarrier.spin(REVERSE)
                controller_1_left_shoulder_control_motors_stopped = False
            elif not controller_1_left_shoulder_control_motors_stopped:
                rearcarrier.stop()
                # set the toggle so that we don't constantly tell the motor to stop when
                # the buttons are released
                controller_1_left_shoulder_control_motors_stopped = True
            # check the buttonR1/buttonR2 status
            # to control goalcarrier
            if controller_1.buttonR1.pressing():
                goalcarrier.spin(FORWARD)
                controller_1_right_shoulder_control_motors_stopped = False
            elif controller_1.buttonR2.pressing():
                goalcarrier.spin(REVERSE)
                controller_1_right_shoulder_control_motors_stopped = False
            elif not controller_1_right_shoulder_control_motors_stopped:
                goalcarrier.stop()
                # set the toggle so that we don't constantly tell the motor to stop when
                # the buttons are released
                controller_1_right_shoulder_control_motors_stopped = True
        # wait before repeating the process
        wait(20, MSEC)

# define variable for remote controller enable/disable
remote_control_code_enabled = True
rc_auto_loop_thread_controller_1 = Thread(rc_auto_loop_function_controller_1)



#endregion VEXcode Generated Robot Configuration

# -------------------------------------------#
#                                            #
# 	Project:      VEXcode Project            #
#	Author:       ***************                 #
#	Created:                                 #
#	Description:this is a program for live   #
#               tipping point matches        #
#                                            #
# -------------------------------------------#

# Library imports
speed2 = 6
from vex import *
stop_following = False
puncher.set(False)
goalcarrier.set_velocity(100, RPM)
rearcarrier.set_velocity(100, RPM)
rings.set_velocity(600, RPM)
rearcarrier.set_stopping(BRAKE)
# Begin project code
controller_1.screen.print(potentiometerc.angle(DEGREES))
inertial.calibrate()
wait(0.5, SECONDS)
gps_6.calibrate()
inertial.calibrate()
wait(2, SECONDS)
inertial.set_heading(180, DEGREES)
inertial.set_rotation(180, DEGREES)
print(inertial.heading(DEGREES))
rearcarrier.set_velocity(100, PERCENT)



def close_claw():
        print("a;lsdhgj;fjsfjdlkgfhkdfslgd")
        while not limit_switch_b.pressing():
            pass
        claw1.set(True)
        goalcarrier.spin(FORWARD)
my_event = Event()
my_event(close_claw)



def pre_autonomous():

    while True:
        drivetrain.set_stopping(BRAKE)



        if controller_1.buttonRight.pressing():
            claw1.set(True)
            
        
        if controller_1.buttonB.pressing():
            claw1.set(False)




        controller_1.screen.clear_screen()
        print(potentiometerc.angle(DEGREES))
        controller_1.screen.print(potentiometerc.angle(DEGREES))
        
        #adjust vision sensor brightness based on where the screen is touched to make sure it
        #works at each field
        
        

def autonomous():
    
    
                        






    def drive(distance):
        drivetrain.set_stopping(BRAKE)
        #erase previous angle, set variables, and drive forward
        encoder_7a.set_position(0, TURNS)
        encoder_7c.set_position(0, TURNS)
        speed = 100
        
        if distance > 0:
            drivetrain.drive(FORWARD)
            #wait until the encoder angle passes the threshold value
            while encoder_7c.position(TURNS) > distance * -0.8898:

                #calculate the speed needed to stop the robot most acurately
                error = (distance * 0.8898 - encoder_7c.position(TURNS)) * speed
                if error > 150:
                    error = 150
                drivetrain.set_drive_velocity(error, RPM)
                
                if drivetrain.velocity(RPM) < 3.6:
                    if drivetrain.velocity(RPM) > -3.6:
                        drivetrain.set_drive_velocity(10, RPM)
                if error < 3.6:
                    if error > -3.6:
                        break
        elif distance < 0:
            drivetrain.drive(REVERSE)
            #wait until the encoder angle passes the threshold value
            while encoder_7c.position(TURNS) < distance * -0.8898:

                #calculate the speed needed to stop the robot most acurately
                error = (distance * 0.8898 - encoder_7c.position(TURNS)) * speed
                if error > 150:
                    error = 150
                drivetrain.set_drive_velocity(error, RPM)
                if drivetrain.velocity(RPM) < 3.6:
                    if drivetrain.velocity(RPM) > -3.6:
                        drivetrain.set_drive_velocity(10, RPM)
                if error < 3.6:
                    if error > -3.6:
                        break

        
        drivetrain.stop()
        print('position found')
    
    def score_rings():
        rearcarrier.set_velocity(25, PERCENT)
            
        if potentiometerc.angle(DEGREES) > 31:
            rearcarrier.spin(FORWARD)
            while potentiometerc.angle(DEGREES) > 29:
               pass
            rearcarrier.stop()
        rings.set_velocity(100, PERCENT)
        rings.spin(FORWARD)
    


    

    def turn_to_position(degrees):
            drivetrain.set_stopping(BRAKE)
            brain.timer.clear()
            #drivetrain.set_turn_velocity(100, PERCENT)
            drivetrain.turn(RIGHT)
            error = 1
            while not round(error) * 20 == 0:
                #set the speed the robot turns based off of
                # how close to the target it is
                heading = inertial.heading(DEGREES)
                #reverse the heading if it is greater than 180 degrees



                
                if degrees > 180:
                    degrees = (degrees - 180) + -180
                if inertial.heading(DEGREES) > 180:
                    if degrees < 170:
                            heading = (inertial.heading(DEGREES) - 180) - 180
                error = (degrees - heading) / 1
                
                drivetrain.set_turn_velocity(error, RPM)
                print(heading)
                if error < 1:
                    if error > -1:
                        drivetrain.set_turn_velocity(10, RPM)
                
                drivetrain.turn(RIGHT)
                if brain.timer.time(SECONDS) > 3:
                    break
                
            print('yay')  
            drivetrain.stop()
            wait(100, MSEC)  
            
    def rear_carrier_down():
        rearcarrier.spin(REVERSE)
        while potentiometerc.angle(DEGREES) > 67:
            print(potentiometerc.angle(DEGREES))
        rearcarrier.stop()

    def rear_carrier_up():
        rearcarrier.spin(FORWARD)
        while not potentiometerc.angle(DEGREES) < 40:
            pass
        rearcarrier.stop()



    
                


    

    
    
    




    #************START SEQUENCE************#
    #************START SEQUENCE************#
    #************START SEQUENCE************#
    #************START SEQUENCE************#
    
    #go_to_position(0,0, True)
    #wait(100000, SECONDS)
    my_event.broadcast()
    brain.timer.clear()
    drivetrain.set_stopping(BRAKE)
    brain.timer.clear()
    print('AAAAAAAAHHHHHHHH')
    
    rearcarrier.spin(REVERSE)
    
    wait(1, SECONDS)
    goalcarrier.spin_to_position(500, DEGREES, wait=False)
    drive(-1.75)
    score_rings()
    drive(1)
    drive(-0.75)
    goalcarrier.spin(REVERSE)
    turn_to_position(-90)
    my_event.broadcast()
    drive(7.5)
    turn_to_position(0)
    rearcarrier.spin(REVERSE)
    rearcarrier.set_velocity(100, PERCENT)
    turn_to_position(0)
    wait(1, SECONDS)
    drive(2.5)
    turn_to_position(-90)
    rearcarrier.spin_to_position(0, DEGREES, wait=False)
    drive(1)
    
    goalcarrier.spin(REVERSE)
    wait(0.5, SECONDS)
    goalcarrier.spin(FORWARD)
    wait(0.5, SECONDS)
    claw1.set(False)
    goalcarrier.spin(REVERSE)
    goalcarrier.spin(REVERSE)
    goalcarrier.spin(REVERSE)
    goalcarrier.spin(REVERSE)
    goalcarrier.spin(REVERSE)
    goalcarrier.spin(REVERSE)
    drive(-1)
    turn_to_position(-175)
    drivetrain.drive(FORWARD)
    my_event.broadcast()
    while not limit_switch_b.pressing():
        drivetrain.set_drive_velocity(100, PERCENT)
    wait(0.5, SECONDS)
    drive((gps_6.y_position(INCHES) + 6) / 12)
    
    turn_to_position(-90)
    drive(1)
    claw1.set(False)










def driver_control():
    
            
    speed2 = 5.8
    while True:
        
        drivetrain.set_stopping(BRAKE)
        if controller_1.buttonX.pressing():
            claw1.set(True)
        if controller_1.buttonB.pressing():
            claw1.set(False)
        
        rearcarrier.set_stopping(HOLD)
        if controller_1.buttonUp.pressing():
            rings.spin(FORWARD)
        if controller_1.buttonDown.pressing():
            rings.stop()
        if gps_6.x_position(INCHES) > 0 and gps_6.y_position(INCHES) < 0:
            print("ASGDHGGFDFGHGFDFGHGFGHGFGHG")
        if controller_1.buttonLeft.pressing():
            speed2 += 0.05
        if controller_1.buttonRight.pressing():
            speed2 -= 0.05
        controller_1.screen.clear_screen()
        controller_1.screen.set_cursor(1,1)
        controller_1.screen.print(speed2)

    
    
    
    
    
    
competition = Competition(driver_control, autonomous)

I removed the junk I wasn’t using.

edit: mods added code tags, please remember to use them

ok, so if you see the issue power cycle the gps. I have seen this before, but when it happens only once every few hours it’s very difficult to determine what the exact cause is.

3 Likes

yes, I try to do that. Unfortunately, I only notice the issue when I start the program. And it seems to happen about every other time I turn the robot on.

That’s not good, have you tried another cable and/or port on the V5 brain ?

So which line of code do you think is being skipped ?

3 Likes

there are a lot of lines being skipped, but I will highlight the few that are prominent:Screen Shot 2022-04-01 at 11.27.48 AM
the “goal carrier.spin(REVERSE)” line. I had to add a bunch in the code to ensure it isn’t being skipped.

Screen Shot 2022-04-01 at 11.28.23 AM
the “wait(1, SECONDS)” line. I had to add an extra function to make it wait.

Well, you have other threads that may be sending control to that motor, perhaps the nasty auto generated controller thread is sending stop or something.
disable rc_auto_loop_function_controller_1 and see if you still have that issue.
you also have the close_claw event that may still be running.

not sure what you mean here, what was the extra function ?

4 Likes

nope, I only have 1 function controlling it, and the close_claw event only closes the claw, it doesn’t control anything else.

the extra function was the “turn_to_position(0)” function, which is a proportional turning function.

well, looks like you do.

# define variable for remote controller enable/disable
remote_control_code_enabled = True
rc_auto_loop_thread_controller_1 = Thread(rc_auto_loop_function_controller_1)

and close claw spins it forward.

def close_claw():
        print("a;lsdhgj;fjsfjdlkgfhkdfslgd")
        while not limit_switch_b.pressing():
            pass
        claw1.set(True)
        goalcarrier.spin(FORWARD)
my_event = Event()
my_event(close_claw)
3 Likes

How did you determine it wasn’t waiting 1 second ? or was the actual issue that drive(2.5) didn’t work ?

3 Likes

I know it didn’t wait a second because when it stopped turning, it didn’t pause, it went straight to the next function.

and isn’t the rc_auto_loop_function_controller_1 prewritten for driver control? I thought it only runs when driver control starts.

as for the second function running the goal carrier, the function doesn’t repeat spinning it. it spins it once, and then it exits the function. I’m not sure if I did something wrong, but that exact function seems to work in every single one of my programs except right here. It even works in other parts of the program.

I just found out exactly when the GPS glitch would happen:
when I start the program without the robot knowing where it is, after I turn the robot so it can read the GPS code, it gives the NaN value.

1 Like

ok, that’s good feedback. The gps should find its position after a few seconds, you can help with that by setting an initial position for the gps, for example.

    Gps3.setLocation( 10, -35, inches, 180, degrees );

(you would change the numbers for you starting position).

That gives the gps a hint as to where it should be when it can finally see the field strip.

but we will take a look and see if there’s an issue when we work on the next V5 vexos update later this year.

4 Likes