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