Announcement

Collapse
No announcement yet.

Gamepad.java gamepadChanged()

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

  • Gamepad.java gamepadChanged()

    So Gamepad.java is able to take a callback that can be called whenever the gamepad state changes. It would be nice to use, because otherwise we have to write code to constantly check the state ourselves. But we haven't figured out how to get it to work.

    I tried using this in my opMode.
    gamepad1.copy(new Gamepad(this)) ; But that doesn't work because copy() doesn't copy the callback. I really don't want to edit Gamepad.java and rebuild the RobotCore.jar. (I'm not even sure if that is legal, is it?). So... any clever solutions out there?

  • #2
    Sorry the formatting didn't work so well there. What i meant to say was:

    I tried using this in my opMode:


    Code:
    gamepad1.copy(new Gamepad(this)) ;
    But that didn't work because copy() doesn't copy the callback.

    Comment


    • #3
      You might be able to just set the gamepad equals to a new gamepad object with the callback you want. I haven't tested this, but after looking through the SDK it appears that the gamepad's state is change only through calls to copy(), which as you said doesn't change the callback.
      Code:
      gamepad1 = new Gamepad(new GamepadCallback(){
      @Override void gamepadChanged(Gamepad gamepad){}
      });

      Comment


      • #4
        Robert Woodley You're actually copying in the wrong direction. You call
        Code:
        copy()
        on the destination gamepad, and pass in the gamepad that you are copying. In addition, you should reuse your Gamepad instance, rather than creating a new one each time.

        I believe that would allow you to use the gamepad callback. But of course you'll need to keep your OpMode's new Gamepad instance up to date. So in the main loop of your OpMode, you'll need to run the copy method again and again, otherwise you'll just have stale data. But guess what the copy method ends up doing? It calls the GamepadCallback! So in the end, your callback method will just get called every time your main loop gets called. So you've gained nothing.

        Noah I couldn't get yours to work, but I'm not sure why.

        Even if you got this working either of the two ways, and the callback was only called when the gamepad changed, the only benefit I can see is greater efficiency. Your callback method would end up looking exactly like the traditional teleop loop. Being told when the state changes doesn't save you any trouble in actually parsing the state.

        Comment


        • #5
          NoahAndrews
          The copy() method doesn't copy the callback onto the gamepad since the callback is a final field. This is why the copy() method approach fails.

          After doing some more digging in the SDK, I found that the gamepad objects are set equal to new gamepad objects before every call to the loop() method, which causes my approach to fail.

          You could store the gamepad data in a separate gamepad object, set gamepad1 to a gamepad with the correct callback, and then copy the gamepad data back onto gamepad1, but as you said doing this would make the callback end up looking exactly like the traditional teleop loop.

          Comment


          • #6
            Noah - I can't over-ride gamepad1 because it is declared 'final'.

            So it sounds like from the above, this is impossible. C'est la vie. Thanks for everyone's help.

            Comment


            • #7
              You might want to consider creating an issue about this topic in the github repository of the app. If it really is impossible to change the gamepad callback, then it doesn't really serve any purpose and should either be made accessible or removed.

              Comment


              • #8
                I just came to the realization that the gamepad callback is probably intended to be used in a LinearOpMode not in a regular OpMode. As already discussed the callback offers no advantages in a regular OpMode, but in a LinearOpMode the only way to get data from the gamepads would be to use the callback.

                Comment


                • #9
                  Actually, in a LinearOpMode, the gamepad object is always up to date, just like for a non-liinear OpMode.

                  Comment


                  • #10
                    Originally posted by NoahAndrews View Post
                    Actually, in a LinearOpMode, the gamepad object is always up to date, just like for a non-liinear OpMode.
                    You're right. In a LinearOpMode the gamepads are updated in the same fashion as in the non-linear OpMode. Well, now I am out of ideas. The callback seems to be totally useless.

                    Comment

                    Working...
                    X