SkillProxy.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <mutex>
6#include <optional>
7#include <string>
8#include <unordered_map>
9
14
15#include <RobotAPI/interface/skills/SkillManagerInterface.h>
21
22namespace armarx
23{
24 namespace skills
25 {
27 {
28 public:
29 using Callback = std::function<void(const skills::SkillStatusUpdate&)>;
30 using CallbackID = std::uint64_t;
31
34 {
35 std::scoped_lock l{callbackMutex};
36 CallbackID id = nextId++;
37 callbacks[id] = cb;
38 return id;
39 }
40
41 void
43 {
44 std::scoped_lock l{callbackMutex};
45 callbacks.erase(id);
46 }
47
48 void
49 update(const skills::SkillStatusUpdate& statusUpdate)
50 {
51 std::scoped_lock l{callbackMutex};
52 for (auto& [id, cb] : callbacks)
53 {
54 cb(statusUpdate);
55 }
56 }
57
58 private:
59 std::mutex callbackMutex;
60 std::unordered_map<CallbackID, Callback> callbacks;
61 CallbackID nextId = 0;
62 };
63
64 extern SkillUpdateManager GlobalSkillUpdateManager;
65
66 /* Manages the remote execution of a skill and converts the ice types */
68 {
69 public:
70 /// We remove the default constructor as every skill proxy requires a manager
71 SkillProxy() = delete;
72
73 /// set the skill proxy using a skillId. Queries the manager to get the description.
74 SkillProxy(const manager::dti::SkillManagerInterfacePrx& manager,
75 const SkillID& skillId);
76
77 /// set the proxy using a skill description
78 SkillProxy(const manager::dti::SkillManagerInterfacePrx& manager,
79 const SkillDescription& skillDesc);
80
81 /// copy ctor
82 SkillProxy(const SkillProxy&) = delete;
83
84 // delete copy assignment
85 SkillProxy& operator=(const SkillProxy&) = delete;
86
88
89 /// get the skill description
91
92 /// get the skill id from the skill description
93 SkillID getSkillId() const;
94
95 // Provide a similar API as the skillprovider
96 /// execute a skill and block until skill terminates
98 executeSkill(const std::string& executorName,
99 const aron::data::DictPtr& params = nullptr) const;
100
101 /// execute a skill. Do not block during execution
102 SkillExecutionID executeSkillAsync(const std::string& executorName,
103 const aron::data::DictPtr& params = nullptr) const;
104
105 /// poll execution status and block until its null or terminated
106 std::optional<TerminatedSkillStatusUpdate>
107 join(const SkillExecutionID& executionId,
109 armarx::Frequency check_frequency = armarx::Frequency::Hertz(20)) const;
110
111 void updateSubSkillStatus(const skills::SkillStatusUpdate& statusUpdate);
112
113 /// ask skill to abort ASAP. Blocks until skill stopped
114 bool abortSkill(const SkillExecutionID& executionId) const;
115
116 /// ask skill to abort ASAP
117 bool abortSkillAsync(const SkillExecutionID& executionId) const;
118
119 // Utiliy methods
120 /// get the default parameters of the skill. TODO: Skill profiles in memory!
122
123 std::optional<SkillStatusUpdate>
124 getExecutionStatus(const SkillExecutionID& executionId) const;
125
126 protected:
127 manager::dti::SkillManagerInterfacePrx manager;
129
130 mutable std::mutex lastUpdatesMutex;
131 std::unordered_map<std::string, SkillStatusUpdate> lastUpdates;
133 };
134 } // namespace skills
135} // namespace armarx
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
static Frequency Hertz(std::int64_t hertz)
Definition Frequency.cpp:20
Base Class for all Logging classes.
Definition Logging.h:240
Represents a duration.
Definition Duration.h:17
Represents a frequency.
Definition Frequency.h:17
TerminatedSkillStatusUpdate executeSkill(const std::string &executorName, const aron::data::DictPtr &params=nullptr) const
execute a skill and block until skill terminates
SkillExecutionID executeSkillAsync(const std::string &executorName, const aron::data::DictPtr &params=nullptr) const
execute a skill. Do not block during execution
SkillProxy & operator=(const SkillProxy &)=delete
SkillDescription skillDescription
Definition SkillProxy.h:128
std::optional< TerminatedSkillStatusUpdate > join(const SkillExecutionID &executionId, armarx::Duration max_wait_time=armarx::Duration::Seconds(1), armarx::Frequency check_frequency=armarx::Frequency::Hertz(20)) const
poll execution status and block until its null or terminated
manager::dti::SkillManagerInterfacePrx manager
Definition SkillProxy.h:127
std::unordered_map< std::string, SkillStatusUpdate > lastUpdates
Definition SkillProxy.h:131
std::optional< SkillStatusUpdate > getExecutionStatus(const SkillExecutionID &executionId) const
SkillProxy()=delete
We remove the default constructor as every skill proxy requires a manager.
bool abortSkillAsync(const SkillExecutionID &executionId) const
ask skill to abort ASAP
bool abortSkill(const SkillExecutionID &executionId) const
ask skill to abort ASAP. Blocks until skill stopped
SkillID getSkillId() const
get the skill id from the skill description
SkillProxy(const SkillProxy &)=delete
copy ctor
aron::data::DictPtr getRootProfileParameters() const
get the default parameters of the skill. TODO: Skill profiles in memory!
void updateSubSkillStatus(const skills::SkillStatusUpdate &statusUpdate)
SkillDescription getSkillDescription() const
get the skill description
SkillUpdateManager::CallbackID callbackId
Definition SkillProxy.h:132
std::function< void(const skills::SkillStatusUpdate &)> Callback
Definition SkillProxy.h:29
void removeCallback(CallbackID id)
Definition SkillProxy.h:42
CallbackID addCallback(Callback cb)
Definition SkillProxy.h:33
void update(const skills::SkillStatusUpdate &statusUpdate)
Definition SkillProxy.h:49
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
This file is part of ArmarX.
SkillUpdateManager GlobalSkillUpdateManager
This file offers overloads of toIce() and fromIce() functions for STL container types.