|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 NextFTC Team |
| 3 | + * |
| 4 | + * Use of this source code is governed by an BSD-3-clause |
| 5 | + * license that can be found in the LICENSE.md file at the root of this repository or at |
| 6 | + * https://opensource.org/license/bsd-3-clause. |
| 7 | + */ |
| 8 | + |
| 9 | +package dev.nextftc.robot.drive |
| 10 | + |
| 11 | +import com.pedropathing.ivy.Command |
| 12 | +import com.pedropathing.ivy.commands.Commands |
| 13 | +import com.qualcomm.robotcore.hardware.Gamepad |
| 14 | +import dev.nextftc.control.drive.DriveInput |
| 15 | +import dev.nextftc.control.drive.MecanumKinematics |
| 16 | +import dev.nextftc.control.drive.TankKinematics |
| 17 | +import dev.nextftc.hardware.actuators.NextMotor |
| 18 | +import java.util.function.Supplier |
| 19 | +import kotlin.math.cos |
| 20 | +import kotlin.math.sin |
| 21 | + |
| 22 | +/** Multiplier applied to all drive input; re-read live each loop so it can be adjusted mid-match (e.g. slow-mode). */ |
| 23 | +var scalar: Double = 1.0 |
| 24 | + set(value) { |
| 25 | + field = value |
| 26 | + } |
| 27 | + |
| 28 | +/** Command that drives a mecanum drivetrain from [gamepad]'s sticks. */ |
| 29 | +fun mecanumDrive( |
| 30 | + frontLeft: NextMotor, |
| 31 | + frontRight: NextMotor, |
| 32 | + backLeft: NextMotor, |
| 33 | + backRight: NextMotor, |
| 34 | + gamepad: Gamepad, |
| 35 | + kinematics: MecanumKinematics = MecanumKinematics(), |
| 36 | +): Command = Commands.infinite { |
| 37 | + val powers = kinematics.calculate( |
| 38 | + DriveInput( |
| 39 | + y = gamepad.left_stick_x.toDouble() * scalar, |
| 40 | + x = -gamepad.left_stick_y.toDouble() * scalar, |
| 41 | + rx = gamepad.right_stick_x.toDouble() * scalar, |
| 42 | + ), |
| 43 | + ) |
| 44 | + frontLeft.setThrottle(powers.frontLeft) |
| 45 | + frontRight.setThrottle(powers.frontRight) |
| 46 | + backLeft.setThrottle(powers.backLeft) |
| 47 | + backRight.setThrottle(powers.backRight) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Command that drives a mecanum drivetrain from [gamepad]'s sticks, field-centric. |
| 52 | + * |
| 53 | + * Rotates stick input by [heading] (radians, field-relative) so "forward" on the |
| 54 | + * stick always moves the robot away from the driver, regardless of robot orientation. |
| 55 | + * |
| 56 | + * @param heading Supplies the robot's current field-relative heading in radians — |
| 57 | + * e.g. `{ follower.pose.heading }` from Pedro Pathing, or an IMU reading zeroed |
| 58 | + * to match the robot's starting orientation on the field. |
| 59 | + */ |
| 60 | +fun mecanumDriveFieldCentric( |
| 61 | + frontLeft: NextMotor, |
| 62 | + frontRight: NextMotor, |
| 63 | + backLeft: NextMotor, |
| 64 | + backRight: NextMotor, |
| 65 | + gamepad: Gamepad, |
| 66 | + heading: Supplier<Double>, |
| 67 | + kinematics: MecanumKinematics = MecanumKinematics(), |
| 68 | +): Command = Commands.infinite { |
| 69 | + val rawY = gamepad.left_stick_x.toDouble() |
| 70 | + val rawX = -gamepad.left_stick_y.toDouble() |
| 71 | + val h = heading.get() |
| 72 | + val c = cos(-h) |
| 73 | + val sinH = sin(-h) |
| 74 | + |
| 75 | + val powers = kinematics.calculate( |
| 76 | + DriveInput( |
| 77 | + y = (rawX * c - rawY * sinH) * scalar, |
| 78 | + x = (rawX * sinH + rawY * c) * scalar, |
| 79 | + rx = gamepad.right_stick_x.toDouble() * scalar, |
| 80 | + ), |
| 81 | + ) |
| 82 | + frontLeft.setThrottle(powers.frontLeft) |
| 83 | + frontRight.setThrottle(powers.frontRight) |
| 84 | + backLeft.setThrottle(powers.backLeft) |
| 85 | + backRight.setThrottle(powers.backRight) |
| 86 | +} |
| 87 | + |
| 88 | +/** Command that drives a tank drivetrain from [gamepad]'s sticks. */ |
| 89 | +fun tankDrive( |
| 90 | + frontLeft: NextMotor, |
| 91 | + frontRight: NextMotor, |
| 92 | + backLeft: NextMotor, |
| 93 | + backRight: NextMotor, |
| 94 | + gamepad: Gamepad, |
| 95 | + kinematics: TankKinematics = TankKinematics(), |
| 96 | +): Command = Commands.infinite { |
| 97 | + val powers = kinematics.calculate( |
| 98 | + DriveInput( |
| 99 | + y = 0.0, |
| 100 | + x = -gamepad.left_stick_y.toDouble() * scalar, |
| 101 | + rx = gamepad.right_stick_x.toDouble() * scalar, |
| 102 | + ), |
| 103 | + ) |
| 104 | + frontLeft.setThrottle(powers.left) |
| 105 | + backLeft.setThrottle(powers.left) |
| 106 | + frontRight.setThrottle(powers.right) |
| 107 | + backRight.setThrottle(powers.right) |
| 108 | +} |
0 commit comments