23 #include <MMM/Motion/Plugin/KinematicPlugin/KinematicSensor.h>
24 #include <MMM/Motion/Plugin/ModelPosePlugin/ModelPoseSensor.h>
25 #include "../Environment.h"
26 #include "../KinematicSolver.h"
27 #include <MMM/RapidXML/rapidxml.hpp>
28 #include <VirtualRobot/RobotNodeSet.h>
29 #include "../Model/UserWaypoint.h"
30 #include <boost/make_shared.hpp>
31 #include <MMM/RapidXML/RapidXMLReader.h>
32 #include <MMM/Motion/MotionReaderXML.h>
35 #include <boost/lexical_cast.hpp>
36 #include <MMM/Motion/Plugin/KinematicPlugin/KinematicSensorFactory.h>
38 static constexpr
const char* NODE_METADATA =
"RobotTrajectoryDesignerData";
39 static constexpr
const char* NODE_USERWAYPOINTS =
"UserWaypoints";
40 static constexpr
const char* NODE_USERWAYPOINT =
"UserWaypoint";
41 static constexpr
const char* NODE_JOINTANGLES =
"JointAngles";
42 static constexpr
const char* NODE_TIMEOPTIMALTIMESTAMP =
"TimeOptimalTimeStamp";
43 static constexpr
const char* NODE_ISBREAKPOINT =
"IsBreakPoint";
44 static constexpr
const char* NODE_IKSELECTION =
"CartesianSelection";
46 static constexpr
const char* NODE_TRANSITIONS =
"Transitions";
47 static constexpr
const char* NODE_TRANSITION =
"Transition";
48 static constexpr
const char* NODE_TIMEOPTIMALDURATION =
"TimeOptimalDuration";
49 static constexpr
const char* NODE_USERDURATION =
"UserDuration";
50 static constexpr
const char* NODE_INTERPOLATIONTYPE =
"InterpolationType";
52 static constexpr
const char* NODE_INTERBREAKPOINTTRAJECTORIES =
"InterBreakpointTrajectories";
53 static constexpr
const char* NODE_INTERBREAKPOINTTRAJECTORY =
"InterBreakpointTrajectory";
55 static constexpr
const char* NODE_ROBOT =
"Robot";
65 std::vector<std::string> libpaths;
66 MMM::MotionReaderXMLPtr motionReader = boost::make_shared<MMM::MotionReaderXML>(libpaths);
68 MMM::MotionList motions = motionReader->loadAllMotions(file,
true);
71 std::vector<DesignerTrajectoryPtr> trajectories;
72 std::set<std::string> nodeSets;
74 MMM::RapidXMLReaderPtr metaDataReader = MMM::RapidXMLReader::FromFile(file);
75 MMM::RapidXMLReaderNodePtr mmmNode = metaDataReader->getRoot();
76 MMM::RapidXMLReaderNodePtr metaDataNode = mmmNode->first_node(NODE_METADATA);
77 MMM::RapidXMLReaderNodePtr robotNode = metaDataNode->first_node(NODE_ROBOT);
78 if (robotNode->value() != environment->getRobot()->getFilename())
81 throw InvalidArgumentException(
"MMM file was exported for another robot");
83 for (MMM::MotionPtr motion : motions)
85 if (motion->getSensorsByType(
"ModelPoseSensor").size() > 1)
91 MMM::SensorPtr sensor = motion->getSensorsByType(
"ModelPose")[0];
96 MMM::ModelPoseSensor mp =
dynamic_cast<MMM::ModelPoseSensor&
>(*sensor);
97 MMM::ModelPoseSensorPtr modelPoseSensor = boost::make_shared<MMM::ModelPoseSensor>(mp);
98 if (!environment->getRobot()->hasRobotNodeSet(motion->getName()))
105 VirtualRobot::RobotNodeSetPtr rns = environment->getRobot()->getRobotNodeSet(motion->getName());
106 MMM::RapidXMLReaderNodePtr trajectoryNode = metaDataNode->first_node(motion->getName().c_str());
107 MMM::RapidXMLReaderNodePtr userWaypointsNode = trajectoryNode->first_node(NODE_USERWAYPOINTS);
108 MMM::RapidXMLReaderNodePtr transitionsNode = trajectoryNode->first_node(NODE_TRANSITIONS);
110 MMM::RapidXMLReaderNodePtr userWaypointNode = userWaypointsNode->first_node(NODE_USERWAYPOINT);
111 MMM::RapidXMLReaderNodePtr transitionNode = transitionsNode->first_node(NODE_TRANSITION);
112 std::vector<float> times = modelPoseSensor->getTimesteps();
113 std::vector<UserWaypointPtr> userPoints;
114 unsigned int breakpoints = 1;
116 for (
double t : times)
120 userWaypointNode = userWaypointNode->next_sibling(NODE_USERWAYPOINT);
122 MMM::ModelPoseSensorMeasurementPtr measurement = modelPoseSensor->getDerivedMeasurement(t);
128 MMM::RapidXMLReaderNodePtr jointAnglesNode = userWaypointNode->first_node(NODE_JOINTANGLES);
129 std::string jointAngleValues = jointAnglesNode->value();
131 std::istringstream js(jointAngleValues);
134 while (getline(js, j,
' '))
136 jointAngles.push_back(boost::lexical_cast<double>(j));
139 PoseBasePtr pose = ks->doForwardKinematicRelative(rns,
jointAngles);
141 userPoint->setUserTimestamp(t);
144 MMM::RapidXMLReaderNodePtr timeOptimalTimeStampNode = userWaypointNode->first_node(NODE_TIMEOPTIMALTIMESTAMP);
145 userPoint->setTimeOptimalTimestamp(boost::lexical_cast<double>(timeOptimalTimeStampNode->value()));
147 MMM::RapidXMLReaderNodePtr ikSelectionNode = userWaypointNode->first_node(NODE_IKSELECTION);
148 userPoint->setIKSelection((
IKSelection)(std::stoi(ikSelectionNode->value())));
150 MMM::RapidXMLReaderNodePtr isTimeOptimalBreakPointNode = userWaypointNode->first_node(NODE_ISBREAKPOINT);
151 userPoint->setIsTimeOptimalBreakpoint(boost::lexical_cast<bool>(isTimeOptimalBreakPointNode->value()));
152 if (boost::lexical_cast<bool>(isTimeOptimalBreakPointNode->value()))
154 if (t > 0 && t != times.back())
159 userPoints.push_back(userPoint);
162 for (
unsigned int i = 0; i < userPoints.size(); i++)
166 trajectory->addLastUserWaypoint(userPoints[i]);
170 for (
unsigned int i = 0; i < userPoints.size() - 1; i++)
174 transitionNode = transitionNode->next_sibling(NODE_TRANSITION);
178 MMM::RapidXMLReaderNodePtr timeOptimalDurationNode = transitionNode->first_node(NODE_TIMEOPTIMALDURATION);
179 transition->setTimeOptimalDuration(boost::lexical_cast<double>(timeOptimalDurationNode->value()));
181 MMM::RapidXMLReaderNodePtr userDurationNode = transitionNode->first_node(NODE_USERDURATION);
182 transition->setUserDuration(boost::lexical_cast<double>(userDurationNode->value()));
184 MMM::RapidXMLReaderNodePtr interpolationTypeNode = transitionNode->first_node(NODE_INTERPOLATIONTYPE);
185 transition->setInterpolationType((
InterpolationType)(std::stoi(interpolationTypeNode->value())));
188 std::vector<armarx::TrajectoryPtr> interBreakpointTrajectories;
189 MMM::RapidXMLReaderNodePtr interBreakpointTrajectoriesNode = trajectoryNode->first_node(NODE_INTERBREAKPOINTTRAJECTORIES);
190 MMM::RapidXMLReaderNodePtr interBreakpointTrajectoryNode = interBreakpointTrajectoriesNode->first_node(NODE_INTERBREAKPOINTTRAJECTORY);
192 for (
unsigned int i = 0; i < breakpoints; i++)
196 interBreakpointTrajectoryNode = interBreakpointTrajectoryNode->next_sibling(NODE_INTERBREAKPOINTTRAJECTORY);
198 MMM::KinematicSensorFactory factory;
199 MMM::SensorPtr sensorPtr = factory.createSensor(interBreakpointTrajectoryNode->first_node(MMM::XML::NODE_SENSOR));
200 MMM::KinematicSensor interBreakpointTrajectorySensor =
dynamic_cast<MMM::KinematicSensor&
>(*sensorPtr);
201 std::vector<float> interBreakpointTimeStamps = interBreakpointTrajectorySensor.getTimesteps();
202 std::vector<std::vector<double>> nodeData;
204 for (
unsigned int i = 0; i < rns->getSize(); i++)
206 MMM::KinematicSensorMeasurementPtr measurement = interBreakpointTrajectorySensor.getDerivedMeasurement(0);
207 nodeData.push_back({measurement->getJointAngles()[i]});
210 for (
float t : interBreakpointTimeStamps)
212 if (t == interBreakpointTimeStamps[0])
216 MMM::KinematicSensorMeasurementPtr measurement = interBreakpointTrajectorySensor.getDerivedMeasurement(t);
217 Eigen::VectorXf floatPositions = measurement->getJointAngles();
218 std::vector<double> doublePositions;
219 for (
int j = 0; j < floatPositions.size(); j++)
221 doublePositions.push_back(floatPositions[j]);
224 for (
unsigned int i = 0; i < positions.size(); i++)
226 nodeData[i].push_back(positions[i]);
230 Ice::StringSeq dimensionNames = rns->getNodeNames();
232 std::vector<double>doubleTimeStamps(interBreakpointTimeStamps.begin(), interBreakpointTimeStamps.end());
234 interBreakpointTrajectories.push_back(traj);
236 trajectory->setInterBreakpointTrajectories(interBreakpointTrajectories);
237 trajectories.push_back(trajectory);
238 if (nodeSets.find(rns->getName()) != nodeSets.end())
241 trajectories.clear();
244 nodeSets.insert(rns->getName());
252 throw (
"Not implemented");