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 
14 namespace armarx
15 {
16  class SensorValue1DoFActuatorTorque;
17  class SensorValue1DoFActuatorVelocity;
18  class SensorValue1DoFActuatorPosition;
19  class ControlTarget1DoFActuatorTorque;
20  class SensorValueForceTorque;
21 } // namespace armarx
22 
24 {
26 
27  TYPEDEF_PTRS_HANDLE(NJointAnomalyDetectionAdaptiveWipingController);
28  TYPEDEF_PTRS_HANDLE(NJointAnomalyDetectionAdaptiveWipingControllerControlData);
29 
31  {
32  public:
33  Eigen::VectorXf targetTSVel;
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,
51  const VirtualRobot::RobotPtr&);
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;
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
armarx::NJointAnomalyDetectionAdaptiveWipingControllerInterface::pauseDMP
void pauseDMP()
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:919
armarx::control::deprecated_njoint_mp_controller::adaptive::TYPEDEF_PTRS_HANDLE
TYPEDEF_PTRS_HANDLE(NJointAdaptiveWipingController)
NJointControllerWithTripleBuffer.h
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::runDMP
void runDMP(const Ice::DoubleSeq &goals, Ice::Double tau, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:1003
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingControllerControlData::targetTSPose
Eigen::Matrix4f targetTSPose
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:34
armarx::NJointControllerWithTripleBuffer
Definition: NJointControllerWithTripleBuffer.h:10
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::controllerRun
void controllerRun()
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:252
armarx::NJointAnomalyDetectionAdaptiveWipingControllerInterface::resumeDMP
void resumeDMP()
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::NJointAnomalyDetectionAdaptiveWipingController
NJointAnomalyDetectionAdaptiveWipingController(const RobotUnitPtr &, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:26
armarx::PIDControllerPtr
std::shared_ptr< PIDController > PIDControllerPtr
Definition: PIDController.h:94
PeriodicTask.h
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::isFinished
bool isFinished(const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:66
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:587
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::setTrigerAbnormalEvent
void setTrigerAbnormalEvent(bool abnormal, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:1027
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::learnDMPFromFiles
void learnDMPFromFiles(const Ice::StringSeq &fileNames, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:946
armarx::PeriodicTask::pointer_type
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
Definition: PeriodicTask.h:68
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingControllerControlData::canVal
double canVal
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:35
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::getClassName
std::string getClassName(const Ice::Current &) const
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:246
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:182
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:920
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::setSpeed
void setSpeed(Ice::Double times, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:971
armarx::control::deprecated_njoint_mp_controller::adaptive
Definition: NJointAdaptiveWipingController.cpp:21
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::onInitNJointController
void onInitNJointController()
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:209
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::onDisconnectNJointController
void onDisconnectNJointController()
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:1240
armarx::NJointAnomalyDetectionAdaptiveWipingControllerInterface
Definition: ControllerInterface.ice:267
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::onPublish
virtual void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:1052
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController
The NJointAnomalyDetectionAdaptiveWipingController class.
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:42
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::setGoals
void setGoals(const Ice::DoubleSeq &goals, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:978
armarx::control::deprecated_njoint_mp_controller::tsvmp
Definition: NJointTaskSpaceImpedanceDMPController.h:27
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::learnDMPFromTrajectory
void learnDMPFromTrajectory(const TrajectoryBasePtr &trajectory, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:957
armarx::NJointAnomalyDetectionAdaptiveWipingControllerInterface::getAnomalyInput
Ice::FloatSeq getAnomalyInput()
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
TaskSpaceVMP.h
CycleUtil.h
PIDController.h
armarx::control::deprecated_njoint_mp_controller::tsvmp::TaskSpaceDMPControllerPtr
std::shared_ptr< TaskSpaceDMPController > TaskSpaceDMPControllerPtr
Definition: NJointTaskSpaceImpedanceDMPController.h:31
armarx::NJointControllerWithTripleBuffer< NJointAnomalyDetectionAdaptiveWipingControllerControlData >::MutexType
std::recursive_mutex MutexType
Definition: NJointControllerWithTripleBuffer.h:13
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::setTargetForceInRootFrame
void setTargetForceInRootFrame(Ice::Float force, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:994
IceUtil::Handle< class RobotUnit >
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::setAmplitude
void setAmplitude(Ice::Double amp, const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:986
Eigen::Matrix< float, 6, 1 >
armarx::NJointAnomalyDetectionAdaptiveWipingControllerInterface::getAnomalyOutput
Ice::FloatSeq getAnomalyOutput()
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingControllerControlData
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:30
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingControllerControlData::targetTSVel
Eigen::VectorXf targetTSVel
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:33
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::getCanVal
double getCanVal(const Ice::Current &)
Definition: NJointAnomalyDetectionAdaptiveWipingController.h:78
armarx::control::deprecated_njoint_mp_controller::adaptive::NJointAnomalyDetectionAdaptiveWipingController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
TODO make protected and use attorneys.
Definition: NJointAnomalyDetectionAdaptiveWipingController.cpp:317
armarx::TripleBuffer< DebugBufferData >