This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathDrivetrainSubsystem.java
More file actions
195 lines (152 loc) · 6.43 KB
/
Copy pathDrivetrainSubsystem.java
File metadata and controls
195 lines (152 loc) · 6.43 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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 com.kauailabs.navx.frc.AHRS;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.CANSparkMax.IdleMode;
import edu.wpi.first.math.geometry.Pose2d;
//import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.DifferentialDriveOdometry;
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds;
import edu.wpi.first.wpilibj.SPI;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.interfaces.Gyro;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Constants;
import frc.robot.Constants.DriveTrainConstants;
public class DrivetrainSubsystem extends SubsystemBase {
static CANSparkMax leftFrontMotor = new CANSparkMax(Constants.DriveTrainConstants.leftFrontCANID,
CANSparkMaxLowLevel.MotorType.kBrushless);
static CANSparkMax leftBackMotor = new CANSparkMax(Constants.DriveTrainConstants.leftBackCANID,
CANSparkMaxLowLevel.MotorType.kBrushless);
static CANSparkMax rightFrontMotor = new CANSparkMax(Constants.DriveTrainConstants.rightFrontCANID,
CANSparkMaxLowLevel.MotorType.kBrushless);
static CANSparkMax rightBackMotor = new CANSparkMax(Constants.DriveTrainConstants.rightBackCANID,
CANSparkMaxLowLevel.MotorType.kBrushless);
static RelativeEncoder leftEncoder = leftFrontMotor.getEncoder();
static RelativeEncoder rightEncoder = rightFrontMotor.getEncoder();
static MotorControllerGroup leftControllerGroup = new MotorControllerGroup(leftFrontMotor, leftBackMotor);
static MotorControllerGroup rightControllerGroup = new MotorControllerGroup(rightFrontMotor, rightBackMotor);
static DifferentialDrive differentialDrive = new DifferentialDrive(leftControllerGroup, rightControllerGroup);
public final static Gyro navX = new AHRS(SPI.Port.kMXP);
private final DifferentialDriveOdometry m_odometry;
/** Creates a new ExampleSubsystem. */
public DrivetrainSubsystem() {
leftFrontMotor.restoreFactoryDefaults();
leftBackMotor.restoreFactoryDefaults();
rightFrontMotor.restoreFactoryDefaults();
rightBackMotor.restoreFactoryDefaults();
leftEncoder.setPosition(0);
rightEncoder.setPosition(0);
// zeros out the motors and encoders - J
rightEncoder.setPositionConversionFactor(DriveTrainConstants.kLinearDistanceConversionFactor);
leftEncoder.setPositionConversionFactor(DriveTrainConstants.kLinearDistanceConversionFactor);
rightEncoder.setVelocityConversionFactor(DriveTrainConstants.kLinearDistanceConversionFactor / 60);
leftEncoder.setVelocityConversionFactor(DriveTrainConstants.kLinearDistanceConversionFactor / 60);
leftBackMotor.follow(leftFrontMotor);
rightBackMotor.follow(rightFrontMotor);
// gets the back motors to do the same thing as the front motors - J
rightControllerGroup.setInverted(false);
leftControllerGroup.setInverted(true);
// inverts the right motors so both sides spin in the same direction - J
navX.reset();
navX.calibrate();
resetEncoders();
m_odometry = new DifferentialDriveOdometry(navX.getRotation2d(), 0, 0);
m_odometry.resetPosition(navX.getRotation2d(), 0, 0, new Pose2d());
setBreakMode();
}
public void setBreakMode() {
leftBackMotor.setIdleMode(IdleMode.kBrake);
leftFrontMotor.setIdleMode(IdleMode.kBrake);
rightFrontMotor.setIdleMode(IdleMode.kBrake);
rightBackMotor.setIdleMode(IdleMode.kBrake);
}
public static void setCoastMode() {
leftBackMotor.setIdleMode(IdleMode.kCoast);
leftFrontMotor.setIdleMode(IdleMode.kCoast);
rightFrontMotor.setIdleMode(IdleMode.kCoast);
rightBackMotor.setIdleMode(IdleMode.kCoast);
}
public void resetEncoders() {
rightEncoder.setPosition(0);
leftEncoder.setPosition(0);
}
public static void arcadeDrive(double fwd, double rot) {
differentialDrive.arcadeDrive(fwd, rot);
}
public static void TankDrive(double fwd, double rot) {
double left = fwd - rot;
double right = fwd + rot;
differentialDrive.tankDrive(left, right);
}
public static double getRightEncoderPosition() {
return rightEncoder.getPosition();
}
public static double getLeftEncoderPosition() {
return -leftEncoder.getPosition();
}
public double getRightEncoderVelocity() {
return rightEncoder.getVelocity();
}
public double getLeftEncoderVelocity() {
return -leftEncoder.getVelocity();
}
public double getTurnRate() {
return -navX.getRate();
}
public static double getHeading() {
return navX.getRotation2d().getDegrees();
}
public Pose2d getPose() {
return m_odometry.getPoseMeters();
}
public void resetOdometry(Pose2d pose) {
resetEncoders();
m_odometry.resetPosition(navX.getRotation2d(), 0, 0, pose);
}
public DifferentialDriveWheelSpeeds getWheelSpeeds() {
return new DifferentialDriveWheelSpeeds(getLeftEncoderVelocity(), getRightEncoderVelocity());
}
public void tankDriveVolts(double leftVolts, double rightVolts) {
leftControllerGroup.setVoltage(leftVolts);
rightControllerGroup.setVoltage(rightVolts);
differentialDrive.feed();
}
public static double getAverageEncoderDistance() {
return ((getLeftEncoderPosition() + getRightEncoderPosition()) / 2.0);
}
public RelativeEncoder getLeftEncoder() {
return leftEncoder;
}
public RelativeEncoder getRightEncoder() {
return rightEncoder;
}
public void setMaxOutput(double maxOutput) {
differentialDrive.setMaxOutput(maxOutput);
}
public static void zeroHeading() {
navX.calibrate();
navX.reset();
}
public Gyro getGyro() {
return navX;
}
public DifferentialDriveOdometry getOdometry() {
return m_odometry;
}
@Override
public void periodic() {
// This method will be called once per scheduler run
m_odometry.update(navX.getRotation2d(), leftEncoder.getPosition(),
rightEncoder.getPosition());
SmartDashboard.putNumber("Left encoder value meters", getLeftEncoderPosition());
SmartDashboard.putNumber("RIGHT encoder value meters", getRightEncoderPosition());
SmartDashboard.putNumber("Gyro heading", getHeading());
}
}