diff options
-rw-r--r-- | compat/time.hpp | 8 | ||||
-rw-r--r-- | compat/timer.hpp | 16 | ||||
-rw-r--r-- | compat/util.hpp | 2 | ||||
-rw-r--r-- | tracker-pt/camera.cpp | 35 | ||||
-rw-r--r-- | tracker-pt/ftnoir_tracker_pt.cpp | 14 |
5 files changed, 47 insertions, 28 deletions
diff --git a/compat/time.hpp b/compat/time.hpp index b560da70..c07f29cb 100644 --- a/compat/time.hpp +++ b/compat/time.hpp @@ -1,10 +1,12 @@ #pragma once +#include "compat/functional.hpp" + #include <chrono> namespace time_units { -template<typename repr, typename ratio> +template<typename repr, typename ratio = std::ratio<1>> using duration = std::chrono::duration<repr, ratio>; template<typename t, typename u> @@ -13,8 +15,8 @@ static inline constexpr auto time_cast(const u& in) return std::chrono::duration_cast<t>(in); } -using secs = duration<double, std::ratio<1, 1>>; -using secs_ = duration<long long, std::ratio<1, 1>>; +using secs = duration<double>; +using secs_ = duration<long>; using ms = duration<double, std::milli>; using ms_ = duration<long long, std::milli>; using us = duration<double, std::micro>; diff --git a/compat/timer.hpp b/compat/timer.hpp index 6af7430d..ac255a9d 100644 --- a/compat/timer.hpp +++ b/compat/timer.hpp @@ -20,6 +20,7 @@ #include <ctime> #include "time.hpp" +#include "util.hpp" class OTR_COMPAT_EXPORT Timer final { @@ -43,7 +44,20 @@ public: t elapsed() const { using namespace time_units; - return static_cast<const t&>(ns(elapsed_nsecs())); + return time_cast<t>(ns(elapsed_nsecs())); + } + + template<typename t> + bool is_elapsed(const t& time_value) + { + using namespace time_units; + + if (unlikely(elapsed<ns>() >= time_value)) + { + start(); + return true; + } + return false; } long long elapsed_nsecs() const; diff --git a/compat/util.hpp b/compat/util.hpp index 823b83d1..3a53e203 100644 --- a/compat/util.hpp +++ b/compat/util.hpp @@ -12,7 +12,7 @@ #include <QSharedPointer> #include <QDebug> -#define progn(...) ([&]() { __VA_ARGS__ }()) +#define progn(...) (([&]() { __VA_ARGS__ })()) #define prog1(x, ...) (([&]() { auto _ret1324 = (x); do { __VA_ARGS__; } while (0); return _ret1324; })()) #define once_only(...) progn(static bool once = false; if (!once) { once = true; __VA_ARGS__; }) diff --git a/tracker-pt/camera.cpp b/tracker-pt/camera.cpp index 6a724fc7..b68fd107 100644 --- a/tracker-pt/camera.cpp +++ b/tracker-pt/camera.cpp @@ -7,6 +7,7 @@ #include "camera.h" #include "compat/sleep.hpp" +#include "compat/camera-names.hpp" constexpr double Camera::dt_eps; @@ -46,7 +47,7 @@ DEFUN_WARN_UNUSED Camera::result Camera::get_info() const DEFUN_WARN_UNUSED Camera::result Camera::get_frame(cv::Mat& frame) { - bool new_frame = _get_frame(frame); + const bool new_frame = _get_frame(frame); if (new_frame) { @@ -66,23 +67,26 @@ DEFUN_WARN_UNUSED Camera::result Camera::get_frame(cv::Mat& frame) cam_info.res_x = frame.cols; cam_info.res_y = frame.rows; cam_info.fov = fov; + + return result(true, cam_info); } else - qDebug() << "pt camera: can't get frame"; - - return result(new_frame, cam_info); + return result(false, CamInfo()); } DEFUN_WARN_UNUSED Camera::open_status Camera::start(int idx, int fps, int res_x, int res_y) { if (idx >= 0 && fps >= 0 && res_x >= 0 && res_y >= 0) { - if (!cap || !cap->isOpened() || - cam_desired.idx != idx || + if (cam_desired.idx != idx || cam_desired.fps != fps || cam_desired.res_x != res_x || - cam_desired.res_y != res_y) + cam_desired.res_y != res_y || + !cap || !cap->isOpened() || !cap->grab()) { + stop(); + + desired_name = get_camera_names().value(idx); cam_desired.idx = idx; cam_desired.fps = fps; cam_desired.res_x = res_x; @@ -98,15 +102,14 @@ DEFUN_WARN_UNUSED Camera::open_status Camera::start(int idx, int fps, int res_x, if (cam_desired.fps) cap->set(cv::CAP_PROP_FPS, cam_desired.fps); - if (cap->isOpened()) + if (cap->isOpened() && cap->grab()) { - qDebug() << "pt: opening camera"; - cam_info = CamInfo(); active_name = QString(); cam_info.idx = -1; dt_mean = 0; active_name = desired_name; + t.start(); return open_ok_change; @@ -117,14 +120,12 @@ DEFUN_WARN_UNUSED Camera::open_status Camera::start(int idx, int fps, int res_x, return open_error; } } - } - else - { - stop(); - return open_error; + + return open_ok_no_change; } - return open_ok_no_change; + stop(); + return open_error; } void Camera::stop() @@ -144,7 +145,7 @@ DEFUN_WARN_UNUSED bool Camera::_get_frame(cv::Mat& frame) { if (cap->read(frame)) return true; - portable::sleep(14); + portable::sleep(1); } } return false; diff --git a/tracker-pt/ftnoir_tracker_pt.cpp b/tracker-pt/ftnoir_tracker_pt.cpp index 36eabcc9..2862efdb 100644 --- a/tracker-pt/ftnoir_tracker_pt.cpp +++ b/tracker-pt/ftnoir_tracker_pt.cpp @@ -59,16 +59,16 @@ void Tracker_PT::run() QTextStream log_stream(&log_file); #endif - maybe_reopen_camera(); - while((commands & ABORT) == 0) { CamInfo cam_info; - bool new_frame; + bool new_frame = false; { QMutexLocker l(&camera_mtx); - std::tie(new_frame, cam_info) = camera.get_frame(frame); + + if (likely(camera)) + std::tie(new_frame, cam_info) = camera.get_frame(frame); } if (new_frame) @@ -136,7 +136,6 @@ void Tracker_PT::maybe_reopen_camera() switch (status) { case Camera::open_error: - qDebug() << "can't start camera" << s.camera_name; break; case Camera::open_ok_change: frame = cv::Mat(); @@ -167,7 +166,10 @@ void Tracker_PT::start_tracker(QFrame* video_frame) video_frame->setLayout(layout.data()); //video_widget->resize(video_frame->width(), video_frame->height()); video_frame->show(); - start(); + + maybe_reopen_camera(); + + start(QThread::HighPriority); } void Tracker_PT::data(double *data) |