Skip to content

Autonomous infinite loop problems #48

Description

@virtuald

Welcome to the RobotPy community!

I was looking over your code and noticed an issue you're probably going to want to address. Referring to this bit of code, you have code that looks like this:

@state()
def drive_forward_towards_ball(self) :
    while (self.drive.lfencoder.getDistance() + self.drive.rfencoder.getDistance()) <= 16 :
        self.drive.xboxTankDrive(0.4, 0.4)
    self.next_state(self.swerve_away_from_ball)

The hidden problem here is that if the encode values never goes past 16 (for example, if a wire in the encoder becomes unplugged), your code will never exit autonomous mode. Instead, remember that there's already a while loop running somewhere that will call this function over and over again, so you just need to use an if statement:

@state()
def drive_forward_towards_ball(self) :
    if (self.drive.lfencoder.getDistance() + self.drive.rfencoder.getDistance()) <= 16 :
        self.drive.xboxTankDrive(0.4, 0.4)
    else:
        self.next_state(self.swerve_away_from_ball)

Hope that helps!

Also, self.next_state accepts a string, not a function argument (though, I'll change that soon).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions