2021-01-16 20:41:47 -08:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2018-2020 Photon Vision.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Robot.h"
|
|
|
|
|
|
|
|
|
|
#include <photonlib/PhotonUtils.h>
|
|
|
|
|
|
|
|
|
|
void Robot::TeleopPeriodic() {
|
|
|
|
|
double forwardSpeed;
|
|
|
|
|
double rotationSpeed =
|
|
|
|
|
xboxController.GetX(frc::GenericHID::JoystickHand::kLeftHand);
|
|
|
|
|
|
|
|
|
|
if (xboxController.GetAButton()) {
|
|
|
|
|
// Vision-alignment mode
|
|
|
|
|
// Query the latest result from PhotonVision
|
|
|
|
|
photonlib::PhotonPipelineResult result = camera.GetLatestResult();
|
|
|
|
|
|
|
|
|
|
if (result.HasTargets()) {
|
|
|
|
|
// First calculate range
|
|
|
|
|
units::meter_t range = photonlib::PhotonUtils::CalculateDistanceToTarget(
|
|
|
|
|
CAMERA_HEIGHT, TARGET_HEIGHT, CAMERA_PITCH,
|
|
|
|
|
units::degree_t{result.GetBestTarget().GetPitch()});
|
|
|
|
|
|
|
|
|
|
// Use this range as the measurement we give to the PID controller.
|
2021-01-26 22:26:15 -06:00
|
|
|
forwardSpeed = -1.0 * controller.Calculate(range.to<double>(),
|
2021-01-16 20:41:47 -08:00
|
|
|
GOAL_RANGE_METERS.to<double>());
|
|
|
|
|
} else {
|
|
|
|
|
// If we have no targets, stay still.
|
|
|
|
|
forwardSpeed = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Manual Driver Mode
|
|
|
|
|
forwardSpeed =
|
2021-01-26 22:26:15 -06:00
|
|
|
-1.0 * xboxController.GetY(frc::GenericHID::JoystickHand::kRightHand);
|
2021-01-16 20:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use our forward/turn speeds to control the drivetrain
|
|
|
|
|
drive.ArcadeDrive(forwardSpeed, rotationSpeed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef RUNNING_FRC_TESTS
|
|
|
|
|
int main() { return frc::StartRobot<Robot>(); }
|
|
|
|
|
#endif
|