ocra-recipes
Doxygen documentation for the ocra-recipes repository
Controller.h
Go to the documentation of this file.
1 
14 #ifndef _OCRA_CONTROLLER_H_
15 #define _OCRA_CONTROLLER_H_
16 
18 #include <Eigen/Core>
19 #include <boost/noncopyable.hpp>
20 #include <boost/shared_ptr.hpp>
21 #include <boost/property_tree/ptree.hpp>
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include <ocra/util/Macros.h>
26 
27 // #include "ocra/control/Controller.h"
28 #include "ocra/control/Task.h"
30 #include "ocra/control/Feature.h"
31 #include "ocra/control/Model.h"
32 #include <map>
33 #include <fstream>
34 #include <iostream>
35 
36 
37 
38 namespace ocra
39 {
40  class Task;
41  class Feature;
42  class PointContactFeature;
43  class ContactSet;
44  class Model;
45 }
46 
47 namespace ocra
48 {
50 
53  class Controller
54  : public NamedInstance
55  , boost::noncopyable
56  {
58  protected:
59  Controller(const std::string& name, Model& model);
60 
61  public: // control
62  virtual ~Controller() = 0;
63 
64  void printInfo(int level, const std::string& filename);
65 
66  void setMaxJointTorques(const Eigen::VectorXd& tau_max);
67  const Eigen::VectorXd& getMaxJointTorques() const;
68 
69  void addTask(std::shared_ptr<Task> task);
70  void addTasks(const std::vector<std::shared_ptr<Task>>& tasks);
71 
72  void removeTask(const std::string& taskName);
73  void removeTasks(const std::vector<std::string> tasks);
74 
75  std::vector<std::string> getTaskNames();
76  std::string getTaskPortName(const std::string& taskName);
77  std::vector<std::string> getTaskPortNames();
78 
79 
80  void addContactSet(const ContactSet& contacts);
81  std::shared_ptr<Task> getTask(const std::string& name);
82  const std::shared_ptr<Task> getTask(const std::string& name) const;
83  const std::map<std::string, std::shared_ptr<Task>>& getTasks() const;
84 
86 
90  void computeOutput(Eigen::VectorXd& tau);
91  const Eigen::VectorXd& computeOutput();
92 
94  enum ErrorFlag
96  {
97  SUCCESS = 0,
98  CRITICAL_ERROR = 1, // Flag for errors that trigger the emergency procedure
101  INSTABILITY = 8 + 1, // Instability is a critical error
102  OTHER = 16
103  };
104 
105  void enableErrorHandling();
106  void disableErrorHandling();
107  bool isErrorHandlingEnabled() const;
108  const std::string& getErrorMessage() const;
109  void clearErrorFlag();
110  int getErrorFlag() const;
111  void setMaxJointTorqueNorm(double maxTau);
112  double getMaxJointTorqueNorm() const;
113  void setFixedLinkForOdometry(std::string newFixedLink);
114  void setUseOdometry(bool useOdometry) { this->_useOdometry = useOdometry; }
115  void getFixedLinkForOdometry(std::string& currentFixedLink) { currentFixedLink = this->_fixedLink; }
116  void setContactState(int isInLeftSupport, int isInRightSupport) { this->_isInLeftSupport = isInLeftSupport; this->_isInRightSupport = isInRightSupport;}
117  void getContactState(int& leftSupport, int& rightSupport);
118 
120 
121  public: // factory
123  std::shared_ptr<Task> createTask(const std::string& name, Feature::Ptr feature, Feature::Ptr featureDes) const;
125  std::shared_ptr<Task> createTask(const std::string& name, Feature::Ptr feature) const;
127 
129 
132  std::shared_ptr<Task> createContactTask(const std::string& name, PointContactFeature::Ptr feature, double mu, double margin) const;
133 
134  protected:
135  const std::vector<std::shared_ptr<Task>>& getActiveTasks() const;
136  void setErrorFlag(int eflag);
137  void setErrorMessage(const std::string& msg);
138 
139  protected:
140  virtual void doComputeOutput(Eigen::VectorXd& tau) = 0;
141  virtual void doAddTask(std::shared_ptr<Task> task) = 0;
142  virtual void doAddContactSet(const ContactSet& contacts) = 0;
143  virtual void doSetMaxJointTorques(const Eigen::VectorXd& tauMax); // Does nothing if not overloaded
144 
145  protected: // factory
146  virtual std::shared_ptr<Task> doCreateTask(const std::string& name, Feature::Ptr feature, Feature::Ptr featureDes) const = 0;
147  virtual std::shared_ptr<Task> doCreateTask(const std::string& name, Feature::Ptr feature) const = 0;
148  virtual std::shared_ptr<Task> doCreateContactTask(const std::string& name, PointContactFeature::Ptr feature, double mu, double margin) const = 0;
149 
150  private:
151  struct Pimpl;
152  boost::shared_ptr<Pimpl> pimpl;
153  bool _useOdometry;
154  std::string _fixedLink;
155  int _isInLeftSupport;
156  int _isInRightSupport;
157  };
158 }
159 
160 #endif
161 
162 // cmake:sourcegroup=Api
virtual void doAddTask(std::shared_ptr< Task > task)=0
void setErrorFlag(int eflag)
Definition: Controller.cpp:401
void addContactSet(const ContactSet &contacts)
Definition: Controller.cpp:209
virtual void doSetMaxJointTorques(const Eigen::VectorXd &tauMax)
Definition: Controller.cpp:177
void enableErrorHandling()
Definition: Controller.cpp:313
void removeTask(const std::string &taskName)
Definition: Controller.cpp:197
int getErrorFlag() const
Definition: Controller.cpp:339
void printInfo(int level, const std::string &filename)
Definition: Controller.cpp:119
void setMaxJointTorqueNorm(double maxTau)
Definition: Controller.cpp:344
Contact task factory.
Definition: ContactSet.h:59
void disableErrorHandling()
Definition: Controller.cpp:318
virtual ~Controller()=0
Definition: Controller.cpp:115
const std::string & getErrorMessage() const
Definition: Controller.cpp:328
Controller(const std::string &name, Model &model)
Definition: Controller.cpp:106
bool isErrorHandlingEnabled() const
Definition: Controller.cpp:323
Declaration file of the Model class.
void addTasks(const std::vector< std::shared_ptr< Task >> &tasks)
Definition: Controller.cpp:191
A class hierarchy to compute task errors based on control frames.
Model class.
Definition: Model.h:38
void removeTasks(const std::vector< std::string > tasks)
Definition: Controller.cpp:203
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
void getFixedLinkForOdometry(std::string &currentFixedLink)
Definition: Controller.h:115
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void getContactState(int &leftSupport, int &rightSupport)
Definition: Controller.cpp:306
virtual void doAddContactSet(const ContactSet &contacts)=0
std::vector< std::string > getTaskPortNames()
Definition: Controller.cpp:238
Interface for controllers.
Definition: Controller.h:53
std::shared_ptr< Task > getTask(const std::string &name)
Definition: Controller.cpp:214
void setFixedLinkForOdometry(std::string newFixedLink)
Definition: Controller.cpp:300
const Eigen::VectorXd & getMaxJointTorques() const
Definition: Controller.cpp:179
void setUseOdometry(bool useOdometry)
Definition: Controller.h:114
std::string getTaskPortName(const std::string &taskName)
Definition: Controller.cpp:228
std::shared_ptr< Task > createContactTask(const std::string &name, PointContactFeature::Ptr feature, double mu, double margin) const
Creates a contact task.
Definition: Controller.cpp:378
virtual void doComputeOutput(Eigen::VectorXd &tau)=0
virtual std::shared_ptr< Task > doCreateTask(const std::string &name, Feature::Ptr feature, Feature::Ptr featureDes) const =0
virtual std::shared_ptr< Task > doCreateContactTask(const std::string &name, PointContactFeature::Ptr feature, double mu, double margin) const =0
const Eigen::VectorXd & computeOutput()
Definition: Controller.cpp:252
void setMaxJointTorques(const Eigen::VectorXd &tau_max)
Definition: Controller.cpp:172
double getMaxJointTorqueNorm() const
Definition: Controller.cpp:349
ErrorFlag
Error handling.
Definition: Controller.h:95
void setErrorMessage(const std::string &msg)
Definition: Controller.cpp:406
const std::map< std::string, std::shared_ptr< Task > > & getTasks() const
Definition: Controller.cpp:411
std::shared_ptr< Task > createTask(const std::string &name, Feature::Ptr feature, Feature::Ptr featureDes) const
Generic task creation.
Definition: Controller.cpp:354
void addTask(std::shared_ptr< Task > task)
Definition: Controller.cpp:184
void setContactState(int isInLeftSupport, int isInRightSupport)
Definition: Controller.h:116
std::vector< std::string > getTaskNames()
Definition: Controller.cpp:219
const std::vector< std::shared_ptr< Task > > & getActiveTasks() const
Definition: Controller.cpp:394