9 std::map<std::string, std::shared_ptr<T>> data;
13 TaskMap(
const std::string& id_): id(id_) {}
15 const std::shared_ptr<T>
get(
const std::string& name)
const 17 typename std::map<std::string, std::shared_ptr<T>>::const_iterator it = data.find(name);
18 if(it == data.end()) {
19 std::cout <<
"[Controller::"+
id+
" set]: element with name "+name+
" not found!" << std::endl;
25 std::shared_ptr<T>
get(
const std::string& name)
27 typename std::map<std::string, std::shared_ptr<T>>::iterator it = data.find(name);
28 if(it == data.end()) {
29 std::cout <<
"[Controller::"+
id+
" set]: element with name "+name+
" not found!" << std::endl;
35 void add(
const std::string& name, std::shared_ptr<T> elm)
37 typename std::map<std::string, std::shared_ptr<T>>::const_iterator it = data.find(name);
38 if(it != data.end()) {
39 std::cout <<
"[Controller::"+
id+
" set]: element with name "+name+
" already registered!" << std::endl;
45 const std::map<std::string, std::shared_ptr<T>>& getData()
const 50 void erase(
const std::string& name)
52 this->data.erase(name);
56 void getIf(P predicate, std::vector<std::shared_ptr<T>>& result)
const 58 typename std::map<std::string, std::shared_ptr<T>>::const_iterator it = data.begin();
59 for(; it != data.end(); ++it)
61 if(predicate(it->second))
62 result.push_back(it->second);
70 bool operator()(
const std::shared_ptr<ocra::Task> task)
72 return (task->isActiveAsObjective() ||task->isActiveAsConstraint());
94 , tau_max( VectorXd::Constant(m.nbInternalDofs(), std::numeric_limits<double>::max()) )
95 , tau( VectorXd::Constant(m.nbInternalDofs(), 0.) )
97 , taskInterfaces(
"taskInterfaces")
106 Controller::Controller(
const std::string& name,
Model& model)
108 , pimpl(new
Pimpl(model))
111 this->_isInLeftSupport = 1;
112 this->_isInRightSupport = 1;
122 if(!filename.empty())
123 file.open(filename.c_str(), std::ios::app);
124 std::ostream& os = filename.empty() ? std::cout : file;
126 os <<
"Controller Info {\nController name: " <<
getName() << std::endl;
134 const std::vector<std::shared_ptr<Task>>& activeTasks =
getActiveTasks();
136 if(activeTasks.empty())
137 os <<
"\tNo active task" << std::endl;
139 os <<
"\tList of active tasks:" << std::endl;
141 for(
size_t i = 0; i < activeTasks.size(); ++i)
143 Task& t = *activeTasks[i];
144 os <<
"\t- " << t.
getName() <<
":" << std::endl;
147 os <<
"\t\tLast output: " << t.
getOutput().transpose() << std::endl;
148 os <<
"\t\tError: " << t.
getError().transpose() << std::endl;
149 os <<
"\t\tErrorDot: " << t.
getErrorDot().transpose() << std::endl;
150 os <<
"\t\tJacobian\n" << t.
getJacobian() << std::endl;
154 os <<
"\t\tWeight: " << t.
getWeight().transpose() << std::endl;
157 os <<
"\t\tDamping\n" << t.
getDamping() << std::endl;
158 os <<
"\t\tStiffness\n" << t.
getStiffness() << std::endl;
159 os <<
"\t\tRef force: " << t.
getEffort().transpose() << std::endl;
164 os <<
"\t\t\tmargin: " << t.
getMargin() << std::endl;
169 os <<
"}" << std::endl;
174 pimpl->tau_max = tau_max;
181 return pimpl->tau_max;
186 pimpl->tasks.add(task->getName(), task);
188 pimpl->taskInterfaces.add(task->getName(), std::make_shared<TaskYarpInterface>(task));
193 for(
size_t i = 0; i < tasks.size(); ++i)
199 pimpl->taskInterfaces.erase(taskName);
200 pimpl->tasks.erase(taskName);
205 for(
auto task : tasks)
216 return pimpl->tasks.get(name);
221 std::vector<std::string> taskNames;
222 for (
auto mapItem : pimpl->tasks.getData()) {
223 taskNames.push_back(mapItem.first);
230 auto interface = pimpl->taskInterfaces.get(taskName);
232 return interface->getPortName();
240 std::vector<std::string> taskPortNames;
241 for (
auto mapItem : pimpl->taskInterfaces.getData()) {
242 taskPortNames.push_back(mapItem.second->getPortName());
244 return taskPortNames;
249 return pimpl->tasks.get(name);
262 if(pimpl->handleError)
264 tau.resize(pimpl->model.nbInternalDofs());
274 if(pimpl->handleError)
276 tau.resize(pimpl->model.nbInternalDofs());
282 const double tauNorm = tau.norm();
283 if(tauNorm > pimpl->maxTau)
285 if(pimpl->handleError)
287 tau.resize(pimpl->model.nbInternalDofs());
291 std::stringstream ss;
292 ss <<
"Instability suspected: norm of joint torques is " << tauNorm <<
" Nm.\n" 293 <<
"Maximum authorized is " << pimpl->maxTau <<
" Nm." << std::endl;
302 this->_fixedLink = newFixedLink;
308 leftSupport = this->_isInLeftSupport;
309 rightSupport = this->_isInRightSupport;
315 pimpl->handleError =
true;
320 pimpl->handleError =
false;
325 return pimpl->handleError;
330 return pimpl->errorMessage;
336 pimpl->errorMessage =
"";
341 return pimpl->errorFlag;
346 pimpl->maxTau = maxTau;
351 return pimpl->maxTau;
354 std::shared_ptr<Task>
Controller::createTask(
const std::string& name, Feature::Ptr feature, Feature::Ptr featureDes)
const 356 std::shared_ptr<Task> task(
doCreateTask(name, feature, featureDes));
357 task->setDesiredMassToActualOne();
358 task->setDamping(0.);
359 task->setStiffness(0.);
368 std::shared_ptr<Task> task(
doCreateTask(name, feature));
369 task->setDesiredMassToActualOne();
370 task->setDamping(0.);
371 task->setStiffness(0.);
383 task->setDesiredMassToActualOne();
384 task->setDamping(0.);
385 task->setStiffness(0.);
386 task->setFrictionCoeff(mu);
387 task->setMargin(margin);
389 task->activateContactMode();
396 pimpl->activeTasks.clear();
397 pimpl->tasks.getIf(isTaskActive(), pimpl->activeTasks);
398 return pimpl->activeTasks;
403 pimpl->errorFlag = eflag;
408 pimpl->errorMessage = msg;
413 return pimpl->tasks.getData();
virtual void doAddTask(std::shared_ptr< Task > task)=0
void setErrorFlag(int eflag)
const Eigen::MatrixXd & getJacobian() const
void addContactSet(const ContactSet &contacts)
std::vector< std::shared_ptr< Task > > activeTasks
virtual void doSetMaxJointTorques(const Eigen::VectorXd &tauMax)
void enableErrorHandling()
void removeTask(const std::string &taskName)
void printInfo(int level, const std::string &filename)
void setMaxJointTorqueNorm(double maxTau)
void disableErrorHandling()
const Eigen::VectorXd & getErrorDot() const
const std::string & getErrorMessage() const
bool isErrorHandlingEnabled() const
const Eigen::MatrixXd & getDamping() const
const std::string & getName() const
const Eigen::VectorXd & getError() const
void addTasks(const std::vector< std::shared_ptr< Task >> &tasks)
void removeTasks(const std::vector< std::string > tasks)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void getContactState(int &leftSupport, int &rightSupport)
virtual void doAddContactSet(const ContactSet &contacts)=0
std::vector< std::string > getTaskPortNames()
Interface for controllers.
std::shared_ptr< Task > getTask(const std::string &name)
void setFixedLinkForOdometry(std::string newFixedLink)
TaskMap< TaskYarpInterface > taskInterfaces
const Eigen::VectorXd & getMaxJointTorques() const
std::string getTaskPortName(const std::string &taskName)
std::shared_ptr< Task > createContactTask(const std::string &name, PointContactFeature::Ptr feature, double mu, double margin) const
Creates a contact task.
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()
bool isDesiredMassTheActualOne() const
void setMaxJointTorques(const Eigen::VectorXd &tau_max)
double getMaxJointTorqueNorm() const
void setErrorMessage(const std::string &msg)
double getFrictionCoeff() const
const std::map< std::string, std::shared_ptr< Task > > & getTasks() const
std::shared_ptr< Task > createTask(const std::string &name, Feature::Ptr feature, Feature::Ptr featureDes) const
Generic task creation.
const Eigen::VectorXd & getEffort() const
bool isContactModeActive() const
void addTask(std::shared_ptr< Task > task)
const Eigen::VectorXd & getOutput() const
const Eigen::VectorXd & getWeight() const
std::vector< std::string > getTaskNames()
const std::vector< std::shared_ptr< Task > > & getActiveTasks() const
const Eigen::MatrixXd & getStiffness() const
const Eigen::MatrixXd & getDesiredMass() const