3 #include <SimoxUtility/json.h>
9 #include <armarx/control/common/mp/aron/MPConfig.aron.generated.h>
22 ARMARX_INFO <<
"----------------------------------- creating MPs "
23 "-------------------------------------------------";
24 ARMARX_INFO <<
" -- in MPPool, we have " << mpListConfig.mpList.size() <<
" MPs";
27 for (
const MPConfig&
c : mpListConfig.mpList)
29 auto className = mpClass::_from_string_nocase(
c.className.c_str());
31 <<
"\n---- class: " <<
c.className <<
"\n---- type: " <<
c.mpTypeString
32 <<
"\n---- mode: " <<
c.mpMode <<
"\n---- style: " <<
c.mpStyle;
41 mps.insert({
c.name, {jsmp, in, out}});
44 case mpClass::JSVelMP:
51 mps.insert({
c.name, {tsmp, in, out}});
54 case mpClass::KeypointsMP:
59 mps.insert({
c.name, {tsmp, in, out}});
62 case mpClass::TSVelMP:
66 <<
"\nsupported names include: "
67 "JSMP, JSVelMP, TSMP, TSVelMP, KeypointsMP";
71 ARMARX_INFO <<
"---------------------------------- All MPs created "
72 "-----------------------------------------------";
87 if (rtSafe or mp.second.mp->isFirstRun())
89 mp.second.mp->run(mp.second.input, mp.second.output);
97 std::string allMPs =
"";
100 allMPs = allMPs + mp.second.mp->getMPName() +
", ";
109 const ::Ice::Current&)
111 for (
auto& pair :
mps)
113 auto it = durationSec.find(pair.first);
114 auto is = startVec.find(pair.first);
115 auto ig = goalVec.find(pair.first);
117 if (it != durationSec.end())
119 if (it->second > 0.0)
121 pair.second.mp->setDurationSec(it->second);
124 if (is != startVec.end())
126 pair.second.mp->setStart(is->second);
128 if (ig != goalVec.end())
130 pair.second.mp->setGoal(ig->second);
143 const DVec& startVec,
146 const Ice::Current& iceCurrent)
150 ARMARX_INFO <<
"--------------------------------------- start all MPs "
151 "---------------------------------------";
152 ARMARX_INFO <<
" -- To start all MPs at once, it's up to the user to configure the "
153 "start, goal and timeduration properly before this call.";
158 mp.second.mp->start();
165 auto search =
mps.find(mpName);
166 if (search !=
mps.end())
168 if (timeDuration < 0.0)
170 search->second.mp->start(goalVec, startVec);
174 search->second.mp->start(goalVec, startVec, timeDuration);
180 <<
" is not in the MP pool. Please check your configuration file";
199 mp.second.mp->stop();
204 auto search =
mps.find(mpName);
205 if (search !=
mps.end())
207 search->second.mp->stop();
212 <<
" is not in the MP pool. Please check your configuration file";
230 mp.second.mp->pause();
235 auto search =
mps.find(mpName);
236 if (search !=
mps.end())
238 search->second.mp->pause();
243 <<
" is not in the MP pool. Please check your configuration file";
261 mp.second.mp->resume();
266 auto search =
mps.find(mpName);
267 if (search !=
mps.end())
269 search->second.mp->resume();
274 <<
" is not in the MP pool. Please check your configuration file";
292 mp.second.mp->reset();
297 auto search =
mps.find(mpName);
298 if (search !=
mps.end())
300 search->second.mp->reset();
305 <<
" is not in the MP pool. Please check your configuration file";
323 if (!mp.second.mp->isFinished())
332 auto search =
mps.find(mpName);
333 if (search !=
mps.end())
335 return search->second.mp->isFinished();
340 <<
" is not in the MP pool. Please check your configuration file";
349 if (fileNames.size() > 0)
351 ARMARX_WARNING <<
"Be aware that the fileNames ice parameter is not yet used. "
352 <<
"Instead, set the file names in the MPConfig parameter of your "
353 <<
"previous ice interface call.";
355 ARMARX_INFO <<
"--------------------------------------- Train MPs "
356 "----------------------------------------------------";
359 mp.second.mp->learnFromCSV();
362 ARMARX_INFO <<
"--------------------------------------- done "
363 "----------------------------------------------------";
368 const Ice::Current& iceCurrent)
370 ARMARX_INFO <<
"--------------------------------------- Train MPs "
371 "----------------------------------------------------";
374 MultiMPTrajs trajsDict;
375 trajsDict.fromAron(dict);
384 if (trajsDict.data.empty())
386 ARMARX_INFO <<
"Learning from trajectories specified in MPConfig(s) rather than as an "
388 for (
const auto& [mpName, mpInputOutput] :
mps)
390 const auto&
mpConfig = mpInputOutput.mp->getConfig();
392 <<
"For MP " << mpName <<
": No trajectories provided as parameter, "
393 <<
"and not trajectories specified in MPConfig.";
394 mpInputOutput.mp->learnFromTraj(
mpConfig.trajectoryList);
399 for (
const auto& [mpName, trajs] : trajsDict.data)
404 <<
"Trajectories provided for unknown MP " << mpName;
406 <<
"Empty list of trajectories provided for MP " << mpName;
407 mps.at(mpName).mp->learnFromTraj(trajs);
413 ARMARX_INFO <<
"--------------------------------------- done "
414 "----------------------------------------------------";
422 mp.second.mp->trainMP();
438 mp.second.mp->setStartAndGoal(starts, goals);
466 std::vector<double> test{0.1, 0.1};
473 auto search =
mps.find(mpName);
474 if (search !=
mps.end())
476 return search->second.mp->getCanonicalValue();
481 <<
" is not in the MP pool. Please check your configuration file";
494 const Ice::Current& iceCurrent)
496 ARMARX_WARNING <<
"You should overwrite this method in your concrete MP controllers, and call resetMPs(dto, rnsMap)";
501 const std::map<std::string, VirtualRobot::RobotNodeSetPtr>& rnsMap)
503 std::scoped_lock lock(
mtx_mps);
507 "If you want to force reset, please call \n\n"
508 " ctrl->stop('all') \n\n"
509 "before you continue";
513 auto configData = MPListConfig::FromAron(dto);
520 for (
const auto& _mp : configData.mpList)
522 if (rnsMap.find(_mp.nodeSetName) == rnsMap.end())
524 ARMARX_WARNING <<
"You have to make sure the RobotNodeSet " << _mp.nodeSetName
525 <<
" in your MP config "
526 <<
"corresponds to one of the RobotNodeSet in the controllers";
534 ARMARX_INFO <<
"-- successfully updated MP config.";
546 const std::map<std::string, VirtualRobot::RobotNodeSetPtr>& rnsMap)
548 ARMARX_INFO <<
"-- reconfigure MP: reinitialize the mp input output, as well as the rt "
549 "related buffer values";
551 for (
auto& _mp :
mps)
555 VirtualRobot::RobotNodeSetPtr rns = rnsMap.at(_mp.second.mp->getNodeSetName());
557 if (_mp.second.mp->getRole() ==
"taskspace")
561 << _mp.second.mp->getMPName();
562 std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->pose = currentPose;
563 std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->vel.setZero();
564 std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->pose = currentPose;
565 std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->vel.setZero();
567 else if (_mp.second.mp->getRole() ==
"nullspace")
570 << _mp.second.mp->getMPName() <<
" with "
571 << rns->getJointValues().size() <<
" joints";
572 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian =
573 rns->getJointValuesEigen();
574 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angularVel =
575 Eigen::VectorXf::Zero(rns->getJointValues().size());
576 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angleRadian =
577 rns->getJointValuesEigen();
578 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angularVel =
579 Eigen::VectorXf::Zero(rns->getJointValues().size());
581 else if (_mp.second.mp->getRole() ==
"hand")
584 << _mp.second.mp->getMPName() <<
" with "
585 << rns->getJointValues().size() <<
" joints";
586 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian =
587 rns->getJointValuesEigen();
588 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angularVel =
589 Eigen::VectorXf::Zero(rns->getJointValues().size());
590 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angleRadian =
591 rns->getJointValuesEigen();
592 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angularVel =
593 Eigen::VectorXf::Zero(rns->getJointValues().size());