2021-01-18 10:56:04 -05:00
|
|
|
/*
|
2022-01-20 19:35:28 -08:00
|
|
|
* MIT License
|
2021-01-18 10:56:04 -05:00
|
|
|
*
|
2023-04-18 18:49:40 -04:00
|
|
|
* Copyright (c) PhotonVision
|
2021-01-18 10:56:04 -05:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2021-01-18 10:56:04 -05:00
|
|
|
*
|
2022-01-20 19:35:28 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2021-01-18 10:56:04 -05:00
|
|
|
*/
|
2022-01-20 19:35:28 -08:00
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
package frc.robot;
|
2021-01-18 10:56:04 -05:00
|
|
|
|
2021-11-21 17:22:56 -08:00
|
|
|
import edu.wpi.first.math.controller.PIDController;
|
|
|
|
|
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
|
|
|
|
|
import edu.wpi.first.math.geometry.Pose2d;
|
|
|
|
|
import edu.wpi.first.math.kinematics.ChassisSpeeds;
|
|
|
|
|
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics;
|
|
|
|
|
import edu.wpi.first.math.kinematics.DifferentialDriveWheelSpeeds;
|
2021-01-18 10:56:04 -05:00
|
|
|
import edu.wpi.first.wpilibj.Encoder;
|
2021-11-21 17:22:56 -08:00
|
|
|
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
|
|
|
|
|
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
|
2021-01-18 10:56:04 -05:00
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Implements a controller for the drivetrain. Converts a set of chassis motion commands into motor
|
|
|
|
|
* controller PWM values which attempt to speed up or slow down the wheels to match the desired
|
|
|
|
|
* speed.
|
|
|
|
|
*/
|
2021-01-18 10:56:04 -05:00
|
|
|
public class Drivetrain {
|
|
|
|
|
// PWM motor controller output definitions
|
|
|
|
|
PWMVictorSPX leftLeader = new PWMVictorSPX(Constants.kDtLeftLeaderPin);
|
|
|
|
|
PWMVictorSPX leftFollower = new PWMVictorSPX(Constants.kDtLeftFollowerPin);
|
|
|
|
|
PWMVictorSPX rightLeader = new PWMVictorSPX(Constants.kDtRightLeaderPin);
|
|
|
|
|
PWMVictorSPX rightFollower = new PWMVictorSPX(Constants.kDtRightFollowerPin);
|
|
|
|
|
|
2021-11-21 17:22:56 -08:00
|
|
|
MotorControllerGroup leftGroup = new MotorControllerGroup(leftLeader, leftFollower);
|
|
|
|
|
MotorControllerGroup rightGroup = new MotorControllerGroup(rightLeader, rightFollower);
|
2021-01-18 10:56:04 -05:00
|
|
|
|
|
|
|
|
// Drivetrain wheel speed sensors
|
|
|
|
|
// Used both for speed control and pose estimation.
|
|
|
|
|
Encoder leftEncoder = new Encoder(Constants.kDtLeftEncoderPinA, Constants.kDtLeftEncoderPinB);
|
|
|
|
|
Encoder rightEncoder = new Encoder(Constants.kDtRightEncoderPinA, Constants.kDtRightEncoderPinB);
|
|
|
|
|
|
|
|
|
|
// Drivetrain Pose Estimation
|
2022-12-16 17:05:23 -08:00
|
|
|
final DrivetrainPoseEstimator poseEst;
|
2021-01-18 10:56:04 -05:00
|
|
|
|
|
|
|
|
// Kinematics - defines the physical size and shape of the drivetrain, which is
|
|
|
|
|
// required to convert from
|
|
|
|
|
// chassis speed commands to wheel speed commands.
|
|
|
|
|
DifferentialDriveKinematics kinematics = new DifferentialDriveKinematics(Constants.kTrackWidth);
|
|
|
|
|
|
|
|
|
|
// Closed-loop PIDF controllers for servoing each side of the drivetrain to a
|
|
|
|
|
// specific speed.
|
|
|
|
|
// Gains are for example purposes only - must be determined for your own robot!
|
|
|
|
|
SimpleMotorFeedforward feedforward = new SimpleMotorFeedforward(1, 3);
|
|
|
|
|
PIDController leftPIDController = new PIDController(8.5, 0, 0);
|
|
|
|
|
PIDController rightPIDController = new PIDController(8.5, 0, 0);
|
|
|
|
|
|
|
|
|
|
public Drivetrain() {
|
|
|
|
|
// Set the distance per pulse for the drive encoders. We can simply use the
|
|
|
|
|
// distance traveled for one rotation of the wheel divided by the encoder
|
|
|
|
|
// resolution.
|
|
|
|
|
leftEncoder.setDistancePerPulse(
|
|
|
|
|
2 * Math.PI * Constants.kWheelRadius / Constants.kEncoderResolution);
|
|
|
|
|
rightEncoder.setDistancePerPulse(
|
|
|
|
|
2 * Math.PI * Constants.kWheelRadius / Constants.kEncoderResolution);
|
|
|
|
|
|
|
|
|
|
leftEncoder.reset();
|
|
|
|
|
rightEncoder.reset();
|
|
|
|
|
|
|
|
|
|
rightGroup.setInverted(true);
|
2022-12-16 17:05:23 -08:00
|
|
|
|
|
|
|
|
poseEst = new DrivetrainPoseEstimator(leftEncoder.getDistance(), rightEncoder.getDistance());
|
2021-01-18 10:56:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Given a set of chassis (fwd/rev + rotate) speed commands, perform all periodic tasks to assign
|
|
|
|
|
* new outputs to the motor controllers.
|
|
|
|
|
*
|
|
|
|
|
* @param xSpeed Desired chassis Forward or Reverse speed (in meters/sec). Positive is forward.
|
|
|
|
|
* @param rot Desired chassis rotation speed in radians/sec. Positive is counter-clockwise.
|
|
|
|
|
*/
|
2021-01-18 10:56:04 -05:00
|
|
|
public void drive(double xSpeed, double rot) {
|
|
|
|
|
// Convert our fwd/rev and rotate commands to wheel speed commands
|
|
|
|
|
DifferentialDriveWheelSpeeds speeds =
|
|
|
|
|
kinematics.toWheelSpeeds(new ChassisSpeeds(xSpeed, 0, rot));
|
|
|
|
|
|
|
|
|
|
// Calculate the feedback (PID) portion of our motor command, based on desired
|
|
|
|
|
// wheel speed
|
|
|
|
|
var leftOutput = leftPIDController.calculate(leftEncoder.getRate(), speeds.leftMetersPerSecond);
|
|
|
|
|
var rightOutput =
|
|
|
|
|
rightPIDController.calculate(rightEncoder.getRate(), speeds.rightMetersPerSecond);
|
|
|
|
|
|
|
|
|
|
// Calculate the feedforward (F) portion of our motor command, based on desired
|
|
|
|
|
// wheel speed
|
|
|
|
|
var leftFeedforward = feedforward.calculate(speeds.leftMetersPerSecond);
|
|
|
|
|
var rightFeedforward = feedforward.calculate(speeds.rightMetersPerSecond);
|
|
|
|
|
|
|
|
|
|
// Update the motor controllers with our new motor commands
|
|
|
|
|
leftGroup.setVoltage(leftOutput + leftFeedforward);
|
|
|
|
|
rightGroup.setVoltage(rightOutput + rightFeedforward);
|
|
|
|
|
|
|
|
|
|
// Update the pose estimator with the most recent sensor readings.
|
2022-12-16 17:05:23 -08:00
|
|
|
poseEst.update(leftEncoder.getDistance(), rightEncoder.getDistance());
|
2021-01-18 10:56:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* Force the pose estimator and all sensors to a particular pose. This is useful for indicating to
|
|
|
|
|
* the software when you have manually moved your robot in a particular position on the field (EX:
|
|
|
|
|
* when you place it on the field at the start of the match).
|
|
|
|
|
*
|
|
|
|
|
* @param pose
|
|
|
|
|
*/
|
2021-01-18 10:56:04 -05:00
|
|
|
public void resetOdometry(Pose2d pose) {
|
|
|
|
|
leftEncoder.reset();
|
|
|
|
|
rightEncoder.reset();
|
2022-12-16 17:05:23 -08:00
|
|
|
poseEst.resetToPose(pose, leftEncoder.getDistance(), rightEncoder.getDistance());
|
2021-01-18 10:56:04 -05:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 21:04:04 -04:00
|
|
|
/**
|
|
|
|
|
* @return The current best-guess at drivetrain Pose on the field.
|
|
|
|
|
*/
|
2021-01-18 10:56:04 -05:00
|
|
|
public Pose2d getCtrlsPoseEstimate() {
|
|
|
|
|
return poseEst.getPoseEst();
|
|
|
|
|
}
|
|
|
|
|
}
|