Problem with turn to target

So we are having an issue with turn to target with limelight. IT will start turning but then stop, and we won’t be able to start.
We do get a warning when we start the bot and sometimes when running. printLoopOverrunMessage.
Our turn looks like this

public void turnToTarget(Drivetrain drivetrain, Shooter shooter){
    if (this.table.getEntry("ledMode").setNumber(1)){
      SmartDashboard.putString("ledMode ","Set");
    } else {
      SmartDashboard.putString("ledMode ","Not set");
    }
    SmartDashboard.putString("turnToTarget ","Started");
      double turn = 0;
    double min = ShooterConstants.MIN_TURN;
    boolean check = hasTarget();
    SmartDashboard.putString("Target ","" + check);
    SmartDashboard.putString("Initial TY","" + getTy().getDouble(0.0));
    int loop = 0;
    if(Math.abs(getTy().getDouble(0.0)) >= 3 && hasTarget()){
          turn = getTy().getDouble(0.0)*0.03;    
          if (Math.abs(turn) < min){
            turn = turn > 0 ? min:-min;
          }
          drivetrain.getDrive().tankDrive(-turn, turn);
          SmartDashboard.putString("Loop TY:",loop++ + ":" + getTy().getDouble(0.0));
        }
        this.table.getEntry("ledMode").setNumber(3);
       SmartDashboard.putString("Ending TY","" + getTy().getDouble(0.0));
        SmartDashboard.putString("Turning Complete","Turning Complete");
  }

And is called like this

new JoystickButton(driverController, XboxController.Button.kBumperRight.value)
      .whileHeld(new InstantCommand(() -> m_Limelight
.turnToTarget(m_driveTrain,shooter) , m_driveTrain, m_Limelight, shooter ) 
.andThen(new InstantCommand(() -> SmartDashboard.putString("Info", "Turn To Target Command Finsihed"))));

It’s much easier if you post your code to github (or similar), or enclose it with three ` characters so it formats

in a code block
1 Like

Do you have a default command as well for your drivetrain?

An InstantCommand only runs for one scheduler loop iiteration and ends immediately. So you may see your motors start, then your instantcommand ends, and your default command takes over which stops the motors because you aren’t passing any joystick values in.

Here’s our version of a similar idea, that is also bound to a joystick buttton whileHeld() trigger. It may help you for reference.

1 Like

The command runs while held out output statements keep refreshing, so should run multiple times. Thanks will look at the code.

If we went back to drivetrain telop drive being the default command, and requiring the drivetrain and had the turnToTarget require drivetrain then that should keep the drive from stepping on the turn, though i am not convinced that is what is happening because our method executes repeatedly while held.

Even if that isn’t the cause of this problem, that is certainly the way you should be designing things with the command based framework.

1 Like

IT did end up being the problem, We had tried drive both as a default command and periodic, and with and without requires, but it seems when we tried default command with requires we didn’t take it out of periodic.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.