InterventionObserver.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::InterventionObserver
17 * @author Niklas Arlt ( niklas dot arlt at kit dot edu )
18 * @date 2026
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25
26#include <atomic>
27#include <mutex>
28#include <set>
29
31#include <ArmarXCore/interface/components/EmergencyStopInterface.h>
32
33#include <RobotAPI/interface/components/InterventionObserverInterface.h>
34#include <RobotAPI/interface/units/PlatformUnitInterface.h>
39
40namespace armarx
41{
42 /**
43 * @class InterventionObserverPropertyDefinitions
44 * @brief
45 */
47 {
48 public:
51 {
53 "PlatformUnitName", "Armar6IcePlatformUnit", "Name of the platform unit to use");
55 "GamepadTopicName", "GamepadValues", "Name of the Gamepad Topic");
57 "EmergencyStopTopic", "EmergencyStop", "Name of the Emergency Stop Topic");
59 "AgentName", "ARMAR-7", "Agent name used in TaskOutcome commits");
61 "AssistanceSessionTimeoutSeconds",
62 2.0f,
63 "Seconds of inactivity before an assistance session is committed to memory");
64 }
65 };
66
67 /**
68 * @defgroup Component-InterventionObserver InterventionObserver
69 * @ingroup RobotAPI-Components
70 * @class InterventionObserver
71 * @ingroup Component-InterventionObserver
72 * @brief Observes human interventions during autonomous robot execution and
73 * records them as TaskOutcome entries in the failure memory.
74 *
75 * Listens to gamepad and emergency stop topics. When the operator suspends
76 * the robot (back button / emergency stop), a SUSPENDED TaskOutcome is
77 * committed whose skill name is resolved from the Skill memory's latest
78 * SkillEvent. When the operator resumes (start button / emergency stop
79 * release), the most recent SUSPENDED entry is updated with recovery info.
80 *
81 * Additionally tracks continuous gamepad usage as "assistance sessions"
82 * (e.g. manual platform navigation) and commits a summary once the
83 * operator stops providing input for a configurable timeout period.
84 */
86 virtual public armarx::Component,
87 virtual public InterventionObserverInterface,
89 {
90 public:
92
93 /**
94 * @see armarx::ManagedIceObject::getDefaultName()
95 */
96 std::string
97 getDefaultName() const override
98 {
99 return "InterventionObserver";
100 }
101
102 protected:
103 /**
104 * @see armarx::ManagedIceObject::onInitComponent()
105 */
106 void onInitComponent() override;
107
108 /**
109 * @see armarx::ManagedIceObject::onConnectComponent()
110 */
111 void onConnectComponent() override;
112
113 /**
114 * @see armarx::ManagedIceObject::onDisconnectComponent()
115 */
116 void onDisconnectComponent() override;
117
118 /**
119 * @see armarx::ManagedIceObject::onExitComponent()
120 */
121 void onExitComponent() override;
122
123 /**
124 * @see PropertyUser::createPropertyDefinitions()
125 */
127
128 public:
129 void reportGamepadState(const std::string& device,
130 const std::string& name,
131 const GamepadData& data,
132 const TimestampBasePtr& timestamp,
133 const Ice::Current& c) override;
134
135 void reportEmergencyStopState(EmergencyStopState state, const Ice::Current&) override;
136
137 private:
138 /// Lazily connects to the TaskOutcome and Skill memory servers.
139 /// Called on every incoming event; returns false if not yet available.
140 bool ensureMemoryConnection();
141
142 /// Queries the Skill memory's SkillEvent segment for the most recently
143 /// started skill and returns its name. Falls back to "unknown".
144 std::string queryLatestSkillName();
145
146 // TaskOutcome memory: writer commits interventions, reader queries
147 // previous SUSPENDED entries when checking for recovery.
150
151 // Skill memory reader: used to look up which skill was running when
152 // an intervention (emergency stop / gamepad back) occurred.
153 armem::client::Reader skillMemoryReader;
154
155 std::atomic_bool memoryConnected{false};
156 // Protects the one-time connection setup against concurrent event callbacks.
157 std::mutex memoryConnectionMutex;
158
159 // Rising-edge detection state for gamepad buttons.
160 bool prevBackButton = false;
161 bool prevStartButton = false;
162
163 // State transition detection for emergency stop events.
164 EmergencyStopState prevEmergencyStopState = EmergencyStopState::eEmergencyStopInactive;
165
166 std::string agentName;
167 static constexpr const char* providerName = "InterventionObserver";
168
169 // Gamepad assistance session tracking
170 bool assistanceSessionActive = false;
171 armarx::core::time::DateTime assistanceSessionStart;
172 armarx::core::time::DateTime assistanceLastActivityTime;
173 std::set<std::string> assistanceActionsInSession;
174 float assistanceSessionTimeoutSeconds = 2.0f;
175 std::string lastGamepadDevice;
176 std::string lastGamepadName;
177 };
178} // namespace armarx
std::string timestamp()
constexpr T c
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportEmergencyStopState(EmergencyStopState state, const Ice::Current &) override
void reportGamepadState(const std::string &device, const std::string &name, const GamepadData &data, const TimestampBasePtr &timestamp, const Ice::Current &c) override
std::string getDefaultName() const override
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
Reads data from a memory server.
Definition Reader.h:25
Represents a point in time.
Definition DateTime.h:25
plugins::ListeningPluginUser ComponentPluginUser
Definition plugins.h:10
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.