|
| 1 | +/*----------------------------------------------------------------------------*/ |
| 2 | +/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ |
| 3 | +/* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | +/* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | +/* the project. */ |
| 6 | +/*----------------------------------------------------------------------------*/ |
| 7 | + |
| 8 | +package frc.robot.commands |
| 9 | + |
| 10 | +import edu.wpi.first.wpilibj.GenericHID |
| 11 | +import edu.wpi.first.wpilibj.XboxController |
| 12 | +import edu.wpi.first.wpilibj2.command.CommandBase |
| 13 | +import frc.robot.subsystems.DrivetrainSubsystem |
| 14 | +import kotlin.math.abs |
| 15 | +import kotlin.math.pow |
| 16 | + |
| 17 | +/** |
| 18 | + * Drive the drivetrain based on a joystick |
| 19 | + */ |
| 20 | +class XboxDriveSpecial(val driveSubsystem: DrivetrainSubsystem, val controller: XboxController) : CommandBase() { |
| 21 | + init { |
| 22 | + addRequirements(driveSubsystem) |
| 23 | + } |
| 24 | + |
| 25 | + fun joystickToSpeed(joystickPosSigned: Double): Double { |
| 26 | + val gainSwitchThresh = 0.25 |
| 27 | + val gainSwitchPos = 0.25 |
| 28 | + |
| 29 | + val modPower = 4 |
| 30 | + val joystickPos = abs(joystickPosSigned) |
| 31 | + val joystickSign = (joystickPosSigned / joystickPos) |
| 32 | + return if (joystickPos < gainSwitchThresh) { |
| 33 | + joystickPosSigned * (gainSwitchPos / gainSwitchThresh) |
| 34 | + } else { |
| 35 | + ((joystickPos - gainSwitchThresh).pow(modPower) * joystickSign * 1.1) + (gainSwitchPos * joystickSign) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fun joystickToRadius(joystickPosSigned: Double) : Double { |
| 40 | + val gainSwitchThresh = 0.25 |
| 41 | + val gainSwitchPos = 0.25 |
| 42 | + |
| 43 | + val radiusMod = 2 // make this a constant after tuning is finished! |
| 44 | + val radiusPower = 2 // see above |
| 45 | + |
| 46 | + val joystickPos = abs(joystickPosSigned) |
| 47 | + val joystickPosSign = (joystickPosSigned / joystickPos) |
| 48 | + return if (joystickPos < gainSwitchThresh) { |
| 49 | + joystickPosSigned * (gainSwitchPos / gainSwitchThresh) // probably bad but eh |
| 50 | + } else { |
| 51 | + radiusMod * joystickPosSign * (1/(joystickPos - gainSwitchThresh).pow(radiusPower) - 1 + (gainSwitchPos * joystickPosSign)) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + override fun execute() { |
| 57 | + driveSubsystem.driveArcadeSpecial(-controller.getY(GenericHID.Hand.kLeft), joystickToRadius(controller.getX(GenericHID.Hand.kRight)), false) |
| 58 | + } |
| 59 | + |
| 60 | + override fun end(interrupted: Boolean) { |
| 61 | + driveSubsystem.driveArcadeSpecial(0.0, 0.0, true) |
| 62 | + } |
| 63 | + |
| 64 | + override fun isFinished() = false |
| 65 | +} |
0 commit comments