PID no work

This commit is contained in:
Michael Mikovsky
2024-06-18 11:52:58 -06:00
parent 5b0132a259
commit d19ff47543
9 changed files with 220 additions and 437 deletions
@@ -1,165 +1,165 @@
// 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.
// // 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 frc4388.robot.subsystems;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
// import java.io.IOException;
// import java.util.ArrayList;
// import java.util.List;
// import java.util.Optional;
import org.photonvision.EstimatedRobotPose;
import org.photonvision.PhotonCamera;
import org.photonvision.PhotonPoseEstimator;
import org.photonvision.PhotonPoseEstimator.PoseStrategy;
import org.photonvision.common.hardware.VisionLEDMode;
import org.photonvision.targeting.PhotonPipelineResult;
import org.photonvision.targeting.PhotonTrackedTarget;
import org.photonvision.targeting.TargetCorner;
// // import org.photonvision.EstimatedRobotPose;
// // import org.photonvision.PhotonCamera;
// // import org.photonvision.PhotonPoseEstimator;
// // import org.photonvision.PhotonPoseEstimator.PoseStrategy;
// // import org.photonvision.common.hardware.VisionLEDMode;
// // import org.photonvision.targeting.PhotonPipelineResult;
// // import org.photonvision.targeting.PhotonTrackedTarget;
// // import org.photonvision.targeting.TargetCorner;
import edu.wpi.first.apriltag.AprilTag;
import edu.wpi.first.apriltag.AprilTagFieldLayout;
import edu.wpi.first.apriltag.AprilTagFields;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc4388.robot.Constants.VisionConstants;
// import edu.wpi.first.apriltag.AprilTag;
// import edu.wpi.first.apriltag.AprilTagFieldLayout;
// import edu.wpi.first.apriltag.AprilTagFields;
// import edu.wpi.first.math.geometry.Pose2d;
// import edu.wpi.first.wpilibj.DriverStation;
// import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
// import edu.wpi.first.wpilibj2.command.SubsystemBase;
// import frc4388.robot.Constants.VisionConstants;
public class Limelight extends SubsystemBase {
// public class Limelight extends SubsystemBase {
private PhotonCamera cam;
private PhotonPoseEstimator photonPoseEstimator;
// // private PhotonCamera cam;
// // private PhotonPoseEstimator photonPoseEstimator;
private boolean lightOn;
// private boolean lightOn;
/** Creates a new Limelight. */
public Limelight() {
cam = new PhotonCamera(VisionConstants.NAME);
cam.setDriverMode(false);
}
// /** Creates a new Limelight. */
// public Limelight() {
// // cam = new PhotonCamera(VisionConstants.NAME);
// // cam.setDriverMode(false);
// }
public void setLEDs(boolean on) {
lightOn = on;
cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
}
// public void setLEDs(boolean on) {
// lightOn = on;
// // cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
// }
public void toggleLEDs() {
lightOn = !lightOn;
cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
}
// public void toggleLEDs() {
// lightOn = !lightOn;
// // cam.setLED(lightOn ? VisionLEDMode.kOn : VisionLEDMode.kOff);
// }
public void setDriverMode(boolean driverMode) {
cam.setDriverMode(driverMode);
}
// public void setDriverMode(boolean driverMode) {
// // cam.setDriverMode(driverMode);
// }
public void setToLimePipeline() {
cam.setPipelineIndex(1);
setLEDs(true);
}
// public void setToLimePipeline() {
// // cam.setPipelineIndex(1);
// setLEDs(true);
// }
public void setToAprilPipeline() {
cam.setPipelineIndex(0);
setLEDs(false);
}
// public void setToAprilPipeline() {
// // cam.setPipelineIndex(0);
// setLEDs(false);
// }
public PhotonTrackedTarget getAprilPoint() {
if (!cam.isConnected()) return null;
// public PhotonTrackedTarget getAprilPoint() {
// // if (!cam.isConnected()) return null;
PhotonPipelineResult result = cam.getLatestResult();
// // PhotonPipelineResult result = cam.getLatestResult();
if (!result.hasTargets()) return null;
// // if (!result.hasTargets()) return null;
return result.getBestTarget();
}
// return result.getBestTarget();
// }
private List<TargetCorner> getAprilCorners() {
if (!cam.isConnected()) return null;
// private List<TargetCorner> getAprilCorners() {
// if (!cam.isConnected()) return null;
PhotonPipelineResult result = cam.getLatestResult();
// PhotonPipelineResult result = cam.getLatestResult();
if (!result.hasTargets()) return null;
// if (!result.hasTargets()) return null;
return result.getBestTarget().getDetectedCorners();
}
// return result.getBestTarget().getDetectedCorners();
// }
public double getAprilSkew() {
List<TargetCorner> corners = getAprilCorners();
ArrayList<TargetCorner> bottomSide = getAprilBottomSide(corners);
// public double getAprilSkew() {
// List<TargetCorner> corners = getAprilCorners();
// ArrayList<TargetCorner> bottomSide = getAprilBottomSide(corners);
if (bottomSide == null) return 0;
// if (bottomSide == null) return 0;
TargetCorner bottomRight = bottomSide.get(0).x > bottomSide.get(1).x ? bottomSide.get(0) : bottomSide.get(1);
TargetCorner bottomLeft = bottomRight.x == bottomSide.get(0).x ? bottomSide.get(1) : bottomSide.get(0);
// TargetCorner bottomRight = bottomSide.get(0).x > bottomSide.get(1).x ? bottomSide.get(0) : bottomSide.get(1);
// TargetCorner bottomLeft = bottomRight.x == bottomSide.get(0).x ? bottomSide.get(1) : bottomSide.get(0);
return bottomLeft.y - bottomRight.y;
}
// return bottomLeft.y - bottomRight.y;
// }
private ArrayList<TargetCorner> getAprilBottomSide(List<TargetCorner> box) {
if (box == null) return null;
// private ArrayList<TargetCorner> getAprilBottomSide(List<TargetCorner> box) {
// if (box == null) return null;
ArrayList<TargetCorner> bottomSide = new ArrayList<>();
// ArrayList<TargetCorner> bottomSide = new ArrayList<>();
TargetCorner l1 = new TargetCorner(-1, -1);
TargetCorner l2 = new TargetCorner(-1, -1);
// TargetCorner l1 = new TargetCorner(-1, -1);
// TargetCorner l2 = new TargetCorner(-1, -1);
for (TargetCorner c : box) {
if (c.y > l1.y) l1 = c;
}
// for (TargetCorner c : box) {
// if (c.y > l1.y) l1 = c;
// }
for (TargetCorner c : box) {
if (c.y == l1.y) continue;
if (c.y > l2.y) l2 = c;
}
// for (TargetCorner c : box) {
// if (c.y == l1.y) continue;
// if (c.y > l2.y) l2 = c;
// }
bottomSide.add(l1);
bottomSide.add(l2);
// bottomSide.add(l1);
// bottomSide.add(l2);
return bottomSide;
}
// return bottomSide;
// }
public double getDistanceToApril() {
PhotonTrackedTarget aprilPoint = getAprilPoint();
if (aprilPoint == null) return -1;
// public double getDistanceToApril() {
// PhotonTrackedTarget aprilPoint = getAprilPoint();
// if (aprilPoint == null) return -1;
double aprilHeight = VisionConstants.APRIL_HEIGHT - VisionConstants.LIME_HEIGHT;
double theta = 35.0 + aprilPoint.getPitch();
// double aprilHeight = VisionConstants.APRIL_HEIGHT - VisionConstants.LIME_HEIGHT;
// double theta = 35.0 + aprilPoint.getPitch();
double distanceToApril = aprilHeight / Math.tan(Math.toRadians(theta));
return distanceToApril;
}
// double distanceToApril = aprilHeight / Math.tan(Math.toRadians(theta));
// return distanceToApril;
// }
public PhotonTrackedTarget getLowestTape() {
if (!cam.isConnected()) return null;
// public PhotonTrackedTarget getLowestTape() {
// if (!cam.isConnected()) return null;
PhotonPipelineResult result = cam.getLatestResult();
// PhotonPipelineResult result = cam.getLatestResult();
if (!result.hasTargets()) return null;
// if (!result.hasTargets()) return null;
ArrayList<PhotonTrackedTarget> points = (ArrayList<PhotonTrackedTarget>) result.getTargets();
// ArrayList<PhotonTrackedTarget> points = (ArrayList<PhotonTrackedTarget>) result.getTargets();
PhotonTrackedTarget lowest = points.get(0);
for (PhotonTrackedTarget point : points) {
if (point.getPitch() < lowest.getPitch()) {
lowest = point;
}
}
// PhotonTrackedTarget lowest = points.get(0);
// for (PhotonTrackedTarget point : points) {
// if (point.getPitch() < lowest.getPitch()) {
// lowest = point;
// }
// }
return lowest;
}
// return lowest;
// }
public double getDistanceToTape() {
PhotonTrackedTarget tapePoint = getLowestTape();
if (tapePoint == null) return -1;
// public double getDistanceToTape() {
// PhotonTrackedTarget tapePoint = getLowestTape();
// if (tapePoint == null) return -1;
double tapeHeight = VisionConstants.MID_TAPE_HEIGHT - VisionConstants.LIME_HEIGHT;
double theta = 35.0 + tapePoint.getPitch();
// double tapeHeight = VisionConstants.MID_TAPE_HEIGHT - VisionConstants.LIME_HEIGHT;
// double theta = 35.0 + tapePoint.getPitch();
double distanceToTape = tapeHeight / Math.tan(Math.toRadians(theta));
return distanceToTape;
}
// double distanceToTape = tapeHeight / Math.tan(Math.toRadians(theta));
// return distanceToTape;
// }
@Override
public void periodic() {}
}
// @Override
// public void periodic() {}
// }
@@ -53,9 +53,15 @@ public class SwerveDrive extends SubsystemBase {
this.modules = new SwerveModule[] {this.leftFront, this.rightFront, this.leftBack, this.rightBack};
}
public void oneModuleTest(SwerveModule module, Translation2d leftStick, Translation2d rightStick){
double ang = (Math.atan2(rightStick.getY(), rightStick.getX()) / (Math.PI*2));
System.out.println(ang);
module.go(ang);
}
boolean stopped = false;
public void driveWithInput(Translation2d leftStick, Translation2d rightStick, boolean fieldRelative) {
rightFront.go(leftStick);
// if (fieldRelative) {
// double rot = 0;
@@ -14,12 +14,16 @@ import com.ctre.phoenix.motorcontrol.RemoteSensorSource;
// import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
// import com.ctre.phoenix.sensors.CANCoder;
// import com.ctre.phoenix.sensors.SensorInitializationStrategy;
import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.Utils;
import com.ctre.phoenix6.configs.FeedbackConfigs;
import com.ctre.phoenix6.configs.Slot0Configs;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.controls.DutyCycleOut;
import com.ctre.phoenix6.controls.Follower;
import com.ctre.phoenix6.controls.PositionVoltage;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.signals.FeedbackSensorSourceValue;
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.hardware.CANcoder;
@@ -38,6 +42,8 @@ public class SwerveModule extends SubsystemBase {
private TalonFX driveMotor;
private TalonFX angleMotor;
private CANcoder encoder;
// private final StatusSignal<Double> cc_pos;
// private final StatusSignal<Double> cc_vel;
// private int selfid;
// private ConfigurableDouble offsetGetter;
private static int swerveId = 0;
@@ -49,19 +55,42 @@ public class SwerveModule extends SubsystemBase {
this.driveMotor = driveMotor;
this.angleMotor = angleMotor;
this.encoder = encoder;
// TalonFXConfiguration pidConfigs = new TalonFXConfiguration();
// pidConfigs.Slot0.kP = 2.4; // An error of 1 rotation results in 2.4 V output
// pidConfigs.Slot0.kI = 0; // no output for integrated error
// pidConfigs.Slot0.kD = 0.1; // A velocity of 1 rps results in 0.1 V output
// angleMotor.getConfigurator().apply(slot0Configs);
// var fx_cfg = new FeedbackConfigs();
// fx_cfg.FeedbackRemoteSensorID = encoder.getDeviceID();
// fx_cfg.FeedbackSensorSource = FeedbackSensorSourceValue.FusedCANcoder;
// angleMotor.getConfigurator().apply(fx_cfg);
// // this.offsetGetter = new ConfigurableDouble("Swerve id " + swerveId, offset);
// this.selfid = swerveId;
// swerveId++;
// TalonFXConfiguration angleConfig = new TalonFXConfiguration();
// angleConfig.slot0.kP = swerveGains.kP;
// angleConfig.slot0.kI = swerveGains.kI;
// angleConfig.slot0.kD = swerveGains.kD;
TalonFXConfiguration angleConfig = new TalonFXConfiguration();
// angleConfig.Slot0.kP = swerveGains.kP;
// angleConfig.Slot0.kI = swerveGains.kI;
// angleConfig.Slot0.kD = swerveGains.kD;
angleConfig.Slot0.kP = 2.4; // An error of 1 rotation results in 2.4 V output
angleConfig.Slot0.kI = 0; // no output for integrated error
angleConfig.Slot0.kD = 0.1; // A velocity of 1 rps results in 0.1 V output
// // use the CANcoder as the remote sensor for the primary TalonFX PID
// angleConfig.remoteFilter0.remoteSensorDeviceID = encoder.getDeviceID();
// angleConfig.remoteFilter0.remoteSensorSource = RemoteSensorSource.CANCoder;
// angleConfig.primaryPID.selectedFeedbackSensor = FeedbackDevice.RemoteSensor0;
// angleMotor.configAllSettings(angleConfig);
// var fx_cfg = new FeedbackConfigs();
angleConfig.Feedback.FeedbackRemoteSensorID = encoder.getDeviceID();
angleConfig.Feedback.FeedbackSensorSource = FeedbackSensorSourceValue.RemoteCANcoder;
// angleConfig.Feedback. = FeedbackDevice.RemoteSensor0;
// angleConfig.Feedback = fx_cfg;
angleMotor.getConfigurator().apply(angleConfig);
// use the CANcoder as the remote sensor for the primary TalonFX PID
// angleConfig.Fee = encoder.getDeviceID();
// angleConfig.FeedbackSensorSource = RemoteSensorSource.CANCoder;
// angleConfig.primaryPID.selectedFeedbackSensor = FeedbackDevice.RemoteSensor0;
// angleMotor.getConfigurator().apply(angleConfig);
// //encoder.configSensorInitializationStrategy(SensorInitializationStrategy.BootToAbsolutePosition);
// reset(0);
@@ -71,9 +100,16 @@ public class SwerveModule extends SubsystemBase {
// driveMotor.config_kP(0, 0.2);
}
public void go(Translation2d leftStick){
System.out.println(leftStick.getY());
driveMotor.set(leftStick.getY());
public void go(double ang){
double curang = this.encoder.getAbsolutePosition().getValue();
System.out.println(ang-curang);
final PositionVoltage m_request = new PositionVoltage(0).withSlot(0);
// set position to 10 rotations
angleMotor.setControl(m_request.withPosition(ang));
// System.out.println(this.cc_pos.getValue());
}
@Override
@@ -166,26 +202,26 @@ public class SwerveModule extends SubsystemBase {
* Set the speed and rotation of the SwerveModule from a SwerveModuleState object
* @param desiredState a SwerveModuleState representing the desired new state of the module
// */
// public void setDesiredState(SwerveModuleState desiredState) {
// Rotation2d currentRotation = this.getAngle();
public void setDesiredState(SwerveModuleState desiredState) {
// Rotation2d currentRotation = this.getAngle();
// SwerveModuleState state = SwerveModuleState.optimize(desiredState, currentRotation);
// SwerveModuleState state = SwerveModuleState.optimize(desiredState, currentRotation);
// // calculate the difference between our current rotational position and our new rotational position
// Rotation2d rotationDelta = state.angle.minus(currentRotation);
// // calculate the difference between our current rotational position and our new rotational position
// Rotation2d rotationDelta = state.angle.minus(currentRotation);
// // calculate the new absolute position of the SwerveModule based on the difference in rotation
// double deltaTicks = (rotationDelta.getDegrees() / 360.) * SwerveDriveConstants.Conversions.CANCODER_TICKS_PER_ROTATION;
// // calculate the new absolute position of the SwerveModule based on the difference in rotation
// double deltaTicks = (rotationDelta.getDegrees() / 360.) * SwerveDriveConstants.Conversions.CANCODER_TICKS_PER_ROTATION;
// // convert the CANCoder from its position reading to ticks
// double currentTicks = encoder.getPosition() / encoder.configGetFeedbackCoefficient();
// // convert the CANCoder from its position reading to ticks
// double currentTicks = encoder.getPosition() / encoder.configGetFeedbackCoefficient();
// angleMotor.set(TalonFXControlMode.Position, currentTicks + deltaTicks);
// angleMotor.set(TalonFXControlMode.Position, currentTicks + deltaTicks);
// double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
// double feetPerSecond = Units.metersToFeet(state.speedMetersPerSecond);
// driveMotor.set((feetPerSecond / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
// }
// driveMotor.set((feetPerSecond / SwerveDriveConstants.MAX_SPEED_FEET_PER_SECOND));
}
// public void reset(double position) {
// encoder.setPositionToAbsolute();