KeypointsMPController.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  * @package ...
17  * @author Jianfeng Gao ( jianfeng dot gao at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <VirtualRobot/Robot.h>
26 #include <VirtualRobot/IK/DifferentialIK.h>
27 
32 
33 #include <armarx/control/common/FTSensor.h>
35 #include <armarx/control/njoint_mp_controller/task_space/ControllerInterface.h>
36 
37 #include <mplib/core/SampledTrajectory.h>
38 #include <mplib/representation/vmp/PrincipalComponentVMP.h>
39 #include <mplib/factories/VMPFactory.h>
40 #include <mplib/factories/AbstractMPFactory.h>
41 #include <mplib/factories/MPFactoryCreator.h>
42 
43 
45 {
46  namespace common = armarx::control::common;
48 
49  /**
50  * @defgroup Library-KeypointMPController KeypointMPController
51  * @ingroup Library-RobotUnit-NJointControllers
52  * A description of the library KeypointMPController.
53  *
54  * @class KeypointMPController
55  * @ingroup Library-KeypointMPController
56  * @brief Brief description of class KeypointMPController.
57  *
58  * Detailed description of class KeypointMPController.
59  */
61  public NJointControllerWithTripleBuffer<law::TaskspaceKeypointsAdmittanceController::Config>,
62  public KeypointMPControllerInterface
63  {
64  public:
65  using ConfigPtrT = KeypointMPControllerConfigPtr;
66  using VMPPtr = std::shared_ptr<mplib::representation::vmp::PrincipalComponentVMP>;
67  // using VMPPtr = std::shared_ptr<mplib::representation::AbstractMovementPrimitive>;
68 
69  KeypointMPController(const RobotUnitPtr& robotUnit, const NJointControllerConfigPtr& config, const VirtualRobot::RobotPtr&);
70  std::string getClassName(const Ice::Current&) const override;
71  void rtRun(const IceUtil::Time& sensorValuesTimestamp, const IceUtil::Time& timeSinceLastIteration) override;
72 
73  protected:
74  // NJointController interface
75  void rtPreActivateController() override;
76  void onInitNJointController() override;
77 
78  public:
79  // KeypointMPControllerInterface interface
80  void setKeypoints(const Eigen::VectorXf&, const Ice::Current &) override;
81  void resetKeypoints(const int n_points, const float keypoint_stiffness, const bool is_rigid, const Eigen::VectorXf& ctrl_mask, const Eigen::VectorXf& init_keypoints_position, const Ice::Current &) override;
82 
83  public:
84  // NJointTaskspaceAdmittanceControllerInterface interface
85  std::string getKinematicChainName(const Ice::Current &) override;
86  void setTCPPose(const Eigen::Matrix4f&, const Ice::Current&) override;
87  // void setNullspaceJointAngles(const Eigen::VectorXf&, const Ice::Current&) override;
88 
89  /// set control parameter
90  void setControlParameters(const std::string&, const Eigen::VectorXf&, const Ice::Current&) override;
91  void setForceTorqueBaseline(const Eigen::Vector3f&, const Eigen::Vector3f&, const Ice::Current&) override;
92 
93  /// ft sensor
94  void toggleGravityCompensation(const bool toggle, const Ice::Current &) override;
95  void setTCPMass(Ice::Float, const Ice::Current&) override;
96  void setTCPCoMInFTFrame(const Eigen::Vector3f&, const Ice::Current&) override;
97  void calibrateFTSensor(const Ice::Current&) override;
98 
99  public:
100  // MPPoolInterface interface
101  std::string getNames(const Ice::Current &) override;
102  void start(const Ice::DoubleSeq &, const Ice::Current &) override;
103  void startWithTime(const Ice::DoubleSeq &, Ice::Double, const Ice::Current &) override;
104  void startAsTraj(const Ice::Current &) override;
105  void startAsTrajWithTime(Ice::Double, const Ice::Current &) override;
106  void stop(const Ice::Current &) override;
107  void pause(const Ice::Current &) override;
108  void resume(const Ice::Current &) override;
109  void reset(const Ice::Current &) override;
110  bool isFinished(const Ice::Current &) override;
111  void learnFromCSV(const Ice::StringSeq &, const Ice::Current &) override;
112  void setGoal(const Ice::DoubleSeq &, const Ice::Current &) override;
113  void setStartAndGoal(const Ice::DoubleSeq &, const Ice::DoubleSeq &, const Ice::Current &) override;
114  void setViaPoint(Ice::Double, const Ice::DoubleSeq &, const Ice::Current &) override;
115  void removeAllViaPoint(const Ice::Current &) override;
116  std::string serialize(const Ice::Current &) override;
117  Ice::DoubleSeq deserialize(const std::string& mpAsString, const Ice::Current &) override;
118  Ice::Double getCanVal(const Ice::Current &) override;
119 
120  void toggleMP(const bool enableMP, const Ice::Current &) override;
121  bool getMPEnabled(const Ice::Current &) override;
122 
123  protected:
124  void onPublish(const SensorAndControl&, const DebugDrawerInterfacePrx&, const DebugObserverInterfacePrx&) override;
125 
126  void publishPose(StringVariantBaseMap& datafields, const std::string& name, const Eigen::Matrix4f& pose);
127  void publishVec6f(StringVariantBaseMap& datafields, const std::string& name, const Eigen::Vector6f& vec);
128  void publishVecXf(StringVariantBaseMap& datafields, const std::string& name, const Eigen::Vector6f& vec);
129 
130  private:
131  /// devices
132  std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
133  std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
134  std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
135  std::vector<ControlTarget1DoFActuatorTorque*> targets;
136 
137  /// set buffers
138  struct debugRTInfo
139  {
140  StringFloatDictionary desired_torques;
141  };
142  TripleBuffer<debugRTInfo> debugRTBuffer;
145 
146  struct rt2mpInfo
147  {
148  mplib::core::DVec2d currentState;
149  double deltaT;
150  };
151  TripleBuffer<rt2mpInfo> rt2mpBuffer;
152 
153  /// variables
154  std::string kinematicChainName;
155  std::vector<std::string> jointNames;
156  law::TaskspaceKeypointsAdmittanceController controller;
157  common::FTSensor ftsensor;
158 
159  std::atomic_bool rtFirstRun = true;
160  std::atomic_bool rtReady = false;
161 
162  /// vmp
163  VMPPtr pointVMP;
164  Eigen::VectorXf initialStateEigen;
165  mplib::core::DVec2d initialState;
166  mplib::core::DVec2d currentState;
167  mplib::core::DVec targetPosition;
168  mplib::core::DVec goal;
169  int numPoints = 0;
170  void runMP();
171  double canVal = 1.0;
172  double timeDuration = 0.0;
173  std::atomic_bool mpRunning = false;
174  std::atomic_bool mpFinished = false;
175 
176  std::atomic_bool mpEnabled {false};
177 
178  // NJointTaskspaceAdmittanceControllerInterface interface
179  public:
180  void reconfigureController(const std::string &, const Ice::Current &) override;
181  };
182 }
armarx::control::njoint_mp_controller::task_space::KeypointMPController::deserialize
Ice::DoubleSeq deserialize(const std::string &mpAsString, const Ice::Current &) override
Definition: KeypointsMPController.cpp:666
armarx::control::njoint_mp_controller::task_space::KeypointMPController::KeypointMPController
KeypointMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: KeypointsMPController.cpp:42
armarx::control::njoint_mp_controller::task_space::KeypointMPController::publishPose
void publishPose(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Matrix4f &pose)
Definition: KeypointsMPController.cpp:331
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::control::njoint_mp_controller::task_space::KeypointMPController::getKinematicChainName
std::string getKinematicChainName(const Ice::Current &) override
Definition: KeypointsMPController.cpp:182
armarx::control::njoint_mp_controller::task_space::KeypointMPController
Brief description of class KeypointMPController.
Definition: KeypointsMPController.h:60
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::control::njoint_mp_controller::task_space::KeypointMPController::publishVec6f
void publishVec6f(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Vector6f &vec)
Definition: KeypointsMPController.cpp:339
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setTCPPose
void setTCPPose(const Eigen::Matrix4f &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:466
armarx::NJointControllerWithTripleBuffer
Definition: NJointControllerWithTripleBuffer.h:10
RobotUnit.h
armarx::control::njoint_mp_controller::task_space::KeypointMPController::reconfigureController
void reconfigureController(const std::string &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:326
armarx::control::njoint_mp_controller::task_space::KeypointMPController::VMPPtr
std::shared_ptr< mplib::representation::vmp::PrincipalComponentVMP > VMPPtr
Definition: KeypointsMPController.h:66
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setStartAndGoal
void setStartAndGoal(const Ice::DoubleSeq &, const Ice::DoubleSeq &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:642
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:586
armarx::control::njoint_mp_controller::task_space::KeypointMPController::start
void start(const Ice::DoubleSeq &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:556
armarx::control::njoint_mp_controller::task_space::KeypointMPController::getCanVal
Ice::Double getCanVal(const Ice::Current &) override
Definition: KeypointsMPController.cpp:681
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setKeypoints
void setKeypoints(const Eigen::VectorXf &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:741
armarx::control::njoint_mp_controller::task_space::KeypointMPController::serialize
std::string serialize(const Ice::Current &) override
Definition: KeypointsMPController.cpp:657
armarx::control::common
This file is part of ArmarX.
Definition: aron_conversions.cpp:29
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setForceTorqueBaseline
void setForceTorqueBaseline(const Eigen::Vector3f &, const Eigen::Vector3f &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:525
armarx::control::njoint_mp_controller::task_space::KeypointMPController::calibrateFTSensor
void calibrateFTSensor(const Ice::Current &) override
Definition: KeypointsMPController.cpp:546
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::control::njoint_mp_controller::task_space::KeypointMPController::pause
void pause(const Ice::Current &) override
Definition: KeypointsMPController.cpp:586
armarx::control::njoint_mp_controller::task_space::KeypointMPController::resetKeypoints
void resetKeypoints(const int n_points, const float keypoint_stiffness, const bool is_rigid, const Eigen::VectorXf &ctrl_mask, const Eigen::VectorXf &init_keypoints_position, const Ice::Current &) override
Definition: KeypointsMPController.cpp:749
armarx::control::njoint_mp_controller::task_space::KeypointMPController::startAsTrajWithTime
void startAsTrajWithTime(Ice::Double, const Ice::Current &) override
Definition: KeypointsMPController.cpp:576
controller
Definition: AddOperation.h:39
armarx::control::njoint_mp_controller::task_space::KeypointMPController::onPublish
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
Definition: KeypointsMPController.cpp:357
armarx::control::njoint_mp_controller::task_space::KeypointMPController::getNames
std::string getNames(const Ice::Current &) override
Definition: KeypointsMPController.cpp:551
armarx::control::njoint_mp_controller::task_space::KeypointMPController::startWithTime
void startWithTime(const Ice::DoubleSeq &, Ice::Double, const Ice::Current &) override
Definition: KeypointsMPController.cpp:562
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::control::njoint_mp_controller::task_space::KeypointMPController::learnFromCSV
void learnFromCSV(const Ice::StringSeq &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:610
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setTCPCoMInFTFrame
void setTCPCoMInFTFrame(const Eigen::Vector3f &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:539
armarx::control::njoint_mp_controller::task_space::KeypointMPController::getMPEnabled
bool getMPEnabled(const Ice::Current &) override
Definition: KeypointsMPController.cpp:284
armarx::control::njoint_mp_controller::task_space::KeypointMPController::toggleGravityCompensation
void toggleGravityCompensation(const bool toggle, const Ice::Current &) override
ft sensor
Definition: KeypointsMPController.cpp:686
armarx::control::common::control_law
This file is part of ArmarX.
Definition: aron_conversions.cpp:68
ControlTarget1DoFActuator.h
armarx::control::njoint_mp_controller::task_space::KeypointMPController::resume
void resume(const Ice::Current &) override
Definition: KeypointsMPController.cpp:591
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setGoal
void setGoal(const Ice::DoubleSeq &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:637
KeypointsAdmittanceController.h
armarx::control::njoint_mp_controller::task_space
This file is part of ArmarX.
Definition: AdmittanceController.cpp:26
armarx::control::njoint_mp_controller::task_space::KeypointMPController::isFinished
bool isFinished(const Ice::Current &) override
Definition: KeypointsMPController.cpp:605
armarx::control::njoint_mp_controller::task_space::KeypointMPController::toggleMP
void toggleMP(const bool enableMP, const Ice::Current &) override
Definition: KeypointsMPController.cpp:278
NJointController.h
armarx::control::njoint_mp_controller::task_space::KeypointMPController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: KeypointsMPController.cpp:693
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::njoint_mp_controller::task_space::KeypointMPController::publishVecXf
void publishVecXf(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Vector6f &vec)
Definition: KeypointsMPController.cpp:349
armarx::control::njoint_mp_controller::task_space::KeypointMPController::removeAllViaPoint
void removeAllViaPoint(const Ice::Current &) override
Definition: KeypointsMPController.cpp:652
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setViaPoint
void setViaPoint(Ice::Double, const Ice::DoubleSeq &, const Ice::Current &) override
Definition: KeypointsMPController.cpp:647
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::control::njoint_mp_controller::task_space::KeypointMPController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: KeypointsMPController.cpp:177
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setControlParameters
void setControlParameters(const std::string &, const Eigen::VectorXf &, const Ice::Current &) override
set control parameter
Definition: KeypointsMPController.cpp:480
armarx::control::njoint_mp_controller::task_space::KeypointMPController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: KeypointsMPController.cpp:187
armarx::control::njoint_mp_controller::task_space::KeypointMPController::setTCPMass
void setTCPMass(Ice::Float, const Ice::Current &) override
Definition: KeypointsMPController.cpp:532
Eigen::Matrix< float, 6, 1 >
armarx::control::njoint_mp_controller::task_space::KeypointMPController::reset
void reset(const Ice::Current &) override
Definition: KeypointsMPController.cpp:596
armarx::control::njoint_mp_controller::task_space::KeypointMPController::onInitNJointController
void onInitNJointController() override
Definition: KeypointsMPController.cpp:723
SensorValue1DoFActuator.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::control::njoint_mp_controller::task_space::KeypointMPController::stop
void stop(const Ice::Current &) override
Definition: KeypointsMPController.cpp:581
armarx::TripleBuffer< debugRTInfo >
armarx::control::njoint_mp_controller::task_space::KeypointMPController::startAsTraj
void startAsTraj(const Ice::Current &) override
Definition: KeypointsMPController.cpp:570