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 
27 #include <ArmarXCore/interface/core/BasicTypes.h>
28 #include <ArmarXCore/interface/serialization/Eigen.h>
29 
30 #include <armarx/control/common/mp/aron/MPConfig.aron.generated.h>
31 
32 #include <mplib/factories/AbstractMPFactory.h>
33 #include <mplib/factories/MPFactoryCreator.h>
34 #include <mplib/factories/VMPFactory.h>
35 #include <mplib/representation/vmp/PrincipalComponentVMP.h>
36 #include <mplib/representation/vmp/TaskSpacePrincipalComponentVMP.h>
37 #include <mplib/representation/vmp/VMP.h>
38 
40 {
41  using namespace arondto;
42  using VMPPtr = std::shared_ptr<mplib::representation::vmp::PrincipalComponentVMP>;
43  using MutexType = std::recursive_mutex;
44  using LockGuardType = std::lock_guard<std::recursive_mutex>;
45 
46  using DVec = Ice::DoubleSeq;
47  using DVecVec = DoubleSeqSeq;
48 
49  struct MPInput
50  {
51  virtual ~MPInput()
52  {
53  }
54  };
55 
56  struct MPOutput
57  {
58  virtual ~MPOutput()
59  {
60  }
61  };
62 
63  using MPInputPtr = std::shared_ptr<MPInput>;
64  using MPOutputPtr = std::shared_ptr<MPOutput>;
65  using MPTrajs = std::vector<MPTraj>;
66 
67  class MP
68  {
69  public:
70  MP(const MPConfig& c);
71  virtual ~MP() = default;
72 
73  const std::string& getMPName() const;
74  const std::string& getClassName() const;
75  const std::string& getNodeSetName() const;
76  const std::string& getRole() const;
77 
78  /// control
79  void start();
80  void start(const DVec& goals);
81  void start(const DVec& goals, Ice::Double timeDuration);
82  void start(const DVec& goals, const DVec& starts, Ice::Double timeDuration);
83  void start(const DVec& goals, const DVec& starts);
84  void stop();
85  void pause();
86  void resume();
87  void reset();
88 
89  /// status
90  bool isFinished();
91  bool isRunning();
92  bool isFirstRun();
93  MPConfig getConfig();
94 
95  /// setting
96  void learnFromCSV(const Ice::StringSeq& fileNames = std::vector<std::string>());
97  void learnFromTraj(const MPTrajs& trajs = MPTrajs());
98  void trainMPFromTraj(std::vector<mplib::core::SampledTrajectory>& trajs);
99  void trainMP();
100 
101  void setDurationSec(Ice::Double timeDuration);
102  void setGoal(const DVec& goals);
103  void setStart(const DVec& starts);
104  std::vector<double> getStartVec();
105  void setStartAndGoal(const DVec& starts, const DVec& goals);
106  void setViaPoint(Ice::Double u, const DVec& viapoint);
107  void removeAllViaPoint();
108 
109  virtual void validateInitialState(const DVec& starts){};
110 
111  /// serialze
112  void setWeight(const DVecVec& weights);
113  void setTranslationWeights(const DVecVec& weights);
114  void setRotationWeights(const DVecVec& weights);
115  DVecVec getWeight();
116  std::string serialize(const std::string& mode = "string");
117  DVec deserialize(const std::string&, const std::string& mode = "string");
118 
119  ///
120  double getCanonicalValue();
121  virtual void run(MPInputPtr, MPOutputPtr) = 0;
122 
123  private:
124  void
125  resetVMPType(const std::string& mpTypeString)
126  {
127  if (mpTypeString != "")
128  {
129  vmpType = mplib::representation::VMPType::_from_string_nocase(mpTypeString.c_str());
130  }
131  }
132 
133  protected:
134  MPConfig cfg;
135 
136  bool isDisturbance = false;
137 
140  mplib::representation::VMPType vmpType = mplib::representation::VMPType::PrincipalComponent;
141 
142  std::atomic_bool running = false;
143  std::atomic_bool finished = false;
144  std::atomic_bool paused = false;
145  double canonicalValue = 1.0;
146  std::atomic_bool firstRun = true;
147 
148  std::atomic_bool goalSetByUser{false};
149  /// Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined, if you set
150  /// viapoints you will have memory allocation error. So make sure mp is trained before setting any viapoint.
151  std::atomic_bool mpTrained{false};
154  std::vector<mplib::representation::MPState> currentState;
155  std::vector<std::pair<double, DVec>> userDefinedViaPoints;
156  };
157 
158  using MPPtr = std::shared_ptr<MP>;
159 
160 } // namespace armarx::control::common::mp
armarx::control::common::mp::MP
Definition: MP.h:67
armarx::control::common::MPStatus::paused
@ paused
armarx::control::common::mp::DVecVec
DoubleSeqSeq DVecVec
Definition: MP.h:47
armarx::control::common::MPStatus::finished
@ finished
armarx::control::common::mp::MutexType
std::recursive_mutex MutexType
Definition: MP.h:43
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::mp::MP::targetPoseVecInTraj
DVec targetPoseVecInTraj
Definition: MP.h:153
armarx::control::common::mp
This file is part of ArmarX.
Definition: aron_conversions.cpp:331
armarx::control::common::mp::VMPPtr
std::shared_ptr< mplib::representation::vmp::PrincipalComponentVMP > VMPPtr
Definition: MP.h:42
armarx::control::common::mp::MP::cfg
MPConfig cfg
Definition: MP.h:134
armarx::control::common::mp::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: MP.h:44
armarx::control::common::mp::MP::userDefinedViaPoints
std::vector< std::pair< double, DVec > > userDefinedViaPoints
Definition: MP.h:155
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::control::common::mp::MPPtr
std::shared_ptr< MP > MPPtr
Definition: MP.h:158
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::TaskStatus::isRunning
bool isRunning(Status status)
Returns whether the given task status describes a state where a path is planned.
Definition: PlanningUtil.cpp:67
armarx::control::common::mp::MPInputPtr
std::shared_ptr< MPInput > MPInputPtr
Definition: MP.h:63
armarx::control::common::mp::MP::vmp
VMPPtr vmp
Definition: MP.h:139
armarx::control::common::mp::MP::currentState
std::vector< mplib::representation::MPState > currentState
Definition: MP.h:154
armarx::control::common::mp::MPInput
Definition: MP.h:49
armarx::control::common::mp::MPOutput::~MPOutput
virtual ~MPOutput()
Definition: MP.h:58
armarx::control::common::mp::MPOutputPtr
std::shared_ptr< MPOutput > MPOutputPtr
Definition: MP.h:64
armarx::control::common::mp::MPTrajs
std::vector< MPTraj > MPTrajs
Definition: MP.h:65
Logging.h
armarx::control::common::mp::MP::mpMutex
MutexType mpMutex
Definition: MP.h:138
armarx::control::common::mp::MPInput::~MPInput
virtual ~MPInput()
Definition: MP.h:51
armarx::control::common::mp::MP::targetPoseVec
DVec targetPoseVec
Definition: MP.h:152
armarx::control::common::MPStatus::reset
@ reset
armarx::control::common::mp::MP::validateInitialState
virtual void validateInitialState(const DVec &starts)
Definition: MP.h:109
armarx::control::common::MPStatus::running
@ running
armarx::control::common::mp::MPOutput
Definition: MP.h:56