|
| 1 | +# Flywheel Example |
| 2 | + |
| 3 | +Flywheels are a commonly used mechanism in FTC for launching game elements, |
| 4 | +and most often controlled with a velocity PID feedback controller. |
| 5 | + |
| 6 | +With NextControl, that would be implemented like this (using hypothetical constants): |
| 7 | + |
| 8 | +::: tabs key:code |
| 9 | + |
| 10 | +== Kotlin |
| 11 | + |
| 12 | +```kotlin |
| 13 | +controlSystem { |
| 14 | + velPid(0.001, 0.0, 0.0) |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +== Java |
| 19 | + |
| 20 | +```java |
| 21 | +ControlSystem.builder() |
| 22 | + .velPid(0.001, 0.0, 0.0) |
| 23 | + .build(); |
| 24 | +``` |
| 25 | + |
| 26 | +::: |
| 27 | + |
| 28 | +## What can I do with this? |
| 29 | + |
| 30 | +If you're using a command-based framework, like NextFTC, |
| 31 | +you can create commands using your control system. |
| 32 | + |
| 33 | +If not, we can create OpModes that use our controllers directly. |
| 34 | +For example, let's say we wanted to change the target velocity of our flywheel using a button press. |
| 35 | +We can easily do that by simply changing the `goal` of our `ControlSystem`: |
| 36 | + |
| 37 | +::: tabs key:code |
| 38 | + |
| 39 | +== Kotlin |
| 40 | + |
| 41 | +```kotlin |
| 42 | +class FlywheelExample() : OpMode() { |
| 43 | + val flywheelMotor by lazy { hardwareMap.get(DcMotorEx.class, "flywheel") } |
| 44 | + val controller = controlSystem { |
| 45 | + velPid(0.001, 0.0, 0.0) |
| 46 | + } |
| 47 | + |
| 48 | + override fun init() { |
| 49 | + controller.goal = KineticState(0.0) |
| 50 | + } |
| 51 | + |
| 52 | + override fun loop() { |
| 53 | + if (gamepad1.aWasPressed()) { |
| 54 | + controller.goal = KineticState(0.0, 2000.0) |
| 55 | + } else if (gamepad1.bWasPressed()) { |
| 56 | + controller.goal = KineticState(0.0, 0.0) |
| 57 | + } else if (gamepad1.xWasPressed()) { |
| 58 | + controller.goal = KineticState(0.0, 1000.0) |
| 59 | + } |
| 60 | + |
| 61 | + flywheelMotor.power = controller.calculate( |
| 62 | + flywheelMotor.currentPosition, |
| 63 | + flywheelMotor.velocity |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +== Java |
| 70 | + |
| 71 | +```java |
| 72 | +public class FlywheelExample extends OpMode { |
| 73 | + private DcMotorEx flywheelMotor; |
| 74 | + private ControlSystem controller; |
| 75 | + |
| 76 | + @Override |
| 77 | + public void init() { |
| 78 | + flywheelMotor = hardwareMap.get(DcMotorEx.class, "flywheel"); |
| 79 | + |
| 80 | + controller = ControlSystem.builder() |
| 81 | + .velPid(0.001, 0.0, 0.0) |
| 82 | + .build(); |
| 83 | + |
| 84 | + controller.goal = new KineticState(0.0); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void loop() { |
| 89 | + if (gamepad1.aWasPressed()) { |
| 90 | + controller.goal = new KineticState(0.0, 2000.0); |
| 91 | + } else if (gamepad1.bWasPressed()) { |
| 92 | + controller.goal = new KineticState(0.0, 0.0); |
| 93 | + } else if (gamepad1.xWasPressed()) { |
| 94 | + controller.goal = new KineticState(0.0, 1000.0); |
| 95 | + } |
| 96 | + |
| 97 | + flywheelMotor.setPower(controller.calculate( |
| 98 | + flywheelMotor.getCurrentPosition(), |
| 99 | + flywheelMotor.getVelocity() |
| 100 | + )); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +::: |
| 106 | + |
0 commit comments