diff options
-rw-r--r-- | tracker-trackhat/camera.cpp | 71 | ||||
-rw-r--r-- | tracker-trackhat/dialog.cpp | 4 | ||||
-rw-r--r-- | tracker-trackhat/dialog.ui | 4 | ||||
-rw-r--r-- | tracker-trackhat/frame.cpp | 13 | ||||
-rw-r--r-- | tracker-trackhat/settings.cpp | 2 | ||||
-rw-r--r-- | tracker-trackhat/trackhat.hpp | 7 |
6 files changed, 66 insertions, 35 deletions
diff --git a/tracker-trackhat/camera.cpp b/tracker-trackhat/camera.cpp index 1c87229e..5338f11c 100644 --- a/tracker-trackhat/camera.cpp +++ b/tracker-trackhat/camera.cpp @@ -37,7 +37,11 @@ trackhat_camera::trackhat_camera() s.set_raii_dtor_state(false); t.set_raii_dtor_state(false); - QObject::connect(t.b.get(), &options::bundle_::changed, + QObject::connect(&t.exposure, options::value_::value_changed<options::slider_value>(), + &sig, &trackhat_impl::setting_receiver::settings_changed, + Qt::DirectConnection); + + QObject::connect(&t.threshold, options::value_::value_changed<options::slider_value>(), &sig, &trackhat_impl::setting_receiver::settings_changed, Qt::DirectConnection); } @@ -49,34 +53,36 @@ trackhat_camera::~trackhat_camera() pt_camera::result trackhat_camera::get_frame(pt_frame& frame_) { +start: + if (status < th_running || error_code != TH_SUCCESS) + goto error; + if (sig.test_and_clear()) { set_pt_options(); - qDebug() << "tracker/trackhat: set regs"; - init_regs(); + if ((error_code = (decltype(error_code))init_regs()) != TH_SUCCESS) + goto error; } - auto& ret = *frame_.as<trackhat_frame>(); - trackhat_frame frame; - trackHat_ExtendedPoints_t points = {}; - - if (status < th_running || error_code != TH_SUCCESS) - goto error; - - if (TH_ErrorCode error = trackHat_GetDetectedPointsExtended(&device, &points); error != TH_SUCCESS) + if (trackHat_ExtendedPoints_t points = {}; + (error_code = trackHat_GetDetectedPointsExtended(&device, &points)) == TH_SUCCESS) { - error_code = error; - goto error; + auto& frame = *frame_.as<trackhat_frame>(); + frame.init_points(points, t.min_pt_size, t.max_pt_size); } + else + goto error; - ret.init_points(points, s.min_point_size, s.max_point_size); return {true, get_desired()}; error: if (status >= th_running) + { qDebug() << "trackhat: error" << (void*)error_code; - ret.init_points(points, s.min_point_size, s.max_point_size); + } stop(); + if (start(s)) + goto start; return {false, get_desired()}; } @@ -93,23 +99,40 @@ error: int trackhat_camera::init_regs() { - constexpr uint8_t regs[][3] = { - { 0x0c, 0x0f, 0xf0 }, // exposure lo - { 0x0c, 0x10, 0x7f }, // exposure hi - { 0x00, 0x0b, 0xff }, // blob area max size - { 0x00, 0x0c, 0x03 }, // blob area min size - { 0x00, 0x01, 0x01 }, // bank0 sync - { 0x01, 0x01, 0x01 }, // bank1 sync + auto exp = (uint8_t)t.exposure; + auto thres = (uint8_t)t.threshold; + unsigned attempts = 0; + constexpr unsigned max_attempts = 5; + + const uint8_t regs[][3] = { + { 0x0c, 0x0f, 0xf0 }, // exposure lo + { 0x0c, 0x10, exp }, // exposure hi + { 0x00, 0x0b, 0xff }, // blob area max size + { 0x00, 0x0c, 0x03 }, // blob area min size + { 0x0c, 0x47, thres }, // min brightness + { 0x00, 0x0f, (uint8_t)(thres/10) }, // brightness margin, formula is `thres >= px > thres - fuzz' + { 0x00, 0x01, 0x01 }, // bank0 sync + { 0x01, 0x01, 0x01 }, // bank1 sync }; +start: + for (const auto& reg : regs) { trackHat_SetRegister_t r{reg[0], reg[1], reg[2]}; if (TH_ErrorCode error = trackHat_SetRegisterValue(&device, &r); error != TH_SUCCESS) - return error; + goto error; } return TH_SUCCESS; +error: + if (attempts++ < max_attempts) + { + portable::sleep(50); + goto start; + } + + return error_code; } bool trackhat_camera::start(const pt_settings&) @@ -123,7 +146,7 @@ bool trackhat_camera::start(const pt_settings&) start: stop(); - trackHat_EnableDebugMode(); + trackHat_DisableDebugMode(); [[maybe_unused]] uint32_t uptime = 0; error_code = TH_SUCCESS; diff --git a/tracker-trackhat/dialog.cpp b/tracker-trackhat/dialog.cpp index c51b0d59..6a9ad56c 100644 --- a/tracker-trackhat/dialog.cpp +++ b/tracker-trackhat/dialog.cpp @@ -24,6 +24,10 @@ trackhat_dialog::trackhat_dialog() for (auto x : model_types) ui.model_type->addItem(QIcon{}, tr(x.name), (QVariant)(int)x.t); + ui.exposure_label->setValue((int)*t.exposure); + ui.threshold_label->setValue((int)*t.threshold); + ui.point_filter_label->setValue((int)*t.point_filter_coefficient); + tie_setting(t.min_pt_size, ui.min_point_size); tie_setting(t.max_pt_size, ui.max_point_size); tie_setting(t.exposure, ui.exposure_slider); diff --git a/tracker-trackhat/dialog.ui b/tracker-trackhat/dialog.ui index ade5f6b2..5eb21956 100644 --- a/tracker-trackhat/dialog.ui +++ b/tracker-trackhat/dialog.ui @@ -39,10 +39,10 @@ </sizepolicy> </property> <property name="maximum"> - <number>65535</number> + <number>239</number> </property> <property name="pageStep"> - <number>50</number> + <number>10</number> </property> <property name="orientation"> <enum>Qt::Horizontal</enum> diff --git a/tracker-trackhat/frame.cpp b/tracker-trackhat/frame.cpp index c008ac87..750c46df 100644 --- a/tracker-trackhat/frame.cpp +++ b/tracker-trackhat/frame.cpp @@ -75,23 +75,25 @@ void trackhat_preview::draw_points() ? cv::Scalar{255, 255, 0} : cv::Scalar{192, 192, 192}; - cv::ellipse(frame_bgr, cv::Point{x, y}, {W, H}, + cv::ellipse(frame_bgr, {x, y}, {W, H}, 0, 0, 360, point_color, -1, cv::LINE_AA); cv::ellipse(frame_bgr, {x, y}, {iround(W + 2*dpi), iround(H + 2*dpi)}, 0, 0, 360, outline_color, iround(dpi), cv::LINE_AA); } } -void trackhat_frame::init_points(trackHat_ExtendedPoints_t points_, double min_size, double max_size) +void trackhat_frame::init_points(const trackHat_ExtendedPoints_t& points_, double min_size, double max_size) { - std::sort(std::begin(points_.m_point), std::end(points_.m_point), + trackHat_ExtendedPoints_t copy = points_; + + std::sort(std::begin(copy.m_point), std::end(copy.m_point), [](trackHat_ExtendedPoint_t p1, trackHat_ExtendedPoint_t p2) { return p1.m_averageBrightness > p2.m_averageBrightness; }); unsigned i = 0; - for (const trackHat_ExtendedPoint_t& pt : points_.m_point) + for (const trackHat_ExtendedPoint_t& pt : copy.m_point) { if (pt.m_averageBrightness == 0) continue; @@ -110,4 +112,7 @@ void trackhat_frame::init_points(trackHat_ExtendedPoints_t points_, double min_s points[i++] = p; } + + for (; i < std::size(points); i++) + points[i] = {}; } diff --git a/tracker-trackhat/settings.cpp b/tracker-trackhat/settings.cpp index a2fd5c8c..ab9329b1 100644 --- a/tracker-trackhat/settings.cpp +++ b/tracker-trackhat/settings.cpp @@ -61,6 +61,4 @@ void trackhat_camera::set_pt_options() s.active_model_panel = t.model == model_cap ? 1 : 0; s.enable_point_filter = t.enable_point_filter; s.point_filter_coefficient = *t.point_filter_coefficient; - - s.b->save(); } diff --git a/tracker-trackhat/trackhat.hpp b/tracker-trackhat/trackhat.hpp index 2cdc083b..9f87da66 100644 --- a/tracker-trackhat/trackhat.hpp +++ b/tracker-trackhat/trackhat.hpp @@ -1,10 +1,11 @@ #pragma once #include "../tracker-pt/pt-api.hpp" -#include "track_hat_driver.h" #include "compat/macros.hpp" #include "options/options.hpp" +#include <track_hat_driver.h> + #include <array> #include <atomic> #include <opencv2/core.hpp> @@ -25,7 +26,7 @@ using namespace options; struct trackhat_settings : opts { trackhat_settings(); - value<slider_value> exposure{b, "exposure", {0xfff0, 1, 0xfff0}}; + value<slider_value> exposure{b, "exposure", {0x80, 0x10, 0xff}}; value<slider_value> threshold{b, "threshold", {0x97, 64, 0xff}}; value<model_type> model{b, "model", model_mini_clip_left}; value<double> min_pt_size{b, "min-point-size", 2}; @@ -111,7 +112,7 @@ private: struct trackhat_frame final : pt_frame { - void init_points(trackHat_ExtendedPoints_t points, double min_size, double max_size); + void init_points(const trackHat_ExtendedPoints_t& points, double min_size, double max_size); trackhat_frame() = default; ~trackhat_frame() override = default; |