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
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
#define VAROUT(x)
constexpr T c
void run(MPInputPtr, MPOutputPtr, const PhaseStopResult &) override
MP(const MPConfig &c)
Definition MP.cpp:17
std::atomic_bool running
Definition MP.h:167
std::vector< mplib::representation::MPState > currentState
Definition MP.h:180
std::atomic_bool firstRun
Definition MP.h:172
std::atomic_bool paused
Definition MP.h:169
#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...
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
This file is part of ArmarX.
std::shared_ptr< KeypointsMPInput > KeypointsMPInputPtr
Definition KeypointsMP.h:45
std::lock_guard< std::recursive_mutex > LockGuardType
Definition MP.h:45
std::shared_ptr< MPInput > MPInputPtr
Definition MP.h:70
std::shared_ptr< KeypointsMPOutput > KeypointsMPOutputPtr
Definition KeypointsMP.h:46
std::shared_ptr< MPOutput > MPOutputPtr
Definition MP.h:71