Announcement

Collapse
No announcement yet.

OpMode type in program

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpMode type in program

    Is there a way to know what type of opMode is current ie teleop or autonomous? We have a method that we want to behave differently in teleop than auto.

  • #2
    You could always pass a parameter to the method that specifies autonomous vs teleop, for example, an isAutonomous boolean. That would be the easiest way.

    Comment


    • #3
      We have a convention that any Autonomous opmodes must have "FtcAuto" as part of its name and any TeleOp opmodes must have "FtcTeleOp" in its opmodes. So the code can determine if it's autonomous or teleop by parsing the name. In fact, we even have a "Test" opmode category.

      In theory, since we annotate opmodes (e.g. @Autonomous(name="Autonomous", group="Auto"), one could find out what it is although I haven't spent the time to figure out how.

      Comment


      • #4
        A little googling found some info about retrieving annotation:
        Code:
        boolean isAutonomous = getClass().getAnnotation(Autonomous.class) != null;
        boolean isTeleOp = getClass().getAnnotation(TeleOp.class) != null;

        Comment


        • #5
          A more concise answer is to use
          Code:
          isTeleOp = getClass().isAnnotationPresent(TeleOp.class);
          . Keep in mind this statement as-is must be ran from within the same OpMode or a parent class of the OpMode you want to target.

          The general function is:
          Code:
          public static boolean isTeleOp(OpMode opMode) {
              return opMode.getClass().isAnnotationPresent(TeleOp.class);
          }
          Deriving a similiar function for the Autonomous OpMode requires only a substitution of arguments in the isAnnotationPresent call in both cases.

          Comment

          Working...
          X