Scheduler.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <experimental/memory>
5#include <functional>
6#include <cstdint>
7#include <map>
8#include <mutex>
9#include <optional>
10#include <set>
11#include <string>
12#include <vector>
13
14#include <Eigen/Core>
15
16#include <VirtualRobot/VirtualRobot.h>
17
22
25
27
28#include <armarx/view_selection/gaze_controller/ice/GazeControllerStatus.h>
33
35{
36
38 {
39 public:
50
51 struct Params
52 {
53 std::string robotName;
54 Eigen::Vector3f defaultTarget;
55 std::string gazeOriginFrameName = "AzureKinect_RGB";
56
57 // Visualization
63
65 };
66
67 Scheduler(const InjectedServices& srv, const Params& params);
68 virtual ~Scheduler();
69
71 void resetPriorityQueue();
72
73 /**
74 * @brief Queue a request.
75 *
76 * @param gazeTargetID The memory ID of the request snapshot. Carries the requester and the
77 * target name, and pins the exact request, so status updates can be
78 * attributed even when two targets share an entity name. An invalid ID
79 * (the internal idle target) suppresses status publishing.
80 */
82
83 /// Feed one report from the low-level controller.
85
86 private:
87 VirtualRobot::RobotPtr robot_ = nullptr;
88
89 gaze_targets::GazeTarget _idleGazeTarget;
90
91 // submit next target from queue based on priority and preemption attributes
92 // WARNING: can only be safely called after acquiring the targetLock
93 void scheduleNextTarget();
94
95 void submitControlTarget(const gaze_targets::GazeTarget& target);
96
97 /**
98 * @brief The single place where a target stops being the active one.
99 *
100 * Losing the head is not one outcome but two, and every caller has to make the same
101 * distinction: a target that was already reached gave its requester what it asked for
102 * (Released), one bumped before the gaze settled did not (Aborted). A target that stays
103 * queued is merely Preempted and may come back.
104 */
105 void leaveActiveSlot(const gaze_targets::GazeTarget& target,
106 const std::string& reason,
107 bool staysQueued);
108
109 /// The request snapshot a queued target came from, by target name. No entry for the idle
110 /// target, which nobody requested and which therefore gets no status updates.
111 std::optional<armem::MemoryID> gazeTargetIDOf(const std::string& targetName) const;
112
113 // an irreplaceable default target with low priority used if no other
114 // targets are submitted
115 void resetRequestedTargets();
116
117 void visualizeActiveTarget();
118
119 const InjectedServices srv;
120 const Params params;
121
122 void init();
123
124 // target managemant needs to be synchronized
125 std::atomic_bool _clearGazeTargets = false;
126 std::mutex _newGazeTargetsMutex;
127 std::vector<gaze_targets::GazeTarget> _newGazeTargets;
128 using RequestedTargets = std::multiset<gaze_targets::GazeTarget, std::greater<>>;
129 RequestedTargets requestedTargets;
131
132 /// Request snapshot per queued target name, for addressing status updates.
133 std::map<std::string, armem::MemoryID> gazeTargetIDs;
134
135 /// Identifies the control target currently handed to the low-level controller.
136 std::int64_t currentTargetId = 0;
137
138 /// Whether Reached/Aborted was already emitted for the current control target.
139 bool reachedEmitted = false;
140 bool unreachableEmitted = false;
141
142 GazeTargetReachedMonitor reachedMonitor;
143 armarx::DateTime currentDeadline = DateTime::Invalid();
146 };
147
148} // namespace armarx::view_selection::gaze_scheduler
static DateTime Invalid()
Definition DateTime.cpp:57
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
A simple triple buffer for lockfree comunication between a single writer and a single reader.
Represents a point in time.
Definition DateTime.h:25
Debounces the low-level controller's residual into a "reached" / "unreachable" decision for one contr...
void handleControllerStatus(const gaze_controller::GazeControllerStatus &status)
Feed one report from the low-level controller.
void submitToQueue(gaze_targets::GazeTarget target, armem::MemoryID gazeTargetID)
Queue a request.
Scheduler(const InjectedServices &srv, const Params &params)
Definition Scheduler.cpp:35
void handleTargetUpdate(const gaze_targets::GazeTarget &memoryTarget)
Business Object (BO) class of GazeTarget.
Definition GazeTarget.h:22
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
Periodic report of the low-level gaze controller's residual.
Thresholds deciding when the gaze counts as fixed on the active target.
std::experimental::observer_ptr< viz::Client > arviz
Definition Scheduler.h:44
std::experimental::observer_ptr< ControllerHandlerInterface > controllerHandler
Definition Scheduler.h:46
std::experimental::observer_ptr< StatusPublisherInterface > statusPublisher
Definition Scheduler.h:48
std::experimental::observer_ptr< armem::robot_state::VirtualRobotReader > virtualRobotReader
Definition Scheduler.h:43