Skip to content

Commit 8409c39

Browse files
authored
Merge pull request #7 from team5115/testing
2 parents 56a96c0 + dad8d82 commit 8409c39

24 files changed

+80793
-230
lines changed

Z_our_docs/sysid_drivetrain_updated_3-29-23.json

Lines changed: 80501 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/frc/team5115/Classes/Hardware/HardwareDrivetrain.java

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,37 @@
66
import com.revrobotics.RelativeEncoder;
77
import com.revrobotics.SparkMaxRelativeEncoder;
88
import edu.wpi.first.math.controller.*;
9+
import frc.team5115.Classes.Software.Arm;
910
import edu.wpi.first.math.MathUtil;
1011

1112
public class HardwareDrivetrain{
13+
// Competition feedforward values - 6 inch diameter on KITT comp robot with arm and ballasts
14+
private final double leftKs = 0.0378;
15+
private final double leftKv = 2.7479;
16+
private final double leftKa = 0.32825;
17+
18+
private final double rightKs = 0.0528;
19+
private final double rightKv = 2.8399;
20+
private final double rightKa = 0.26071;
21+
22+
// private final double combinedkP = 3.4349;
23+
// END of comp robot values
1224

13-
14-
15-
// Competition feedforward and feedback (pid) values
16-
// 6 inch diameter on COMP ROBOT WITH ARM and dumbells in back
17-
private final double leftKs = 0.17463;
18-
private final double leftKv = 2.8104;
19-
private final double leftKa = 0.82143;
25+
// Testbed feedforward values - 6 inch diameter on KATT testbed
26+
// private final double leftKs = 0.090949;
27+
// private final double leftKv = 2.783;
28+
// private final double leftKa = 0.16477;
2029

21-
private final double rightKs = 0.17463;
22-
private final double rightKv = 2.8104;
23-
private final double rightKa = 0.82143;
24-
25-
// private final double leftKp = 1.6455;
26-
// private final double rightKp = 1.6220;
27-
// private final double Ki = 0.0;
28-
// private final double Kd = 0.0;
29-
// // END of comp robot values
30-
31-
// Testbed feedforward and feedback (pid) values - 6 inch diameter on testbed
32-
/* private final double leftKs = 0.090949;
33-
private final double leftKv = 2.783;
34-
private final double leftKa = 0.16477;
30+
// private final double rightKs = 0.099706;
31+
// private final double rightKv = 2.8314;
32+
// private final double rightKa = 0.14565;
33+
// END of testbed values
3534

36-
private final double rightKs = 0.099706;
37-
private final double rightKv = 2.8314;
38-
private final double rightKa = 0.14565;
39-
*/
40-
41-
private final double leftKp = 0.0; // 3.7203 according to sysid
42-
private final double rightKp = 0.0; // 3.7203 according to sysid
43-
private final double Ki = 0.0;
44-
private final double Kd = 0.0;
35+
36+
private final double leftKp = 0.0;
37+
private final double rightKp = 0.0;
38+
private final double Ki = 0.1;
39+
private final double Kd = 0.1;
4540
// END of testbed values
4641

4742
private final SimpleMotorFeedforward leftFeedForward = new SimpleMotorFeedforward(leftKs, leftKv, leftKa);
@@ -57,7 +52,10 @@ public class HardwareDrivetrain{
5752
private final RelativeEncoder leftEncoder = frontLeft.getEncoder(SparkMaxRelativeEncoder.Type.kHallSensor, 42);
5853
private final RelativeEncoder rightEncoder = frontRight.getEncoder(SparkMaxRelativeEncoder.Type.kHallSensor, 42);
5954

60-
public HardwareDrivetrain(){
55+
private final Arm arm;
56+
57+
public HardwareDrivetrain(Arm arm){
58+
this.arm = arm;
6159
resetEncoders();
6260
frontRight.setInverted(true);
6361
}
@@ -141,21 +139,23 @@ public void PlugandVoltDrive(double frontLeftVoltage, double frontRightVoltage,
141139
* @param rightSpeed the speed for the right motors in meters per second
142140
*/
143141
public void plugandFFDrive(double leftSpeed, double rightSpeed) {
144-
145-
if(Math.abs(leftSpeed - (leftEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION))>1.5){
146-
//System.out.println("Left too fast");
147-
leftSpeed = (leftEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION) + 1.5*Math.signum((leftSpeed - (leftEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION)));
148-
}
142+
final double accelerationLimit = getAccelerationLimit(); // can't bother figuring the units, but it's not m/s^2
143+
final double currentLeftVelocity = getLeftVelocity();
144+
final double currentRightVelocity = getRightVelocity();
149145

150-
if(Math.abs(rightSpeed - (rightEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION))>1){
151-
//System.out.println("Right too fast");
152-
rightSpeed = (rightEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION) + 1.5*Math.signum((rightSpeed - (rightEncoder.getVelocity()*NEO_VELOCITY_CALIBRATION)));
153-
}
146+
// limit left acceleration
147+
if(Math.abs(leftSpeed - currentLeftVelocity) > accelerationLimit) {
148+
leftSpeed = currentLeftVelocity + accelerationLimit * Math.signum(leftSpeed - currentLeftVelocity);
149+
}
150+
// limit right acceleration
151+
if(Math.abs(rightSpeed - currentRightVelocity) > accelerationLimit) {
152+
rightSpeed = currentRightVelocity + accelerationLimit * Math.signum(rightSpeed - currentRightVelocity);
153+
}
154154

155155
double leftVoltage = leftFeedForward.calculate(leftSpeed);
156156
double rightVoltage = rightFeedForward.calculate(rightSpeed);
157-
leftVoltage += leftPID.calculate(leftEncoder.getVelocity() * NEO_VELOCITY_CALIBRATION, leftSpeed);
158-
rightVoltage += rightPID.calculate(rightEncoder.getVelocity() * NEO_VELOCITY_CALIBRATION, rightSpeed);
157+
leftVoltage += leftPID.calculate(currentLeftVelocity, leftSpeed);
158+
rightVoltage += rightPID.calculate(currentRightVelocity, rightSpeed);
159159
// Work on better PID Analyzer
160160

161161
leftVoltage = MathUtil.clamp(leftVoltage, -DRIVE_MOTOR_MAX_VOLTAGE, DRIVE_MOTOR_MAX_VOLTAGE);
@@ -167,8 +167,25 @@ public void plugandFFDrive(double leftSpeed, double rightSpeed) {
167167
frontRight.setVoltage(rightVoltage);
168168
}
169169

170+
private double getAccelerationLimit() {
171+
return 1.5;
172+
// final double angle = arm.getAngle();
173+
// final double length = (arm.getBottomWinchLength() + arm.getTopWinchLength()) / 2;
174+
// final double angleConstant = 0.01;
175+
// final double lengthConstant = 0.017;
176+
// return 1.0 / (((angle + 90.0) * angleConstant) + (length * lengthConstant) + 1.0) + 0.5;
177+
}
178+
170179
public void resetEncoders(){
171180
leftEncoder.setPosition(0);
172181
rightEncoder.setPosition(0);
173182
}
183+
184+
public double getLeftVelocity() {
185+
return leftEncoder.getVelocity() * NEO_VELOCITY_CALIBRATION;
186+
}
187+
188+
public double getRightVelocity() {
189+
return rightEncoder.getVelocity() * NEO_VELOCITY_CALIBRATION;
190+
}
174191
}

src/main/java/frc/team5115/Classes/Hardware/HardwareIntake.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void close(){
5959
private void setLights(boolean wantsCones) {
6060
coneLight.set(wantsCones);
6161
cubeLight.set(!wantsCones);
62-
System.out.println(wantsCones);
62+
//System.out.println(wantsCones);
6363
}
6464

6565
}

src/main/java/frc/team5115/Classes/Hardware/NAVx.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ public void resetNAVx() {
3333
* @return the yaw of the navx from the last reset, ranging from -180 to 180 degrees
3434
*/
3535
public double getYawDeg() {
36-
double angle = 0;
37-
if(ahrs.getYaw()- yawAtReset < 0){
38-
angle = 360+(ahrs.getYaw()- yawAtReset);
39-
}
40-
else{
41-
angle = (ahrs.getYaw()- yawAtReset);
42-
}
43-
44-
// if (angle > 180) {
45-
// return -360 + angle;
46-
// }
47-
return Math.abs(angle);
36+
return clampAngle(ahrs.getYaw() - yawAtReset);
4837
}
4938

5039
/**
@@ -57,11 +46,16 @@ public double getPitchDeg() {
5746
// for sideways mount
5847
// double angle = ahrs.getRoll() - pitchAtReset;
5948
// for flat mount
60-
double angle = -ahrs.getRoll() - pitchAtReset;
61-
if (angle > 180) {
62-
return -360 + angle;
63-
}
64-
return angle;
49+
return clampAngle(-ahrs.getRoll() - pitchAtReset);
50+
}
51+
52+
/**
53+
* Converts an angle into an equivalent angle in the range -180 to 180
54+
* @param angle the input angle to convert
55+
* @return the equivalent angle from -180 to 180
56+
*/
57+
public static double clampAngle(double angle) {
58+
return ((angle + 180.0) % 360.0) - 180.0;
6559
}
6660

6761
public double getYawRad() {

src/main/java/frc/team5115/Classes/Software/Arm.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,8 @@ public void moveTop(){
219219
public void moveBottom(){
220220
intake.setBottomWinch(-0.2);
221221
}
222+
223+
public double getAngle() {
224+
return angle;
225+
}
222226
}

src/main/java/frc/team5115/Classes/Software/Drivetrain.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public class Drivetrain extends SubsystemBase{
4747
public static final double bA = 10;
4848
public static final double MaxArea = 0.1;
4949

50-
public Drivetrain(PhotonVision photonVision) {
50+
public Drivetrain(PhotonVision photonVision, Arm arm) {
5151
this.photonVision = photonVision;
5252
throttle = new ThrottleControl(3, -3, 0.2);
53-
anglePID = new PIDController(0.013, 0.0001, 0.0015);
53+
anglePID = new PIDController(0.016, 0.0001, 0.0015);
5454

5555
movingPID = new PIDController(0.01, 0, 0);
56-
drivetrain = new HardwareDrivetrain();
56+
drivetrain = new HardwareDrivetrain(arm);
5757
ramseteController = new RamseteController();
5858
kinematics = new DifferentialDriveKinematics(TRACKING_WIDTH_METERS);
5959
navx = new NAVx();
@@ -68,14 +68,14 @@ public void init() {
6868
// );
6969

7070
poseEstimator = new DifferentialDrivePoseEstimator(
71-
kinematics, navx.getYawRotation2D(), getLeftDistance(), getRightDistance(), new Pose2d()
71+
kinematics, navx.getYawRotation2D(), getLeftDistance(), getRightDistance(), new Pose2d(), VecBuilder.fill(1, 1, 1), VecBuilder.fill(0, 0, 0)
7272
);
7373
System.out.println("Angle from navx" + navx.getYawDeg()
7474
);
7575
}
7676

7777
public void stop() {
78-
drivetrain.plugandFFDrive(0, 0);
78+
drivetrain.PlugandVoltDrive(0, 0, 0, 0);
7979
}
8080

8181
public double getLeftDistance(){
@@ -152,15 +152,15 @@ else if (Math.abs(y) > 1){
152152
return new double[] {x, y};
153153
}
154154

155-
@Deprecated
156-
public void TankDriveToAngle(double angleDegrees) {
155+
public boolean TankDriveToAngle(double angleDegrees) {
157156
double rotationDegrees = navx.getYawDeg();
158-
System.out.println(rotationDegrees);
159-
double turn = MathUtil.clamp(anglePID.calculate(rotationDegrees, angleDegrees), -1, 1);
157+
System.out.println("remaining degrees: " + (rotationDegrees-angleDegrees));
158+
double turn = MathUtil.clamp(anglePID.calculate(rotationDegrees, angleDegrees), -0.6, 0.6);
160159
leftSpeed = turn;
161160
rightSpeed = -turn;
162161

163162
drivetrain.plugandFFDrive(leftSpeed, rightSpeed);
163+
return Math.abs(rotationDegrees-angleDegrees)<17;
164164
}
165165

166166
public void TankDriveToTrajectoryState(Trajectory.State tState) {
@@ -180,11 +180,13 @@ public void UpdateOdometry() {
180180
Optional<EstimatedRobotPose> result = photonVision.getEstimatedGlobalPose();
181181
if (result.isPresent()) {
182182
EstimatedRobotPose camPose = result.get();
183+
System.out.println("its really working");
183184
poseEstimator.addVisionMeasurement(camPose.estimatedPose.toPose2d(), camPose.timestampSeconds);
184185
}
185186
}
186187

187188
public Pose2d getEstimatedPose() {
189+
UpdateOdometry();
188190
return poseEstimator.getEstimatedPosition();
189191
}
190192

@@ -199,14 +201,12 @@ public boolean UpdateMoving(double dist, double startLeftDist, double startRight
199201
return Math.abs(remainingLeftDistance) < tolerance || Math.abs(remainingRightDistance) < tolerance;
200202
}
201203

202-
public boolean UpdateTurning(double setpointAngle) {
203-
double currentAngle = navx.getYawDeg();
204-
double turn = MathUtil.clamp(anglePID.calculate(currentAngle, setpointAngle), -1, 1);
205-
//System.out.println("turning @ " + turn + " m/s");
206-
System.out.println(currentAngle);
207-
drivetrain.plugandFFDrive(turn, -turn);
208-
209-
return Math.abs(currentAngle-setpointAngle)<5;
204+
public boolean UpdateMovingWithVision(double dist, Pose2d pose, double speedMagnitude) {
205+
double realdist = pose.getTranslation().getDistance(getEstimatedPose().getTranslation());
206+
final double speed = speedMagnitude * Math.signum(dist);
207+
drivetrain.plugandFFDrive(speed, speed);
208+
final double tolerance = 0.1;
209+
return Math.abs(realdist-dist) < tolerance;
210210
}
211211

212212
public void resetNAVx(){

src/main/java/frc/team5115/Classes/Software/PhotonVision.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import frc.team5115.Constants.*;
1616

1717
public class PhotonVision extends SubsystemBase{
18-
// private PhotonCamera photonCameraL;
18+
private PhotonCamera photonCameraL;
1919
// private PhotonCamera photonCameraR;
20-
// private PhotonPoseEstimator photonPoseEstimatorL;
20+
private PhotonPoseEstimator photonPoseEstimatorL;
2121
// private PhotonPoseEstimator photonPoseEstimatorR;
2222

2323
public PhotonVision() {
24-
// photonCameraL = new PhotonCamera(VisionConstants.leftCameraName);
24+
photonCameraL = new PhotonCamera(VisionConstants.leftCameraName);
2525
// photonCameraR = new PhotonCamera(VisionConstants.rightCameraName);
2626
ArrayList<AprilTag> aprilTagList = new ArrayList<AprilTag>();
2727

@@ -42,15 +42,15 @@ public PhotonVision() {
4242
aprilTagList.add(GenerateAprilTag(5, -7.908830, +2.741613, 000));
4343

4444
AprilTagFieldLayout fieldLayout = new AprilTagFieldLayout(aprilTagList, FieldConstants.length, FieldConstants.width);
45-
// photonPoseEstimatorL = new PhotonPoseEstimator(fieldLayout, PoseStrategy.CLOSEST_TO_REFERENCE_POSE, photonCameraL, VisionConstants.robotToCamL);
45+
photonPoseEstimatorL = new PhotonPoseEstimator(fieldLayout, PoseStrategy.CLOSEST_TO_CAMERA_HEIGHT, photonCameraL, VisionConstants.robotToCamL);
4646
// photonPoseEstimatorR = new PhotonPoseEstimator(fieldLayout, PoseStrategy.CLOSEST_TO_REFERENCE_POSE, photonCameraR, VisionConstants.robotToCamR);
4747
}
4848

4949
public Optional<EstimatedRobotPose> getEstimatedGlobalPose() {
5050
// The teeam assignment of the first grid the robot looks at is the team assignment of the robot
5151
// otherwise if we cant see any april tags trust the team assignment inputted on shuffle board
5252
//Trusting the left camera more, no idea on how to use filters to get the most information out of both cameras 2-6-2022
53-
// if(photonPoseEstimatorL.update().isPresent()) return photonPoseEstimatorL.update();
53+
if(photonPoseEstimatorL.update().isPresent()) return photonPoseEstimatorL.update();
5454
// if(photonPoseEstimatorR.update().isPresent()) return photonPoseEstimatorR.update();
5555
return Optional.empty();
5656
}

0 commit comments

Comments
 (0)