NJointTSDMPController.h
Go to the documentation of this file.
1#pragma once
2
3
4// armarx
5#include <VirtualRobot/VirtualRobot.h>
6
7// DMP
8#include <dmp/representation/dmp/umitsmp.h>
9
10// armarx
12
18
19// control
21#include <armarx/control/deprecated_njoint_mp_controller/task_space/ControllerInterface.h>
22
23namespace armarx
24{
25 class SensorValue1DoFActuatorTorque;
26 class SensorValue1DoFActuatorVelocity;
27 class SensorValue1DoFActuatorPosition;
28 class ControlTarget1DoFActuatorTorque;
30} // namespace armarx
31
33{
34 namespace tsvmp = armarx::control::deprecated_njoint_mp_controller::tsvmp;
35
38
39 using ViaPoint = std::pair<double, DMP::DVec>;
40 using ViaPointsSet = std::vector<ViaPoint>;
41
43 {
44 public:
46 Eigen::Matrix4f targetPose;
47 // cartesian velocity control data
48 std::vector<float> nullspaceJointVelocities;
50 std::vector<float> torqueKp;
51 std::vector<float> torqueKd;
52 VirtualRobot::IKSolver::CartesianSelection mode = VirtualRobot::IKSolver::All;
53 };
54
55 /**
56 * @brief The NJointTSDMPController class
57 * @ingroup Library-RobotUnit-NJointControllers
58 */
60 public NJointControllerWithTripleBuffer<NJointTSDMPControllerControlData>,
62 {
63 class pidController
64 {
65 public:
66 float Kp = 0, Kd = 0;
67 float lastError = 0;
68
69 float
70 update(float dt, float error)
71 {
72 float derivative = (error - lastError) / dt;
73 float retVal = Kp * error + Kd * derivative;
74 lastError = error;
75 return retVal;
76 }
77 };
78
79 public:
80 using ConfigPtrT = NJointTaskSpaceDMPControllerConfigPtr;
82 const NJointControllerConfigPtr& config,
84
85 // NJointControllerInterface interface
86 std::string getClassName(const Ice::Current&) const override;
87
88 // NJointController interface
89
90 void rtRun(const IceUtil::Time& sensorValuesTimestamp,
91 const IceUtil::Time& timeSinceLastIteration) override;
92
93 // NJointTSDMPControllerInterface interface
94 void learnDMPFromFiles(const Ice::StringSeq& fileNames, const Ice::Current&) override;
95
96 bool
97 isFinished(const Ice::Current&) override
98 {
99 return finished;
100 }
101
102 void runDMP(const Ice::DoubleSeq& goals, Ice::Double tau, const Ice::Current&) override;
103 void runDMPWithTime(const Ice::DoubleSeq& goals,
104 Ice::Double timeDuration,
105 const Ice::Current&) override;
106
107 void setSpeed(Ice::Double times, const Ice::Current&) override;
108 void
109 setViaPoints(Ice::Double u, const Ice::DoubleSeq& viapoint, const Ice::Current&) override;
110 void removeAllViaPoints(const Ice::Current&) override;
111
112 void setGoals(const Ice::DoubleSeq& goals, const Ice::Current&) override;
113
114 void setControllerTarget(Ice::Float avoidJointLimitsKp,
116 const Ice::Current&) override;
117 void setTorqueKp(const StringFloatDictionary& torqueKp, const Ice::Current&) override;
118 void setNullspaceJointVelocities(const StringFloatDictionary& nullspaceJointVelocities,
119 const Ice::Current&) override;
120
121
122 void pauseDMP(const Ice::Current&) override;
123 void resumeDMP(const Ice::Current&) override;
124
125 void resetDMP(const Ice::Current&) override;
126 void stopDMP(const Ice::Current&) override;
127
128 double
129 getCanVal(const Ice::Current&) override
130 {
131 return dmpCtrl->canVal;
132 }
133
134 std::string getDMPAsString(const Ice::Current&) override;
135 std::vector<double> createDMPFromString(const std::string& dmpString,
136 const Ice::Current&) override;
137
138 // VirtualRobot::IKSolver::CartesianSelection ModeFromIce(const NJointTaskSpaceDMPControllerMode::CartesianSelection mode);
139 Eigen::VectorXf calcIK(const Eigen::VectorXf& cartesianVel,
140 const Eigen::VectorXf& nullspace,
141 VirtualRobot::IKSolver::CartesianSelection mode);
142
143
144 void setMPWeights(const DoubleSeqSeq& weights, const Ice::Current&) override;
145 DoubleSeqSeq getMPWeights(const Ice::Current&) override;
146
147 void setLinearVelocityKd(Ice::Float kd, const Ice::Current& = Ice::emptyCurrent) override;
148 void setLinearVelocityKp(Ice::Float kp, const Ice::Current& = Ice::emptyCurrent) override;
149 void setAngularVelocityKd(Ice::Float kd, const Ice::Current& = Ice::emptyCurrent) override;
150 void setAngularVelocityKp(Ice::Float kp, const Ice::Current& = Ice::emptyCurrent) override;
151
152 protected:
153 void rtPreActivateController() override;
154 void rtPostDeactivateController() override;
155 VirtualRobot::IKSolver::CartesianSelection
157 virtual void onPublish(const SensorAndControl&,
159 const DebugObserverInterfacePrx&) override;
160
161 void onInitNJointController() override;
162 void onDisconnectNJointController() override;
163 void controllerRun();
164
165 private:
166 std::vector<std::string> jointNames;
167
168 struct DebugBufferData
169 {
170 StringFloatDictionary latestTargetVelocities;
171 StringFloatDictionary dmpTargets;
172 StringFloatDictionary currentPose;
173 double currentCanVal;
174 double mpcFactor;
175 double error;
176 double phaseStop;
177 double posError;
178 double oriError;
179 double deltaT;
180 };
181
182 TripleBuffer<DebugBufferData> debugOutputData;
183
184 struct RTDebugData
185 {
186 Eigen::VectorXf targetJointVels;
187 };
188
189 TripleBuffer<RTDebugData> rtDebugData;
190
191 struct RTToControllerData
192 {
193 double currentTime;
194 double deltaT;
195 Eigen::Matrix4f currentPose;
196 Eigen::VectorXf currentTwist;
197 };
198
200
201 struct RTToUserData
202 {
203 Eigen::Matrix4f currentTcpPose;
204 };
205
206 TripleBuffer<RTToUserData> rt2UserData;
207
208
210
211 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
212 std::vector<const SensorValue1DoFGravityTorque*> gravityTorqueSensors;
213 std::vector<ControlTarget1DoFActuatorVelocity*> targets;
214 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
215 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
216
217 // velocity ik controller parameters
218 std::vector<pidController> torquePIDs;
219 CartesianVelocityControllerPtr tcpController;
220 std::string nodeSetName;
221
222 // dmp parameters
223 bool finished;
224 bool started;
225
226 VirtualRobot::DifferentialIKPtr ik;
227 VirtualRobot::RobotNodePtr tcp;
228 VirtualRobot::RobotNodePtr refFrame;
229 Eigen::Vector6f targetVels;
230 Eigen::Matrix4f targetPose;
231
232 NJointTaskSpaceDMPControllerConfigPtr cfg;
233 mutable MutexType controllerMutex;
235
236
237 std::string debugName;
238
239 Eigen::VectorXf filtered_qvel;
240 Eigen::Vector3f filtered_position;
241 float vel_filter_factor;
242 float pos_filter_factor;
243 bool firstRun;
244
245 float KpF;
246 float DpF;
247 float KoF;
248 float DoF;
249
250 Eigen::VectorXf jointHighLimits;
251 Eigen::VectorXf jointLowLimits;
252 };
253
254} // namespace armarx::control::deprecated_njoint_mp_controller::task_space
#define TYPEDEF_PTRS_HANDLE(T)
constexpr T dt
NJointControllerWithTripleBuffer(const NJointTSDMPControllerControlData &initialCommands=NJointTSDMPControllerControlData())
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 setMPWeights(const DoubleSeqSeq &weights, const Ice::Current &) override
void setGoals(const Ice::DoubleSeq &goals, const Ice::Current &) override
void setLinearVelocityKd(Ice::Float kd, const Ice::Current &=Ice::emptyCurrent) override
void learnDMPFromFiles(const Ice::StringSeq &fileNames, const Ice::Current &) override
void setAngularVelocityKp(Ice::Float kp, const Ice::Current &=Ice::emptyCurrent) 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.
void setAngularVelocityKd(Ice::Float kd, const Ice::Current &=Ice::emptyCurrent) override
void setLinearVelocityKp(Ice::Float kp, const Ice::Current &=Ice::emptyCurrent) override
VirtualRobot::IKSolver::CartesianSelection ModeFromIce(const NJointTaskSpaceDMPControllerMode::CartesianSelection mode)
std::vector< double > createDMPFromString(const std::string &dmpString, const Ice::Current &) override
void setViaPoints(Ice::Double u, const Ice::DoubleSeq &viapoint, const Ice::Current &) override
NJointTSDMPController(const RobotUnitPtr &, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
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