Aiming at a Target

Knowledge and Equipment Needed

  • Robot with a vision system running PhotonVision

  • Target

  • Ability to track a target by properly tuning a pipeline

Code

Now that you have properly set up your vision system and have tuned a pipeline, you can now aim your robot/turret at the target using the data from PhotonVision. This data is reported over NetworkTables and includes: latency, whether there is a target detected or not, pitch, yaw, area, skew, and target pose relative to the robot. This data will be used/manipulated by our vendor dependency, PhotonLib. The documentation for the Network Tables API can be found here and the documentation for PhotonLib here. For right now, all we will be using is yaw. In this example, while the operator holds a button down, the robot will turn towards the goal using the P term of a PID loop. To learn more about how PID loops work, how WPILib implements them, and more, visit Advanced Controls (PID) and PID Control in WPILib.

The following example is from the PhotonLib example repository (Java/C++).

 1/*
 2 * Copyright (C) Photon Vision.
 3 *
 4 * This program is free software: you can redistribute it and/or modify
 5 * it under the terms of the GNU General Public License as published by
 6 * the Free Software Foundation, either version 3 of the License, or
 7 * (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17package org.photonlib.examples.aimattarget;
18
19import edu.wpi.first.math.controller.PIDController;
20import edu.wpi.first.math.util.Units;
21import edu.wpi.first.wpilibj.TimedRobot;
22import edu.wpi.first.wpilibj.XboxController;
23import edu.wpi.first.wpilibj.drive.DifferentialDrive;
24import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
25import org.photonvision.PhotonCamera;
26
27/**
28 * The VM is configured to automatically run this class, and to call the functions corresponding to
29 * each mode, as described in the TimedRobot documentation. If you change the name of this class or
30 * the package after creating this project, you must also update the build.gradle file in the
31 * project.
32 */
33public class Robot extends TimedRobot {
34    // Constants such as camera and target height stored. Change per robot and goal!
35    final double CAMERA_HEIGHT_METERS = Units.inchesToMeters(24);
36    final double TARGET_HEIGHT_METERS = Units.feetToMeters(5);
37    // Angle between horizontal and the camera.
38    final double CAMERA_PITCH_RADIANS = Units.degreesToRadians(0);
39
40    // How far from the target we want to be
41    final double GOAL_RANGE_METERS = Units.feetToMeters(3);
42
43    // Change this to match the name of your camera
44    PhotonCamera camera = new PhotonCamera("photonvision");
45
46    // PID constants should be tuned per robot
47    final double LINEAR_P = 0.1;
48    final double LINEAR_D = 0.0;
49    PIDController forwardController = new PIDController(LINEAR_P, 0, LINEAR_D);
50
51    final double ANGULAR_P = 0.1;
52    final double ANGULAR_D = 0.0;
53    PIDController turnController = new PIDController(ANGULAR_P, 0, ANGULAR_D);
54
55    XboxController xboxController = new XboxController(0);
56
57    // Drive motors
58    PWMVictorSPX leftMotor = new PWMVictorSPX(0);
59    PWMVictorSPX rightMotor = new PWMVictorSPX(1);
60    DifferentialDrive drive = new DifferentialDrive(leftMotor, rightMotor);
61
62    @Override
63    public void teleopPeriodic() {
64        double forwardSpeed;
65        double rotationSpeed;
66
67        forwardSpeed = -xboxController.getRightY();
68
69        if (xboxController.getAButton()) {
70            // Vision-alignment mode
71            // Query the latest result from PhotonVision
72            var result = camera.getLatestResult();
73
74            if (result.hasTargets()) {
75                // Calculate angular turn power
76                // -1.0 required to ensure positive PID controller effort _increases_ yaw
77                rotationSpeed = -turnController.calculate(result.getBestTarget().getYaw(), 0);
78            } else {
79                // If we have no targets, stay still.
80                rotationSpeed = 0;
81            }
82        } else {
83            // Manual Driver Mode
84            rotationSpeed = xboxController.getLeftX();
85        }
86
87        // Use our forward/turn speeds to control the drivetrain
88        drive.arcadeDrive(forwardSpeed, rotationSpeed);
89    }
90}
 1/*
 2 * Copyright (C) Photon Vision.
 3 *
 4 * This program is free software: you can redistribute it and/or modify
 5 * it under the terms of the GNU General Public License as published by
 6 * the Free Software Foundation, either version 3 of the License, or
 7 * (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <photonlib/PhotonCamera.h>
21
22#include <frc/TimedRobot.h>
23#include <frc/XboxController.h>
24#include <frc/controller/PIDController.h>
25#include <frc/drive/DifferentialDrive.h>
26#include <frc/motorcontrol/PWMVictorSPX.h>
27#include <units/angle.h>
28#include <units/length.h>
29
30class Robot : public frc::TimedRobot {
31 public:
32  void TeleopPeriodic() override;
33
34 private:
35  // Change this to match the name of your camera
36  photonlib::PhotonCamera camera{"photonvision"};
37  // PID constants should be tuned per robot
38  frc2::PIDController controller{.1, 0, 0};
39
40  frc::XboxController xboxController{0};
41
42  // Drive motors
43  frc::PWMVictorSPX leftMotor{0};
44  frc::PWMVictorSPX rightMotor{1};
45  frc::DifferentialDrive drive{leftMotor, rightMotor};
46};
 1/*
 2 * Copyright (C) Photon Vision.
 3 *
 4 * This program is free software: you can redistribute it and/or modify
 5 * it under the terms of the GNU General Public License as published by
 6 * the Free Software Foundation, either version 3 of the License, or
 7 * (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "Robot.h"
19
20#include <photonlib/PhotonUtils.h>
21
22void Robot::TeleopPeriodic() {
23  double forwardSpeed = -xboxController.GetRightY();
24  double rotationSpeed;
25
26  if (xboxController.GetAButton()) {
27    // Vision-alignment mode
28    // Query the latest result from PhotonVision
29    photonlib::PhotonPipelineResult result = camera.GetLatestResult();
30
31    if (result.HasTargets()) {
32      // Rotation speed is the output of the PID controller
33      rotationSpeed = -controller.Calculate(result.GetBestTarget().GetYaw(), 0);
34    } else {
35      // If we have no targets, stay still.
36      rotationSpeed = 0;
37    }
38  } else {
39    // Manual Driver Mode
40    rotationSpeed = xboxController.GetLeftX();
41  }
42
43  // Use our forward/turn speeds to control the drivetrain
44  drive.ArcadeDrive(forwardSpeed, rotationSpeed);
45}
46
47#ifndef RUNNING_FRC_TESTS
48int main() { return frc::StartRobot<Robot>(); }
49#endif