Announcement

Collapse
No announcement yet.

Event Handling (Rev Expansion Hub Java)?

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

  • Event Handling (Rev Expansion Hub Java)?

    I do not see any examples or discussion of event driven programming. Although people refer to the opMode as an event loop, it appears to be more of a status loop. Meaning, if I press a controller button, the expectation is that code will run based on whether the button is down, not because it was pushed.

    The difference is significant. If I press a button in the status-driven model, the code may be repeatedly triggered (with every loop) if I am not super quick about releasing the button. In the event driven model, code would be executed only once because the "button down" event only happened once.

    Has anyone here implemented event driven code?

    Thanks in advance.


  • #2
    It seems you are a newbie to embedded programming, so let me explain how this works and suggest ways of achieving what you are looking for. The FTC architecture is built on a simple embedded loop running on a java layer. (basically the worst of all worlds when it comes to controlling something reliably) There is no supervisory OS nor the ability to vector to interrupt branches.

    That said, there are easy ways to accomplish what you are asking by using state flags. It is a bit flag, true or false Initialize it false (zero) Each loop, test the button, If your logic sees the button pressed, test the flag, if false, you can increment a state flag, and then do what you want to do with the routine associated with that button. If you see button pressed and If the flag is already one, bail on the routine. If when you test the button and it is false, then clear the flag regardless of state.

    I don't know what you are trying to do, but keep in mind most operations run longer than one loop, so make sure your code accommodates your operation in context of the button pressing and flags.

    Comment


    • #3


      From the SensorMRColor example program -
      Code:
      // check the status of the x button on either gamepad.
      bCurrState = gamepad1.x;
      
      // check for button state transitions.
      if (bCurrState && (bCurrState != bPrevState))  {
      
        // button is transitioning to a pressed state. So Toggle LED
        bLedOn = !bLedOn;
        colorSensor.enableLed(bLedOn);
      }
      
      // update previous state variable.
      bPrevState = bCurrState;
      Here's another way that also walks through more than one outcome -
      Code:
      //gamepad1.b only increments once for each button press
      if (gamepad1.b && buttonReleased) {
          buttonReleased = false;
          goodCnt ++;
          slowMode = !slowMode;
          multimode += 1;
          if (multimode > 2) multimode = 0;
      }
      if (!gamepad1.b && !buttonReleased) {
          buttonReleased = true;
      }


      Comment


      • #4
        Thanks much. That will work.

        Comment


        • #5
          We wrapped buttons up in a debouncing class, so that you can get this behavior with any button, and don't have to copy and paste that code around:

          https://github.com/HF-Robotics/ftc_a...cedButton.java

          OnOffButton is an interface the software team wrote to let the code treat the push buttons and dpad as well as joysticks and triggers as "buttons" when needed.

          - Mark

          Comment

          Working...
          X