TPIK  1.0
Task Priority Inverse Kinematics
Loading...
Searching...
No Matches
ActionManager.h
Go to the documentation of this file.
1#ifndef __ACTIONMANAGER_H__
2#define __ACTIONMANAGER_H__
3
4#include "Action.h"
5#include "PriorityLevel.h"
6#include "TPIKDefines.h"
7#include "TPIKExceptions.h"
8#include <chrono>
9#include <iostream>
10#include <vector>
11
12namespace tpik {
13/*
14* @brief Action Manager Class.
15* @details Implementation of the Action Manager class. Such class offers an API in order to create the tpik::PriorityLevel, create the unified hierarchy,
16* define the tpik::Action.
17* The tpik::ActionManager can be used in simulation by setting the related boolean to true (via SetIsSimulation()) and setting the simulationTime (via SetTime()).
18* @note The order in which the tpik::PriorityLevel are created defines their priority in the unified hierarchy.
19*/
21public:
22 /*
23 * @brief Action Manager Default constructor.
24 * @details The ActionManager is, by default, NOT used in a simulation, hence the time is syncronized with the system clock time.
25 * @note The default current action is an empty action.
26 */
28 /*
29 * @brief Action Manager Default deconstructor.
30 */
32 /*
33 * @brief Method creating a new tpik::PriorityLevel in the unified hierarchy.
34 * The order in which the tpik::PriorityLevel are created defines their priority in the unified hierarchy.
35 * @param[in] priorityLevelID priority level id.
36 */
37 void AddPriorityLevel(const std::string priorityLevelID);
38 /*
39 * @brief Method creating a new tpik::PriorityLevel in the unified hierarchy and specifies its rml::RegularizationData.
40 * The order in which the tpik::PriorityLevel are created defines their priority in the unified hierarchy.
41 * @param[in] priorityLevelID priority level id.
42 * @param[in] regularizationData rml::RegularizationData of the PriorityLevel.
43 */
44 void AddPriorityLevelWithRegularization(const std::string priorityLevelID, const rml::RegularizationData regularizationData);
45 /*
46 * @brief Method adding a tpik::Task to a tpik::PriorityLevel.
47 * @note An exception is thrown if the tpik::PriorityLevel does not exist in the unified hierarchy (Via GetPriorityLevel method).
48 * @param[in] task std::shared_ptr to the tpik::Task.
49 * @param[in] priorityLevelID tpik::PriorityLevel ID.
50 */
51 void AddTaskToPriorityLevel(const std::shared_ptr<Task> task, const std::string priorityLevelID);
52 /*
53 * @brief Method setting the unified hierarhcy
54 * @param unifiedHierarchy pl id ordered by priority
55 */
56 void SetUnifiedHierarchy(std::vector<std::string> unifiedHierarchy);
57 /*
58 * @brief Method adding a tpik::Action to the action list.
59 * @param[in] actionID Action ID.
60 * @param[in] priorityLevelsID std::vector containing the IDs of the tpik::PriorityLevel composing the tpik::Action.
61 */
62 void AddAction(const std::string actionID, const std::vector<std::string> priorityLevelsID);
63 /*
64 * @brief Method that sets the current Action.
65 * @param[in] newAction new current action ID.
66 * @param[out] bool Action changed: true, Action not changed: false
67 */
68 bool SetAction(const std::string newAction, bool transition);
69 /*
70 * @brief Method returning the current Action ID.
71 * @return current action id
72 */
73 auto CurrentActionID() const -> const std::string& { return currentAction_->ID(); }
74 /*
75 * @brief Method which computes and sets the external activation functions in the unified hierarchy tpik::PriorityLevel.
76 * The external activation functions depend on the current action, the past action and the time elapsed since the last change of action.
77 * @note An exception is thrown if the unified hierarchy has not been specified yet.
78 */
79 void ComputeActionTransitionActivation() noexcept(false);
80 /*
81 * @brief Method which returns the unified hierarchy.
82 * @return Unified Hierarchy.
83 * @note An exception is thrown if the unified hierarchy has not been specified yet.
84 */
85 const Hierarchy& GetHierarchy() const noexcept(false);
86 /*
87 * @brief Method which returns a tpik::PriorityLevel.
88 * @param[in] priorityLevelID ID of the tpik::PriorityLevel to find.
89 * @return std::shared_ptr to tpik::PriorityLevel.
90 * @note An exception is thrown if the priority level does not exist in the unified hierarchy.
91 */
92 auto GetPriorityLevel(const std::string priorityLevelID) const -> const std::shared_ptr<PriorityLevel>& { return priorityLevelIDMap_.at(priorityLevelID); }
93 /*
94 * @brief Method to set whether the actionManager is used in a simulation.
95 * @param[in] isSimulated true if it is simulated, false otherwise.
96 */
97 auto IsSimulation(bool isSimulated) -> void { isSimulated_ = isSimulated; }
98 /*
99 * @brief Method telling wether a task is present in the current action
100 */
101 bool IsTaskInCurrentAction(const std::string& task_id);
102 /*
103 * Method that set the transition duration, in milliseconds.
104 */
105 void SetTransitionDuration(double duration) { transitionDurationMs_ = duration; }
106 /*
107 * Method that gets the transition duration, in milliseconds.
108 */
109 auto GetTransitionDuration() const -> double { return transitionDurationMs_; }
110 /*
111 * @brief Method to set the time if the tpik::ActionManager is used in a simulation.
112 * @param[in] simulationTime currentSimulationTime in ms.
113 */
114 auto Time(long simulationTime) -> void { simulationTime_ = simulationBegin_ + std::chrono::milliseconds(simulationTime); }
115 /*
116 * @brief Method which returns the current time. If the tpik::ActionManager is simulated the simulation time is returned,
117 * else the current system clock is returned.
118 * @return current time.
119 */
120 const std::chrono::system_clock::time_point Time();
121 /*
122 * @brief Method which returns a tpik::Action.
123 * @param[in] ActionID ID of the Action to find.
124 * @return std::shared_ptr to Action.
125 * @note An exception is thrown if the action set is not present in the action list.
126 */
127 const std::shared_ptr<Action>& GetAction(const std::string& ActionID) noexcept(false);
128 /*
129 * @brief Overload of the cout operator
130 */
131 friend std::ostream& operator<<(std::ostream& os, ActionManager const& actionManager)
132 {
133 return os << "\033[1;37m"
134 << "ActionManager \n"
135 << std::setprecision(4) << "Current Action "
136 << "\033[0m" << *actionManager.currentAction_ << "\033[1;37m"
137 << "OldAction "
138 << "\033[0m" << *actionManager.oldAction_ << "\033[1;37m" << std::endl;
139 }
140
141protected:
142 std::unordered_map<std::string, std::shared_ptr<tpik::PriorityLevel>> priorityLevelIDMap_; //map of priority levels
143 std::unordered_map<std::string, bool> taskInCurrentActionMap_; //map of task presence in action
144 std::vector<std::shared_ptr<Action>> actions_; // The action list.
145 Hierarchy hierarchy_; // The unified hierarchy.
146 std::shared_ptr<Action> currentAction_; // The current action.
147 std::shared_ptr<Action> oldAction_; // The previous action.
148 std::chrono::system_clock::time_point time_; // The current time.
150 bool isSimulated_; // The boolean stating whether the action manager is used in a simulation.
151 std::chrono::system_clock::time_point simulationTime_; // The simulation time.
152 std::chrono::system_clock::time_point simulationBegin_; // The time when the simulation begin.
153 bool transitionInBetweenActions_; //Flag that enables transition between two action
154};
155}
156
157#endif
Definition ActionManager.h:20
void AddPriorityLevel(const std::string priorityLevelID)
std::chrono::system_clock::time_point simulationTime_
Definition ActionManager.h:151
bool SetAction(const std::string newAction, bool transition)
std::chrono::system_clock::time_point time_
Definition ActionManager.h:148
auto GetPriorityLevel(const std::string priorityLevelID) const -> const std::shared_ptr< PriorityLevel > &
Definition ActionManager.h:92
void SetUnifiedHierarchy(std::vector< std::string > unifiedHierarchy)
std::vector< std::shared_ptr< Action > > actions_
Definition ActionManager.h:144
auto Time(long simulationTime) -> void
Definition ActionManager.h:114
const Hierarchy & GetHierarchy() const noexcept(false)
std::chrono::system_clock::time_point simulationBegin_
Definition ActionManager.h:152
std::shared_ptr< Action > currentAction_
Definition ActionManager.h:146
std::unordered_map< std::string, bool > taskInCurrentActionMap_
Definition ActionManager.h:143
bool transitionInBetweenActions_
Definition ActionManager.h:153
void AddAction(const std::string actionID, const std::vector< std::string > priorityLevelsID)
auto GetTransitionDuration() const -> double
Definition ActionManager.h:109
std::unordered_map< std::string, std::shared_ptr< tpik::PriorityLevel > > priorityLevelIDMap_
Definition ActionManager.h:142
~ActionManager()
Definition ActionManager.h:31
const std::shared_ptr< Action > & GetAction(const std::string &ActionID) noexcept(false)
void ComputeActionTransitionActivation() noexcept(false)
friend std::ostream & operator<<(std::ostream &os, ActionManager const &actionManager)
Definition ActionManager.h:131
double transitionDurationMs_
Definition ActionManager.h:149
std::shared_ptr< Action > oldAction_
Definition ActionManager.h:147
const std::chrono::system_clock::time_point Time()
auto CurrentActionID() const -> const std::string &
Definition ActionManager.h:73
Hierarchy hierarchy_
Definition ActionManager.h:145
bool IsTaskInCurrentAction(const std::string &task_id)
void AddPriorityLevelWithRegularization(const std::string priorityLevelID, const rml::RegularizationData regularizationData)
void AddTaskToPriorityLevel(const std::shared_ptr< Task > task, const std::string priorityLevelID)
auto IsSimulation(bool isSimulated) -> void
Definition ActionManager.h:97
void SetTransitionDuration(double duration)
Definition ActionManager.h:105
bool isSimulated_
Definition ActionManager.h:150
Definition PriorityLevel.h:19
Definition Action.h:9
std::vector< std::shared_ptr< tpik::PriorityLevel > > Hierarchy
Definition Action.h:13