Skip to content

Commit f02b555

Browse files
committed
use aWasPressed() etc in teleop actions
1 parent b4d20e9 commit f02b555

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/nextrunner/examples/teleop-actions.md

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,33 @@ public class MyTeleOp extends OpMode {
101101
Now, let's map some controller buttons to actions.
102102
We'll use the `gamepad1` object to get button presses.
103103

104+
To prevent an action from being triggered every loop cycle while a button is held down,
105+
we can use the `aWasPressed()`, `bWasPressed()`, etc. methods.
106+
These methods return true only on the first frame the button is pressed.
107+
104108
`ActionRunner` is a named object,
105109
meaning there is one instance of it that is accessed globally.
106110
Its functions are called like static methods in Java.
107111

108-
> [!CAUTION]
109-
> Because `ActionRunner` does not check for duplicates,
110-
> make sure that you do trigger an action before it completes.
111-
> If you do, it can cause unexpected behavior,
112-
> depending on the action's implementation.
113-
114112
:::tabs key:code
115113

116114
== Kotlin
117115

118116
```kotlin
119117
// in loop()
120-
if (gamepad1.a) {
118+
if (gamepad1.aWasPressed()) {
121119
ActionRunner.run(claw.open())
122120
}
123121

124-
if (gamepad1.b) {
122+
if (gamepad1.bWasPressed()) {
125123
ActionRunner.run(claw.close())
126124
}
127125

128-
if (gamepad1.x) {
126+
if (gamepad1.xWasPressed()) {
129127
ActionRunner.run(lift.goToPosition(1000.0))
130128
}
131129

132-
if (gamepad1.y) {
130+
if (gamepad1.yWasPressed()) {
133131
ActionRunner.run(lift.goToPosition(0.0))
134132
}
135133
```
@@ -138,19 +136,19 @@ if (gamepad1.y) {
138136

139137
```java
140138
// in loop()
141-
if (gamepad1.a) {
139+
if (gamepad1.aWasPressed()) {
142140
ActionRunner.run(claw.open());
143141
}
144142

145-
if (gamepad1.b) {
143+
if (gamepad1.bWasPressed()) {
146144
ActionRunner.run(claw.close());
147145
}
148146

149-
if (gamepad1.x) {
147+
if (gamepad1.xWasPressed()) {
150148
ActionRunner.run(lift.goToPosition(1000));
151149
}
152150

153-
if (gamepad1.y) {
151+
if (gamepad1.yWasPressed()) {
154152
ActionRunner.run(lift.goToPosition(0));
155153
}
156154
```
@@ -226,19 +224,19 @@ class MyTeleOp : OpMode() {
226224
}
227225

228226
override fun loop() {
229-
if (gamepad1.a) {
227+
if (gamepad1.aWasPressed()) {
230228
ActionRunner.run(claw.open())
231229
}
232230

233-
if (gamepad1.b) {
231+
if (gamepad1.bWasPressed()) {
234232
ActionRunner.run(claw.close())
235233
}
236234

237-
if (gamepad1.x) {
235+
if (gamepad1.xWasPressed()) {
238236
ActionRunner.run(lift.goToPosition(1000.0))
239237
}
240238

241-
if (gamepad1.y) {
239+
if (gamepad1.yWasPressed()) {
242240
ActionRunner.run(lift.goToPosition(0.0))
243241
}
244242

@@ -273,19 +271,19 @@ public class MyTeleOp extends OpMode {
273271

274272
@Override
275273
public void loop() {
276-
if (gamepad1.a) {
274+
if (gamepad1.aWasPressed()) {
277275
ActionRunner.run(claw.open());
278276
}
279277

280-
if (gamepad1.b) {
278+
if (gamepad1.bWasPressed()) {
281279
ActionRunner.run(claw.close());
282280
}
283281

284-
if (gamepad1.x) {
282+
if (gamepad1.xWasPressed()) {
285283
ActionRunner.run(lift.goToPosition(1000));
286284
}
287285

288-
if (gamepad1.y) {
286+
if (gamepad1.yWasPressed()) {
289287
ActionRunner.run(lift.goToPosition(0));
290288
}
291289

0 commit comments

Comments
 (0)