wbc
Scene.hpp
Go to the documentation of this file.
1#ifndef Scene_HPP
2#define Scene_HPP
3
4#include "TaskStatus.hpp"
5#include "Constraint.hpp"
7#include "RobotModel.hpp"
8#include "QPSolver.hpp"
9#include "SceneConfig.hpp"
10
11namespace wbc{
12
16class Scene{
17protected:
20 std::vector< std::vector<TaskPtr> > tasks;
21 std::vector< std::vector<ConstraintPtr> > constraints;
24 std::vector<int> n_task_variables_per_prio;
26 base::commands::Joints solver_output_joints;
28 std::vector<TaskConfig> wbc_config;
29 base::VectorXd solver_output;
30
34 virtual TaskPtr createTask(const TaskConfig &config) = 0;
35
39 void clearTasks();
40
41public:
43 ~Scene();
44
49 virtual bool configure(const std::vector<TaskConfig> &config);
50
55 virtual const HierarchicalQP& update() = 0;
56
61 virtual const base::commands::Joints& solve(const HierarchicalQP& hqp) = 0;
62
68 void setReference(const std::string& task_name, const base::samples::Joints& ref);
69
75 void setReference(const std::string& task_name, const base::samples::RigidBodyStateSE3& ref);
76
82 void setTaskWeights(const std::string& task_name, const base::VectorXd &weights);
88 void setTaskActivation(const std::string& task_name, double activation);
92 TaskPtr getTask(const std::string& name);
93
97 bool hasTask(const std::string& name);
98
102 const TasksStatus& getTasksStatus() const { return tasks_status; }
103
107 static void sortTaskConfig(const std::vector<TaskConfig>& config, std::vector< std::vector<TaskConfig> >& sorted_config);
108
112 static uint getNTaskVariables(const std::vector<TaskConfig>& config);
113
117 static std::vector<int> getNTaskVariablesPerPrio(const std::vector<TaskConfig>& config);
118
122 virtual const TasksStatus& updateTasksStatus() = 0;
123
128
132 const base::commands::Joints& getSolverOutput() const { return solver_output_joints; }
133
137 const base::VectorXd& getSolverOutputRaw() const { return solver_output; }
138
142 void setJointWeights(const JointWeights& weights);
143
147 const JointWeights& getJointWeights() const { return joint_weights; }
148
153
158
163
167 std::vector<TaskConfig> getWbcConfig(){return wbc_config;}
168
169};
170
171typedef std::shared_ptr<Scene> ScenePtr;
172
173template<typename T,typename RobotModelPtr,typename QPSolverPtr> Scene* createT(RobotModelPtr robot_model, QPSolverPtr solver, const double dt){return new T(robot_model,solver,dt);}
174
176 typedef std::map<std::string, Scene*(*)(RobotModelPtr, QPSolverPtr, double)> SceneMap;
177
178 static Scene *createInstance(const std::string& name, RobotModelPtr robot_model, QPSolverPtr solver, const double dt) {
179 SceneMap::iterator it = getSceneMap()->find(name);
180 if(it == getSceneMap()->end())
181 throw std::runtime_error("Failed to create instance of plugin " + name + ". Is the plugin registered?");
182 return it->second(robot_model, solver, dt);
183 }
184
185 template<typename T>
186 static T* createInstance(const std::string& name, RobotModelPtr robot_model, QPSolverPtr solver, const double dt){
187 Scene* tmp = createInstance(name, robot_model, solver, dt);
188 T* ret = dynamic_cast<T*>(tmp);
189 return ret;
190 }
191
193 if(!scene_map)
194 scene_map = new SceneMap;
195 return scene_map;
196 }
197
198 static void clear(){
199 scene_map->clear();
200 }
201
202private:
203 static SceneMap *scene_map;
204};
205
206template<typename T>
208 SceneRegistry(const std::string& name) {
209 SceneMap::iterator it = getSceneMap()->find(name);
210 if(it != getSceneMap()->end())
211 throw std::runtime_error("Failed to register plugin with name " + name + ". A plugin with the same name is already registered");
212
213 getSceneMap()->insert(std::make_pair(name, &createT<T,RobotModelPtr,QPSolverPtr>));
214 }
215};
216
217} // namespace wbc
218
219#endif
Definition QuadraticProgram.hpp:10
Base class for all wbc scenes.
Definition Scene.hpp:16
void setReference(const std::string &task_name, const base::samples::Joints &ref)
Set reference input for a joint space task.
Definition Scene.cpp:104
virtual const HierarchicalQP & update()=0
Update the wbc scene and return the (updated) optimization problem.
static uint getNTaskVariables(const std::vector< TaskConfig > &config)
Return number of tasks per priority, given the task config.
Definition Scene.cpp:171
HierarchicalQP hqp
Definition Scene.hpp:23
bool hasTask(const std::string &name)
True in case the given task exists.
Definition Scene.cpp:137
virtual bool configure(const std::vector< TaskConfig > &config)
Configure the WBC scene. Create tasks and sort them by priority.
Definition Scene.cpp:29
std::vector< std::vector< ConstraintPtr > > constraints
Definition Scene.hpp:21
std::vector< std::vector< TaskPtr > > tasks
Definition Scene.hpp:20
RobotModelPtr robot_model
Definition Scene.hpp:18
virtual const base::commands::Joints & solve(const HierarchicalQP &hqp)=0
Solve the given optimization problem.
void clearTasks()
Delete all tasks and free memory.
Definition Scene.cpp:17
const base::VectorXd & getSolverOutputRaw() const
Get current solver output in raw values.
Definition Scene.hpp:137
static void sortTaskConfig(const std::vector< TaskConfig > &config, std::vector< std::vector< TaskConfig > > &sorted_config)
Sort task config by the priorities of the tasks.
Definition Scene.cpp:147
const base::commands::Joints & getSolverOutput() const
Get current solver output.
Definition Scene.hpp:132
TasksStatus tasks_status
Definition Scene.hpp:22
std::vector< TaskConfig > wbc_config
Definition Scene.hpp:28
std::vector< int > n_task_variables_per_prio
Definition Scene.hpp:24
std::vector< TaskConfig > getWbcConfig()
Return task configuration.
Definition Scene.hpp:167
bool configured
Definition Scene.hpp:25
base::VectorXd solver_output
Definition Scene.hpp:29
virtual const TasksStatus & updateTasksStatus()=0
updateTasksStatus Evaluate the fulfillment of the tasks given the current robot state and the solver ...
void getHierarchicalQP(HierarchicalQP &_hqp)
Return tasks sorted by priority for the solver.
Definition Scene.hpp:127
const TasksStatus & getTasksStatus() const
Returns all tasks as vector.
Definition Scene.hpp:102
Scene(RobotModelPtr robot_model, QPSolverPtr solver, const double dt)
Definition Scene.cpp:8
QPSolverPtr getSolver()
Return the current solver.
Definition Scene.hpp:162
void setJointWeights(const JointWeights &weights)
set Joint weights by given name
Definition Scene.cpp:192
TaskPtr getTask(const std::string &name)
Return a Particular task. Throw if the task does not exist.
Definition Scene.cpp:126
virtual TaskPtr createTask(const TaskConfig &config)=0
JointWeights joint_weights
Definition Scene.hpp:27
void setTaskActivation(const std::string &task_name, double activation)
Set Task activation for a task.
Definition Scene.cpp:122
const JointWeights & getActuatedJointWeights() const
Get Joint weights as Named vector.
Definition Scene.hpp:152
~Scene()
Definition Scene.cpp:14
static std::vector< int > getNTaskVariablesPerPrio(const std::vector< TaskConfig > &config)
Return number of tasks per priority, given the task config.
Definition Scene.cpp:178
RobotModelPtr getRobotModel()
Return the current robot model.
Definition Scene.hpp:157
JointWeights actuated_joint_weights
Definition Scene.hpp:27
void setTaskWeights(const std::string &task_name, const base::VectorXd &weights)
Set Task weights input for a task.
Definition Scene.cpp:118
const JointWeights & getJointWeights() const
Get Joint weights as Named vector.
Definition Scene.hpp:147
QPSolverPtr solver
Definition Scene.hpp:19
base::commands::Joints solver_output_joints
Definition Scene.hpp:26
Defines a task in the whole body control problem. Valid Configurations are e.g.
Definition TaskConfig.hpp:39
Definition TaskStatus.hpp:40
Definition ContactsAccelerationConstraint.cpp:3
std::shared_ptr< Task > TaskPtr
Definition Task.hpp:93
QPSolver * createT()
Definition QPSolver.hpp:33
std::shared_ptr< Scene > ScenePtr
Definition Scene.hpp:171
std::shared_ptr< QPSolver > QPSolverPtr
Definition QPSolver.hpp:31
std::shared_ptr< RobotModel > RobotModelPtr
Definition RobotModel.hpp:204
Describes a hierarchy of quadratic programs.
Definition QuadraticProgram.hpp:57
Definition Scene.hpp:175
static SceneMap * getSceneMap()
Definition Scene.hpp:192
static T * createInstance(const std::string &name, RobotModelPtr robot_model, QPSolverPtr solver, const double dt)
Definition Scene.hpp:186
static Scene * createInstance(const std::string &name, RobotModelPtr robot_model, QPSolverPtr solver, const double dt)
Definition Scene.hpp:178
std::map< std::string, Scene *(*)(RobotModelPtr, QPSolverPtr, double)> SceneMap
Definition Scene.hpp:176
static void clear()
Definition Scene.hpp:198
Definition Scene.hpp:207
SceneRegistry(const std::string &name)
Definition Scene.hpp:208