KeypointsMP.cpp
Go to the documentation of this file.
1 #include "KeypointsMP.h"
2 
3 #include <SimoxUtility/math/compare/is_equal.h>
4 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
5 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
6 #include <VirtualRobot/MathTools.h>
7 
9 
10 #include "../utils.h"
11 
13 {
14 
15  KeypointsMP::KeypointsMP(const MPConfig& c) : MP{c}
16 
17  {
18  ARMARX_IMPORTANT << "created KeypointMP with name: " << VAROUT(cfg.name);
19  // if (cfg.viaPoints.size() > 0)
20  // {
21  // for (const auto& vp : cfg.viaPoints)
22  // {
23  // ARMARX_IMPORTANT << VAROUT(vp.canonicalValue) << " " << VAROUT(common::dVecToString(vp.viaPointValue)) << " " << VAROUT(vmp->getUmin());
24  // ARMARX_CHECK_GREATER_EQUAL(vp.canonicalValue, 0);
25  // ARMARX_CHECK_LESS_EQUAL(vp.canonicalValue, 1);
26  // userDefinedViaPoints.push_back({vp.canonicalValue, vp.viaPointValue});
27  // }
28  // }
29  }
30 
31  void
33  {
34  if (not running.load())
35  {
36  return;
37  }
38  KeypointsMPInputPtr in = std::dynamic_pointer_cast<KeypointsMPInput>(input);
39  KeypointsMPOutputPtr out = std::dynamic_pointer_cast<KeypointsMPOutput>(output);
40  ARMARX_CHECK_NOT_NULL(in) << "\nInput pointer for KeypointsMP has wrong type or empty";
41  ARMARX_CHECK_NOT_NULL(out) << "\nOutput pointer for KeypointsMP has wrong type or empty";
42 
43  out->keypointVelocity.setZero(in->keypointPosition.size());
44  if (paused.load())
45  {
46  return;
47  }
48  // ARMARX_INFO << VAROUT(canonicalValue) << VAROUT(out->keypointPosition) << VAROUT(out->keypointVelocity)
49  // << VAROUT(in->keypointPosition) << VAROUT(in->keypointVelocity);
50  double phaseStop = 0;
51  double tau = 1.0;
52 
53  if (canonicalValue < 0.01 && cfg.mpStyle == "Periodic")
54  {
55  ARMARX_IMPORTANT << " -- Periodic mode: new iteration.";
56  canonicalValue = 1.0;
57  }
58  if (canonicalValue < 1e-8 && cfg.mpStyle == "Discrete")
59  {
60  ARMARX_INFO << " -- Discrete mode: MP " << cfg.name << " finished.";
61  running.store(false);
62  return;
63  }
64 
65  LockGuardType guard(mpMutex);
66  ARMARX_CHECK_EQUAL(in->keypointPosition.size(), in->keypointVelocity.size());
67  for (int i = 0; i < in->keypointPosition.size(); i++)
68  {
69  currentState[i].pos = in->keypointPosition(i);
70  currentState[i].vel = in->keypointVelocity(i);
71  }
72  double deltaT = in->deltaT;
73 
74  double timeDuration = cfg.durationSec;
75  canonicalValue -= tau * deltaT / ((1 + phaseStop) * timeDuration);
76 
77  std::vector<mplib::representation::MPState> targetState =
78  std::dynamic_pointer_cast<mplib::representation::vmp::PrincipalComponentVMP>(vmp)
79  ->calculateDesiredState(canonicalValue, currentState);
80 
81  ARMARX_CHECK_EQUAL(out->keypointPosition.size(), out->keypointVelocity.size());
82  for (int i = 0; i < in->keypointPosition.size(); i++)
83  {
84  out->keypointPosition(i) = targetState[i].pos;
85  out->keypointVelocity(i) = targetState[i].vel;
86  // ARMARX_INFO << VAROUT(targetState[i].pos) << VAROUT(targetState[i].vel);
87  }
88  if (firstRun.load())
89  {
90  firstRun.store(false);
91  }
92  }
93 
94 } // namespace armarx::control::common::mp
armarx::control::common::mp::MP
Definition: MP.h:67
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::common::mp::MP::canonicalValue
double canonicalValue
Definition: MP.h:145
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::control::common::mp
This file is part of ArmarX.
Definition: aron_conversions.cpp:331
armarx::control::common::mp::MP::firstRun
std::atomic_bool firstRun
Definition: MP.h:146
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::KeypointsMP::run
void run(MPInputPtr, MPOutputPtr) override
Definition: KeypointsMP.cpp:32
armarx::control::common::mp::KeypointsMPOutputPtr
std::shared_ptr< KeypointsMPOutput > KeypointsMPOutputPtr
Definition: KeypointsMP.h:46
KeypointsMP.h
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::control::common::mp::MP::paused
std::atomic_bool paused
Definition: MP.h:144
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::KeypointsMP::KeypointsMP
KeypointsMP(const MPConfig &c)
Definition: KeypointsMP.cpp:15
ExpressionException.h
armarx::control::common::mp::MPOutputPtr
std::shared_ptr< MPOutput > MPOutputPtr
Definition: MP.h:64
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::control::common::mp::MP::mpMutex
MutexType mpMutex
Definition: MP.h:138
armarx::control::common::mp::KeypointsMPInputPtr
std::shared_ptr< KeypointsMPInput > KeypointsMPInputPtr
Definition: KeypointsMP.h:45
armarx::control::common::mp::MP::running
std::atomic_bool running
Definition: MP.h:142