NJointAnomalyDetectionAdaptiveWipingController.h
Go to the documentation of this file.
1#pragma once
2
3#include <VirtualRobot/VirtualRobot.h>
4
7
10
12#include <armarx/control/deprecated_njoint_mp_controller/adaptive/ControllerInterface.h>
13
14namespace armarx
15{
16 class SensorValue1DoFActuatorTorque;
17 class SensorValue1DoFActuatorVelocity;
18 class SensorValue1DoFActuatorPosition;
19 class ControlTarget1DoFActuatorTorque;
21} // namespace armarx
22
24{
25 namespace tsvmp = armarx::control::deprecated_njoint_mp_controller::tsvmp;
26
29
31 {
32 public:
33 Eigen::VectorXf targetTSVel;
34 Eigen::Matrix4f targetTSPose = Eigen::Matrix4f::Identity();
35 double canVal;
36 };
37
38 /**
39 * @brief The NJointAnomalyDetectionAdaptiveWipingController class
40 * @ingroup Library-RobotUnit-NJointControllers
41 */
44 NJointAnomalyDetectionAdaptiveWipingControllerControlData>,
46 {
47 public:
48 using ConfigPtrT = NJointAnomalyDetectionAdaptiveWipingControllerConfigPtr;
50 const NJointControllerConfigPtr& config,
52
53 // NJointControllerInterface interface
54 std::string getClassName(const Ice::Current&) const;
55
56 // NJointController interface
57
58 void rtRun(const IceUtil::Time& sensorValuesTimestamp,
59 const IceUtil::Time& timeSinceLastIteration);
60
61 // NJointAnomalyDetectionAdaptiveWipingControllerInterface interface
62 void learnDMPFromFiles(const Ice::StringSeq& fileNames, const Ice::Current&);
63 void learnDMPFromTrajectory(const TrajectoryBasePtr& trajectory, const Ice::Current&);
64
65 bool
66 isFinished(const Ice::Current&)
67 {
68 return false;
69 }
70
71 void setSpeed(Ice::Double times, const Ice::Current&);
72 void setGoals(const Ice::DoubleSeq& goals, const Ice::Current&);
73 void setAmplitude(Ice::Double amp, const Ice::Current&);
74 void runDMP(const Ice::DoubleSeq& goals, Ice::Double tau, const Ice::Current&);
75 void setTargetForceInRootFrame(Ice::Float force, const Ice::Current&);
76
77 double
78 getCanVal(const Ice::Current&)
79 {
80 return dmpCtrl->canVal;
81 }
82
83 void setTrigerAbnormalEvent(bool abnormal, const Ice::Current&);
84
85 std::vector<float> getAnomalyInput(const Ice::Current&);
86 std::vector<float> getAnomalyOutput(const Ice::Current&);
87
88 void pauseDMP(const Ice::Current&);
89 void resumeDMP(const Ice::Current&);
90
91 protected:
92 virtual void onPublish(const SensorAndControl&,
95
98 void controllerRun();
99
100 private:
101 struct DebugBufferData
102 {
103 StringFloatDictionary latestTargetVelocities;
104 StringFloatDictionary currentPose;
105 double currentCanVal;
106 double mpcFactor;
107 double error;
108 double phaseStop;
109 double posError;
110 double oriError;
111 double deltaT;
112 };
113
114 TripleBuffer<DebugBufferData> debugOutputData;
115
116 struct DebugRTData
117 {
118 Eigen::Matrix4f targetPose;
119 Eigen::Vector3f filteredForce;
120 Eigen::Vector3f filteredForceInRoot;
121 Eigen::Vector3f filteredTorque;
122
123 Eigen::Vector3f rotationAxis;
124
125 Eigen::Vector3f reactForce;
126 Eigen::Vector3f adaptK;
127 Eigen::Vector3f adaptD;
128 Eigen::VectorXf targetVel;
129 Eigen::Matrix4f currentPose;
130 bool isPhaseStop;
131
132 Eigen::Matrix4f globalPose;
133 Eigen::Vector3f globalFilteredForce;
134 Eigen::Vector3f currentToolDir;
135 Eigen::VectorXf currentTwist;
136
137 float rotAngle;
138
139 int wipingCounter;
140 float mu;
141 Eigen::Vector2f estimatedFriction;
142 Eigen::Vector3f velPInTool;
143 Eigen::Vector2f frictionInToolXY;
144
145 float kpForcePID;
146 float kpRotPID;
147 float loseContactForceIntegral;
148 };
149
151
152 struct RTToControllerData
153 {
154 double currentTime;
155 double deltaT;
156 Eigen::Matrix4f currentPose = Eigen::Matrix4f::Identity();
157 Eigen::VectorXf currentTwist;
158 bool isPhaseStop;
159 };
160
162
163 struct RTToUserData
164 {
165 Eigen::Matrix4f currentTcpPose = Eigen::Matrix4f::Identity();
166 Eigen::Vector3f tcpTranslVel = Eigen::Vector3f::Zero();
167 Eigen::Vector3f forceOutput = Eigen::Vector3f::Zero();
168 float waitTimeForCalibration;
169 };
170
171 TripleBuffer<RTToUserData> rt2UserData;
172
173 struct UserToRTData
174 {
175 float targetForce;
176 };
177
178 TripleBuffer<UserToRTData> user2rtData;
179
180
182
183 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
184 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
185 std::vector<ControlTarget1DoFActuatorTorque*> targets;
186
187 // anomaly detection
188 std::deque<Eigen::VectorXf> velocityHorizonList;
189 size_t velocityHorizon;
190 bool lastAbnormalFlag = false;
191 bool abnormalFlag;
192 bool startLoseContactDetection = false;
193 float forceIntegral;
194 int loseContactCounter = 0;
195 bool isLoseContact = false;
196
197 // velocity ik controller parameters
198 std::string nodeSetName;
199
200 bool started;
201 bool firstRun;
202 bool dmpRunning;
203
204 // friction estimation
205 float mu = 1.5f; // init friction coefficient
206 Eigen::Vector2f lastForceInToolXY;
207 double lastCanVal = 0.0;
208 int wipingCounter = 0;
209 std::deque<float> recordFrictionNorm;
210 std::deque<float> recordForceNormToSurface;
211 size_t frictionHorizon;
212 Eigen::Vector2f estimatedFriction;
213 float safeFrictionConeLowerLimit = 0.2;
214
215 // lose contact detection
216 float loseContactRatio = 0.2f;
217 int makingContactCounter = 0;
218 bool isMakingContact = false;
219 bool isLCREnabled = false;
220 bool isContactedOnce = false;
221 float forceControlGate = 1.0;
222 int lcrCounter = 500;
223
224 VirtualRobot::DifferentialIKPtr ik;
225 VirtualRobot::RobotNodePtr tcp;
226
227 NJointAnomalyDetectionAdaptiveWipingControllerConfigPtr cfg;
228 mutable MutexType controllerMutex;
230 Eigen::Matrix4f targetPose;
231 Eigen::Matrix4f initHandPose;
232
233 Eigen::Vector3f kpos;
234 Eigen::Vector3f dpos;
235 Eigen::Vector3f kori;
236 Eigen::Vector3f dori;
237 Eigen::VectorXf knull;
238 Eigen::VectorXf dnull;
239
240 Eigen::Vector3f adaptK;
241 Eigen::Vector3f adaptD;
242 Eigen::Vector3f adaptKOri;
243 Eigen::Vector3f adaptDOri;
244 Eigen::VectorXf adaptKNull;
245 Eigen::VectorXf adaptDNull;
246
247 Eigen::VectorXf nullSpaceJointsVec;
248 const SensorValueForceTorque* forceSensor;
249
250 // pid controllers
251 bool isForceCtrlInForceDir;
252 bool isForceControlEnabled;
253 bool isRotControlEnabled;
254 bool isTorqueControlEnabled;
255 bool isLCRControlEnabled;
256 PIDControllerPtr forcePID;
257 PIDControllerPtr rotPID;
258 PIDControllerPtr torquePID;
259 PIDControllerPtr lcrPID; // lose contact recover pid controller
260 float adaptKpForce;
261 float adaptKpRot;
262
263 // force torque related
264 Eigen::Vector6f targetFTInToolFrame;
265 Eigen::Vector3f filteredForce;
266 Eigen::Vector3f filteredTorque;
267 Eigen::Vector6f filteredFTCommand;
268 Eigen::Vector3f forceOffset;
269 Eigen::Vector3f currentForceOffset;
270
271 Eigen::Vector3f torqueOffset;
272 Eigen::Vector3f currentTorqueOffset;
273 float handMass;
274 Eigen::Vector3f handCOM;
275 Eigen::Vector3f gravityInRoot;
276
277 Eigen::Vector3f filteredForceInRoot;
278 Eigen::Vector3f filteredTorqueInRoot;
279
280 Eigen::Matrix3f toolTransform;
281 Eigen::Vector3f oriToolDir;
282 Eigen::Matrix3f origHandOri;
283 Eigen::VectorXf qvel_filtered;
284
285 bool useDMPInGlobalFrame;
286
287 float lastDiff;
288 Eigen::Vector2f lastPosition;
289 double changeTimer;
290
291 Eigen::Vector3f toolToFTSensorLink;
292 Eigen::Vector3f positionOffset;
293 };
294} // namespace armarx::control::deprecated_njoint_mp_controller::adaptive
#define TYPEDEF_PTRS_HANDLE(T)
NJointControllerWithTripleBuffer(const NJointAnomalyDetectionAdaptiveWipingControllerControlData &initialCommands=NJointAnomalyDetectionAdaptiveWipingControllerControlData())
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 rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
TODO make protected and use attorneys.
virtual void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &)
NJointAnomalyDetectionAdaptiveWipingController(const RobotUnitPtr &, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
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< PIDController > PIDControllerPtr
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl