28 for(
auto options : optionsVector){
29 TaskBuilder::Ptr builder =
getBuilder(options, model);
32 controller->addTask(builder->getTask());
33 builder->setTaskParameters();
40 TaskBuilder::Ptr TaskBldrPtr;
41 correctArticularVariables(options, model);
44 TaskBldrPtr = std::make_shared<CartesianTaskBuilder>(options, model);
45 }
else if (options.
taskType ==
"pose") {
46 TaskBldrPtr = std::make_shared<PoseTaskBuilder>(options, model);
47 }
else if (options.
taskType ==
"orientation") {
48 TaskBldrPtr = std::make_shared<OrientationTaskBuilder>(options, model);
49 }
else if (options.
taskType ==
"com") {
50 TaskBldrPtr = std::make_shared<ComTaskBuilder>(options, model);
51 }
else if (options.
taskType ==
"fullposture") {
52 TaskBldrPtr = std::make_shared<FullPostureTaskBuilder>(options, model);
53 }
else if (options.
taskType ==
"partialposture") {
54 TaskBldrPtr = std::make_shared<PartialPostureTaskBuilder>(options, model);
55 }
else if (options.
taskType ==
"pointcontact") {
56 TaskBldrPtr = std::make_shared<PointContactTaskBuilder>(options, model);
58 std::cout <<
"[ERROR] (TaskConstructionManager::getBuilder): The task type: '" << options.
taskType <<
"' does not exist. Returning NULL." << std::endl;
65 std::vector<TaskBuilderOptions> emptyOptionsVector;
66 if(boost::filesystem::exists(optionsXmlFilePath.c_str()))
68 TiXmlDocument newTasksFile( optionsXmlFilePath.c_str() );
69 if(!newTasksFile.LoadFile())
71 std::cout <<
"[ERROR] (TaskConstructionManager::parseTaskOptionsFromXml): Could not read the XML file: " << optionsXmlFilePath << std::endl;
72 return emptyOptionsVector;
79 std::cout <<
"[ERROR] (TaskConstructionManager::parseTaskOptionsFromXml): Filepath provided does not exist: "<< optionsXmlFilePath << std::endl;
80 return emptyOptionsVector;
86 std::vector<TaskBuilderOptions> optionsVector;
88 if(newTasksFile == NULL) {
89 std::cout <<
"[ERROR] newTasksFile argument is NULL" << std::endl;
91 for(TiXmlElement* xmlTask = newTasksFile->FirstChildElement(
"task"); xmlTask != NULL; xmlTask = xmlTask->NextSiblingElement(
"task")) {
93 if(parseTaskXmlElement(xmlTask, options)) {
94 optionsVector.push_back(options);
101 bool TaskConstructionManager::parseTaskXmlElement(TiXmlElement* xmlTask,
TaskBuilderOptions& options)
103 if (parseTaskNameAndType(xmlTask, options)) {
105 for(TiXmlElement* taskElem = xmlTask->FirstChildElement(); taskElem != NULL; taskElem = taskElem->NextSiblingElement()) {
107 std::string currentElemString = taskElem->Value();
109 if (currentElemString ==
"params") {
110 parseParamXmlElement(taskElem, options);
111 }
else if (currentElemString ==
"offset") {
112 parseOffsetXmlElement(taskElem, options);
113 }
else if (currentElemString ==
"desired") {
114 parseDesiredXmlElement(taskElem, options);
115 }
else if (currentElemString ==
"segment") {
116 parseSegmentXmlElement(taskElem, options);
117 }
else if (currentElemString ==
"jointIndexes") {
118 parseJointIndexesXmlElement(taskElem, options);
119 }
else if (currentElemString ==
"joints") {
120 parseJointsXmlElement(taskElem, options);
121 }
else if (currentElemString ==
"weights") {
122 parseWeightsXmlElement(taskElem, options);
124 std::cout <<
"[WARNING] (parseTaskOptionsFromXml): Found a task manager tag --> "<< currentElemString <<
" <-- that doesn't match any known tags. Ignoring." << std::endl;
132 bool TaskConstructionManager::parseTaskNameAndType(TiXmlElement* xmlTask,
TaskBuilderOptions& options)
134 if ( (xmlTask->Attribute(
"name") != NULL) && (xmlTask->Attribute(
"type") != NULL) ) {
135 options.
taskName = xmlTask->Attribute(
"name");
143 void TaskConstructionManager::parseParamXmlElement(TiXmlElement* paramElement,
TaskBuilderOptions& options)
146 if (paramElement->QueryDoubleAttribute(
"kp", &options.
kp)==TIXML_NO_ATTRIBUTE) {
151 if (paramElement->QueryDoubleAttribute(
"kd", &options.
kd)==TIXML_NO_ATTRIBUTE) {
152 if (options.
kp > 0.0) {
153 options.
kd=2.0*sqrt(options.
kp);
154 }
else{options.
kd=0.0;}
158 if (paramElement->QueryDoubleAttribute(
"weight", &options.
weight)==TIXML_NO_ATTRIBUTE) {
165 if (paramElement->QueryIntAttribute(
"hierarchyLevel", &options.
hierarchyLevel)==TIXML_NO_ATTRIBUTE) {
170 std::string axesString;
171 if (paramElement->QueryStringAttribute(
"axes", &axesString)==TIXML_NO_ATTRIBUTE) {
178 if (paramElement->QueryDoubleAttribute(
"mu", &options.
mu)==TIXML_NO_ATTRIBUTE) {
183 if (paramElement->QueryDoubleAttribute(
"margin", &options.
margin)==TIXML_NO_ATTRIBUTE) {
188 if (paramElement->Attribute(
"usesYarp") != NULL) {
190 std::string yarpString = std::string(paramElement->Attribute(
"usesYarp"));
191 if (yarpString==
"true" || yarpString==
"1") {
194 else if (yarpString==
"false" || yarpString==
"0") {
208 void TaskConstructionManager::parseOffsetXmlElement(TiXmlElement* offsetElement,
TaskBuilderOptions& options)
211 if (offsetElement->GetText() != NULL) {
219 void TaskConstructionManager::parseDesiredXmlElement(TiXmlElement* desiredElement,
TaskBuilderOptions& options)
221 if (desiredElement->GetText() != NULL) {
228 void TaskConstructionManager::parseSegmentXmlElement(TiXmlElement* segmentElement,
TaskBuilderOptions& options)
230 options.
segment = segmentElement->GetText();
233 void TaskConstructionManager::parseJointIndexesXmlElement(TiXmlElement* jointIndexesElement,
TaskBuilderOptions& options)
238 void TaskConstructionManager::parseJointsXmlElement(TiXmlElement* jointsElement,
TaskBuilderOptions& options)
240 std::string jointIndexString, indexDesiredValueString, nameDesiredValueString, indexWeightString, nameWeightString;
241 for(TiXmlElement* jointElem = jointsElement->FirstChildElement(
"joint"); jointElem != NULL; jointElem = jointElem->NextSiblingElement(
"joint"))
243 if ( (jointElem->Attribute(
"name") != NULL) || (jointElem->Attribute(
"index") != NULL) )
245 if(jointElem->Attribute(
"index") != NULL)
247 jointIndexString += jointElem->Attribute(
"index");
248 jointIndexString +=
" ";
249 if (jointElem->Attribute(
"des") != NULL)
251 indexDesiredValueString += jointElem->Attribute(
"des");
252 indexDesiredValueString +=
" ";
254 indexDesiredValueString +=
"-1000.0 ";
256 if (jointElem->Attribute(
"weight") != NULL)
258 indexWeightString += jointElem->Attribute(
"weight");
259 indexWeightString +=
" ";
263 indexWeightString +=
"-1.0 ";
267 else if(jointElem->Attribute(
"name") != NULL)
269 options.
jointNames.push_back(jointElem->Attribute(
"name"));
270 if (jointElem->Attribute(
"des") != NULL)
272 nameDesiredValueString += jointElem->Attribute(
"des");
273 nameDesiredValueString +=
" ";
275 nameDesiredValueString +=
"-1000.0 ";
277 if (jointElem->Attribute(
"weight") != NULL)
279 nameWeightString += jointElem->Attribute(
"weight");
280 nameWeightString +=
" ";
284 nameWeightString +=
"-1.0 ";
298 void TaskConstructionManager::parseWeightsXmlElement(TiXmlElement* weightsElement,
TaskBuilderOptions& options)
304 void TaskConstructionManager::correctArticularVariables(
TaskBuilderOptions& options, Model::Ptr model)
306 int sizeDof = model->nbInternalDofs();
308 int sizeDesired = options.
desired.rows();
315 int sizeJointNames = options.
jointNames.size();
318 if ((options.
taskType ==
"fullposture") || (options.
taskType ==
"partialposture"))
320 int sizeDofConcerned;
321 if(options.
taskType ==
"fullposture")
323 sizeDofConcerned = sizeDof;
324 }
else if(options.
taskType ==
"partialposture"){
325 sizeDofConcerned = sizeIndexDesired+sizeNameDesired;
328 if (sizeDesired == 0)
330 if(options.
taskType ==
"fullposture")
332 options.
desired = model->getJointPositions();
333 }
else if(options.
taskType ==
"partialposture"){
334 options.
desired = Eigen::VectorXd::Zero(sizeDofConcerned);
339 if (sizeDesired == 1)
341 options.
desired = Eigen::VectorXd::Constant(sizeDofConcerned, options.
desired[0]);
343 else if (sizeDesired != sizeDofConcerned)
345 if(options.
taskType ==
"fullposture")
347 options.
desired = model->getJointPositions();
348 }
else if(options.
taskType ==
"partialposture"){
349 options.
desired = Eigen::VectorXd::Zero(sizeDofConcerned);
355 if (sizeWeightVector != sizeDofConcerned)
362 if(options.
taskType ==
"fullposture")
364 if (sizeJointIndexes>0 && sizeJointIndexes == sizeIndexDesired)
366 for(
int i=0; i<sizeJointIndexes; i++)
381 if (sizeJointNames>0 && sizeJointNames == sizeNameDesired)
383 for(
int i=0; i<sizeJointNames; i++)
399 else if(options.
taskType ==
"partialposture")
401 Eigen::VectorXi jointIndexesTemp(sizeDofConcerned);
402 int indexCounter = 0;
404 if (sizeJointIndexes>0 && sizeJointIndexes == sizeIndexDesired)
406 for(
int i=0; i<sizeJointIndexes; i++)
408 jointIndexesTemp(indexCounter) = options.
jointIndexes(i);
423 if (sizeJointNames>0 && sizeJointNames == sizeNameDesired)
425 for(
int i=0; i<sizeJointNames; i++)
427 jointIndexesTemp(indexCounter) = model->getDofIndex(options.
jointNames[i]);
430 options.
desired(indexCounter) = model->getJointPositions()(model->getDofIndex(options.
jointNames[i]));
Eigen::VectorXd indexWeightVector
Eigen::VectorXd weightVector
Eigen::VectorXi jointIndexes
bool useWeightVectorConstructor
virtual ~TaskConstructionManager()
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
std::vector< TaskBuilderOptions > parseTaskOptionsFromXml(const std::string &optionsXmlFilePath)
ECartesianDof cartesianDofFromString(const std::string &dofString)
Eigen::VectorXd nameDesired
Eigen::Displacementd eigenVectorToDisplacementd(const Eigen::VectorXd &eigenVector)
std::vector< std::string > jointNames
Eigen::VectorXd stringToVectorXd(const char *valueString)
Eigen::VectorXi stringToVectorXi(const char *valueString)
Eigen::VectorXd nameWeightVector
void addTasksToController(Model::Ptr model, Controller::Ptr controller, std::vector< TaskBuilderOptions > optionsVector)
std::string getDisplacementArgs(TiXmlElement *xmlElem)
TaskConstructionManager()
Eigen::Displacementd offset
TaskBuilder::Ptr getBuilder(TaskBuilderOptions options, Model::Ptr model)
std::string convertToLowerCase(const std::string &originalString)
Eigen::VectorXd indexDesired