-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceControlCommand.java
More file actions
113 lines (85 loc) · 4.13 KB
/
BalanceControlCommand.java
File metadata and controls
113 lines (85 loc) · 4.13 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package frc.robot.commands;
import frc.robot.Robot;
import frc.robot.Constants.BalanceConstants;
import frc.robot.subsystems.SwerveDriveSubsystem;
import frc.robot.Constants.DriveConstants;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.subsystems.SwerveDriveSubsystem;
public class BalanceControlCommand extends CommandBase {
private double error;
private double currentAngle;
private double drivePower;
private final SwerveDriveSubsystem swerveSubsystem;
private int counter;
private boolean m_isFinished;
public BalanceControlCommand(SwerveDriveSubsystem swerveSubsystem){
System.out.println("Started Balance");
this.swerveSubsystem = swerveSubsystem;
this.m_isFinished = false;
addRequirements(swerveSubsystem);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
// Uncomment the line below this to simulate the gyroscope axis with a controller joystick
// Double currentAngle = -1 * Robot.controller.getRawAxis(Constants.LEFT_VERTICAL_JOYSTICK_AXIS) * 45;
this.currentAngle = swerveSubsystem.getRoll();
error = BalanceConstants.kBalancingControlGoalDegrees - currentAngle;
drivePower = Math.min(BalanceConstants.kBalancingControlDriveP * error, 1);
if (Math.abs(error) < BalanceConstants.kBalancingControlTresholdDegrees) {
counter += 1;
}
if (counter > 100) {
this.m_isFinished = true;
}
// Our robot needed an extra push to drive up in reverse, probably due to weight imbalances
if (drivePower < 0) {
drivePower *= BalanceConstants.kBalancingControlBackwardsPowerMultiplier;
}
// Limit the max power
if (Math.abs(drivePower) > 0.5) {
drivePower = Math.copySign(0.5, drivePower);
}
// Construct desired chassis speeds
ChassisSpeeds chassisSpeeds;
chassisSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds(
drivePower, 0, 0, swerveSubsystem.getRotation2d());
SwerveModuleState[] moduleStates = DriveConstants.kDriveKinematics.toSwerveModuleStates(chassisSpeeds);
// Output each module states to wheels
this.swerveSubsystem.setModuleStates(moduleStates);
SmartDashboard.putNumber("Current Angle: ", currentAngle);
SmartDashboard.putNumber("Error ", error);
SmartDashboard.putNumber("Drive Power: ", drivePower);
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
System.out.println("COMMAND FINISHED");
ChassisSpeeds chassisSpeedsStopForward;
ChassisSpeeds chassisSpeedsStopSide;
chassisSpeedsStopForward = ChassisSpeeds.fromFieldRelativeSpeeds(0, 0.1, 0, swerveSubsystem.getRotation2d());
chassisSpeedsStopSide = ChassisSpeeds.fromFieldRelativeSpeeds(0.1, 0, 0, swerveSubsystem.getRotation2d());
SwerveModuleState[] moduleStatesForward = DriveConstants.kDriveKinematics.toSwerveModuleStates(chassisSpeedsStopForward);
SwerveModuleState[] moduleStatesSide = DriveConstants.kDriveKinematics.toSwerveModuleStates(chassisSpeedsStopSide);
SwerveModuleState[] combinedStates = new SwerveModuleState[] {
moduleStatesForward[0],
moduleStatesSide[1],
moduleStatesSide[2],
moduleStatesForward[3]
};
swerveSubsystem.setModuleStates(combinedStates);
//swerveSubsystem.stopModules();
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
// End the command when we are within the specified threshold of being 'flat' (gyroscope pitch of 0 degrees)
return this.m_isFinished;
}
}