24 #include <boost/make_shared.hpp>
25 #include "../Model/DesignerTrajectory.h"
26 #include "../Model/UserWaypoint.h"
27 #include <MMM/Motion/Plugin/ModelPosePlugin/ModelPoseSensor.h>
28 #include <MMM/Motion/Plugin/KinematicPlugin/KinematicSensor.h>
29 #include <MMM/Motion/Motion.h>
30 #include <MMM/Motion/MotionWriterXML.h>
31 #include <MMM/Model/Model.h>
32 #include <MMM/Model/ModelReaderXML.h>
33 #include <MMM/RapidXML/rapidxml.hpp>
41 this->environment = environment;
47 setlocale(LC_NUMERIC,
"en_US.UTF-8");
50 MMM::ModelReaderXMLPtr modelReader(
new MMM::ModelReaderXML());
51 MMM::ModelPtr modelptr = modelReader->loadModel(environment->getRobot()->getFilename());
52 MMM::MotionWriterXML motionWriter;
53 MMM::MotionList motions;
54 MMM::RapidXMLWriter writer;
55 MMM::RapidXMLWriterNodePtr metaDataNode = writer.createRootNode(
"RobotTrajectoryDesignerData");
56 metaDataNode->append_string_node(
"Robot", environment->getRobot()->getFilename());
57 MMM::Motion* kinematicMotion =
new MMM::Motion(
"RobotTrajectoryDesigner", modelptr, modelptr,
nullptr);
59 MMM::ModelPoseSensor kinematicModelPoseSensor = MMM::ModelPoseSensor(
"Model");
60 MMM::ModelPoseSensorMeasurementPtr kinematicModelPoseSensorMeasurement = boost::make_shared<MMM::ModelPoseSensorMeasurement>(MMM::ModelPoseSensorMeasurement(0, Eigen::Vector3f(1, 1, 1), MMM::Math::matrix4fToEulerXZY(MMM::Math::quat2eigen4f(0, 0, 0, 0))));
61 kinematicModelPoseSensor.addSensorMeasurement(kinematicModelPoseSensorMeasurement);
62 kinematicMotion->addSensor(boost::make_shared<MMM::ModelPoseSensor>(kinematicModelPoseSensor), 0);
64 MMM::MotionPtr mptr(kinematicMotion);
65 motions.push_back(mptr);
70 std::string location(TRAJECTORYDESIGNER_SOURCEDIR);
71 location +=
"/data/RobotTrajectoryDesigner/Resources/UserWaypoint.xml";
72 MMM::ModelPtr modelptr = modelReader->loadModel(location);
73 MMM::Motion* m =
new MMM::Motion(trajectory->getRns()->getName(), modelptr, modelptr,
nullptr);
74 MMM::ModelPoseSensor modelPoseSensor = MMM::ModelPoseSensor(trajectory->getRns()->getName());
76 std::vector<armarx::UserWaypointPtr> userWayPoints = trajectory->getAllUserWaypoints();
86 MMM::RapidXMLWriterNodePtr trajectoryNode = metaDataNode->append_node(trajectory->getRns()->getName());
87 MMM::RapidXMLWriterNodePtr userWaypointsNode = trajectoryNode->append_node(
"UserWaypoints");
88 MMM::RapidXMLWriterNodePtr transitionsNode = trajectoryNode->append_node(
"Transitions");
93 armarx::Vector3BasePtr pos = userWayPoint->getPose()->position;
94 Eigen::Vector3f position = Eigen::Vector3f(pos->x, pos->y, pos->z);
95 armarx::QuaternionBasePtr quaternion = userWayPoint->getPose()->orientation;
96 Eigen::Matrix4f matrix = MMM::Math::quat2eigen4f(quaternion->qx, quaternion->qy, quaternion->qz, quaternion->qw);
97 Eigen::Vector3f orientation = MMM::Math::matrix4fToEulerXZY(matrix);
99 MMM::ModelPoseSensorMeasurementPtr measurement = boost::make_shared<MMM::ModelPoseSensorMeasurement>(MMM::ModelPoseSensorMeasurement(userWayPoint->getUserTimestamp(), position, orientation));
101 modelPoseSensor.addSensorMeasurement(measurement);
102 MMM::RapidXMLWriterNodePtr userpointNode = userWaypointsNode->append_node(
"UserWaypoint");
103 std::string jointAngleString;
104 for (
unsigned int j = 0; j < userWayPoints[
index]->getJointAngles().size(); j++)
106 jointAngleString += boost::lexical_cast<std::string>(userWayPoints[
index]->getJointAngles()[j]) +
" ";
108 MMM::RapidXMLWriterNodePtr jointAngleNode = userpointNode->append_string_node(
"JointAngles", jointAngleString.substr(0, jointAngleString.size() - 1));
109 MMM::RapidXMLWriterNodePtr timeOptimalTimeStampNode = userpointNode->append_string_node(
"TimeOptimalTimeStamp", boost::lexical_cast<std::string>((userWayPoints[
index]->getTimeOptimalTimestamp())));
110 MMM::RapidXMLWriterNodePtr breakpointNode = userpointNode->append_string_node(
"IsBreakPoint",
std::to_string((
int)(userWayPoints[
index]->getIsTimeOptimalBreakpoint())));
111 MMM::RapidXMLWriterNodePtr cartesianSelectionNode = userpointNode->append_string_node(
"CartesianSelection",
std::to_string((
int)(userWayPoints[
index]->getIKSelection())));
113 for (
unsigned int index = 0;
index < trajectory->getNrOfUserWaypoints() - 1;
index ++)
115 MMM::RapidXMLWriterNodePtr transitionNode = transitionsNode->append_node(
"Transition");
116 MMM::RapidXMLWriterNodePtr timeOptimalDurationNode = transitionNode->append_string_node(
"TimeOptimalDuration", boost::lexical_cast<std::string>(trajectory->getTransition(
index)->getTimeOptimalDuration()));
117 MMM::RapidXMLWriterNodePtr userDurationNode = transitionNode->append_string_node(
"UserDuration", boost::lexical_cast<std::string>(trajectory->getTransition(
index)->getUserDuration()));
118 MMM::RapidXMLWriterNodePtr interpolationTypeNode = transitionNode->append_string_node(
"InterpolationType",
std::to_string((
int)(trajectory->getTransition(
index)->getInterpolationType())));
120 MMM::RapidXMLWriterNodePtr interBreakpointTrajectoriesNode = trajectoryNode->append_node(
"InterBreakpointTrajectories");
121 for (
unsigned int index = 0;
index < trajectory->getInterBreakpointTrajectories().size();
index++)
123 MMM::RapidXMLWriterNodePtr interBreakpointTrajectoryNode = interBreakpointTrajectoriesNode->append_node(
"InterBreakpointTrajectory");
124 MMM::KinematicSensorPtr interBreakpointSensor(
new MMM::KinematicSensor(trajectory->getRns()->getNodeNames(), trajectory->getRns()->getName()));
125 for (
double t : trajectory->getInterBreakpointTrajectories()[
index]->getTimestamps())
127 std::vector<double> positions = trajectory->getInterBreakpointTrajectories()[
index]->getStates(t, 0);
129 const Eigen::VectorXf vector = vectord.cast<
float>();
130 MMM::KinematicSensorMeasurement* measurement =
new MMM::KinematicSensorMeasurement(t, vector);
131 MMM::KinematicSensorMeasurementPtr measurementptr(measurement);
132 interBreakpointSensor->addSensorMeasurement(measurementptr);
134 interBreakpointSensor->setName(trajectory->getRns()->getName());
135 interBreakpointSensor->appendSensorXML(interBreakpointTrajectoryNode);
137 modelPoseSensor.setName(trajectory->getRns()->getName());
139 m->addSensor(boost::make_shared<MMM::ModelPoseSensor>(modelPoseSensor), 0);
141 MMM::MotionPtr mptr(m);
142 motions.push_back(mptr);
144 MMM::KinematicSensorPtr kinematicSensor(
new MMM::KinematicSensor(trajectory->getRns()->getNodeNames(), trajectory->getRns()->getName()));
147 for (
double t : trajectory->getFinalTrajectory()->getTimestamps())
149 std::vector<double> positions = trajectory->getFinalTrajectory()->getStates(t, 0);
151 const Eigen::VectorXf vector = vectord.cast<
float>();
152 MMM::KinematicSensorMeasurement* measurement =
new MMM::KinematicSensorMeasurement(t, vector);
153 MMM::KinematicSensorMeasurementPtr measurementptr(measurement);
154 kinematicSensor->addSensorMeasurement(measurementptr);
156 kinematicSensor->setName(trajectory->getRns()->getName());
157 kinematicMotion->addSensor(kinematicSensor, 0);
163 std::string motionString = motionWriter.toXMLString(motions, file);
165 std::string metaDataString = writer.print(
false);
168 motionString = motionString.substr(0, motionString.find(
"</MMM>"));
170 metaDataString = metaDataString.substr(metaDataString.find(
"<RobotTrajectoryDesignerData"));
171 motionString = motionString + metaDataString +
'\n' +
"</MMM>";
174 QString xmlInput = QString::fromStdString(motionString);
176 QXmlStreamReader qxmlreader(xmlInput);
177 QXmlStreamWriter qxmlwriter(&xmlOutput);
178 qxmlwriter.setAutoFormatting(
true);
179 while (!qxmlreader.atEnd())
181 qxmlreader.readNext();
182 if (!qxmlreader.isWhitespace())
184 qxmlwriter.writeCurrentToken(qxmlreader);
188 std::ofstream output;
190 output << xmlOutput.toStdString();