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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-09-03 19:19:38 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2024-05-10 14:58:18 -04:00
|
|
|
#include <vector>
|
2021-09-03 19:19:38 -07:00
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
#include <networktables/BooleanTopic.h>
|
2023-02-13 17:57:01 -05:00
|
|
|
#include <networktables/DoubleArrayTopic.h>
|
2022-12-16 17:05:23 -08:00
|
|
|
#include <networktables/DoubleTopic.h>
|
|
|
|
|
#include <networktables/IntegerTopic.h>
|
2023-01-05 19:28:32 -05:00
|
|
|
#include <networktables/MultiSubscriber.h>
|
2021-01-16 20:41:47 -08:00
|
|
|
#include <networktables/NetworkTable.h>
|
|
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2022-12-16 17:05:23 -08:00
|
|
|
#include <networktables/RawTopic.h>
|
|
|
|
|
#include <networktables/StringTopic.h>
|
2022-10-21 20:53:28 -04:00
|
|
|
#include <units/time.h>
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2024-08-31 13:44:19 -04:00
|
|
|
#include "photon/targeting/PhotonPipelineResult.h"
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
namespace cv {
|
|
|
|
|
class Mat;
|
|
|
|
|
} // namespace cv
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
namespace photon {
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
enum LEDMode : int { kDefault = -1, kOff = 0, kOn = 1, kBlink = 2 };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a camera that is connected to PhotonVision.ß
|
|
|
|
|
*/
|
|
|
|
|
class PhotonCamera {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a PhotonCamera from a root table.
|
2022-01-24 12:38:45 -05:00
|
|
|
*
|
|
|
|
|
* @param instance The NetworkTableInstance to pull data from. This can be a
|
|
|
|
|
* custom instance in simulation, but should *usually* be the default
|
|
|
|
|
* NTInstance from {@link NetworkTableInstance::getDefault}
|
|
|
|
|
* @param cameraName The name of the camera, as seen in the UI.
|
2021-01-16 20:41:47 -08:00
|
|
|
* over.
|
|
|
|
|
*/
|
2023-01-05 19:28:32 -05:00
|
|
|
explicit PhotonCamera(nt::NetworkTableInstance instance,
|
2022-12-25 07:01:57 +02:00
|
|
|
const std::string_view cameraName);
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a PhotonCamera from the name of the camera.
|
|
|
|
|
* @param cameraName The nickname of the camera (found in the PhotonVision
|
|
|
|
|
* UI).
|
|
|
|
|
*/
|
2022-12-25 07:01:57 +02:00
|
|
|
explicit PhotonCamera(const std::string_view cameraName);
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2023-01-14 09:06:15 -06:00
|
|
|
PhotonCamera(PhotonCamera&&) = default;
|
|
|
|
|
|
2024-08-04 14:23:46 -04:00
|
|
|
~PhotonCamera() = default;
|
2022-12-30 00:40:13 -06:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
/**
|
2024-08-04 14:23:46 -04:00
|
|
|
* The list of pipeline results sent by PhotonVision since the last call to
|
|
|
|
|
* GetAllUnreadResults(). Calling this function clears the internal FIFO
|
|
|
|
|
* queue, and multiple calls to GetAllUnreadResults() will return different
|
|
|
|
|
* (potentially empty) result arrays. Be careful to call this exactly ONCE per
|
|
|
|
|
* loop of your robot code! FIFO depth is limited to 20 changes, so make sure
|
|
|
|
|
* to call this frequently enough to avoid old results being discarded, too!
|
2021-01-16 20:41:47 -08:00
|
|
|
*/
|
2024-08-04 14:23:46 -04:00
|
|
|
std::vector<PhotonPipelineResult> GetAllUnreadResults();
|
|
|
|
|
|
|
|
|
|
[[deprecated("Replace with GetAllUnreadResults")]]
|
|
|
|
|
PhotonPipelineResult GetLatestResult();
|
2021-01-16 20:41:47 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Toggles driver mode.
|
|
|
|
|
* @param driverMode Whether to set driver mode.
|
|
|
|
|
*/
|
|
|
|
|
void SetDriverMode(bool driverMode);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the camera is in driver mode.
|
|
|
|
|
* @return Whether the camera is in driver mode.
|
|
|
|
|
*/
|
|
|
|
|
bool GetDriverMode() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Request the camera to save a new image file from the input
|
|
|
|
|
* camera stream with overlays.
|
|
|
|
|
* Images take up space in the filesystem of the PhotonCamera.
|
|
|
|
|
* Calling it frequently will fill up disk space and eventually
|
|
|
|
|
* cause the system to stop working.
|
|
|
|
|
* Clear out images in /opt/photonvision/photonvision_config/imgSaves
|
|
|
|
|
* frequently to prevent issues.
|
|
|
|
|
*/
|
|
|
|
|
void TakeInputSnapshot(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Request the camera to save a new image file from the output
|
|
|
|
|
* stream with overlays.
|
|
|
|
|
* Images take up space in the filesystem of the PhotonCamera.
|
|
|
|
|
* Calling it frequently will fill up disk space and eventually
|
|
|
|
|
* cause the system to stop working.
|
|
|
|
|
* Clear out images in /opt/photonvision/photonvision_config/imgSaves
|
|
|
|
|
* frequently to prevent issues.
|
|
|
|
|
*/
|
|
|
|
|
void TakeOutputSnapshot(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allows the user to select the active pipeline index.
|
|
|
|
|
* @param index The active pipeline index.
|
|
|
|
|
*/
|
|
|
|
|
void SetPipelineIndex(int index);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the active pipeline index.
|
|
|
|
|
* @return The active pipeline index.
|
|
|
|
|
*/
|
|
|
|
|
int GetPipelineIndex() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the current LED mode.
|
|
|
|
|
* @return The current LED mode.
|
|
|
|
|
*/
|
|
|
|
|
LEDMode GetLEDMode() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the LED mode.
|
|
|
|
|
* @param led The mode to set to.
|
|
|
|
|
*/
|
|
|
|
|
void SetLEDMode(LEDMode led);
|
|
|
|
|
|
2022-12-25 07:01:57 +02:00
|
|
|
/**
|
|
|
|
|
* Returns the name of the camera.
|
|
|
|
|
* This will return the same value that was given to the constructor as
|
|
|
|
|
* cameraName.
|
|
|
|
|
* @return The name of the camera.
|
|
|
|
|
*/
|
|
|
|
|
const std::string_view GetCameraName() const;
|
|
|
|
|
|
2024-05-29 17:28:35 -04:00
|
|
|
using CameraMatrix = Eigen::Matrix<double, 3, 3>;
|
|
|
|
|
using DistortionMatrix = Eigen::Matrix<double, 8, 1>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Get the camera calibration matrix, in standard OpenCV form
|
|
|
|
|
*
|
|
|
|
|
* @return std::optional<cv::Mat>
|
|
|
|
|
*/
|
|
|
|
|
std::optional<CameraMatrix> GetCameraMatrix();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Get the camera calibration distortion coefficients, in OPENCV8 form.
|
|
|
|
|
* Higher order terms are set to zero.
|
|
|
|
|
*
|
|
|
|
|
* @return std::optional<cv::Mat>
|
|
|
|
|
*/
|
|
|
|
|
std::optional<DistortionMatrix> GetDistCoeffs();
|
2023-02-13 17:57:01 -05:00
|
|
|
|
2024-05-19 20:36:44 -04:00
|
|
|
static void SetVersionCheckEnabled(bool enabled);
|
2022-02-28 07:37:52 -05:00
|
|
|
|
2023-12-16 13:41:27 -05:00
|
|
|
std::shared_ptr<nt::NetworkTable> GetCameraTable() const { return rootTable; }
|
|
|
|
|
|
2022-12-30 00:40:13 -06:00
|
|
|
// For use in tests
|
|
|
|
|
bool test = false;
|
2024-08-04 14:23:46 -04:00
|
|
|
std::vector<PhotonPipelineResult> testResult;
|
2022-12-30 00:40:13 -06:00
|
|
|
|
2021-01-16 20:41:47 -08:00
|
|
|
protected:
|
2022-01-24 12:38:45 -05:00
|
|
|
std::shared_ptr<nt::NetworkTable> mainTable;
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> rootTable;
|
2022-12-16 17:05:23 -08:00
|
|
|
nt::RawSubscriber rawBytesEntry;
|
2022-12-30 21:48:28 -06:00
|
|
|
nt::IntegerPublisher inputSaveImgEntry;
|
|
|
|
|
nt::IntegerSubscriber inputSaveImgSubscriber;
|
|
|
|
|
nt::IntegerPublisher outputSaveImgEntry;
|
|
|
|
|
nt::IntegerSubscriber outputSaveImgSubscriber;
|
2023-02-08 21:07:12 -05:00
|
|
|
nt::IntegerPublisher pipelineIndexPub;
|
|
|
|
|
nt::IntegerSubscriber pipelineIndexSub;
|
|
|
|
|
nt::IntegerPublisher ledModePub;
|
|
|
|
|
nt::IntegerSubscriber ledModeSub;
|
2022-12-16 17:05:23 -08:00
|
|
|
nt::StringSubscriber versionEntry;
|
|
|
|
|
|
2023-02-13 17:57:01 -05:00
|
|
|
nt::DoubleArraySubscriber cameraIntrinsicsSubscriber;
|
|
|
|
|
nt::DoubleArraySubscriber cameraDistortionSubscriber;
|
|
|
|
|
|
2022-12-16 17:05:23 -08:00
|
|
|
nt::BooleanSubscriber driverModeSubscriber;
|
2023-01-29 23:30:34 -05:00
|
|
|
nt::BooleanPublisher driverModePublisher;
|
2022-12-16 17:05:23 -08:00
|
|
|
nt::IntegerSubscriber ledModeSubscriber;
|
2022-01-24 12:38:45 -05:00
|
|
|
|
2024-05-10 14:58:18 -04:00
|
|
|
nt::MultiSubscriber topicNameSubscriber;
|
2023-01-05 19:28:32 -05:00
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
std::string path;
|
2024-05-10 14:58:18 -04:00
|
|
|
std::string cameraName;
|
2021-01-16 20:41:47 -08:00
|
|
|
|
2022-01-24 12:38:45 -05:00
|
|
|
private:
|
2022-10-21 20:53:28 -04:00
|
|
|
units::second_t lastVersionCheckTime = 0_s;
|
2024-05-19 20:36:44 -04:00
|
|
|
static bool VERSION_CHECK_ENABLED;
|
2023-12-16 15:26:00 -05:00
|
|
|
inline static int InstanceCount = 0;
|
2022-02-28 07:37:52 -05:00
|
|
|
|
2022-10-21 20:53:28 -04:00
|
|
|
void VerifyVersion();
|
2024-05-10 14:58:18 -04:00
|
|
|
|
|
|
|
|
std::vector<std::string> tablesThatLookLikePhotonCameras();
|
2021-01-16 20:41:47 -08:00
|
|
|
};
|
|
|
|
|
|
2023-11-19 15:16:22 -05:00
|
|
|
} // namespace photon
|