2021-01-16 20:41:47 -08:00
|
|
|
/*
|
2022-01-20 19:35:28 -08:00
|
|
|
* MIT License
|
2021-01-16 20:41:47 -08:00
|
|
|
*
|
2023-04-18 18:49:40 -04:00
|
|
|
* Copyright (c) PhotonVision
|
2021-01-16 20:41:47 -08: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-16 20:41:47 -08: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-16 20:41:47 -08:00
|
|
|
*/
|
2022-01-20 19:35:28 -08:00
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
package frc.robot;
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2021-11-21 17:22:56 -08:00
|
|
|
import edu.wpi.first.math.controller.PIDController;
|
|
|
|
|
import edu.wpi.first.math.util.Units;
|
2021-01-16 20:41:47 -08:00
|
|
|
import edu.wpi.first.wpilibj.TimedRobot;
|
|
|
|
|
import edu.wpi.first.wpilibj.XboxController;
|
|
|
|
|
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
|
2021-11-21 17:22:56 -08:00
|
|
|
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
|
2021-01-16 20:41:47 -08:00
|
|
|
import org.photonvision.PhotonCamera;
|
|
|
|
|
import org.photonvision.PhotonUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-10 11:56:45 -08:00
|
|
|
* The VM is configured to automatically run this class, and to call the functions corresponding to
|
|
|
|
|
* each mode, as described in the TimedRobot documentation. If you change the name of this class or
|
|
|
|
|
* the package after creating this project, you must also update the build.gradle file in the
|
|
|
|
|
* project.
|
|
|
|
|
*/
|
2021-01-16 20:41:47 -08:00
|
|
|
public class Robot extends TimedRobot {
|
|
|
|
|
// Constants such as camera and target height stored. Change per robot and goal!
|
|
|
|
|
final double CAMERA_HEIGHT_METERS = Units.inchesToMeters(24);
|
|
|
|
|
final double TARGET_HEIGHT_METERS = Units.feetToMeters(5);
|
|
|
|
|
|
|
|
|
|
// Angle between horizontal and the camera.
|
|
|
|
|
final double CAMERA_PITCH_RADIANS = Units.degreesToRadians(0);
|
|
|
|
|
|
|
|
|
|
// How far from the target we want to be
|
|
|
|
|
final double GOAL_RANGE_METERS = Units.feetToMeters(3);
|
|
|
|
|
|
|
|
|
|
// Change this to match the name of your camera
|
|
|
|
|
PhotonCamera camera = new PhotonCamera("photonvision");
|
|
|
|
|
|
|
|
|
|
// PID constants should be tuned per robot
|
|
|
|
|
final double P_GAIN = 0.1;
|
|
|
|
|
final double D_GAIN = 0.0;
|
|
|
|
|
PIDController controller = new PIDController(P_GAIN, 0, D_GAIN);
|
|
|
|
|
|
|
|
|
|
XboxController xboxController;
|
|
|
|
|
|
|
|
|
|
// Drive motors
|
|
|
|
|
PWMVictorSPX leftMotor = new PWMVictorSPX(0);
|
|
|
|
|
PWMVictorSPX rightMotor = new PWMVictorSPX(1);
|
|
|
|
|
DifferentialDrive drive = new DifferentialDrive(leftMotor, rightMotor);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void robotInit() {
|
|
|
|
|
xboxController = new XboxController(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void teleopPeriodic() {
|
|
|
|
|
double forwardSpeed;
|
2021-11-21 17:22:56 -08:00
|
|
|
double rotationSpeed = xboxController.getLeftX();
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
if (xboxController.getAButton()) {
|
|
|
|
|
// Vision-alignment mode
|
|
|
|
|
// Query the latest result from PhotonVision
|
|
|
|
|
var result = camera.getLatestResult();
|
|
|
|
|
|
|
|
|
|
if (result.hasTargets()) {
|
|
|
|
|
// First calculate range
|
|
|
|
|
double range =
|
|
|
|
|
PhotonUtils.calculateDistanceToTargetMeters(
|
|
|
|
|
CAMERA_HEIGHT_METERS,
|
|
|
|
|
TARGET_HEIGHT_METERS,
|
|
|
|
|
CAMERA_PITCH_RADIANS,
|
2021-03-23 12:57:32 -05:00
|
|
|
Units.degreesToRadians(result.getBestTarget().getPitch()));
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
// Use this range as the measurement we give to the PID controller.
|
2021-01-26 22:26:15 -06:00
|
|
|
// -1.0 required to ensure positive PID controller effort _increases_ range
|
2021-11-21 17:22:56 -08:00
|
|
|
forwardSpeed = -controller.calculate(range, GOAL_RANGE_METERS);
|
2021-01-16 20:41:47 -08:00
|
|
|
} else {
|
|
|
|
|
// If we have no targets, stay still.
|
|
|
|
|
forwardSpeed = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Manual Driver Mode
|
2021-11-21 17:22:56 -08:00
|
|
|
forwardSpeed = -xboxController.getRightY();
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use our forward/turn speeds to control the drivetrain
|
|
|
|
|
drive.arcadeDrive(forwardSpeed, rotationSpeed);
|
|
|
|
|
}
|
|
|
|
|
}
|