generated from DeepBlueRobotics/EmptyProject2024
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArmTeleop.java
83 lines (65 loc) · 2.61 KB
/
ArmTeleop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.carlmontrobotics.commands;
import org.carlmontrobotics.Constants;
import static org.carlmontrobotics.Constants.Arm.*;
import java.util.function.DoubleSupplier;
import org.carlmontrobotics.subsystems.Arm;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
public class ArmTeleop extends Command {
private final DoubleSupplier joystick;
private final Arm armSubsystem;
private double lastTime = 0;
private TrapezoidProfile.State goalState;
/** Creates a new ArmTeleop. */
public ArmTeleop(Arm armSubsystem, DoubleSupplier joystickSupplier) {
// Use addRequirements() here to declare subsystem dependencies.
addRequirements(this.armSubsystem = armSubsystem);
joystick = joystickSupplier;
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
goalState = new TrapezoidProfile.State(armSubsystem.getArmPos(), armSubsystem.getArmVel());
lastTime = Timer.getFPGATimestamp();
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
// use trapazoid math and controllerMoveArm method from arm subsytem to apply
// voltage to the motor
double speeds = getRequestedSpeeds();
double currTime = Timer.getFPGATimestamp();
double deltaT = currTime - lastTime;
double goalArmRad = goalState.position + speeds * deltaT;
goalArmRad = MathUtil.clamp(goalArmRad, UPPER_ANGLE_LIMIT_RAD, LOWER_ANGLE_LIMIT_RAD);
goalArmRad = MathUtil.clamp(goalArmRad, armSubsystem.getArmPos() + ARM_TELEOP_MAX_GOAL_DIFF_FROM_CURRENT_RAD,
armSubsystem.getArmPos() - ARM_TELEOP_MAX_GOAL_DIFF_FROM_CURRENT_RAD);
goalState = new TrapezoidProfile.State(goalArmRad, 0);
armSubsystem.setArmTarget(goalState.position);
lastTime = currTime;
}
public double getRequestedSpeeds() {
double rawArmVel;
if (Math.abs(joystick.getAsDouble()) <= Constants.OI.JOY_THRESH) {
rawArmVel = 0.0;
} else {
rawArmVel = MAX_FF_VEL_RAD_P_S * joystick.getAsDouble();
}
return rawArmVel;
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}