3 #include <SimoxUtility/json.h>
4 #include <VirtualRobot/Nodes/RobotNode.h>
10 #include <armarx/control/common/mp/aron/MPConfig.aron.generated.h>
23 auto _start = IceUtil::Time::now().toSecondsDouble();
24 ARMARX_INFO <<
"----------------------------------- creating MPs "
25 "-------------------------------------------------";
26 ARMARX_INFO <<
" -- in MPPool, we have " << mpListConfig.mpList.size() <<
" MPs";
29 for (
const MPConfig&
c : mpListConfig.mpList)
31 auto className = mpClass::_from_string_nocase(
c.className.c_str());
33 <<
"\n---- class: " <<
c.className <<
"\n---- type: " <<
c.mpTypeString
34 <<
"\n---- mode: " <<
c.mpMode <<
"\n---- style: " <<
c.mpStyle;
43 mps.insert({
c.name, {jsmp, in, out}});
46 case mpClass::JSVelMP:
53 mps.insert({
c.name, {tsmp, in, out}});
56 case mpClass::KeypointsMP:
61 mps.insert({
c.name, {tsmp, in, out}});
64 case mpClass::TSVelMP:
68 <<
"\nsupported names include: "
69 "JSMP, JSVelMP, TSMP, TSVelMP, KeypointsMP";
73 auto _duration = IceUtil::Time::now().toSecondsDouble() - _start;
74 ARMARX_INFO <<
"---------------------------------- All MPs created in " << _duration
76 "-----------------------------------------------";
91 if (rtSafe or mp.second.mp->isFirstRun())
93 mp.second.mp->run(mp.second.input, mp.second.output);
101 std::string allMPs =
"";
104 allMPs = allMPs + mp.second.mp->getMPName() +
", ";
113 const ::Ice::Current&)
115 for (
auto& pair :
mps)
117 auto it = durationSec.find(pair.first);
118 auto is = startVec.find(pair.first);
119 auto ig = goalVec.find(pair.first);
121 if (it != durationSec.end())
123 if (it->second > 0.0)
125 pair.second.mp->setDurationSec(it->second);
128 if (is != startVec.end())
130 pair.second.mp->setStart(is->second);
132 if (ig != goalVec.end())
134 pair.second.mp->setGoal(ig->second);
147 const DVec& startVec,
150 const Ice::Current& iceCurrent)
152 auto start = IceUtil::Time::now().toSecondsDouble();
155 ARMARX_INFO <<
"--------------------------------------- start all MPs "
156 "---------------------------------------";
157 ARMARX_INFO <<
" -- To start all MPs at once, it's up to the user to configure the "
158 "start, goal and timeduration properly before this call.";
163 mp.second.mp->start();
170 auto search =
mps.find(mpName);
171 if (search !=
mps.end())
173 if (timeDuration < 0.0)
175 search->second.mp->start(goalVec, startVec);
179 search->second.mp->start(goalVec, startVec, timeDuration);
185 <<
" is not in the MP pool. Please check your configuration file";
189 auto duration = IceUtil::Time::now().toSecondsDouble() -
start;
190 ARMARX_INFO <<
"All mp started in " << duration <<
" sec.";
206 mp.second.mp->requestStop();
211 auto search =
mps.find(mpName);
212 if (search !=
mps.end())
214 search->second.mp->requestStop();
219 <<
" is not in the MP pool. Please check your configuration file";
231 mp.second.mp->stop();
236 auto search =
mps.find(mpName);
237 if (search !=
mps.end())
239 search->second.mp->stop();
244 <<
" is not in the MP pool. Please check your configuration file";
262 mp.second.mp->pause();
267 auto search =
mps.find(mpName);
268 if (search !=
mps.end())
270 search->second.mp->pause();
275 <<
" is not in the MP pool. Please check your configuration file";
293 mp.second.mp->resume();
298 auto search =
mps.find(mpName);
299 if (search !=
mps.end())
301 search->second.mp->resume();
306 <<
" is not in the MP pool. Please check your configuration file";
324 mp.second.mp->reset();
329 auto search =
mps.find(mpName);
330 if (search !=
mps.end())
332 search->second.mp->reset();
337 <<
" is not in the MP pool. Please check your configuration file";
353 return std::all_of(
mps.begin(),
355 [](
const auto& pair) { return pair.second.mp->isFinished(); });
358 auto it =
mps.find(mpName);
361 return it->second.mp->isFinished();
364 ARMARX_ERROR << mpName <<
" is not in the MP pool. Please check your configuration file";
371 if (fileNames.size() > 0)
373 ARMARX_WARNING <<
"Be aware that the fileNames ice parameter is not yet used. "
374 <<
"Instead, set the file names in the MPConfig parameter of your "
375 <<
"previous ice interface call.";
377 ARMARX_INFO <<
"--------------------------------------- Train MPs "
378 "----------------------------------------------------";
381 mp.second.mp->learnFromCSV();
384 ARMARX_INFO <<
"--------------------------------------- done "
385 "----------------------------------------------------";
390 const Ice::Current& iceCurrent)
392 auto start = IceUtil::Time::now().toSecondsDouble();
393 ARMARX_INFO <<
"--------------------------------------- Train MPs "
394 "----------------------------------------------------";
397 MultiMPTrajs trajsDict;
398 trajsDict.fromAron(dict);
407 if (trajsDict.data.empty())
409 ARMARX_INFO <<
"Learning from trajectories specified in MPConfig(s) rather than as an "
411 for (
const auto& [mpName, mpInputOutput] :
mps)
413 const auto&
mpConfig = mpInputOutput.mp->getConfig();
415 <<
"For MP " << mpName <<
": No trajectories provided as parameter, "
416 <<
"and not trajectories specified in MPConfig.";
417 mpInputOutput.mp->learnFromTraj(
mpConfig.trajectoryList);
422 for (
const auto& [mpName, trajs] : trajsDict.data)
427 <<
"Trajectories provided for unknown MP " << mpName;
429 <<
"Empty list of trajectories provided for MP " << mpName;
430 mps.at(mpName).mp->learnFromTraj(trajs);
436 auto duration = IceUtil::Time::now().toSecondsDouble() -
start;
437 ARMARX_INFO <<
"--------------------------------------- done in " << duration
439 "----------------------------------------------------";
445 auto start = IceUtil::Time::now().toSecondsDouble();
448 mp.second.mp->trainMP();
451 auto duration = IceUtil::Time::now().toSecondsDouble() -
start;
452 ARMARX_INFO <<
"training finished in " << duration <<
" sec.";
465 mp.second.mp->setStartAndGoal(starts, goals);
493 std::vector<double> test{0.1, 0.1};
500 auto search =
mps.find(mpName);
501 if (search !=
mps.end())
503 return search->second.mp->getCanonicalValue();
508 <<
" is not in the MP pool. Please check your configuration file";
521 const Ice::Current& iceCurrent)
523 ARMARX_WARNING <<
"You should overwrite this method in your concrete MP controllers, and "
524 "call resetMPs(dto, rnsMap)";
529 const std::map<std::string, VirtualRobot::RobotNodeSetPtr>& rnsMap)
531 std::scoped_lock lock(
mtx_mps);
535 "If you want to force reset, please call \n\n"
536 " ctrl->stop('all') \n\n"
537 "before you continue";
541 auto configData = MPListConfig::FromAron(dto);
548 for (
const auto& _mp : configData.mpList)
550 if (rnsMap.find(_mp.nodeSetName) == rnsMap.end())
552 ARMARX_WARNING <<
"You have to make sure the RobotNodeSet " << _mp.nodeSetName
553 <<
" in your MP config "
554 <<
"corresponds to one of the RobotNodeSet in the controllers";
562 ARMARX_INFO <<
"-- successfully updated MP config.";
574 const std::map<std::string, VirtualRobot::RobotNodeSetPtr>& rnsMap)
576 ARMARX_INFO <<
"-- reconfigure MP: reinitialize the mp input output, as well as the rt "
577 "related buffer values";
579 for (
auto& _mp :
mps)
583 VirtualRobot::RobotNodeSetPtr rns = rnsMap.at(_mp.second.mp->getNodeSetName());
585 if (_mp.second.mp->getRole() ==
"taskspace")
589 << _mp.second.mp->getMPName();
590 std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->pose = currentPose;
591 std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input)->vel.setZero();
592 std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->pose = currentPose;
593 std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output)->vel.setZero();
595 else if (_mp.second.mp->getRole() ==
"nullspace")
598 << _mp.second.mp->getMPName() <<
" with "
599 << rns->getJointValues().size() <<
" joints";
600 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian =
601 rns->getJointValuesEigen();
602 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angularVel =
603 Eigen::VectorXf::Zero(rns->getJointValues().size());
604 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angleRadian =
605 rns->getJointValuesEigen();
606 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angularVel =
607 Eigen::VectorXf::Zero(rns->getJointValues().size());
609 else if (_mp.second.mp->getRole() ==
"hand")
612 << _mp.second.mp->getMPName() <<
" with "
613 << rns->getJointValues().size() <<
" joints";
614 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angleRadian =
615 rns->getJointValuesEigen();
616 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input)->angularVel =
617 Eigen::VectorXf::Zero(rns->getJointValues().size());
618 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angleRadian =
619 rns->getJointValuesEigen();
620 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output)->angularVel =
621 Eigen::VectorXf::Zero(rns->getJointValues().size());