MP.h
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 * @author Jianfeng Gao ( jianfeng dot gao at kit dot edu )
17 * @date 2022
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24#include <SimoxUtility/json.h>
25
28#include <ArmarXCore/interface/core/BasicTypes.h>
29#include <ArmarXCore/interface/serialization/Eigen.h>
30
31#include <armarx/control/common/mp/aron/MPConfig.aron.generated.h>
32
33#include <mplib/factories/AbstractMPFactory.h>
34#include <mplib/factories/MPFactoryCreator.h>
35#include <mplib/factories/VMPFactory.h>
36#include <mplib/representation/vmp/PrincipalComponentVMP.h>
37#include <mplib/representation/vmp/TaskSpacePrincipalComponentVMP.h>
38#include <mplib/representation/vmp/VMP.h>
39
41{
42 using namespace arondto;
43 using VMPPtr = std::shared_ptr<mplib::representation::vmp::PrincipalComponentVMP>;
44 using MutexType = std::recursive_mutex;
45 using LockGuardType = std::lock_guard<std::recursive_mutex>;
46
48 {
49 double phaseStop;
50 double mpcFactor;
51 };
52
53 using DVec = Ice::DoubleSeq;
54 using DVecVec = DoubleSeqSeq;
55
56 struct MPInput
57 {
58 virtual ~MPInput()
59 {
60 }
61 };
62
63 struct MPOutput
64 {
65 virtual ~MPOutput()
66 {
67 }
68 };
69
70 using MPInputPtr = std::shared_ptr<MPInput>;
71 using MPOutputPtr = std::shared_ptr<MPOutput>;
72 using MPTrajs = std::vector<MPTraj>;
73
74 class MP
75 {
76 public:
77 MP(const MPConfig& c);
78 virtual ~MP() = default;
79
80 const std::string& getMPName() const;
81 const std::string& getClassName() const;
82 const std::string& getNodeSetName() const;
83 const std::string& getRole() const;
84 std::string getGroupName() const;
85 const arondto::MPGroup& getGroup() const;
86 bool isTaskSpaceMP() const;
87
88 /// control
89 void start();
90 void start(const DVec& goals);
91 void start(const DVec& goals, Ice::Double timeDuration);
92 void start(const DVec& goals, const DVec& starts, Ice::Double timeDuration);
93 void start(const DVec& goals, const DVec& starts);
94 void stop();
95 void requestStop();
96 void pause();
97 void resume();
98 void reset();
99
100 /// status
101 bool isFinished();
102 bool isRunning();
103 bool isFirstRun();
104 bool isStopRequested();
105 MPConfig getConfig();
106
107 /// setting
108 void learnFromCSV(const Ice::StringSeq& fileNames = std::vector<std::string>());
109 void learnFromTraj(const MPTrajs& trajs = MPTrajs());
110 void trainMPFromTraj(std::vector<mplib::core::SampledTrajectory>& trajs);
111 void trainMP();
112
113 void setDurationSec(Ice::Double timeDuration);
114 void setGoal(const DVec& goals);
115 void setStart(const DVec& starts);
116 std::vector<double> getStartVec();
117 void setStartAndGoal(const DVec& starts, const DVec& goals);
118 void setViaPoint(Ice::Double u, const DVec& viapoint);
119 void removeAllViaPoint();
121
123
124 virtual DVec
125 validateViaPoint(const DVec& viapoint)
126 {
127 return viapoint;
128 };
129
130 virtual void validateInitialState(const DVec& starts){};
131
132 /// serialze
133 void setWeight(const DVecVec& weights);
134 void setTranslationWeights(const DVecVec& weights);
135 void setRotationWeights(const DVecVec& weights);
137 std::string serialize(const std::string& mode = "string");
138 DVec deserialize(const std::string&, const std::string& mode = "string");
139
140 ///
141 double getCanonicalValue();
142 virtual void run(MPInputPtr, MPOutputPtr, const PhaseStopResult& phaseStop) = 0;
143
144 virtual void computePhaseStop(const MPInputPtr input,
145 const MPOutputPtr output,
146 PhaseStopResult& phaseStop){};
147
148 private:
149 void
150 resetVMPType(const std::string& mpTypeString)
151 {
152 if (mpTypeString != "")
153 {
154 vmpType = mplib::representation::VMPType::_from_string_nocase(mpTypeString.c_str());
155 }
156 }
157
158 protected:
159 MPConfig cfg;
160
161 bool isDisturbance = false;
162
165 mplib::representation::VMPType vmpType = mplib::representation::VMPType::PrincipalComponent;
166
167 std::atomic_bool running = false;
168 std::atomic_bool finished = false;
169 std::atomic_bool paused = false;
170 std::atomic_bool stopRequested = false;
171 double canonicalValue = 1.0;
172 std::atomic_bool firstRun = true;
173
174 std::atomic_bool goalSetByUser{false};
175 /// Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined, if you set
176 /// viapoints you will have memory allocation error. So make sure mp is trained before setting any viapoint.
177 std::atomic_bool mpTrained{false};
180 std::vector<mplib::representation::MPState> currentState;
181 std::vector<std::pair<double, DVec>> userDefinedViaPoints;
182
184 };
185
186 using MPPtr = std::shared_ptr<MP>;
187
188} // namespace armarx::control::common::mp
constexpr T c
MP(const MPConfig &c)
Definition MP.cpp:17
std::vector< double > getStartVec()
Definition MP.cpp:405
std::atomic_bool stopRequested
Definition MP.h:170
std::atomic_bool running
Definition MP.h:167
std::string getGroupName() const
Definition MP.cpp:71
void setDurationSec(Ice::Double timeDuration)
Definition MP.cpp:108
const std::string & getClassName() const
Definition MP.cpp:53
void setTranslationWeights(const DVecVec &weights)
virtual void run(MPInputPtr, MPOutputPtr, const PhaseStopResult &phaseStop)=0
void setStartAndGoal(const DVec &starts, const DVec &goals)
Definition MP.cpp:423
std::atomic_bool finished
Definition MP.h:168
const std::string & getNodeSetName() const
Definition MP.cpp:59
void setViaPoint(Ice::Double u, const DVec &viapoint)
Definition MP.cpp:430
virtual DVec validateViaPoint(const DVec &viapoint)
Definition MP.h:125
std::vector< std::pair< double, DVec > > userDefinedViaPoints
Definition MP.h:181
void setWeight(const DVecVec &weights)
serialze
Definition MP.cpp:472
armarx::DateTime mpStartTimeSec
Definition MP.h:183
virtual void computePhaseStop(const MPInputPtr input, const MPOutputPtr output, PhaseStopResult &phaseStop)
Definition MP.h:144
bool isTaskSpaceMP() const
Definition MP.cpp:83
std::atomic_bool goalSetByUser
Definition MP.h:174
void setRotationWeights(const DVecVec &weights)
DVec deserialize(const std::string &, const std::string &mode="string")
Definition MP.cpp:502
std::string serialize(const std::string &mode="string")
Definition MP.cpp:484
std::vector< mplib::representation::MPState > currentState
Definition MP.h:180
const arondto::MPGroup & getGroup() const
Definition MP.cpp:77
std::atomic_bool mpTrained
Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined,...
Definition MP.h:177
virtual void validateInitialState(const DVec &starts)
Definition MP.h:130
void trainMPFromTraj(std::vector< mplib::core::SampledTrajectory > &trajs)
Definition MP.cpp:290
void learnFromCSV(const Ice::StringSeq &fileNames=std::vector< std::string >())
setting
Definition MP.cpp:265
std::atomic_bool firstRun
Definition MP.h:172
const std::string & getMPName() const
Definition MP.cpp:47
void setStart(const DVec &starts)
Definition MP.cpp:378
const std::string & getRole() const
Definition MP.cpp:65
mplib::representation::VMPType vmpType
Definition MP.h:165
void setGoal(const DVec &goals)
Definition MP.cpp:350
void learnFromTraj(const MPTrajs &trajs=MPTrajs())
Definition MP.cpp:232
std::atomic_bool paused
Definition MP.h:169
Represents a point in time.
Definition DateTime.h:25
This file is part of ArmarX.
std::vector< MPTraj > MPTrajs
Definition MP.h:72
Ice::DoubleSeq DVec
Definition MP.h:53
std::lock_guard< std::recursive_mutex > LockGuardType
Definition MP.h:45
std::shared_ptr< MPInput > MPInputPtr
Definition MP.h:70
DoubleSeqSeq DVecVec
Definition MP.h:54
std::shared_ptr< mplib::representation::vmp::PrincipalComponentVMP > VMPPtr
Definition MP.h:43
std::shared_ptr< MPOutput > MPOutputPtr
Definition MP.h:71
std::recursive_mutex MutexType
Definition MP.h:44
std::shared_ptr< MP > MPPtr
Definition MP.h:186