MPPool.cpp
Go to the documentation of this file.
3 #include <SimoxUtility/json.h>
4 #include "MPPool.h"
5 #include "TSMP.h"
6 #include "JSMP.h"
7 #include "KeypointsMP.h"
8 #include "../utils.h"
9 
10 
11 
13 {
14 
15 void MPPool::createMPs(const MP::MPListConfig& mpListConfig)
16 {
17  ARMARX_INFO << "----------------------------------- creating MPs -------------------------------------------------";
18  ARMARX_IMPORTANT << "in MPPool, we have " << mpListConfig.mpList.size() << " MPs";
19 
20  for (const MP::MPConfig& c: mpListConfig.mpList)
21  {
22  auto className = mpClass::_from_string_nocase(c.className.c_str());
23  ARMARX_IMPORTANT << " -- creating MP with name " << c.name << " and type: " << c.className << VAROUT(c.mpTypeString) << VAROUT(c.mpMode) << VAROUT(c.mpStyle);
24  switch (className) {
25  case mpClass::JSMP:
26  {
27  JSMPPtr jsmp(new JSMP(c));
28  JSMPInputPtr in(new JSMPInput());
29  JSMPOutputPtr out(new JSMPOutput());
30  mps.insert({c.name, {jsmp, in, out}});
31  break;
32  }
33  case mpClass::JSVelMP:
34  break;
35  case mpClass::TSMP:
36  {
37  TSMPPtr tsmp(new TSMP(c));
38  TSMPInputPtr in(new TSMPInput());
39  TSMPOutputPtr out(new TSMPOutput());
40  mps.insert({c.name, {tsmp, in, out}});
41  break;
42  }
43  case mpClass::KeypointsMP:
44  {
45  KeypointsMPPtr tsmp(new KeypointsMP(c));
48  mps.insert({c.name, {tsmp, in, out}});
49  break;
50  }
51  case mpClass::TSVelMP:
52  break;
53  default:
54  ARMARX_WARNING << "unsupported MP class: " << c.className << "\nsupported names include: "
55  "JSMP, JSVelMP, TSMP, TSVelMP, KeypointsMP";
56  break;
57  }
58  }
59  ARMARX_INFO << "---------------------------------- All MPs created -----------------------------------------------";
60 }
61 
62 void MPPool::reconfigureMPs(const MP::MPListConfig& mpListConfig)
63 {
64  mps.clear();
65  createMPs(mpListConfig);
66 }
67 
69 {
70  for (auto& mp: mps)
71  {
72  mp.second.mp->run(mp.second.input, mp.second.output);
73  }
74 }
75 
76 std::string MPPool::getNames(const Ice::Current &iceCurrent)
77 {
78  std::string allMPs = "";
79  for (auto& mp: mps)
80  {
81  allMPs = allMPs + mp.second.mp->getMPName() + ", ";
82  }
83  return allMPs;
84 }
85 
86 void MPPool::start(const std::string& mpName, const Ice::DoubleSeq& startVec, const Ice::DoubleSeq& goalVec, Ice::Double timeDuration, const Ice::Current &iceCurrent)
87 {
88  if (mpName == "all")
89  {
90  ARMARX_IMPORTANT << "to start all MPs at once, it's up to the user to configure the start, goal and timeduration properly before this call.";
91  for (auto& mp: mps)
92  {
93  mp.second.mp->start();
94  }
95  }
96  else
97  {
98  auto search = mps.find(mpName);
99  if (search != mps.end())
100  {
101  if (timeDuration < 0.0)
102  {
103  search->second.mp->start(goalVec, startVec);
104  }
105  else
106  {
107  search->second.mp->start(goalVec, startVec, timeDuration);
108  }
109  }
110  else
111  {
112  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
113  }
114  }
115  ARMARX_IMPORTANT << "starting mp" << VAROUT(mpName) << "with start: " << dVecToString(startVec) << " and goal: " << dVecToString(goalVec) << " for " << timeDuration << " seconds";
116 }
117 
118 void MPPool::stop(const std::string& mpName, const Ice::Current &)
119 {
120  if (mpName == "all")
121  {
122  for (auto& mp: mps)
123  {
124  mp.second.mp->stop();
125  }
126  }
127  else
128  {
129  auto search = mps.find(mpName);
130  if (search != mps.end())
131  {
132  search->second.mp->stop();
133  }
134  else
135  {
136  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
137  }
138  }
139 }
140 
141 void MPPool::pause(const std::string& mpName, const Ice::Current &)
142 {
143  if (mpName == "all")
144  {
145  for (auto& mp: mps)
146  {
147  mp.second.mp->pause();
148  }
149  }
150  else
151  {
152  auto search = mps.find(mpName);
153  if (search != mps.end())
154  {
155  search->second.mp->pause();
156  }
157  else
158  {
159  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
160  }
161  }
162 }
163 
164 void MPPool::resume(const std::string& mpName, const Ice::Current &)
165 {
166  if (mpName == "all")
167  {
168  for (auto& mp: mps)
169  {
170  mp.second.mp->resume();
171  }
172  }
173  else
174  {
175  auto search = mps.find(mpName);
176  if (search != mps.end())
177  {
178  search->second.mp->resume();
179  }
180  else
181  {
182  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
183  }
184  }
185 }
186 
187 void MPPool::reset(const std::string& mpName, const Ice::Current &)
188 {
189  if (mpName == "all")
190  {
191  for (auto& mp: mps)
192  {
193  mp.second.mp->reset();
194  }
195  }
196  else
197  {
198  auto search = mps.find(mpName);
199  if (search != mps.end())
200  {
201  search->second.mp->reset();
202  }
203  else
204  {
205  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
206  }
207  }
208 }
209 
210 bool MPPool::isFinished(const std::string& mpName, const Ice::Current &)
211 {
212  if (mpName == "all")
213  {
214  for (auto& mp: mps)
215  {
216  if (!mp.second.mp->isFinished())
217  {
218  return false;
219  }
220  }
221  return true;
222  }
223  else
224  {
225  auto search = mps.find(mpName);
226  if (search != mps.end())
227  {
228  return search->second.mp->isFinished();
229  }
230  else
231  {
232  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
233  return true;
234  }
235  }
236 }
237 
238 void MPPool::learnFromCSV(const Ice::StringSeq &fileNames, const Ice::Current &iceCurrent)
239 {
240  ARMARX_INFO << "--------------------------------------- Train MPs ----------------------------------------------------";
241  for (auto& mp: mps)
242  {
243  mp.second.mp->learnFromCSV();
244  }
245  ARMARX_INFO << "--------------------------------------- done ----------------------------------------------------";
246 }
247 
248 void MPPool::setGoal(const Ice::DoubleSeq &goals, const Ice::Current &iceCurrent)
249 {
250 
251 }
252 
253 void MPPool::setStartAndGoal(const Ice::DoubleSeq &starts, const Ice::DoubleSeq &goals, const Ice::Current &iceCurrent)
254 {
255  for (auto& mp: mps)
256  {
257  mp.second.mp->setStartAndGoal(starts, goals);
258  }
259 }
260 
261 void MPPool::setViaPoint(Ice::Double u, const Ice::DoubleSeq &viapoint, const Ice::Current &iceCurrent)
262 {
263 
264 }
265 
266 void MPPool::removeAllViaPoint(const Ice::Current &iceCurrent)
267 {
268 
269 }
270 
271 //void MPPool::setWeight(const Ice::DoubleSeqSeq& weights, const Ice::Current &iceCurrent)
272 //{
273 
274 //}
275 
276 std::string MPPool::serialize(const Ice::Current &iceCurrent)
277 {
278  return "";
279 }
280 
281 Ice::DoubleSeq MPPool::deserialize(const std::string &, const Ice::Current &iceCurrent)
282 {
283  std::vector<double> test {0.1, 0.1};
284  return test;
285 }
286 
287 Ice::Double MPPool::getCanVal(const std::string &mpName, const Ice::Current &iceCurrent)
288 {
289  auto search = mps.find(mpName);
290  if (search != mps.end())
291  {
292  return search->second.mp->getCanonicalValue();
293  }
294  else
295  {
296  ARMARX_ERROR << mpName << " is not in the MP pool. Please check your configuration file";
297  return true;
298  }
299 }
300 
301 bool MPPool::getMPEnabled(const Ice::Current &)
302 {
303  return true;
304 }
305 
306 
307 } /// namespace armarx::control::common::mp
308 
309 
310 
armarx::control::common::mp::MP::MPListConfig::mpList
std::vector< MPConfig > mpList
Definition: MP.h:104
armarx::control::common::mp::MPPool::getCanVal
Ice::Double getCanVal(const std::string &mpName, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:287
armarx::MPPoolInterface::getNames
string getNames()
armarx::control::common::mp::MPPool::deserialize
Ice::DoubleSeq deserialize(const std::string &, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:281
armarx::MPPoolInterface::getMPEnabled
bool getMPEnabled()
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
TSMP.h
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:45
armarx::control::common::mp::MPPool::stop
void stop(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:118
armarx::control::common::mp::TSMPPtr
std::shared_ptr< TSMP > TSMPPtr
Definition: TSMP.h:62
JSMP.h
armarx::control::common::mp::MPPool::reset
void reset(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:187
armarx::control::common::mp::TSMP
Definition: TSMP.h:51
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::mp::MPPool::runMPs
void runMPs()
Definition: MPPool.cpp:68
armarx::control::common::mp
namespace armarx::control::common::control_law
Definition: aron_conversions.cpp:135
armarx::control::common::mp::MPPool::setGoal
void setGoal(const Ice::DoubleSeq &goals, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:248
armarx::control::common::mp::JSMPOutput
Definition: JSMP.h:39
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:46
armarx::control::common::mp::MPPool::createMPs
void createMPs(const MP::MPListConfig &mpListConfig)
Definition: MPPool.cpp:15
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::control::common::dVecToString
std::string dVecToString(const mplib::core::DVec &dvec)
Definition: utils.cpp:228
armarx::control::common::mp::KeypointsMPOutput
Definition: KeypointsMP.h:39
armarx::control::common::mp::TSMPOutputPtr
std::shared_ptr< TSMPOutput > TSMPOutputPtr
Definition: TSMP.h:47
armarx::control::common::mp::MPPool::resume
void resume(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:164
armarx::control::common::mp::KeypointsMPOutputPtr
std::shared_ptr< KeypointsMPOutput > KeypointsMPOutputPtr
Definition: KeypointsMP.h:46
armarx::MPPoolInterface::removeAllViaPoint
void removeAllViaPoint()
KeypointsMP.h
armarx::control::common::mp::JSMPPtr
std::shared_ptr< JSMP > JSMPPtr
Definition: JSMP.h:62
armarx::control::common::mp::MP::MPConfig
Definition: MP.h:63
armarx::control::common::mp::MPPool::pause
void pause(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:141
armarx::control::common::mp::MPPool::setViaPoint
void setViaPoint(Ice::Double u, const Ice::DoubleSeq &viapoint, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:261
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::control::common::mp::MPPool::reconfigureMPs
void reconfigureMPs(const MP::MPListConfig &mpListConfig)
Definition: MPPool.cpp:62
armarx::control::common::mp::MPPool::learnFromCSV
void learnFromCSV(const Ice::StringSeq &fileNames=std::vector< std::string >(), const Ice::Current &iceCurrent=Ice::emptyCurrent) override
setting
Definition: MPPool.cpp:238
ExpressionException.h
armarx::control::common::mp::MPPool::setStartAndGoal
void setStartAndGoal(const Ice::DoubleSeq &starts, const Ice::DoubleSeq &goals, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:253
armarx::control::common::mp::MP::MPListConfig
Definition: MP.h:102
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::common::mp::MPPool::start
void start(const std::string &mpName="all", const Ice::DoubleSeq &startVec=std::vector< double >(), const Ice::DoubleSeq &goalVec=std::vector< double >(), Ice::Double timeDuration=-1.0, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
control
Definition: MPPool.cpp:86
armarx::control::common::mp::KeypointsMPInput
Definition: KeypointsMP.h:32
armarx::control::common::mp::TSMPOutput
Definition: TSMP.h:40
armarx::control::common::mp::JSMP
Definition: JSMP.h:50
armarx::control::common::mp::JSMPInput
Definition: JSMP.h:32
armarx::control::common::mp::TSMPInput
Definition: TSMP.h:33
armarx::control::common::mp::MPPool::mps
std::map< std::string, MPInputOutput > mps
Definition: MPPool.h:91
armarx::control::common::mp::TSMPInputPtr
std::shared_ptr< TSMPInput > TSMPInputPtr
Definition: TSMP.h:46
armarx::MPPoolInterface::serialize
string serialize()
serialze
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
MPPool.h
armarx::control::common::mp::KeypointsMP
Definition: KeypointsMP.h:50
ArmarXDataPath.h
armarx::control::common::mp::KeypointsMPPtr
std::shared_ptr< KeypointsMP > KeypointsMPPtr
Definition: KeypointsMP.h:59
armarx::control::common::mp::KeypointsMPInputPtr
std::shared_ptr< KeypointsMPInput > KeypointsMPInputPtr
Definition: KeypointsMP.h:45
armarx::control::common::mp::MPPool::isFinished
bool isFinished(const std::string &mpName="all", const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MPPool.cpp:210