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