2016-12-21 21:58:42 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
|
2016-12-21 21:58:42 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "vision/VisionRunner.h"
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
#include <thread>
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <opencv2/core/mat.hpp>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "cameraserver/CameraServerShared.h"
|
2016-12-21 21:58:42 -08:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
VisionRunnerBase::VisionRunnerBase(cs::VideoSource videoSource)
|
2017-12-10 20:58:14 -08:00
|
|
|
: m_image(std::make_unique<cv::Mat>()),
|
|
|
|
|
m_cvSink("VisionRunner CvSink"),
|
|
|
|
|
m_enabled(true) {
|
2016-12-21 21:58:42 -08:00
|
|
|
m_cvSink.SetSource(videoSource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Located here and not in header due to cv::Mat forward declaration.
|
|
|
|
|
VisionRunnerBase::~VisionRunnerBase() {}
|
|
|
|
|
|
|
|
|
|
void VisionRunnerBase::RunOnce() {
|
2018-04-29 13:29:07 -07:00
|
|
|
auto csShared = frc::GetCameraServerShared();
|
|
|
|
|
auto res = csShared->GetRobotMainThreadId();
|
|
|
|
|
if (res.second && (std::this_thread::get_id() == res.first)) {
|
|
|
|
|
csShared->SetVisionRunnerError(
|
2016-12-21 21:58:42 -08:00
|
|
|
"VisionRunner::RunOnce() cannot be called from the main robot thread");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto frameTime = m_cvSink.GrabFrame(*m_image);
|
|
|
|
|
if (frameTime == 0) {
|
|
|
|
|
auto error = m_cvSink.GetError();
|
2018-04-29 13:29:07 -07:00
|
|
|
csShared->ReportDriverStationError(error);
|
2016-12-21 21:58:42 -08:00
|
|
|
} else {
|
|
|
|
|
DoProcess(*m_image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VisionRunnerBase::RunForever() {
|
2018-04-29 13:29:07 -07:00
|
|
|
auto csShared = frc::GetCameraServerShared();
|
|
|
|
|
auto res = csShared->GetRobotMainThreadId();
|
|
|
|
|
if (res.second && (std::this_thread::get_id() == res.first)) {
|
|
|
|
|
csShared->SetVisionRunnerError(
|
2016-12-21 21:58:42 -08:00
|
|
|
"VisionRunner::RunForever() cannot be called from the main robot "
|
|
|
|
|
"thread");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-10 20:58:14 -08:00
|
|
|
while (m_enabled) {
|
2016-12-21 21:58:42 -08:00
|
|
|
RunOnce();
|
|
|
|
|
}
|
2017-08-19 22:14:34 -07:00
|
|
|
}
|
2017-12-10 20:58:14 -08:00
|
|
|
|
|
|
|
|
void VisionRunnerBase::Stop() { m_enabled = false; }
|