MemorySubscriber.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <mutex>
3#include <string>
4#include <type_traits>
5#include <vector>
6
10
18
20#include <armarx/navigation/core/aron/Events.aron.generated.h>
23
25{
26
27 MemorySubscriber::MemorySubscriber(const std::string& callerId,
29 callerId(callerId), memoryNameSystem(mns), lastMemoryPoll(armem::Time::Now())
30 {
31 const std::string memoryName = "Navigation";
32
34 memoryReader = memoryNameSystem.getReader(armem::MemoryID().withMemoryName(memoryName));
35
36 ARMARX_INFO << "MemorySubscriber: will handle all events for caller id " << QUOTED(callerId)
37 << " newer than " << lastMemoryPoll << ".";
38
39 // subscription api
40 armem::MemoryID subscriptionID;
41 subscriptionID.memoryName = memoryName;
42 subscriptionID.coreSegmentName = "Events";
43 subscriptionID.providerSegmentName = callerId;
44
46 subscriptionHandle =
47 memoryNameSystem.subscribe(subscriptionID, this, &MemorySubscriber::onEntityUpdate);
48 }
49
50 void
51 MemorySubscriber::onEntityUpdate(const std::vector<armem::MemoryID>& snapshotIDs)
52 {
53 ARMARX_VERBOSE << "Received " << snapshotIDs.size() << " events from memory";
54 const armem::client::QueryResult qResult = memoryReader.queryMemoryIDs(snapshotIDs);
55
56 if (not qResult.success) /* c++20 [[unlikely]] */
57 {
58 ARMARX_WARNING << deactivateSpam(0.1F) << "Memory lookup failed.";
59 return;
60 }
61
63 handleEvents(qResult.memory);
64 }
65
66 template <typename AronEventT, typename EventT>
67 auto
68 fromAron(const armem::wm::EntityInstance& entity, EventT& bo)
69 {
70 static_assert(std::is_base_of<armarx::aron::codegenerator::cpp::AronGeneratedClass,
71 AronEventT>::value);
72
73 // see events::Writer::storeImpl
74 const auto dataDict =
75 aron::data::Dict::DynamicCastAndCheck(entity.data()->getElement("data"));
76 ARMARX_CHECK_NOT_NULL(dataDict);
77 const auto dto = AronEventT::FromAron(dataDict);
78
79 core::fromAron(dto, bo);
80 return bo;
81 }
82
83 void
85 {
87
88 // only one event will be handled at a time
89 std::lock_guard g{eventHandlingMtx};
90
91 ARMARX_CHECK(memoryEntity.id().hasEntityName());
92 const auto& eventName = memoryEntity.id().entityName;
93
94 ARMARX_VERBOSE << "Handling event " << QUOTED(eventName);
95
96 // factory
98 {
101
103 }
104 else if (eventName == core::event_names::LocalPlanningFailed)
105 {
108
110 }
111 else if (eventName == core::event_names::MovementStarted)
112 {
115
116 movementStarted(evt);
117 }
118 else if (eventName == core::event_names::GoalReached)
119 {
122
123 goalReached(evt);
124 }
125 else if (eventName == core::event_names::WaypointReached)
126 {
129
130 waypointReached(evt);
131 }
133 {
136
138 }
139 else if (eventName == core::event_names::SafetyStopTriggered)
140 {
143
145 }
146 else if (eventName == core::event_names::UserAbortTriggered)
147 {
150
152 }
153 else if (eventName == core::event_names::InternalError)
154 {
157
158 internalError(evt);
159 }
160 else if (eventName == core::event_names::GlobalTrajectoryUpdated)
161 {
164
166 }
167 else if (eventName == core::event_names::LocalTrajectoryUpdated)
168 {
171
173 }
174 else
175 {
176 ARMARX_WARNING << "Unknown event " << QUOTED(eventName);
177 }
178 }
179
181
182 void
184 {
186
187 // clang-format off
188 const armem::wm::CoreSegment& coreSegment = memory
189 .getCoreSegment("Events");
190 // clang-format on
191
192 std::vector<armem::wm::EntityInstance> events;
193
194 coreSegment.forEachInstance([&](const armem::wm::EntityInstance& instance)
195 { events.push_back(instance); });
196
197 const auto sortByTimestampAsc = [](const armem::wm::EntityInstance& a,
198 const armem::wm::EntityInstance& b) -> bool
199 {
200 ARMARX_CHECK(a.id().hasTimestamp());
201 ARMARX_CHECK(b.id().hasTimestamp());
202
203 return a.id().timestamp < b.id().timestamp;
204 };
205
206 std::sort(events.begin(), events.end(), sortByTimestampAsc);
207
208 std::for_each(events.begin(), events.end(), [&](const auto& event) { handleEvent(event); });
209 }
210
211
212} // namespace armarx::navigation::client
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
#define QUOTED(x)
std::string coreSegmentName
Definition MemoryID.h:51
bool hasEntityName() const
Definition MemoryID.h:121
std::string entityName
Definition MemoryID.h:53
std::string memoryName
Definition MemoryID.h:50
std::string providerSegmentName
Definition MemoryID.h:52
The memory name system (MNS) client.
Client-side working memory core segment.
Client-side working entity instance.
Client-side working memory.
void handleEvents(const armem::wm::Memory &memory)
void handleEvent(const armem::wm::EntityInstance &memoryEntity)
MemorySubscriber(const std::string &callerId, armem::client::MemoryNameSystem &mns)
void onEntityUpdate(const std::vector< armem::MemoryID > &snapshotIDs)
void globalTrajectoryUpdated(const core::GlobalTrajectoryUpdatedEvent &event) override
void safetyThrottlingTriggered(const core::SafetyThrottlingTriggeredEvent &event) override
Will be called whenever safety throttling is triggered to a certain degree (configurable).
void movementStarted(const core::MovementStartedEvent &event) override
void localTrajectoryUpdated(const core::LocalTrajectoryUpdatedEvent &event) override
void safetyStopTriggered(const core::SafetyStopTriggeredEvent &event) override
Will be called whenever a safety stop is triggered.
void localPlanningFailed(const core::LocalPlanningFailedEvent &event) override
void waypointReached(const core::WaypointReachedEvent &event) override
Will be called whenever the navigator reached a user-defined waypoint.
void globalPlanningFailed(const core::GlobalPlanningFailedEvent &event) override
void userAbortTriggered(const core::UserAbortTriggeredEvent &event) override
Will be called whenever the user aborts the current navigation.
void internalError(const core::InternalErrorEvent &event) override
Will be called whenever an internal error occurs.
void goalReached(const core::GoalReachedEvent &event) override
Will be called whenever the navigator reached the goal.
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
aron::cpp::AronGeneratedClass AronGeneratedClass
This file is part of ArmarX.
auto fromAron(const armem::wm::EntityInstance &entity, EventT &bo)
const std::string SafetyStopTriggered
Definition events.h:24
const std::string SafetyThrottlingTriggered
Definition events.h:23
const std::string UserAbortTriggered
Definition events.h:25
const std::string LocalTrajectoryUpdated
Definition events.h:17
const std::string MovementStarted
Definition events.h:20
const std::string WaypointReached
Definition events.h:22
const std::string GlobalTrajectoryUpdated
Definition events.h:16
const std::string GlobalPlanningFailed
Definition events.h:18
const std::string InternalError
Definition events.h:26
const std::string LocalPlanningFailed
Definition events.h:19
void fromAron(const arondto::GlobalTrajectoryPoint &dto, GlobalTrajectoryPoint &bo)
Result of a QueryInput.
Definition Query.h:51
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58
Event describing that the global trajectory was updated.
Definition events.h:114
Event describing that the targeted goal was successfully reached.
Definition events.h:53
Event describing the occurance of an internal unhandled error.
Definition events.h:105
Event describing that the local trajectory was updated.
Definition events.h:122
Event describing that for security reasons, the robot was stopped completely.
Definition events.h:88
Event desciribing that a significant safety throttling factor was reached.
Definition events.h:72
Event describing that the user aborted the current execution.
Definition events.h:97
Event describing that a user-defined waypoint was successfully reached.
Definition events.h:61
#define ARMARX_TRACE
Definition trace.h:77