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/VirtualRobot.h>
26
27// #include <RobotAPI/components/units/RobotUnit/RobotUnit.h>
29// #include <RobotAPI/components/units/RobotUnit/ControlTargets/ControlTarget1DoFActuator.h>
30// #include <RobotAPI/components/units/RobotUnit/SensorValues/SensorValue1DoFActuator.h>
31
32#include <armarx/control/common/FTSensor.h>
34#include <armarx/control/njoint_mp_controller/task_space/ControllerInterface.h>
35
36#include <mplib/core/SampledTrajectory.h>
37#include <mplib/factories/AbstractMPFactory.h>
38#include <mplib/factories/MPFactoryCreator.h>
39#include <mplib/factories/VMPFactory.h>
40#include <mplib/representation/vmp/PrincipalComponentVMP.h>
41
43{
44 namespace common = armarx::control::common;
45 namespace law = armarx::control::common::control_law;
46
47 /**
48 * @defgroup Library-KeypointMPController KeypointMPController
49 * @ingroup Library-RobotUnit-NJointControllers
50 * A description of the library KeypointMPController.
51 *
52 * @class KeypointMPController
53 * @ingroup Library-KeypointMPController
54 * @brief Brief description of class KeypointMPController.
55 *
56 * Detailed description of class KeypointMPController.
57 */
60 law::TaskspaceKeypointsAdmittanceController::Config>,
61 public KeypointMPControllerInterface
62 {
63 public:
64 using ConfigPtrT = KeypointMPControllerConfigPtr;
65 using VMPPtr = std::shared_ptr<mplib::representation::vmp::PrincipalComponentVMP>;
66 // using VMPPtr = std::shared_ptr<mplib::representation::AbstractMovementPrimitive>;
67
68 KeypointMPController(const RobotUnitPtr& robotUnit,
69 const NJointControllerConfigPtr& config,
71 std::string getClassName(const Ice::Current&) const override;
72 void rtRun(const IceUtil::Time& sensorValuesTimestamp,
73 const IceUtil::Time& timeSinceLastIteration) override;
74
75 protected:
76 // NJointController interface
77 void rtPreActivateController() override;
78 void onInitNJointController() override;
79
80 public:
81 // KeypointMPControllerInterface interface
82 void setKeypoints(const Eigen::VectorXf&, const Ice::Current&) override;
83 void resetKeypoints(const int n_points,
84 const float keypoint_stiffness,
85 const bool is_rigid,
86 const Eigen::VectorXf& ctrl_mask,
87 const Eigen::VectorXf& init_keypoints_position,
88 const Ice::Current&) override;
89
90 public:
91 // NJointTaskspaceAdmittanceControllerInterface interface
92 std::string getKinematicChainName(const Ice::Current&) override;
93 void setTCPPose(const Eigen::Matrix4f&, const Ice::Current&) override;
94 // void setNullspaceJointAngles(const Eigen::VectorXf&, const Ice::Current&) override;
95
96 /// set control parameter
97 void setControlParameters(const std::string&,
98 const Eigen::VectorXf&,
99 const Ice::Current&) override;
100 void setForceTorqueBaseline(const Eigen::Vector3f&,
101 const Eigen::Vector3f&,
102 const Ice::Current&) override;
103
104 /// ft sensor
105 void toggleGravityCompensation(const bool toggle, const Ice::Current&) override;
106 void setTCPMass(Ice::Float, const Ice::Current&) override;
107 void setTCPCoMInFTFrame(const Eigen::Vector3f&, const Ice::Current&) override;
108 void calibrateFTSensor(const Ice::Current&) override;
109
110 public:
111 // MPPoolInterface interface
112 std::string getNames(const Ice::Current&) override;
113 void start(const Ice::DoubleSeq&, const Ice::Current&) override;
114 void startWithTime(const Ice::DoubleSeq&, Ice::Double, const Ice::Current&) override;
115 void startAsTraj(const Ice::Current&) override;
116 void startAsTrajWithTime(Ice::Double, const Ice::Current&) override;
117 void stop(const Ice::Current&) override;
118 void pause(const Ice::Current&) override;
119 void resume(const Ice::Current&) override;
120 void reset(const Ice::Current&) override;
121 bool isFinished(const Ice::Current&) override;
122 void learnFromCSV(const Ice::StringSeq&, const Ice::Current&) override;
123 void setGoal(const Ice::DoubleSeq&, const Ice::Current&) override;
124 void
125 setStartAndGoal(const Ice::DoubleSeq&, const Ice::DoubleSeq&, const Ice::Current&) override;
126 void setViaPoint(Ice::Double, const Ice::DoubleSeq&, const Ice::Current&) override;
127 void removeAllViaPoint(const Ice::Current&) override;
128 std::string serialize(const Ice::Current&) override;
129 Ice::DoubleSeq deserialize(const std::string& mpAsString, const Ice::Current&) override;
130 Ice::Double getCanVal(const Ice::Current&) override;
131
132 void toggleMP(const bool enableMP, const Ice::Current&) override;
133 bool getMPEnabled(const Ice::Current&) override;
134
135 protected:
136 void onPublish(const SensorAndControl&,
138 const DebugObserverInterfacePrx&) override;
139
140 void publishPose(StringVariantBaseMap& datafields,
141 const std::string& name,
142 const Eigen::Matrix4f& pose);
143 void publishVec6f(StringVariantBaseMap& datafields,
144 const std::string& name,
145 const Eigen::Vector6f& vec);
146 void publishVecXf(StringVariantBaseMap& datafields,
147 const std::string& name,
148 const Eigen::Vector6f& vec);
149
150 private:
151 /// devices
152 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
153 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
154 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
155 std::vector<ControlTarget1DoFActuatorTorque*> targets;
156
157 /// set buffers
158 struct debugRTInfo
159 {
160 StringFloatDictionary desired_torques;
161 };
162
163 TripleBuffer<debugRTInfo> debugRTBuffer;
166
167 struct rt2mpInfo
168 {
169 mplib::core::DVec2d currentState;
170 double deltaT;
171 };
172
173 TripleBuffer<rt2mpInfo> rt2mpBuffer;
174
175 /// variables
176 std::string kinematicChainName;
177 std::vector<std::string> jointNames;
178 law::TaskspaceKeypointsAdmittanceController controller;
179 common::FTSensor ftsensor;
180
181 std::atomic_bool rtFirstRun = true;
182 std::atomic_bool rtReady = false;
183
184 /// vmp
185 VMPPtr pointVMP;
186 Eigen::VectorXf initialStateEigen;
187 mplib::core::DVec2d initialState;
188 mplib::core::DVec2d currentState;
189 mplib::core::DVec targetPosition;
190 mplib::core::DVec goal;
191 int numPoints = 0;
192 void runMP();
193 double canVal = 1.0;
194 double timeDuration = 0.0;
195 std::atomic_bool mpRunning = false;
196 std::atomic_bool mpFinished = false;
197
198 std::atomic_bool mpEnabled{false};
199
200 // NJointTaskspaceAdmittanceControllerInterface interface
201 public:
202 void reconfigureController(const std::string&, const Ice::Current&) override;
203 };
204} // namespace armarx::control::njoint_mp_controller::task_space
NJointControllerWithTripleBuffer(const law::TaskspaceKeypointsAdmittanceController::Config &initialCommands=law::TaskspaceKeypointsAdmittanceController::Config())
A simple triple buffer for lockfree comunication between a single writer and a single reader.
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
void setGoal(const Ice::DoubleSeq &, const Ice::Current &) override
void reconfigureController(const std::string &, const Ice::Current &) override
void start(const Ice::DoubleSeq &, const Ice::Current &) override
void learnFromCSV(const Ice::StringSeq &, const Ice::Current &) override
KeypointMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
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
void setKeypoints(const Eigen::VectorXf &, const Ice::Current &) override
std::shared_ptr< mplib::representation::vmp::PrincipalComponentVMP > VMPPtr
void toggleMP(const bool enableMP, const Ice::Current &) override
void setStartAndGoal(const Ice::DoubleSeq &, const Ice::DoubleSeq &, const Ice::Current &) override
void setForceTorqueBaseline(const Eigen::Vector3f &, const Eigen::Vector3f &, const Ice::Current &) override
Ice::DoubleSeq deserialize(const std::string &mpAsString, const Ice::Current &) override
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
void setTCPCoMInFTFrame(const Eigen::Vector3f &, const Ice::Current &) override
void publishPose(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Matrix4f &pose)
void setViaPoint(Ice::Double, const Ice::DoubleSeq &, const Ice::Current &) override
void setTCPPose(const Eigen::Matrix4f &, const Ice::Current &) override
void startWithTime(const Ice::DoubleSeq &, Ice::Double, const Ice::Current &) override
void publishVec6f(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Vector6f &vec)
void setControlParameters(const std::string &, const Eigen::VectorXf &, const Ice::Current &) override
set control parameter
void publishVecXf(StringVariantBaseMap &datafields, const std::string &name, const Eigen::Vector6f &vec)
void rtPreActivateController() override
This function is called before the controller is activated.
void toggleGravityCompensation(const bool toggle, const Ice::Current &) override
ft sensor
Matrix< float, 6, 1 > Vector6f
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file is part of ArmarX.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl