Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
control_logic.h
Go to the documentation of this file.
1
13#pragma once
14
15#include "aim_policy.h"
16#include "button_classifier.h"
17#include "calibration_session.h"
18#include "control_status.h"
19#include "remote_inbox.h"
21#include "track_snapshot.h"
22
23#include <cstdint>
24#include <optional>
25
26namespace control
27{
29constexpr uint32_t kJogPeriodMs = 40;
30
32constexpr uint32_t kHudRecordTimeoutMs = 1000;
33
36 int x = 0;
37 int y = 0;
38};
39
42 std::optional<remote::RemoteMode> mode;
43 std::optional<remote::CalAction> cal;
44 std::optional<remote::AimPixel> aim;
45};
46
61
63struct LogRecord {
64 LogEvent event = LogEvent::ModeChanged;
66 int points = 0;
67 const char* failure = nullptr;
68};
69
77{
78public:
79 virtual ~ControlPorts() = default;
80
82 virtual StickDeflection read_stick() noexcept = 0;
83
85 virtual remote::Jog read_hud_jog() noexcept = 0;
86
88 virtual HudRequests take_hud_requests() noexcept = 0;
89
91 virtual TrackSnapshot read_track() noexcept = 0;
92
94 virtual aiming::Angles turret_pose() noexcept = 0;
95
97 virtual float turret_pan_speed() noexcept = 0;
98
100 virtual void turret_jog(float pan, float tilt) noexcept = 0;
101
103 virtual void turret_set_target(const aiming::Angles& target) noexcept = 0;
104
106 virtual void turret_home() noexcept = 0;
107
109 virtual void reset_detection() noexcept = 0;
110
112 virtual bool save_calibration(const aiming::AimMap& map) noexcept = 0;
113
115 virtual void log(const LogRecord& record) noexcept = 0;
116};
117
148{
149public:
151 explicit ControlLogic(ControlPorts& io) : io_(io), policy_(aiming::default_map()) {}
152
154 void load_calibration(const aiming::AimMap& map);
155
162 void run(uint32_t now_ms, bool held);
163
170 ControlStatus status(uint32_t now_ms, const aiming::Angles& pose, remote::JogSpeed speed) const;
171
173 ControlMode mode() const { return mode_; }
174
175private:
176 void jog_from_inputs(uint32_t now_ms, bool force_stop);
177 void stop_jog();
178 void set_mode(ControlMode mode);
179 void enter_calibration();
180 void cancel_calibration();
181 void record_cal_point(const aiming::Angles& pose);
182 void apply_cal_request(remote::CalAction action, uint32_t now_ms);
183 void service_hud_record(uint32_t now_ms);
184 void on_long_press();
185 void on_short_press();
186
187 ControlPorts& io_;
189 aiming::AimPolicy policy_;
192 ControlMode mode_before_cal_ = ControlMode::Ai;
193 bool calibrated_ = false;
194 uint32_t cal_saves_ = 0;
195 uint32_t cal_miss_ = 0;
196 bool hud_record_pending_ = false;
197 uint32_t hud_record_since_ms_ = 0;
198 bool aim_hold_ = false;
199 bool stick_ = false;
200 uint32_t aims_ = 0;
201 bool prev_held_ = false;
202 aiming::Angles cal_pose_at_press_;
203 bool cal_pose_captured_ = false;
204 uint32_t last_jog_ms_ = 0;
205};
206} // namespace control
Decides where the turret should point from the tracker state (pure C++20, natively tested).
Turns a raw push-button level into Short / Long press events (pure C++20, natively tested).
Two-point joystick calibration as a state machine (pure C++20, natively tested).
Definition aim_policy.h:41
Definition button_classifier.h:23
Definition calibration_session.h:21
The control loop's decisions, one pass per run() call: button gestures, HUD requests,...
Definition control_logic.h:148
ControlMode mode() const
The active operating mode.
Definition control_logic.h:173
ControlLogic(ControlPorts &io)
Definition control_logic.h:151
Everything ControlLogic reads from or commands on the device, called from the loop task.
Definition control_logic.h:77
virtual void turret_set_target(const aiming::Angles &target) noexcept=0
Absolute move (BaseTurret::set_target_deg).
virtual void turret_jog(float pan, float tilt) noexcept=0
Manual velocity per axis, -1..1 (BaseTurret::jog).
virtual TrackSnapshot read_track() noexcept=0
Latest tracker output from the detection module.
virtual StickDeflection read_stick() noexcept=0
Current joystick deflection. Read only on a jog pass when the jog is not forced to stop.
virtual void log(const LogRecord &record) noexcept=0
Emits one log line.
virtual float turret_pan_speed() noexcept=0
Current pan stepper speed, 0 at rest (BaseTurret::pan_speed).
virtual HudRequests take_hud_requests() noexcept=0
Takes (and clears) the HUD's pending mode, calibration and aim requests.
virtual aiming::Angles turret_pose() noexcept=0
Current turret pose (BaseTurret::position_deg).
virtual void reset_detection() noexcept=0
Asks the detection task to drop its track and start fresh (BaseDetectionModule::reset).
virtual void turret_home() noexcept=0
Return home (BaseTurret::home).
virtual remote::Jog read_hud_jog() noexcept=0
The HUD's jog with the clock read at this call, under the inbox lock (dead man; see Controller).
virtual bool save_calibration(const aiming::AimMap &map) noexcept=0
Persists a fresh calibration; false if the write failed.
LogEvent
Something worth a log line; the glue picks the level and the text.
Definition control_logic.h:48
@ CalPointRecorded
info; LogRecord::points is the number of points now recorded
@ HudPoseTimeout
warning: the pan still moved kHudRecordTimeoutMs after cal:point, current pose used
@ CalComplete
info: the fit is active, back to Ai
@ CalSaveFailed
error: the NVS write failed, the fit stays active until reboot
@ CalRestarted
info: a record after a failed fit restarted at point 1
@ CalStarted
info; LogRecord::mode is the mode calibration was started from
@ PressPoseFallback
warning: no stationary pose was captured during the press, current pose used
@ CalMiss
warning: record ignored, no target visible
@ CalCancelled
info; LogRecord::mode is the restored mode
@ CalFailed
warning; LogRecord::failure is the session's reason (may be null)
@ ModeChanged
info; LogRecord::mode is the new mode
constexpr uint32_t kJogPeriodMs
Minimum time between jog passes in User and Calibrate (25 Hz).
Definition control_logic.h:29
constexpr uint32_t kHudRecordTimeoutMs
Longest wait for the pan to stop before a HUD cal:point records the current pose anyway.
Definition control_logic.h:32
Read-only view of the controller state for the HUD and stats line.
JogSpeed
HUD jog magnitude setting.
Definition remote_command.h:28
CalAction
Calibration action requested by the HUD; same semantics as the physical button.
Definition remote_command.h:21
Holds the latest remote command state with a dead-man jog timeout (pure C++20, natively tested).
Snapshot of the controller, published once per control pass.
Definition control_status.h:17
Definition track_snapshot.h:14
pan = pan_a * x + pan_b, tilt = tilt_a * y + tilt_b.
Definition aim_map.h:50
Turret pose: pan degrees from home (positive = right), tilt servo degrees (larger = up).
Definition aim_map.h:30
The HUD's one-shot requests, taken from the inbox together once per pass.
Definition control_logic.h:41
One log event and its arguments; fields other than the event's own are left at their defaults.
Definition control_logic.h:63
Joystick deflection per axis, -255..255 (Joystick::get_speed_x / get_speed_y); y positive = up.
Definition control_logic.h:35
Operating modes of the turret system.
ControlMode
Who drives the turret right now.
Definition system_control_types.h:14
Plain copies of the tracker output and detection timings, safe to hand across tasks.