JSMP.cpp
Go to the documentation of this file.
1 #include "JSMP.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  JSMP::JSMP(const arondto::MPConfig& c) : MP{c}
16  {
17  ARMARX_INFO << "-- created JSMP with name: " << VAROUT(cfg.name);
18  // if (cfg.viaPoints.size() > 0)
19  // {
20  // for (const auto& vp : cfg.viaPoints)
21  // {
22  // ARMARX_IMPORTANT << VAROUT(vp.canonicalValue) << " " << VAROUT(common::dVecToString(vp.viaPointValue)) << " " << VAROUT(vmp->getUmin());
23  // ARMARX_CHECK_GREATER_EQUAL(vp.canonicalValue, 0);
24  // ARMARX_CHECK_LESS_EQUAL(vp.canonicalValue, 1);
25  // userDefinedViaPoints.push_back({vp.canonicalValue, vp.viaPointValue});
26  // }
27  // }
28  }
29 
30  void
32  {
33  if (not running.load())
34  {
35  return;
36  }
37  JSMPInputPtr in = std::dynamic_pointer_cast<JSMPInput>(input);
38  JSMPOutputPtr out = std::dynamic_pointer_cast<JSMPOutput>(output);
39  ARMARX_CHECK_NOT_NULL(in) << "\nInput pointer for JSMP has wrong type or empty";
40  ARMARX_CHECK_NOT_NULL(out) << "\nOutput pointer for JSMP has wrong type or empty";
41 
42  out->angularVel.setZero(in->angularVel.size());
43  if (paused.load())
44  {
45  return;
46  }
47 
48  // ARMARX_INFO << VAROUT(canonicalValue);
49  double phaseStop = 0;
50  double tau = 1.0;
51 
52  if (canonicalValue < 0.01 && cfg.mpStyle == "Periodic")
53  {
54  canonicalValue = 1.0;
55  }
56  if (canonicalValue < 1e-8 && cfg.mpStyle == "Discrete")
57  {
58  ARMARX_INFO << " -- Discrete mode: MP " << cfg.name << " finished.";
59  running.store(false);
60  return;
61  }
62 
63  LockGuardType guard(mpMutex);
64  ARMARX_CHECK_EQUAL(in->angleRadian.size(), in->angularVel.size());
65  for (int i = 0; i < in->angleRadian.size(); i++)
66  {
67  currentState[i].pos = in->angleRadian(i);
68  currentState[i].vel = in->angularVel(i);
69  }
70  double deltaT = in->deltaT;
71 
72  double timeDuration = cfg.durationSec;
73  canonicalValue -= tau * deltaT / ((1 + phaseStop) * timeDuration);
74 
75  std::vector<mplib::representation::MPState> targetState =
76  std::dynamic_pointer_cast<mplib::representation::vmp::PrincipalComponentVMP>(vmp)
77  ->calculateDesiredState(canonicalValue, currentState);
78 
79  ARMARX_CHECK_EQUAL(out->angleRadian.size(), out->angularVel.size());
80  for (int i = 0; i < in->angleRadian.size(); i++)
81  {
82  out->angleRadian(i) = targetState[i].pos;
83  out->angularVel(i) = targetState[i].vel;
84  // ARMARX_INFO << VAROUT(targetState[i].pos) << VAROUT(targetState[i].vel);
85  }
86  if (firstRun.load())
87  {
88  firstRun.store(false);
89  }
90  }
91 
92 } // namespace armarx::control::common::mp
armarx::control::common::mp::MP
Definition: MP.h:67
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:47
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
JSMP.h
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::JSMP::JSMP
JSMP(const arondto::MPConfig &c)
Definition: JSMP.cpp:15
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:48
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::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::JSMP::run
void run(MPInputPtr, MPOutputPtr) override
Definition: JSMP.cpp:31
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::MP::running
std::atomic_bool running
Definition: MP.h:142