-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drivetrain code #13
base: master
Are you sure you want to change the base?
Drivetrain code #13
Changes from 9 commits
1e1295b
b5d401b
6d2afcf
5235d65
9e51ef5
8cea7cc
50ccea3
6b7e705
3785959
dcb7c3b
449aaa9
f2f0267
ba55b71
340c854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,90 @@ | ||
package com.nutrons.nu17.subsystems; | ||
|
||
import com.ctre.CANTalon; | ||
import com.ctre.CANTalon.FeedbackDevice; | ||
import com.ctre.CANTalon.TalonControlMode; | ||
import com.nutrons.nu17.OperatorInterface; | ||
import com.nutrons.nu17.Robot; | ||
import com.nutrons.nu17.RobotMap; | ||
|
||
import edu.wpi.first.wpilibj.AnalogGyro; | ||
import edu.wpi.first.wpilibj.Encoder; | ||
import edu.wpi.first.wpilibj.PIDController; | ||
import edu.wpi.first.wpilibj.PIDOutput; | ||
import edu.wpi.first.wpilibj.PIDSource; | ||
import edu.wpi.first.wpilibj.PIDSourceType; | ||
import edu.wpi.first.wpilibj.RobotDrive; | ||
import edu.wpi.first.wpilibj.command.Subsystem; | ||
import lib.EncoderWrapper; | ||
import lib.GyroWrapper; | ||
import lib.HoldPid; | ||
|
||
|
||
public class Drivetrain extends Subsystem { | ||
|
||
/** | ||
* Drivetrain four wheel drive. | ||
*/ | ||
public Drivetrain() { | ||
// empty | ||
this.setPercentDrive(); | ||
|
||
this.holdHeading.setInputRange(-180.0, 180.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment explaining this |
||
this.holdHeading.setOutputRange(-1.0, 1.0); | ||
this.holdHeading.setAbsoluteTolerance(5.0); | ||
this.holdHeading.setContinuous(); | ||
|
||
this.turnToAngle.setAbsoluteTolerance(10.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be a constant, and we need an explanation comment |
||
|
||
this.frontLeft.changeControlMode(TalonControlMode.PercentVbus); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need comment explaining this |
||
this.frontLeft.setFeedbackDevice(FeedbackDevice.QuadEncoder); | ||
this.frontLeft.configEncoderCodesPerRev((int) (256 / 0.14)); | ||
this.frontLeft.reverseOutput(true); | ||
this.frontLeft.reverseSensor(true); | ||
this.frontLeft.setPID(0.02, 0.0, 0.08); | ||
this.frontLeft.configNominalOutputVoltage(+0.0f, -0.0f); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's an automatic coercion from double to float |
||
this.frontLeft.configPeakOutputVoltage(12.0f, -12.0f); | ||
|
||
this.backLeft.changeControlMode(TalonControlMode.Follower); | ||
this.backLeft.set(this.frontLeft.getDeviceID()); | ||
this.backLeft.configNominalOutputVoltage(+0.0f, -0.0f); | ||
this.backLeft.configPeakOutputVoltage(12.0f, -12.0f); | ||
|
||
this.frontRight.changeControlMode(TalonControlMode.PercentVbus); | ||
this.frontRight.setFeedbackDevice(FeedbackDevice.QuadEncoder); | ||
this.frontRight.configEncoderCodesPerRev((int) (256 / 0.14)); | ||
this.frontRight.setPID(0.02, 0.0, 0.08); | ||
this.frontRight.configNominalOutputVoltage(+0.0f, -0.0f); | ||
this.frontRight.configPeakOutputVoltage(12.0f, -12.0f); | ||
|
||
this.backRight.changeControlMode(TalonControlMode.Follower); | ||
this.backRight.set(this.frontRight.getDeviceID()); | ||
this.backRight.configNominalOutputVoltage(+0.0f, -0.0f); | ||
this.backRight.configPeakOutputVoltage(12.0f, -12.0f); | ||
|
||
this.disableBreakMode(); | ||
|
||
this.gyro.calibrate(); | ||
} | ||
|
||
// Motors | ||
public CANTalon frontLeft = new CANTalon(RobotMap.FRONT_LEFT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make these final and private |
||
public CANTalon backLeft = new CANTalon(RobotMap.BACK_LEFT); | ||
public CANTalon frontRight = new CANTalon(RobotMap.FRONT_RIGHT); | ||
public CANTalon backRight = new CANTalon(RobotMap.BACK_RIGHT); | ||
public CANTalon backRight = new CANTalon(RobotMap.FRONT_RIGHT); | ||
public CANTalon frontRight = new CANTalon(RobotMap.BACK_RIGHT); | ||
|
||
// Sensors | ||
private final AnalogGyro headingGyro = new AnalogGyro(RobotMap.DRIVETRAIN_HEADING_GYRO); | ||
private final AnalogGyro gyro = new AnalogGyro(RobotMap.DRIVETRAIN_HEADING_GYRO); | ||
|
||
// TODO: Tune Ports | ||
private final Encoder driveDistanceEncoder = new Encoder( | ||
RobotMap.LEFT_WHEEL_DRIVE_DISTANCE_ENCODER_1, RobotMap.LEFT_WHEEL_DRIVE_DISTANCE_ENCODER_2); | ||
|
||
// Drive | ||
public RobotDrive drive = new RobotDrive(frontLeft, backLeft, frontRight, backRight); | ||
|
||
// PID | ||
// Wrappers | ||
private EncoderWrapper encWrap = new EncoderWrapper(PIDSourceType.kDisplacement, | ||
driveDistanceEncoder, RobotMap.LEFT_WHEEL_DRIVE_DISTANCE_ENCODER_1, | ||
RobotMap.LEFT_WHEEL_DRIVE_DISTANCE_ENCODER_2); | ||
private GyroWrapper gyroWrap = | ||
new GyroWrapper(PIDSourceType.kDisplacement, headingGyro, RobotMap.DRIVETRAIN_HEADING_GYRO); | ||
|
||
// Holders | ||
private HoldPid distanceHolder = new HoldPid(); | ||
private HoldPid headingPidHolder = new HoldPid(); | ||
|
||
// TODO: tune these constants | ||
private static final double P_DRIVE = 0; | ||
private static final double I_DRIVE = 0; | ||
private static final double D_DRIVE = 0; | ||
|
||
// TODO: tune these constants | ||
private static final double P_HEADING = 0; | ||
private static final double I_HEADING = 0; | ||
private static final double D_HEADING = 0; | ||
|
||
public final PIDController distancePid = | ||
new PIDController(P_DRIVE, I_DRIVE, D_DRIVE, encWrap, distanceHolder); | ||
public final PIDController headingPid = | ||
new PIDController(P_HEADING, I_HEADING, D_HEADING, gyroWrap, headingPidHolder); | ||
public RobotDrive drive = new RobotDrive(frontLeft, backLeft, backRight, frontRight); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this private and final |
||
|
||
/** | ||
* Returns the angle, in degrees, away from the initial gyro position. | ||
* | ||
* @return angle Angular displacement from the initial gyro position. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use initial angle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "@return displacement from the initial angle (degrees)" |
||
*/ | ||
public double getAngleInDegrees() { | ||
return this.headingGyro.getAngle(); | ||
return this.gyro.getAngle(); | ||
} | ||
|
||
/** | ||
|
@@ -93,11 +108,110 @@ public void resetEncoder() { | |
* Resets the gyro. | ||
*/ | ||
public void resetGyro() { | ||
this.headingGyro.reset(); | ||
this.gyro.reset(); | ||
|
||
} | ||
|
||
public void setPercentDrive() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setFollowerControlMode Make private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine into initializeFollowerModes |
||
this.frontLeft.changeControlMode(TalonControlMode.PercentVbus); | ||
this.frontRight.changeControlMode(TalonControlMode.PercentVbus); | ||
} | ||
|
||
public void disableBreakMode() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine into above |
||
this.frontLeft.enableBrakeMode(false); | ||
this.frontRight.enableBrakeMode(false); | ||
} | ||
|
||
public void initDefaultCommand() { | ||
drive.tankDrive(OperatorInterface.DRIVER_PAD.getY(), OperatorInterface.DRIVER_PAD.getX()); | ||
} | ||
|
||
// PID | ||
// TODO: tune these constants | ||
private static final double P_HEADING = 0; | ||
private static final double I_HEADING = 0; | ||
private static final double D_HEADING = 0; | ||
|
||
// TODO: tune these constants | ||
private static final double P_TURN = 0; | ||
private static final double I_TURN = 0; | ||
private static final double D_TURN = 0; | ||
|
||
// TODO: tune these constants | ||
private static final double P_DISTANCE = 0; | ||
private static final double I_DISTANCE = 0; | ||
private static final double D_DISTANCE = 0; | ||
|
||
private double heading; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. holdHeading |
||
public final PIDController holdHeading = new PIDController(P_HEADING, I_HEADING, D_HEADING, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make private |
||
new GyroWrapper(), new HoldHeadingOutput()); | ||
private double angle; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. goalAngle |
||
public final PIDController turnToAngle = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make private |
||
new PIDController(P_TURN, I_TURN, D_TURN, new GyroWrapper(), new TurnToAngleOutput()); | ||
private double distance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename goalDistance |
||
public final PIDController driveDistance = new PIDController(P_DISTANCE, I_DISTANCE, D_DISTANCE, | ||
new EncoderWrapper(), new DriveDistanceOutput()); | ||
|
||
private class GyroWrapper implements PIDSource { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this into separate file or explain why second one is needed. If they serve the same function, create a common interface |
||
|
||
@Override | ||
public PIDSourceType getPIDSourceType() { | ||
return PIDSourceType.kDisplacement; | ||
} | ||
|
||
@Override | ||
public double pidGet() { | ||
return getAngleInDegrees(); | ||
} | ||
|
||
@Override | ||
public void setPIDSourceType(PIDSourceType arg0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename from arg0 |
||
|
||
} | ||
} | ||
|
||
public class EncoderWrapper implements PIDSource { | ||
|
||
@Override | ||
public PIDSourceType getPIDSourceType() { | ||
// TODO Auto-generated method stub | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment |
||
return PIDSourceType.kDisplacement; | ||
} | ||
|
||
@Override | ||
public double pidGet() { | ||
// TODO Auto-generated method stub | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment |
||
return Robot.DRIVETRAIN.frontRight.getPosition(); | ||
} | ||
|
||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if method is abstract and remove auto generated comment |
||
public void setPIDSourceType(PIDSourceType arg0) { | ||
// TODO Auto-generated method stub | ||
} | ||
} | ||
|
||
private class HoldHeadingOutput implements PIDOutput { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should not change the outer class |
||
|
||
@Override | ||
public void pidWrite(double wheel) { | ||
heading = wheel; | ||
} | ||
} | ||
|
||
public class TurnToAngleOutput implements PIDOutput { | ||
|
||
@Override | ||
public void pidWrite(double power) { | ||
angle = power; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should not change the outer class |
||
} | ||
} | ||
|
||
public class DriveDistanceOutput implements PIDOutput { | ||
|
||
@Override | ||
public void pidWrite(double power) { | ||
distance = power; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should not change the outer class |
||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drivetrain subsystem, configured for 4 wheel drive