1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#include "trackhat.hpp"
#include "compat/sleep.hpp"
#include "compat/timer.hpp"
namespace trackhat_impl {
trackhat_settings::trackhat_settings() : opts{"tracker-trackhat"}
{
}
void setting_receiver::settings_changed()
{
changed = true;
}
bool setting_receiver::test_and_clear()
{
bool x = true;
return changed.compare_exchange_strong(x, false);
}
setting_receiver::setting_receiver(bool value) : changed{value}
{
}
} // ns trackhat_impl
void trackhat_camera::set_pt_options()
{
s.min_point_size = t.min_pt_size;
s.max_point_size = t.max_pt_size;
switch (t.model)
{
default:
case model_cap:
s.t_MH_x = 0; s.t_MH_y = 0; s.t_MH_z = 0;
break;
case model_mystery_meat:
case model_clip_left:
case model_mini_clip_left:
s.t_MH_x = -135; s.t_MH_y = 0; s.t_MH_z = 0;
break;
case model_clip_right:
case model_mini_clip_right:
s.t_MH_x = 135; s.t_MH_y = 0; s.t_MH_z = 0;
break;
}
switch (t.model)
{
default:
eval_once(qDebug() << "tracker/trackhat: unknown model");
[[fallthrough]];
case model_clip_left:
case model_clip_right:
s.clip_tz = 27; s.clip_ty = 43; s.clip_by = 62; s.clip_bz = 74;
break;
case model_mini_clip_left:
case model_mini_clip_right:
s.clip_tz = 13; s.clip_ty = 42; s.clip_by = 60; s.clip_bz = 38;
break;
case model_cap:
s.cap_x = 60; s.cap_y = 90; s.cap_z = 95;
break;
case model_mystery_meat:
break;
}
s.dynamic_pose = t.model == model_cap;
s.init_phase_timeout = 500;
s.camera_name = "TrackHat Sensor (WIP)";
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;
}
bool trackhat_camera::set_regs(const uint8_t(*regs)[3], unsigned len)
{
unsigned attempts = 0;
constexpr unsigned max_attempts = 5;
start:
for (unsigned i = 0; i < len; i++)
{
const auto& reg = regs[i];
trackHat_SetRegister_t r{reg[0], reg[1], reg[2]};
auto status = th_check(trackHat_SetRegisterValue(&*device, &r));
if (status == TH_SUCCESS)
continue;
else if (status == TH_FAILED_TO_SET_REGISTER)
goto retry;
else if (status == TH_ERROR_DEVICE_COMUNICATION_TIMEOUT)
goto retry;
else
goto error;
}
return true;
retry:
if (attempts++ < max_attempts)
{
auto dbg = qDebug();
dbg << "tracker/trackhat: set register retry attempt";
dbg.space(); dbg.nospace();
dbg << attempts << "/" << max_attempts;
portable::sleep(50);
goto start;
}
error:
return false;
}
bool trackhat_camera::init_regs()
{
auto exp = (uint8_t)t.exposure;
auto exp2 = (uint8_t)(exp == 0xff ? 0xf0 : 0xff);
auto thres = (uint8_t)t.threshold;
auto thres2 = (uint8_t)std::clamp((int)*t.threshold_2, 0, std::max(64, thres-1));
auto gain = (uint8_t)((int)*t.gain);
auto gain_c = (uint8_t)(gain/0x10);
gain %= 0x10; gain_c %= 4;
const uint8_t regs[][3] = {
{ 0x0c, 0x0f, exp2 }, // exposure lo
{ 0x0c, 0x10, exp }, // exposure hi
{ 0x00, 0x0b, 0xff }, // blob area max size
{ 0x00, 0x0c, 0x03 }, // blob area min size
{ 0x0c, 0x08, gain }, // gain
{ 0x0c, 0x0c, gain_c }, // gain multiplier
{ 0x0c, 0x47, thres }, // min brightness
{ 0x00, 0x0f, thres2 }, // brightness margin, formula is `thres >= px > thres - fuzz'
{ 0x00, 0x01, 0x01 }, // bank0 sync
{ 0x01, 0x01, 0x01 }, // bank1 sync
};
Timer t;
if (!set_regs(regs, std::size(regs)))
{
device.disconnect();
return false;
}
if (int elapsed = (int)t.elapsed_ms(); elapsed > 500)
qDebug() << "tracker/trackhat: setting registers took" << elapsed << "ms";
return true;
}
|