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 
25 #include <ArmarXCore/interface/core/BasicTypes.h>
26 #include <ArmarXCore/interface/serialization/Eigen.h>
27 
28 #include <mplib/factories/VMPFactory.h>
29 #include <mplib/factories/AbstractMPFactory.h>
30 #include <mplib/factories/MPFactoryCreator.h>
31 #include <mplib/representation/vmp/VMP.h>
32 #include <mplib/representation/vmp/PrincipalComponentVMP.h>
33 #include <mplib/representation/vmp/TaskSpacePrincipalComponentVMP.h>
34 #include <SimoxUtility/json.h>
35 
36 
38 {
39  using VMPPtr = std::shared_ptr<mplib::representation::vmp::PrincipalComponentVMP>;
40  using MutexType = std::recursive_mutex;
41  using LockGuardType = std::lock_guard<std::recursive_mutex>;
42 
43  struct MPInput {virtual ~MPInput() {}};
44  struct MPOutput {virtual ~MPOutput() {}};
45  using MPInputPtr = std::shared_ptr<MPInput>;
46  using MPOutputPtr = std::shared_ptr<MPOutput>;
47 
48  class MP
49  {
50  public:
51  struct ListViaPoint
52  {
54  std::vector<double> viaPointValue;
55  };
56 
57  struct DictViaPoint
58  {
60  std::map<std::string, double> viaPointValue;
61  };
62 
63  struct MPConfig
64  {
65  std::string name;
66  std::string className = "MP";
67  std::string mpTypeString = "PrincipalComponent";
68 // mplib::representation::VMPType vmpType = mplib::representation::VMPType::PrincipalComponent;
69  std::string mpMode = "MinimumJerk";
70  std::string mpStyle = "Discrete";
71  std::string regressionModel = "default";
72  int kernelSize = 100;
73  float damping = 20.0f;
74  double tau = 1;
75  double amplitude = 1;
76  double durationSec = 10;
77 
78  std::vector<std::string> fileList;
79  bool stopWithMP = false;
80  std::vector<ListViaPoint> viaPoints;
81 
82  /// phase stop parameters ps = PhaseStop
83  bool enablePhaseStop = false;
84  double maxValue = 100;
85  double slop = 1000;
86  double goDist = 80;
87  double backDist = 50;
88  double psKpPos = 1;
89  double psKdPos = 2;
90  double psKpOri = 1;
91  double psKdOri = 0.1;
92  double psMM2Radian = 100;
93 
94 // void resetVMPType()
95 // {
96 // if (mpTypeString != "")
97 // {
98 // vmpType = mplib::representation::VMPType::_from_string_nocase(mpTypeString.c_str());
99 // }
100 // }
101  };
103  {
104  std::vector<MPConfig> mpList;
105  };
106 
107 // MP(const std::string& name, nlohmann::json& userConfig, nlohmann::json& defaultConfig);
108  MP(const MPConfig& c);
109  virtual ~MP() = default;
110 
111 
112 
113  std::string getMPName();
114  std::string getClassName();
115 
116  /// control
117  void start();
118  void start(const Ice::DoubleSeq& goals);
119  void start(const Ice::DoubleSeq& goals, Ice::Double timeDuration);
120  void start(const Ice::DoubleSeq& goals, const Ice::DoubleSeq& starts, Ice::Double timeDuration);
121  void start(const Ice::DoubleSeq& goals, const Ice::DoubleSeq& starts);
122  void stop();
123  void pause();
124  void resume();
125  void reset();
126  bool isFinished();
127 
128  /// setting
129  void learnFromCSV(const Ice::StringSeq& fileNames = std::vector<std::string>());
130 
131  void setGoal(const Ice::DoubleSeq& goals);
132  void setStart(const Ice::DoubleSeq& starts);
133  void setStartAndGoal(const Ice::DoubleSeq& starts, const Ice::DoubleSeq& goals);
134  void setViaPoint(Ice::Double u, const Ice::DoubleSeq& viapoint);
135  void removeAllViaPoint();
136 
137  /// serialze
138  void setWeight(const std::vector<std::vector<double>> &weights);
139  void setTranslationWeights(const std::vector<std::vector<double>>& weights);
140  void setRotationWeights(const std::vector<std::vector<double>>& weights);
141  DoubleSeqSeq getWeight();
142  std::string serialize(const std::string& mode = "string");
143  Ice::DoubleSeq deserialize(const std::string&, const std::string& mode = "string");
144 
145  ///
146  double getCanonicalValue();
147  virtual void run(MPInputPtr, MPOutputPtr) = 0;
148 
149  private:
150  void 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:
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  double canonicalValue = 1.0;
171 
172  std::atomic_bool goalSetByUser {false};
173  /// Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined, if you set
174  /// viapoints you will have memory allocation error. So make sure mp is trained before setting any viapoint.
175  std::atomic_bool mpTrained {false};
176  mplib::core::DVec targetPoseVec;
177  mplib::core::DVec targetPoseVecInTraj;
178  std::vector<mplib::representation::MPState> currentState;
179  std::vector<std::pair<double, mplib::core::DVec>> userDefinedViaPoints;
180  };
181 
182  using MPPtr = std::shared_ptr<MP>;
183 
184 } /// namespace armarx::control::common::mp
armarx::control::common::mp::MP::MPListConfig::mpList
std::vector< MPConfig > mpList
Definition: MP.h:104
armarx::control::common::mp::MP::reset
void reset()
Definition: MP.cpp:116
armarx::control::common::mp::MP::ListViaPoint::viaPointValue
std::vector< double > viaPointValue
Definition: MP.h:54
armarx::control::common::mp::MP
Definition: MP.h:48
armarx::control::common::mp::MP::setRotationWeights
void setRotationWeights(const std::vector< std::vector< double >> &weights)
armarx::control::common::mp::MP::MPConfig::psKdPos
double psKdPos
Definition: MP.h:89
armarx::control::common::mp::MP::deserialize
Ice::DoubleSeq deserialize(const std::string &, const std::string &mode="string")
Definition: MP.cpp:295
armarx::control::common::mp::MP::MPConfig::backDist
double backDist
Definition: MP.h:87
armarx::control::common::mp::MP::MPConfig::name
std::string name
Definition: MP.h:65
armarx::control::common::mp::MP::run
virtual void run(MPInputPtr, MPOutputPtr)=0
armarx::control::common::mp::MP::MPConfig::kernelSize
int kernelSize
Definition: MP.h:72
armarx::control::common::mp::MP::canonicalValue
double canonicalValue
Definition: MP.h:170
armarx::control::common::mp::MP::MPConfig::viaPoints
std::vector< ListViaPoint > viaPoints
Definition: MP.h:80
armarx::control::common::mp::MutexType
std::recursive_mutex MutexType
Definition: MP.h:40
armarx::control::common::mp::MP::learnFromCSV
void learnFromCSV(const Ice::StringSeq &fileNames=std::vector< std::string >())
setting
Definition: MP.cpp:139
armarx::control::common::mp::MP::MPConfig::stopWithMP
bool stopWithMP
Definition: MP.h:79
armarx::control::common::mp::MP::stop
void stop()
Definition: MP.cpp:101
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::mp::MP::pause
void pause()
Definition: MP.cpp:106
armarx::control::common::mp::MP::targetPoseVec
mplib::core::DVec targetPoseVec
Definition: MP.h:176
armarx::control::common::mp::MP::ListViaPoint
Definition: MP.h:51
armarx::control::common::mp::MP::MPConfig::psKdOri
double psKdOri
Definition: MP.h:91
armarx::control::common::mp
namespace armarx::control::common::control_law
Definition: aron_conversions.cpp:135
armarx::control::common::mp::VMPPtr
std::shared_ptr< mplib::representation::vmp::PrincipalComponentVMP > VMPPtr
Definition: MP.h:39
armarx::control::common::mp::MP::DictViaPoint::canonicalValue
double canonicalValue
Definition: MP.h:59
armarx::control::common::mp::MP::getMPName
std::string getMPName()
Definition: MP.cpp:39
armarx::control::common::mp::MP::mpTrained
std::atomic_bool mpTrained
Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined,...
Definition: MP.h:175
armarx::control::common::mp::MP::MPConfig::goDist
double goDist
Definition: MP.h:86
armarx::control::common::mp::MP::MP
MP(const MPConfig &c)
Definition: MP.cpp:16
armarx::control::common::mp::MP::cfg
MPConfig cfg
Definition: MP.h:159
armarx::control::common::mp::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: MP.h:41
armarx::control::common::mp::MP::MPConfig::mpMode
std::string mpMode
Definition: MP.h:69
armarx::control::common::mp::MP::MPConfig::className
std::string className
Definition: MP.h:66
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::control::common::mp::MPPtr
std::shared_ptr< MP > MPPtr
Definition: MP.h:182
armarx::control::common::mp::MP::setWeight
void setWeight(const std::vector< std::vector< double >> &weights)
serialze
Definition: MP.cpp:268
armarx::control::common::mp::MP::finished
std::atomic_bool finished
Definition: MP.h:168
armarx::control::common::mp::MP::setViaPoint
void setViaPoint(Ice::Double u, const Ice::DoubleSeq &viapoint)
Definition: MP.cpp:236
armarx::control::common::mp::MP::MPConfig::enablePhaseStop
bool enablePhaseStop
phase stop parameters ps = PhaseStop
Definition: MP.h:83
armarx::control::common::mp::MP::MPConfig::slop
double slop
Definition: MP.h:85
armarx::control::common::mp::MP::getCanonicalValue
double getCanonicalValue()
Definition: MP.cpp:262
armarx::control::common::mp::MP::serialize
std::string serialize(const std::string &mode="string")
Definition: MP.cpp:278
armarx::control::common::mp::MP::goalSetByUser
std::atomic_bool goalSetByUser
Definition: MP.h:172
armarx::control::common::mp::MP::paused
std::atomic_bool paused
Definition: MP.h:169
armarx::control::common::mp::MP::MPConfig
Definition: MP.h:63
armarx::control::common::mp::MP::MPConfig::fileList
std::vector< std::string > fileList
Definition: MP.h:78
armarx::control::common::mp::MPInputPtr
std::shared_ptr< MPInput > MPInputPtr
Definition: MP.h:45
armarx::control::common::mp::MP::vmp
VMPPtr vmp
Definition: MP.h:164
armarx::control::common::mp::MP::MPConfig::mpStyle
std::string mpStyle
Definition: MP.h:70
armarx::control::common::mp::MP::MPConfig::mpTypeString
std::string mpTypeString
Definition: MP.h:67
armarx::control::common::mp::MP::DictViaPoint
Definition: MP.h:57
armarx::control::common::mp::MP::currentState
std::vector< mplib::representation::MPState > currentState
Definition: MP.h:178
armarx::control::common::mp::MPInput
Definition: MP.h:43
armarx::control::common::mp::MP::removeAllViaPoint
void removeAllViaPoint()
Definition: MP.cpp:256
armarx::control::common::mp::MP::MPConfig::psKpOri
double psKpOri
Definition: MP.h:90
armarx::control::common::mp::MP::MPConfig::psKpPos
double psKpPos
Definition: MP.h:88
armarx::control::common::mp::MP::MPConfig::tau
double tau
Definition: MP.h:74
armarx::control::common::mp::MP::userDefinedViaPoints
std::vector< std::pair< double, mplib::core::DVec > > userDefinedViaPoints
Definition: MP.h:179
armarx::control::common::mp::MP::DictViaPoint::viaPointValue
std::map< std::string, double > viaPointValue
Definition: MP.h:60
armarx::control::common::mp::MPOutput::~MPOutput
virtual ~MPOutput()
Definition: MP.h:44
armarx::control::common::mp::MP::isDisturbance
bool isDisturbance
Definition: MP.h:161
armarx::control::common::mp::MP::MPListConfig
Definition: MP.h:102
armarx::control::common::mp::MPOutputPtr
std::shared_ptr< MPOutput > MPOutputPtr
Definition: MP.h:46
armarx::control::common::mp::MP::setTranslationWeights
void setTranslationWeights(const std::vector< std::vector< double >> &weights)
armarx::control::common::mp::MP::ListViaPoint::canonicalValue
double canonicalValue
Definition: MP.h:53
armarx::control::common::mp::MP::getClassName
std::string getClassName()
Definition: MP.cpp:44
armarx::control::common::mp::MP::~MP
virtual ~MP()=default
armarx::control::common::mp::MP::MPConfig::maxValue
double maxValue
Definition: MP.h:84
armarx::control::common::mp::MP::setStart
void setStart(const Ice::DoubleSeq &starts)
Definition: MP.cpp:212
armarx::control::common::mp::MP::setGoal
void setGoal(const Ice::DoubleSeq &goals)
Definition: MP.cpp:189
armarx::control::common::mp::MP::targetPoseVecInTraj
mplib::core::DVec targetPoseVecInTraj
Definition: MP.h:177
armarx::control::common::mp::MP::start
void start()
control
Definition: MP.cpp:49
armarx::control::common::mp::MP::vmpType
mplib::representation::VMPType vmpType
Definition: MP.h:165
Logging.h
armarx::control::common::mp::MP::MPConfig::regressionModel
std::string regressionModel
Definition: MP.h:71
armarx::control::common::mp::MP::isFinished
bool isFinished()
Definition: MP.cpp:134
armarx::control::common::mp::MP::mpMutex
MutexType mpMutex
Definition: MP.h:163
armarx::control::common::mp::MPInput::~MPInput
virtual ~MPInput()
Definition: MP.h:43
armarx::control::common::mp::MP::MPConfig::psMM2Radian
double psMM2Radian
Definition: MP.h:92
armarx::control::common::mp::MP::MPConfig::damping
float damping
Definition: MP.h:73
armarx::control::common::mp::MP::resume
void resume()
Definition: MP.cpp:111
armarx::control::common::mp::MP::MPConfig::durationSec
double durationSec
Definition: MP.h:76
armarx::control::common::mp::MP::MPConfig::amplitude
double amplitude
Definition: MP.h:75
armarx::control::common::mp::MP::getWeight
DoubleSeqSeq getWeight()
Definition: MP.cpp:273
armarx::control::common::mp::MP::setStartAndGoal
void setStartAndGoal(const Ice::DoubleSeq &starts, const Ice::DoubleSeq &goals)
Definition: MP.cpp:230
armarx::control::common::mp::MP::running
std::atomic_bool running
Definition: MP.h:167
armarx::control::common::mp::MPOutput
Definition: MP.h:44