NJointCCDMPController.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 // Simox
5 #include <VirtualRobot/Robot.h>
6 #include <VirtualRobot/IK/DifferentialIK.h>
7 
8 // DMP
9 #include <dmp/representation/dmp/umitsmp.h>
10 
11 // armarx
17 
18 // control
19 #include <armarx/control/deprecated_njoint_mp_controller/task_space/ControllerInterface.h>
20 
21 
23 {
24 
25  TYPEDEF_PTRS_HANDLE(NJointCCDMPController);
26  TYPEDEF_PTRS_HANDLE(NJointCCDMPControllerControlData);
27 
28  using ViaPoint = std::pair<double, DMP::DVec >;
29  using ViaPointsSet = std::vector<ViaPoint >;
31  {
32  public:
33  Eigen::VectorXf targetTSVel;
34  // cartesian velocity control data
35  std::vector<float> nullspaceJointVelocities;
36  float avoidJointLimitsKp = 0;
37  std::vector<float> torqueKp;
38  std::vector<float> torqueKd;
39  VirtualRobot::IKSolver::CartesianSelection mode = VirtualRobot::IKSolver::All;
40  };
41 
42  /**
43  * @brief The NJointCCDMPController class
44  * @ingroup Library-RobotUnit-NJointControllers
45  */
47  public NJointControllerWithTripleBuffer<NJointCCDMPControllerControlData>,
49  {
50  class pidController
51  {
52  public:
53  float Kp = 0, Kd = 0;
54  float lastError = 0;
55  float update(float dt, float error)
56  {
57  float derivative = (error - lastError) / dt;
58  float retVal = Kp * error + Kd * derivative;
59  lastError = error;
60  return retVal;
61  }
62  };
63  public:
64  using ConfigPtrT = NJointCCDMPControllerConfigPtr;
65  NJointCCDMPController(const RobotUnitPtr& robotUnit, const NJointControllerConfigPtr& config, const VirtualRobot::RobotPtr&);
66 
67  // NJointControllerInterface interface
68  std::string getClassName(const Ice::Current&) const override;
69 
70  // NJointController interface
71 
72  void rtRun(const IceUtil::Time& sensorValuesTimestamp, const IceUtil::Time& timeSinceLastIteration) override;
73 
74  // NJointCCDMPControllerInterface interface
75  void learnDMPFromFiles(int dmpId, const Ice::StringSeq& fileNames, const Ice::Current&) override;
76  bool isFinished(const Ice::Current&) override
77  {
78  return finished;
79  }
80 
81  void runDMP(const Ice::Current&) override;
82  void setTemporalFactor(int dmpId, Ice::Double tau, const Ice::Current&) override;
83  void setViaPoints(int dmpId, Ice::Double u, const Ice::DoubleSeq& viapoint, const Ice::Current&) override;
84  void setGoals(int dmpId, const Ice::DoubleSeq& goals, const Ice::Current&) override;
85 
86  void setControllerTarget(Ice::Float avoidJointLimitsKp, NJointTaskSpaceDMPControllerMode::CartesianSelection mode, const Ice::Current&) override;
87  void setTorqueKp(const StringFloatDictionary& torqueKp, const Ice::Current&) override;
88  void setNullspaceJointVelocities(const StringFloatDictionary& nullspaceJointVelocities, const Ice::Current&) override;
89  protected:
90  void rtPreActivateController() override;
91  void rtPostDeactivateController() override;
93  virtual void onPublish(const SensorAndControl&, const DebugDrawerInterfacePrx&, const DebugObserverInterfacePrx&) override;
94 
95  void onInitNJointController() override;
96  void onDisconnectNJointController() override;
97  void controllerRun();
98 
99  private:
100  struct DebugBufferData
101  {
102  StringFloatDictionary latestTargetVelocities;
103  StringFloatDictionary dmpTargets;
104  StringFloatDictionary realTCP;
105 
106  double mpcFactor;
107  double error;
108  double phaseStop;
109  double posError;
110  double oriError;
111  double deltaT;
112  double canVal0;
113  };
114 
115  TripleBuffer<DebugBufferData> debugOutputData;
116 
117  struct NJointCCDMPControllerSensorData
118  {
119  double currentTime;
120  double deltaT;
121  Eigen::Matrix4f currentPose;
122  };
124 
125  std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
126  std::vector<const SensorValue1DoFGravityTorque*> gravityTorqueSensors;
127  std::vector<ControlTarget1DoFActuatorVelocity*> targets;
128 
129  // velocity ik controller parameters
130  std::vector<pidController> torquePIDs;
131  CartesianVelocityControllerPtr tcpController;
132  std::string nodeSetName;
133 
134  // dmp parameters
135  std::vector<DMP::UMITSMPPtr > dmpPtrList;
136  std::vector<double> canVals;
137  std::vector<double> timeDurations;
138  std::vector<std::string > dmpTypes;
139  std::vector<double> amplitudes;
140 
141  std::vector<DMP::Vec<DMP::DMPState > > currentStates;
142  std::vector<DMP::DVec > targetSubStates;
143 
144  bool finished;
145  double tau;
146  ViaPointsSet viaPoints;
147  bool isDisturbance;
148 
149 
150  // phaseStop parameters
151  double phaseL;
152  double phaseK;
153  double phaseDist0;
154  double phaseDist1;
155  double phaseKpPos;
156  double phaseKpOri;
157 
158  double posToOriRatio;
159 
160  Eigen::VectorXf targetVels;
161 
162  std::vector<int> learnedDMP;
163 
164 
165 
166 
167 
168  NJointCCDMPControllerConfigPtr cfg;
169  VirtualRobot::RobotNodePtr tcp;
170  Eigen::Vector3f tcpPosition;
171  Eigen::Quaterniond tcpOrientation;
172 
173  Eigen::Matrix4f oldPose;
174  VirtualRobot::DifferentialIKPtr ik;
175 
176  DMP::DVec targetState;
177  mutable MutexType controllerMutex;
179  };
180 
181 } // namespace armarx
182 
armarx::control::deprecated_njoint_mp_controller::task_space::ViaPointsSet
std::vector< ViaPoint > ViaPointsSet
Definition: DeprecatedNJointTSDMPController.h:28
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::ModeFromIce
VirtualRobot::IKSolver::CartesianSelection ModeFromIce(const NJointTaskSpaceDMPControllerMode::CartesianSelection mode)
Definition: NJointCCDMPController.cpp:498
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::NJointControllerWithTripleBuffer
Definition: NJointControllerWithTripleBuffer.h:10
RobotUnit.h
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setTemporalFactor
void setTemporalFactor(int dmpId, Ice::Double tau, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:563
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setControllerTarget
void setControllerTarget(Ice::Float avoidJointLimitsKp, NJointTaskSpaceDMPControllerMode::CartesianSelection mode, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:486
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::torqueKd
std::vector< float > torqueKd
Definition: NJointCCDMPController.h:38
armarx::control::deprecated_njoint_mp_controller::task_space
Definition: DeprecatedNJointPeriodicTSDMPCompliantController.cpp:6
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:586
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::learnDMPFromFiles
void learnDMPFromFiles(int dmpId, const Ice::StringSeq &fileNames, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:374
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::isFinished
bool isFinished(const Ice::Current &) override
Definition: NJointCCDMPController.h:76
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::mode
VirtualRobot::IKSolver::CartesianSelection mode
Definition: NJointCCDMPController.h:39
armarx::control::deprecated_njoint_mp_controller::task_space::ViaPoint
std::pair< double, DMP::DVec > ViaPoint
Definition: DeprecatedNJointTSDMPController.h:27
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setGoals
void setGoals(int dmpId, const Ice::DoubleSeq &goals, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:480
armarx::CartesianVelocityControllerPtr
std::shared_ptr< CartesianVelocityController > CartesianVelocityControllerPtr
Definition: CartesianVelocityController.h:34
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::onInitNJointController
void onInitNJointController() override
Definition: NJointCCDMPController.cpp:616
armarx::NJointCCDMPControllerInterface
Definition: ControllerInterface.ice:152
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::torqueKp
std::vector< float > torqueKp
Definition: NJointCCDMPController.h:37
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setTorqueKp
void setTorqueKp(const StringFloatDictionary &torqueKp, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:519
ControlTarget1DoFActuator.h
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::onPublish
virtual void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
Definition: NJointCCDMPController.cpp:580
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::nullspaceJointVelocities
std::vector< float > nullspaceJointVelocities
Definition: NJointCCDMPController.h:35
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setNullspaceJointVelocities
void setNullspaceJointVelocities(const StringFloatDictionary &nullspaceJointVelocities, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:531
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::NJointCCDMPController
NJointCCDMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: NJointCCDMPController.cpp:9
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData
Definition: NJointCCDMPController.h:30
CartesianVelocityController.h
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::controllerRun
void controllerRun()
Definition: NJointCCDMPController.cpp:126
NJointController.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: NJointCCDMPController.cpp:575
armarx::ViaPointsSet
std::vector< ViaPoint > ViaPointsSet
Definition: NJointBimanualDMPForceController.h:25
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: NJointCCDMPController.cpp:120
armarx::NJointControllerWithTripleBuffer< NJointCCDMPControllerControlData >::MutexType
std::recursive_mutex MutexType
Definition: NJointControllerWithTripleBuffer.h:13
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::onDisconnectNJointController
void onDisconnectNJointController() override
Definition: NJointCCDMPController.cpp:624
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointCCDMPController.cpp:347
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::control::deprecated_njoint_mp_controller::task_space::TYPEDEF_PTRS_HANDLE
TYPEDEF_PTRS_HANDLE(DeprecatedNJointPeriodicTSDMPCompliantController)
Eigen::Quaterniond
Quaternion< double, 0 > Quaterniond
Definition: EigenForwardDeclarations.h:62
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController
The NJointCCDMPController class.
Definition: NJointCCDMPController.h:46
armarx::NJointCCDMPControllerInterface::runDMP
void runDMP()
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::setViaPoints
void setViaPoints(int dmpId, Ice::Double u, const Ice::DoubleSeq &viapoint, const Ice::Current &) override
Definition: NJointCCDMPController.cpp:469
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:42
SensorValue1DoFActuator.h
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::targetTSVel
Eigen::VectorXf targetTSVel
Definition: NJointCCDMPController.h:33
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::TripleBuffer< DebugBufferData >
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointCCDMPController.cpp:570
armarx::control::deprecated_njoint_mp_controller::task_space::NJointCCDMPControllerControlData::avoidJointLimitsKp
float avoidJointLimitsKp
Definition: NJointCCDMPController.h:36