This repository was archived by the owner on May 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBuddyBalanceSubsystem.java
More file actions
110 lines (95 loc) · 5.39 KB
/
BuddyBalanceSubsystem.java
File metadata and controls
110 lines (95 loc) · 5.39 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
// 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 frc.robot.subsystems;
import edu.wpi.first.networktables.GenericEntry;
import edu.wpi.first.util.datalog.DataLog;
import edu.wpi.first.util.datalog.DoubleLogEntry;
import edu.wpi.first.wpilibj.DataLogManager;
import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.hardware.MotorController;
import frc.robot.hardware.MotorController.MotorConfig;
import frc.robot.classes.TunableNumber;
import com.revrobotics.CANSparkMax;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.SparkMaxPIDController;
public class BuddyBalanceSubsystem extends SubsystemBase {
private static double kBalancedPosition; // Buddy balance PID reference point when lifting a robot and engaging charge station
private static double kDeployedPosition; // Buddy balance PID reference point when setting down a robot/initial position when deployed
// TODO: Make the reference point constants and default motor PID values final when they are done being tuned with TunableNumbers
private static final double kDefaultMotorP = 1e-6;
private static final double kDefaultMotorI = 0;
private static final double kDefaultMotorD = 1e-6;
private static final int deployServoID = 1;
private static double kServoDeployedPos = 1;
private TunableNumber refPointBalancedTuner;
private TunableNumber refPointDeployedTuner;
private TunableNumber refPointServoTuner;
private TunableNumber tunerNumRightP;
private TunableNumber tunerNumRightI;
private TunableNumber tunerNumRightD;
private TunableNumber tunerNumLeftP;
private TunableNumber tunerNumLeftI;
private TunableNumber tunerNumLeftD;
private Servo activateDeploy;
private CANSparkMax rightMotor;
private CANSparkMax leftMotor;
private SparkMaxPIDController rightPIDController;
private SparkMaxPIDController leftPIDController;
private RelativeEncoder rightEncoder;
private RelativeEncoder leftEncoder;
private boolean isDeployed = false;
private DataLog datalog = DataLogManager.getLog();
private DoubleLogEntry buddyBalancePosition = new DoubleLogEntry(datalog, "/buddybalance/position");
private ShuffleboardTab buddyBalanceTab = Shuffleboard.getTab("Buddy Balance"); // TODO: Replace buddy balance tab with whatever tab the position should be logged to
private GenericEntry positionEntry;
public BuddyBalanceSubsystem() {
rightMotor = MotorController.constructMotor(MotorConfig.BuddyBalanceRight);
leftMotor = MotorController.constructMotor(MotorConfig.BuddyBalanceLeft);
activateDeploy = new Servo(deployServoID);
rightPIDController = rightMotor.getPIDController();
leftPIDController = leftMotor.getPIDController();
rightPIDController.setP(kDefaultMotorP);
rightPIDController.setI(kDefaultMotorI);
rightPIDController.setD(kDefaultMotorD);
leftPIDController.setP(kDefaultMotorP);
leftPIDController.setI(kDefaultMotorI);
leftPIDController.setD(kDefaultMotorD);
rightEncoder = rightMotor.getEncoder();
leftEncoder = leftMotor.getEncoder();
tunerNumRightP = new TunableNumber("Buddy Balance Right Motor P", kDefaultMotorP, rightPIDController::setP);
tunerNumRightI = new TunableNumber("Buddy Balance Right Motor I", kDefaultMotorI, rightPIDController::setI);
tunerNumRightD = new TunableNumber("Buddy Balance Right Motor D", kDefaultMotorD, rightPIDController::setD);
tunerNumLeftP = new TunableNumber("Buddy Balance Left Motor P", kDefaultMotorP, leftPIDController::setP);
tunerNumLeftI = new TunableNumber("Buddy Balance Left Motor I", kDefaultMotorI, leftPIDController::setI);
tunerNumLeftD = new TunableNumber("Buddy Balance Left Motor D", kDefaultMotorD, leftPIDController::setD);
refPointBalancedTuner = new TunableNumber("Ref Point Balanced", 15, (a) -> {kBalancedPosition = a;});
refPointDeployedTuner = new TunableNumber("Ref Point Deployed", 0, (a) -> {kDeployedPosition = a;});
refPointServoTuner = new TunableNumber("Ref Point Servo", 1, (a) -> {kServoDeployedPos = a;});
positionEntry = buddyBalanceTab.add("Buddy Balance Position", 0).getEntry();
}
public boolean getIsDeployed() {
return isDeployed;
}
public void deployBuddyBalance() {
activateDeploy.set(kServoDeployedPos);
isDeployed = true;
}
public void retrieveBuddy() { // Used to pick up the buddy robot while the lift is already underneath it
MotorController.errorCheck(rightPIDController.setReference(kBalancedPosition, CANSparkMax.ControlType.kPosition));
MotorController.errorCheck(leftPIDController.setReference(kBalancedPosition, CANSparkMax.ControlType.kPosition));
}
public void releaseBuddy() { // Used to set down the robot
MotorController.errorCheck(rightPIDController.setReference(kDeployedPosition, CANSparkMax.ControlType.kPosition));
MotorController.errorCheck(leftPIDController.setReference(kDeployedPosition, CANSparkMax.ControlType.kPosition));
}
@Override
public void periodic() {
// This method will be called once per scheduler run
buddyBalancePosition.append(rightEncoder.getPosition()); // Logging the position of the buddy balance lift
buddyBalancePosition.append(leftEncoder.getPosition());
}
}