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