DeprecatedNJointTSDMPController.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <VirtualRobot/IK/IKSolver.h>
5#include <VirtualRobot/VirtualRobot.h>
6
9
12
14#include <armarx/control/deprecated_njoint_mp_controller/task_space/ControllerInterface.h>
15
16#include <dmp/representation/dmp/umitsmp.h>
17
18namespace armarx
19{
20 class SensorValue1DoFActuatorTorque;
21 class SensorValue1DoFActuatorVelocity;
22 class SensorValue1DoFActuatorPosition;
23 class ControlTarget1DoFActuatorTorque;
24 class ControlTarget1DoFActuatorVelocity;
25 class SensorValue1DoFGravityTorque;
27} // namespace armarx
28
30{
31 namespace tsvmp = armarx::control::deprecated_njoint_mp_controller::tsvmp;
32
35
36 using ViaPoint = std::pair<double, DMP::DVec>;
37 using ViaPointsSet = std::vector<ViaPoint>;
38
40 {
41 public:
43 Eigen::Matrix4f targetPose;
44 // cartesian velocity control data
45 std::vector<float> nullspaceJointVelocities;
47 std::vector<float> torqueKp;
48 std::vector<float> torqueKd;
49 VirtualRobot::IKSolver::CartesianSelection mode = VirtualRobot::IKSolver::All;
50 };
51
52 /**
53 * @brief The DeprecatedNJointTSDMPController class
54 * @ingroup Library-RobotUnit-NJointControllers
55 */
57 public NJointControllerWithTripleBuffer<DeprecatedNJointTSDMPControllerControlData>,
59 {
60 class pidController
61 {
62 public:
63 float Kp = 0, Kd = 0;
64 float lastError = 0;
65
66 float
67 update(float dt, float error)
68 {
69 float derivative = (error - lastError) / dt;
70 float retVal = Kp * error + Kd * derivative;
71 lastError = error;
72 return retVal;
73 }
74 };
75
76 public:
77 using ConfigPtrT = DeprecatedNJointTaskSpaceDMPControllerConfigPtr;
79 const NJointControllerConfigPtr& config,
81
82 // NJointControllerInterface interface
83 std::string getClassName(const Ice::Current&) const override;
84
85 // NJointController interface
86
87 void rtRun(const IceUtil::Time& sensorValuesTimestamp,
88 const IceUtil::Time& timeSinceLastIteration) override;
89
90 // DeprecatedNJointTSDMPControllerInterface interface
91 void learnDMPFromFiles(const Ice::StringSeq& fileNames, const Ice::Current&) override;
92
93 bool
94 isFinished(const Ice::Current&) override
95 {
96 return finished;
97 }
98
99 void runDMP(const Ice::DoubleSeq& goals, Ice::Double tau, const Ice::Current&) override;
100 void runDMPWithTime(const Ice::DoubleSeq& goals,
101 Ice::Double timeDuration,
102 const Ice::Current&) override;
103
104 void setSpeed(Ice::Double times, const Ice::Current&) override;
105 void
106 setViaPoints(Ice::Double u, const Ice::DoubleSeq& viapoint, const Ice::Current&) override;
107 void removeAllViaPoints(const Ice::Current&) override;
108
109 void setGoals(const Ice::DoubleSeq& goals, const Ice::Current&) override;
110
111 void setControllerTarget(Ice::Float avoidJointLimitsKp,
113 const Ice::Current&) override;
114 void setTorqueKp(const StringFloatDictionary& torqueKp, const Ice::Current&) override;
115 void setNullspaceJointVelocities(const StringFloatDictionary& nullspaceJointVelocities,
116 const Ice::Current&) override;
117
118
119 void pauseDMP(const Ice::Current&) override;
120 void resumeDMP(const Ice::Current&) override;
121
122 void resetDMP(const Ice::Current&) override;
123 void stopDMP(const Ice::Current&) override;
124
125 bool
126 isDMPRunning(const Ice::Current&) override
127 {
128 return started;
129 }
130
131 double
132 getCanVal(const Ice::Current&) override
133 {
134 return dmpCtrl->canVal;
135 }
136
137 std::string getDMPAsString(const Ice::Current&) override;
138 std::vector<double> createDMPFromString(const std::string& dmpString,
139 const Ice::Current&) override;
140
141 // VirtualRobot::IKSolver::CartesianSelection ModeFromIce(const DeprecatedNJointTaskSpaceDMPControllerMode::CartesianSelection mode);
142 Eigen::VectorXf calcIK(const Eigen::VectorXf& cartesianVel,
143 const Eigen::VectorXf& nullspace,
144 VirtualRobot::IKSolver::CartesianSelection mode);
145
146
147 void setMPWeights(const DoubleSeqSeq& weights, const Ice::Current&) override;
148 DoubleSeqSeq getMPWeights(const Ice::Current&) override;
149
150 void setLinearVelocityKd(Ice::Float kd, const Ice::Current& = Ice::emptyCurrent) override;
151 void setLinearVelocityKp(Ice::Float kp, const Ice::Current& = Ice::emptyCurrent) override;
152 void setAngularVelocityKd(Ice::Float kd, const Ice::Current& = Ice::emptyCurrent) override;
153 void setAngularVelocityKp(Ice::Float kp, const Ice::Current& = Ice::emptyCurrent) override;
154
155 void
156 enableForceStop(const Ice::Current&) override
157 {
158 useForceStop = true;
159 }
160
161 void
162 disableForceStop(const Ice::Current&) override
163 {
164 useForceStop = false;
165 }
166
167 void
168 setForceThreshold(const Eigen::Vector3f& f, const Ice::Current& current) override
169 {
170 forceThreshold.getWriteBuffer() = f;
171 forceThreshold.commitWrite();
172 }
173
174 protected:
175 void rtPreActivateController() override;
176 void rtPostDeactivateController() override;
177 VirtualRobot::IKSolver::CartesianSelection
179 virtual void onPublish(const SensorAndControl&,
181 const DebugObserverInterfacePrx&) override;
182
183 void onInitNJointController() override;
184 void onDisconnectNJointController() override;
185 void controllerRun();
186
187 private:
188 std::vector<std::string> jointNames;
189
190 struct DebugBufferData
191 {
192 StringFloatDictionary latestTargetVelocities;
193 StringFloatDictionary dmpTargets;
194 StringFloatDictionary currentPose;
195 double currentCanVal;
196 double mpcFactor;
197 double error;
198 double phaseStop;
199 double posError;
200 double oriError;
201 double deltaT;
202 };
203
204 TripleBuffer<DebugBufferData> debugOutputData;
205
206 struct RTDebugData
207 {
208 Eigen::VectorXf targetJointVels;
209 };
210
211 TripleBuffer<RTDebugData> rtDebugData;
212
213 struct RTToControllerData
214 {
215 double currentTime;
216 double deltaT;
217 Eigen::Matrix4f currentPose;
218 Eigen::VectorXf currentTwist;
219 };
220
222
223 struct RTToUserData
224 {
225 Eigen::Matrix4f currentTcpPose;
226 };
227
228 TripleBuffer<RTToUserData> rt2UserData;
229
230
232
233 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
234 std::vector<const SensorValue1DoFGravityTorque*> gravityTorqueSensors;
235 std::vector<ControlTarget1DoFActuatorVelocity*> targets;
236 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
237 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
238
239 // velocity ik controller parameters
240 std::vector<pidController> torquePIDs;
241 CartesianVelocityControllerPtr tcpController;
242 std::string nodeSetName;
243
244 // dmp parameters
245 bool finished;
246 bool started;
247
248 VirtualRobot::DifferentialIKPtr ik;
249 VirtualRobot::RobotNodePtr tcp;
250 VirtualRobot::RobotNodePtr refFrame;
251 Eigen::Vector6f targetVels;
252 Eigen::Matrix4f targetPose;
253
254 DeprecatedNJointTaskSpaceDMPControllerConfigPtr cfg;
255 mutable MutexType controllerMutex;
257
258
259 std::string debugName;
260
261 Eigen::VectorXf filtered_qvel;
262 Eigen::Vector3f filtered_position;
263 float vel_filter_factor;
264 float pos_filter_factor;
265 bool firstRun;
266
267 float KpF;
268 float DpF;
269 float KoF;
270 float DoF;
271
272 Eigen::VectorXf jointHighLimits;
273 Eigen::VectorXf jointLowLimits;
274
275 Eigen::Vector3f filteredForce;
276 Eigen::Vector3f forceOffset;
277 Eigen::Vector3f filteredForceInRoot;
278 WriteBufferedTripleBuffer<Eigen::Vector3f> forceThreshold;
279 std::atomic<bool> useForceStop;
280 const SensorValueForceTorque* forceSensor;
281
282 std::atomic<float> timeForCalibration;
283 std::atomic_bool stopped = false;
284 };
285
286} // namespace armarx::control::deprecated_njoint_mp_controller::task_space
#define TYPEDEF_PTRS_HANDLE(T)
constexpr T dt
NJointControllerWithTripleBuffer(const DeprecatedNJointTSDMPControllerControlData &initialCommands=DeprecatedNJointTSDMPControllerControlData())
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
A simple triple buffer for lockfree comunication between a single writer and a single reader.
void setTorqueKp(const StringFloatDictionary &torqueKp, const Ice::Current &) override
virtual void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
void runDMP(const Ice::DoubleSeq &goals, Ice::Double tau, const Ice::Current &) override
void setNullspaceJointVelocities(const StringFloatDictionary &nullspaceJointVelocities, const Ice::Current &) override
Eigen::VectorXf calcIK(const Eigen::VectorXf &cartesianVel, const Eigen::VectorXf &nullspace, VirtualRobot::IKSolver::CartesianSelection mode)
void setControllerTarget(Ice::Float avoidJointLimitsKp, NJointTaskSpaceDMPControllerMode::CartesianSelection mode, const Ice::Current &) override
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
VirtualRobot::IKSolver::CartesianSelection ModeFromIce(const NJointTaskSpaceDMPControllerMode::CartesianSelection mode)
std::vector< double > createDMPFromString(const std::string &dmpString, const Ice::Current &) override
DeprecatedNJointTSDMPController(const RobotUnitPtr &, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
void setViaPoints(Ice::Double u, const Ice::DoubleSeq &viapoint, const Ice::Current &) override
void rtPreActivateController() override
This function is called before the controller is activated.
void runDMPWithTime(const Ice::DoubleSeq &goals, Ice::Double timeDuration, const Ice::Current &) override
Matrix< float, 6, 1 > Vector6f
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::shared_ptr< CartesianVelocityController > CartesianVelocityControllerPtr
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl