|
| 1 | +# Motor Commands |
| 2 | + |
| 3 | +NextFTC has a bunch of motor commands so that you don't have to write your own! |
| 4 | + |
| 5 | +## `RunToPosition` |
| 6 | + |
| 7 | +`RunToPosition` is likely the most common motor command you will use. It takes a `Controllable` (like a `MotorEx` or a `MotorGroup`), a `Controller`, and a target. It finishes once the motor is in a tolerable distance of the target. |
| 8 | + |
| 9 | +:::tabs key:code |
| 10 | +== Kotlin |
| 11 | + |
| 12 | +```kotlin |
| 13 | +RunToPosition(motor, target, controller, setOf(subsystems)) |
| 14 | + |
| 15 | +// or in a subsystem: |
| 16 | +RunToPosition(motor, target, controller, this) |
| 17 | +``` |
| 18 | + |
| 19 | +== Java |
| 20 | + |
| 21 | +```java |
| 22 | +new RunToPosition(motor, target, controller, Set.of(subsystems)) |
| 23 | + |
| 24 | +// or in a subsystem: |
| 25 | +new RunToPosition(motor, target, controller, this) |
| 26 | +``` |
| 27 | + |
| 28 | +::: |
| 29 | + |
| 30 | +The tolerance of the controller can be set with `controller.setPointTolerance` as follows: |
| 31 | + |
| 32 | +:::tabs key:code |
| 33 | +== Kotlin |
| 34 | + |
| 35 | +```kotlin |
| 36 | +controller.setPointTolerance(20.0) // default is 10 |
| 37 | +``` |
| 38 | + |
| 39 | +== Java |
| 40 | + |
| 41 | +```java |
| 42 | +controller.setPointTolerance(20.0); // default is 10 |
| 43 | +``` |
| 44 | + |
| 45 | +::: |
| 46 | + |
| 47 | +## `HoldPosition` |
| 48 | + |
| 49 | +`HoldPosition` holds a motor at the position it was at when the command was scheduled. |
| 50 | + |
| 51 | +:::tabs key:code |
| 52 | +== Kotlin |
| 53 | + |
| 54 | +```kotlin |
| 55 | +HoldPosition(motor, controller, setOf(subsystems)) |
| 56 | +``` |
| 57 | + |
| 58 | +== Java |
| 59 | + |
| 60 | +```java |
| 61 | +new HoldPosition(motor, controller, Set.of(subsystems)) |
| 62 | +``` |
| 63 | + |
| 64 | +::: |
| 65 | + |
| 66 | +It is mostly commonly used as the default command in a subsystem, which means it will run when no other command is being run. |
| 67 | + |
| 68 | +:::tabs key:code |
| 69 | +== Kotlin |
| 70 | + |
| 71 | +```kotlin |
| 72 | +override val defaultCommand: Command |
| 73 | + get() = HoldPosition(motor, controller, this) |
| 74 | +``` |
| 75 | + |
| 76 | +== Java |
| 77 | + |
| 78 | +```java |
| 79 | +@Override |
| 80 | +public Command getDefaultCommand() { |
| 81 | + return new HoldPosition(motor, controller, this); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +::: |
| 86 | + |
| 87 | +## `SetPower` |
| 88 | + |
| 89 | +`SetPower` is very simple: it sets the power and ends instantly. |
| 90 | + |
| 91 | +:::tabs key:code |
| 92 | +== Kotlin |
| 93 | + |
| 94 | +```kotlin |
| 95 | +SetPower(motor, power, setOf(subsystems)) |
| 96 | + |
| 97 | +// or in a subsystem: |
| 98 | +SetPower(motor, power, this) |
| 99 | +``` |
| 100 | + |
| 101 | +== Java |
| 102 | + |
| 103 | +```java |
| 104 | +new SetPower(motor, power, Set.of(subsystems)) |
| 105 | + |
| 106 | +// or in a subsystem: |
| 107 | +new SetPower(motor, power, this) |
| 108 | +``` |
| 109 | + |
| 110 | +::: |
| 111 | + |
| 112 | +## `RunToVelocity` |
| 113 | + |
| 114 | +`RunToVelocity` uses a controller on a motor until it reaches a set velocity and then *depowers the motor*. |
| 115 | + |
| 116 | +:::tabs key:code |
| 117 | +== Kotlin |
| 118 | + |
| 119 | +```kotlin |
| 120 | +RunToVelocity(motor, velocity, controller, setOf(subsystems)) |
| 121 | + |
| 122 | +// or in a subsystem: |
| 123 | +RunToVelocity(motor, velocity, controller, this) |
| 124 | +``` |
| 125 | + |
| 126 | +== Java |
| 127 | + |
| 128 | +```java |
| 129 | +new RunToVelocity(motor, velocity, controller, Set.of(subsystems)) |
| 130 | + |
| 131 | +// or in a subsystem: |
| 132 | +new RunToVelocity(motor, velocity, controller, this) |
| 133 | +``` |
| 134 | + |
| 135 | +::: |
| 136 | + |
| 137 | +Optionally, you can pass the condition when it ends as well. |
| 138 | + |
| 139 | +:::tabs key:code |
| 140 | +== Kotlin |
| 141 | + |
| 142 | +```kotlin |
| 143 | +RunToVelocity( |
| 144 | + motor, |
| 145 | + velocity, |
| 146 | + controller, |
| 147 | + setOf(subsystems), |
| 148 | + { abs(motor.velocity) - velocity < 10 } // this is the default |
| 149 | +) |
| 150 | +``` |
| 151 | + |
| 152 | +== Java |
| 153 | + |
| 154 | +```java |
| 155 | +new RunToVelocity( |
| 156 | + motor, |
| 157 | + velocity, |
| 158 | + controller, |
| 159 | + Set.of(subsystems), |
| 160 | + () -> Math.abs(motor.getVelocity()) - velocity < 10 // this is the default |
| 161 | +) |
| 162 | +``` |
| 163 | + |
| 164 | +::: |
| 165 | + |
| 166 | +## `HoldVelocity` |
| 167 | + |
| 168 | +`HoldVelocity` is like a combination of `RunToVelocity` and `HoldPosition`: it keeps a motor at the velocity it was at when the command was scheduled. |
| 169 | + |
| 170 | + |
| 171 | +:::tabs key:code |
| 172 | +== Kotlin |
| 173 | + |
| 174 | +```kotlin |
| 175 | +HoldVelocity(motor, controller, setOf(subsystems)) |
| 176 | +``` |
| 177 | + |
| 178 | +== Java |
| 179 | + |
| 180 | +```java |
| 181 | +new HoldVelocity(motor, controller, Set.of(subsystems)) |
| 182 | +``` |
| 183 | + |
| 184 | +::: |
| 185 | + |
| 186 | +Like `HoldPosition`, `HoldVelocity` is most commonly used as the default command in a subsystem. This is useful for something like a flywheel. You can have `RunToVelocity` commands that bring it to a velocity (spinning or stopped), and the `HoldVelocity` command will run when those stop to keep it at that velocity. |
| 187 | + |
| 188 | +:::tabs key:code |
| 189 | +== Kotlin |
| 190 | + |
| 191 | +```kotlin |
| 192 | +override val defaultCommand: Command |
| 193 | + get() = HoldVelocity(motor, controller, this) |
| 194 | +``` |
| 195 | + |
| 196 | +== Java |
| 197 | + |
| 198 | +```java |
| 199 | +@Override |
| 200 | +public Command getDefaultCommand() { |
| 201 | + return new HoldVelocity(motor, controller, this); |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +::: |
| 206 | + |
| 207 | +## `ResetEncoder` |
| 208 | + |
| 209 | +`ResetEncoder` does exactly what it sounds like: it resets the encoder. |
| 210 | + |
| 211 | +:::tabs key:code |
| 212 | +== Kotlin |
| 213 | + |
| 214 | +```kotlin |
| 215 | +ResetEncoder(motor, setOf(subsystems)) |
| 216 | + |
| 217 | +// or in a subsystem: |
| 218 | +ResetEncoder(motor, this) |
| 219 | +``` |
| 220 | + |
| 221 | +== Java |
| 222 | + |
| 223 | +```java |
| 224 | +new ResetEncoder(motor, Set.of(subsystems)) |
| 225 | + |
| 226 | +// or in a subsystem: |
| 227 | +new ResetEncoder(motor, this) |
| 228 | +``` |
| 229 | + |
| 230 | +::: |
| 231 | + |
| 232 | +> [!NOTE] |
| 233 | +> See the [controllables reference](https://docs.rowanmcalpin.com/reference/ftc/com.rowanmcalpin.nextftc.ftc.hardware.controllables/) for more information. |
0 commit comments