TrajectoryImporter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarXGuiPlugins::RobotTrajectoryDesigner::ImportExport
17 * @author Liran Dattner
18 * @date 2018
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22#include "TrajectoryImporter.h"
23
24#include <algorithm>
25#include <fstream>
26#include <iostream>
27
28#include <Ice/CommunicatorF.h>
29
30#include <ArmarXCore/interface/observers/Serialization.h>
31#include <ArmarXCore/interface/serialization/JSONSerialization.h>
33
35
36#include "../KinematicSolver.h"
37
39 environment(environment)
40{
41}
42
43std::vector<armarx::DesignerTrajectoryPtr>
45{
46 //Use initialized ice communicator instead of initializing a new one
47 const Ice::Current& c = Ice::emptyCurrent;
48 auto communicator = c.adapter.get()->getCommunicator();
50
51 std::string jsonString;
52
53 //Read a serialized trajectory from a file
54 std::ifstream input;
55 input.open(file);
56 std::stringstream b;
57 b << input.rdbuf();
58 input.close();
59
60 jsonString = b.str();
61
62 //deserialize using the jsonSerializer
63 JSONObject jsonSerializer = JSONObject(ic);
64 jsonSerializer.fromString(jsonString);
65
67 trajectory->deserialize(&jsonSerializer, c);
68 std::vector<std::string> nodeNames = trajectory->getDimensionNames();
69 std::vector<VirtualRobot::RobotNodePtr> nodeSet;
70 for (unsigned int i = 0; i < nodeNames.size(); i++)
71 {
72 nodeSet.push_back(environment->getRobot()->getRobotNode(nodeNames[i]));
73 }
74
75 VirtualRobot::RobotNodeSetPtr rns;
76 //Get the correct rns
77 for (VirtualRobot::RobotNodeSetPtr set :
78 environment->getRobot()
79 ->getRobotNodeSets()) //Check if Robot has an rns that has EXACTLY the needed nodes.
80 {
81 next:
82 std::vector<VirtualRobot::RobotNodePtr> nodes = set->getAllRobotNodes();
83 for (VirtualRobot::RobotNodePtr node : nodeSet)
84 {
85 if (std::find(nodes.begin(), nodes.end(), node) == nodes.end())
86 {
87 goto next; //continue next;
88 }
89 }
90 if (rns->getAllRobotNodes().size() == nodes.size())
91 {
92 rns = set;
93 break;
94 }
95 }
96
97 if (rns == NULL)
98 {
99 throw NotImplementedYetException(); //No rns has been found
100 }
101 //Make all points of the trajectory userWaypoints
102 std::vector<UserWaypointPtr> points = std::vector<UserWaypointPtr>();
103
104 for (unsigned int i = 1; i < trajectory->size(); i++)
105 {
106 std::vector<double> jointAngles;
107 for (const Trajectory::TrajData& element : *trajectory)
108 {
109 jointAngles = element.getPositions();
110 }
111 PoseBasePtr pose =
112 KinematicSolver::getInstance(environment->getScene(), environment->getRobot())
113 ->doForwardKinematic(rns, jointAngles);
114 UserWaypointPtr userPoint = std::make_shared<UserWaypoint>(UserWaypoint(pose));
115 //Make the points timeOptimalBreakPoints, so the calculation does not automatically get called
116 userPoint->setIsTimeOptimalBreakpoint(true);
117 points.push_back(userPoint);
118 }
119 DesignerTrajectoryPtr result =
120 std::make_shared<DesignerTrajectory>(DesignerTrajectory(points[0], rns));
121 for (unsigned int i = 1; i < points.size(); i++)
122 {
123 result->addLastUserWaypoint(points[i]);
124 }
125 return {result};
126}
constexpr T c
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
static std::shared_ptr< KinematicSolver > getInstance(VirtualRobot::ScenePtr scenario, VirtualRobot::RobotPtr robot)
SINGLETON-FEATURES///////////////////////////////////////////////////////////////////////////////////...
TrajectoryImporter(EnvironmentPtr environment)
std::vector< DesignerTrajectoryPtr > importTrajectory(std::string file)
Import a serialized instance of the Trajectory class from the target file as a trajectory.
The Trajectory class represents n-dimensional sampled trajectories.
Definition Trajectory.h:77
The UserWaypoint class represents a waypoint of the trajectory.
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
IceInternal::Handle< Trajectory > TrajectoryPtr
Definition Trajectory.h:52
std::shared_ptr< Environment > EnvironmentPtr
Definition Environment.h:29
std::shared_ptr< DesignerTrajectory > DesignerTrajectoryPtr
std::shared_ptr< UserWaypoint > UserWaypointPtr