DSJointCarryController.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package DSController::ArmarXObjects::DSJointCarryController
17  * @author Mahdi Khoramshahi ( m80 dot khoramshahi at gmail dot com )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #ifndef _ARMARX_LIB_DSController_DSJointCarryController_H
24 #define _ARMARX_LIB_DSController_DSJointCarryController_H
25 
26 #include <VirtualRobot/VirtualRobot.h>
27 
29 
35 
36 #include <armarx/control/ds_controller/DSControllerInterface.h>
37 
38 #include "GMRDynamics.h"
39 #include "MathLib.h"
40 
42 {
43 
45 
47  {
48  int K_gmm_;
49  int dim_;
50  std::vector<double> Priors_;
51  std::vector<double> Mu_;
52  std::vector<double> Sigma_;
53  std::vector<double> attractor_;
54  double dt_;
55  };
56 
58  {
59  public:
61  {
62  }
63 
64  JointCarryGMMMotionGen(const std::string& fileName)
65  {
66  getGMMParamsFromJsonFile(fileName);
67  }
68 
71  Eigen::Vector3f guard_target;
72  Eigen::Vector3f guard_desiredVelocity;
73 
74  float scaling;
75  float v_max;
76 
77  void
78  getGMMParamsFromJsonFile(const std::string& fileName)
79  {
80  std::ifstream infile{fileName};
81  std::string objDefs = {std::istreambuf_iterator<char>(infile),
82  std::istreambuf_iterator<char>()};
83  JSONObjectPtr json = new JSONObject();
84  json->fromString(objDefs);
85  guard_gmmParas.K_gmm_ = json->getInt("K");
86  guard_gmmParas.dim_ = json->getInt("Dim");
87  json->getArray<double>("Priors", guard_gmmParas.Priors_);
88  json->getArray<double>("Mu", guard_gmmParas.Mu_);
89  json->getArray<double>("Attractor", guard_gmmParas.attractor_);
90  json->getArray<double>("Sigma", guard_gmmParas.Sigma_);
91  scaling = json->getDouble("Scaling");
92  v_max = json->getDouble("MaxVelocity");
93 
100  guard_gmm->initGMR(0, 2, 3, 5);
101  std::cout << "line 162." << std::endl;
102 
103 
104  for (int i = 0; i < 3; ++i)
105  {
107  }
108 
109  std::cout << "Finished GMM." << std::endl;
110  }
111 
112  void
113  updateDesiredVelocity(const Eigen::Vector3f& positionInMeter,
114  float positionErrorToleranceInMeter)
115  {
116  MathLib::Vector position_error;
117  position_error.Resize(3);
118 
119  MathLib::Vector desired_vel;
120  desired_vel.Resize(3);
121 
122  Eigen::Vector3f positionError = positionInMeter - guard_target;
123  if (positionError.norm() < positionErrorToleranceInMeter)
124  {
125  positionError.setZero();
126  }
127 
128  for (int i = 0; i < 3; ++i)
129  {
130  position_error(i) = positionError(i);
131  }
132 
133  desired_vel = guard_gmm->getVelocity(position_error);
134 
135  guard_desiredVelocity << desired_vel(0), desired_vel(1), desired_vel(2);
136 
138 
139  float lenVec = guard_desiredVelocity.norm();
140 
141  if (std::isnan(lenVec))
142  {
143  guard_desiredVelocity.setZero();
144  }
145 
146  if (lenVec > v_max)
147  {
149  }
150  }
151  };
152 
154 
156  {
157  public:
158  Eigen::Vector3f leftDesiredLinearVelocity;
159  Eigen::Vector3f leftDesiredAngularError;
160  Eigen::Vector3f rightDesiredLinearVelocity;
161  Eigen::Vector3f rightDesiredAngularError;
162  };
163 
164  /**
165  * @defgroup Library-DSJointCarryController DSJointCarryController
166  * @ingroup armarx_control
167  * A description of the library DSJointCarryController.
168  *
169  * @class DSJointCarryController
170  * @ingroup Library-DSJointCarryController
171  * @brief Brief description of class DSJointCarryController.
172  *
173  * Detailed description of class DSJointCarryController.
174  */
176  public NJointControllerWithTripleBuffer<DSJointCarryControllerControlData>,
178  {
179 
180  // ManagedIceObject interface
181  protected:
182  void onInitNJointController();
184 
185 
186  void controllerRun();
187 
188 
189  // NJointControllerInterface interface
190  public:
191  using ConfigPtrT = DSJointCarryControllerConfigPtr;
192 
193  DSJointCarryController(const RobotUnitPtr& robotUnit,
194  const NJointControllerConfigPtr& config,
195  const VirtualRobot::RobotPtr&);
196 
197  std::string
198  getClassName(const Ice::Current&) const
199  {
200  return "DSJointCarryController";
201  }
202 
203  // NJointController interface
204  void rtRun(const IceUtil::Time& sensorValuesTimestamp,
205  const IceUtil::Time& timeSinceLastIteration);
206 
207  // DSJointCarryControllerInterface interface
208  public:
209  void setGuardInHandPosition(const Ice::FloatSeq& guardPositionToHandInMeter,
210  const Ice::Current&);
211  void setGuardOrientation(const Ice::FloatSeq& guardOrientationInRobotBase,
212  const Ice::Current&);
213  void setDesiredGuardOri(const Ice::FloatSeq& desiredOrientationInRobotBase,
214  const Ice::Current&);
215  void setRotationStiffness(const Ice::FloatSeq& rotationStiffness, const Ice::Current&);
216  void setGuardObsAvoidVel(const Ice::FloatSeq& guardVel, const Ice::Current&);
217  float getGMMVel(const Ice::Current&);
218 
219  private:
220  mutable MutexType interface2CtrlDataMutex;
221 
222  float deadzone(float currentValue, float targetValue, float threshold);
224  quatSlerp(double t, const Eigen::Quaternionf& q0, const Eigen::Quaternionf& q1);
225  JointCarryGMMMotionGenPtr gmmMotionGenerator;
226 
227  struct DSJointCarryControllerSensorData
228  {
229  Eigen::Matrix4f left_tcpPose;
230  Eigen::Matrix4f right_tcpPose;
231  Eigen::Vector3f left_force;
232  Eigen::Vector3f right_force;
233  double currentTime;
234  };
235 
237 
238  struct DSCtrlDebugInfo
239  {
240  Eigen::Vector3f leftDesiredLinearVelocity;
241  Eigen::Vector3f rightDesiredLinearVelocity;
242  };
243 
244  TripleBuffer<DSCtrlDebugInfo> debugCtrlDataInfo;
245 
246  struct Interface2CtrlData
247  {
248  Eigen::Vector3f guardToHandInMeter;
249  Eigen::Quaternionf guardOriInRobotBase;
250  Eigen::Quaternionf desiredGuardOriInRobotBase;
251  Eigen::Vector3f guardRotationStiffness;
252  Eigen::Vector3f guardObsAvoidVel;
253  };
254 
255  TripleBuffer<Interface2CtrlData> interface2CtrlData;
256 
257  struct DSRTDebugInfo
258  {
259  StringFloatDictionary desired_torques;
260  };
261 
262  TripleBuffer<DSRTDebugInfo> debugDataInfo;
263 
264  struct Ctrl2InterfaceData
265  {
266  float guardZVel;
267  };
268 
269  TripleBuffer<Ctrl2InterfaceData> ctrl2InterfaceData;
270 
271  std::vector<const SensorValue1DoFActuatorTorque*> left_torqueSensors;
272  std::vector<const SensorValue1DoFGravityTorque*> left_gravityTorqueSensors;
273  std::vector<const SensorValue1DoFActuatorVelocity*> left_velocitySensors;
274  std::vector<const SensorValue1DoFActuatorPosition*> left_positionSensors;
275 
276  std::vector<const SensorValue1DoFActuatorTorque*> right_torqueSensors;
277  std::vector<const SensorValue1DoFGravityTorque*> right_gravityTorqueSensors;
278  std::vector<const SensorValue1DoFActuatorVelocity*> right_velocitySensors;
279  std::vector<const SensorValue1DoFActuatorPosition*> right_positionSensors;
280 
281  const SensorValueForceTorque* leftForceTorque;
282  const SensorValueForceTorque* rightForceTorque;
283 
284  std::vector<ControlTarget1DoFActuatorTorque*> left_torque_targets;
285  std::vector<ControlTarget1DoFActuatorTorque*> right_torque_targets;
286 
287  VirtualRobot::RobotNodePtr left_arm_tcp;
288  VirtualRobot::RobotNodePtr right_arm_tcp;
289 
290  VirtualRobot::RobotNodePtr left_sensor_frame;
291  VirtualRobot::RobotNodePtr right_sensor_frame;
292 
293  VirtualRobot::DifferentialIKPtr left_ik;
294  VirtualRobot::DifferentialIKPtr right_ik;
295 
296  Eigen::Quaternionf left_desiredQuaternion;
297  Eigen::Quaternionf right_desiredQuaternion;
298 
299  Eigen::Vector3f left_currentTCPLinearVelocity_filtered;
300  Eigen::Vector3f left_currentTCPAngularVelocity_filtered;
301  Eigen::Vector3f right_currentTCPLinearVelocity_filtered;
302  Eigen::Vector3f right_currentTCPAngularVelocity_filtered;
303 
304  Eigen::VectorXf left_jointVelocity_filtered;
305  Eigen::VectorXf right_jointVelocity_filtered;
306 
307  Eigen::VectorXf left_desiredTorques_filtered;
308  Eigen::VectorXf right_desiredTorques_filtered;
309  Eigen::Vector3f left_tcpDesiredTorque_filtered;
310  Eigen::Vector3f right_tcpDesiredTorque_filtered;
311 
312  float smooth_startup;
313 
314  DSJointCarryControllerConfigPtr cfg;
315  float filterTimeConstant;
316 
317  std::vector<std::string> left_jointNames;
318  std::vector<std::string> right_jointNames;
319 
320  float posiKp;
321  float v_max;
322  std::vector<float> Damping;
323  float oriKp;
324  float oriDamping;
325  float torqueLimit;
326  float null_torqueLimit;
327  float nullspaceKp;
328  float nullspaceDamping;
329  Eigen::VectorXf left_qnullspace;
330  Eigen::VectorXf right_qnullspace;
331 
332 
333  Eigen::Quaternionf desired_guardOri;
334 
335  float positionErrorTolerance;
336  bool controllerStopRequested = false;
337  bool controllerRunFinished = false;
338 
339  // NJointController interface
340  protected:
341  void onPublish(const SensorAndControl&,
344 
345  // NJointController interface
346  protected:
349  };
350 
351 } // namespace armarx::control::ds_controller
352 
353 #endif
armarx::control::ds_controller::JointCarryGMMMotionGen::JointCarryGMMMotionGen
JointCarryGMMMotionGen(const std::string &fileName)
Definition: DSJointCarryController.h:64
armarx::control::ds_controller::DSJointCarryControllerControlData::rightDesiredLinearVelocity
Eigen::Vector3f rightDesiredLinearVelocity
Definition: DSJointCarryController.h:160
armarx::control::ds_controller::JointCarryGMRParameters::dim_
int dim_
Definition: DSJointCarryController.h:49
boost::shared_ptr< GMRDynamics >
Vector
Eigen::Matrix< T, 3, 1 > Vector
Definition: UnscentedKalmanFilterTest.cpp:39
JSONObject.h
armarx::NJointControllerWithTripleBuffer
Definition: NJointControllerWithTripleBuffer.h:10
RobotUnit.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
armarx::DebugObserverInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
Definition: JointController.h:44
armarx::control::ds_controller::JointCarryGMRParameters::dt_
double dt_
Definition: DSJointCarryController.h:54
SensorValueForceTorque.h
armarx::control::ds_controller
Definition: DSJointCarryController.cpp:32
armarx::NJointControllerBase::ConfigPtrT
NJointControllerConfigPtr ConfigPtrT
Definition: NJointControllerBase.h:587
armarx::control::ds_controller::JointCarryGMRParameters
Definition: DSJointCarryController.h:46
armarx::control::ds_controller::DSJointCarryController::setDesiredGuardOri
void setDesiredGuardOri(const Ice::FloatSeq &desiredOrientationInRobotBase, const Ice::Current &)
Definition: DSJointCarryController.cpp:680
armarx::control::ds_controller::JointCarryGMMMotionGen::guard_gmm
JointCarryGMMPtr guard_gmm
Definition: DSJointCarryController.h:69
armarx::control::ds_controller::DSJointCarryController::setGuardOrientation
void setGuardOrientation(const Ice::FloatSeq &guardOrientationInRobotBase, const Ice::Current &)
Definition: DSJointCarryController.cpp:652
armarx::control::ds_controller::DSJointCarryController::onPublish
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &)
Definition: DSJointCarryController.cpp:794
armarx::SensorAndControl
detail::ControlThreadOutputBufferEntry SensorAndControl
Definition: NJointControllerBase.h:73
armarx::control::ds_controller::DSJointCarryController::rtPreActivateController
void rtPreActivateController()
This function is called before the controller is activated.
Definition: DSJointCarryController.cpp:823
armarx::control::ds_controller::DSJointCarryController::onInitNJointController
void onInitNJointController()
Definition: DSJointCarryController.cpp:39
armarx::control::ds_controller::DSJointCarryController::DSJointCarryController
DSJointCarryController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: DSJointCarryController.cpp:75
armarx::control::ds_controller::JointCarryGMMMotionGen::v_max
float v_max
Definition: DSJointCarryController.h:75
armarx::DSJointCarryControllerInterface::getGMMVel
float getGMMVel()
armarx::DSJointCarryControllerInterface
Definition: DSControllerInterface.ice:165
IceInternal::Handle< JSONObject >
armarx::control::ds_controller::DSJointCarryController::controllerRun
void controllerRun()
Definition: DSJointCarryController.cpp:303
GMRDynamics
Definition: GMRDynamics.h:21
armarx::control::ds_controller::JointCarryGMRParameters::attractor_
std::vector< double > attractor_
Definition: DSJointCarryController.h:53
armarx::control::ds_controller::JointCarryGMMMotionGenPtr
boost::shared_ptr< JointCarryGMMMotionGen > JointCarryGMMMotionGenPtr
Definition: DSJointCarryController.h:153
armarx::control::ds_controller::JointCarryGMMMotionGen
Definition: DSJointCarryController.h:57
armarx::control::ds_controller::JointCarryGMRParameters::K_gmm_
int K_gmm_
Definition: DSJointCarryController.h:48
armarx::control::ds_controller::DSJointCarryControllerControlData::leftDesiredLinearVelocity
Eigen::Vector3f leftDesiredLinearVelocity
Definition: DSJointCarryController.h:158
armarx::control::ds_controller::JointCarryGMMMotionGen::getGMMParamsFromJsonFile
void getGMMParamsFromJsonFile(const std::string &fileName)
Definition: DSJointCarryController.h:78
armarx::control::ds_controller::DSJointCarryControllerControlData::rightDesiredAngularError
Eigen::Vector3f rightDesiredAngularError
Definition: DSJointCarryController.h:161
armarx::control::ds_controller::DSJointCarryController::rtPostDeactivateController
void rtPostDeactivateController()
This function is called after the controller is deactivated.
Definition: DSJointCarryController.cpp:828
ControlTarget1DoFActuator.h
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
armarx::control::ds_controller::JointCarryGMRParameters::Mu_
std::vector< double > Mu_
Definition: DSJointCarryController.h:51
armarx::control::ds_controller::JointCarryGMMMotionGen::scaling
float scaling
Definition: DSJointCarryController.h:74
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::control::ds_controller::JointCarryGMMMotionGen::updateDesiredVelocity
void updateDesiredVelocity(const Eigen::Vector3f &positionInMeter, float positionErrorToleranceInMeter)
Definition: DSJointCarryController.h:113
armarx::control::ds_controller::JointCarryGMMMotionGen::guard_desiredVelocity
Eigen::Vector3f guard_desiredVelocity
Definition: DSJointCarryController.h:72
NJointController.h
armarx::control::ds_controller::DSJointCarryController::getClassName
std::string getClassName(const Ice::Current &) const
Definition: DSJointCarryController.h:198
armarx::control::ds_controller::DSJointCarryController::setGuardObsAvoidVel
void setGuardObsAvoidVel(const Ice::FloatSeq &guardVel, const Ice::Current &)
Definition: DSJointCarryController.cpp:707
armarx::NJointControllerWithTripleBuffer< DSJointCarryControllerControlData >::MutexType
std::recursive_mutex MutexType
Definition: NJointControllerWithTripleBuffer.h:13
armarx::control::ds_controller::JointCarryGMRParameters::Sigma_
std::vector< double > Sigma_
Definition: DSJointCarryController.h:52
armarx::Quaternion< float, 0 >
armarx::control::ds_controller::DSJointCarryController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
TODO make protected and use attorneys.
Definition: DSJointCarryController.cpp:419
IceUtil::Handle< class RobotUnit >
armarx::control::ds_controller::JointCarryGMMMotionGen::JointCarryGMMMotionGen
JointCarryGMMMotionGen()
Definition: DSJointCarryController.h:60
armarx::control::ds_controller::DSJointCarryController
Brief description of class DSJointCarryController.
Definition: DSJointCarryController.h:175
armarx::control::ds_controller::JointCarryGMMMotionGen::guard_gmmParas
JointCarryGMRParameters guard_gmmParas
Definition: DSJointCarryController.h:70
armarx::control::ds_controller::JointCarryGMRParameters::Priors_
std::vector< double > Priors_
Definition: DSJointCarryController.h:50
armarx::control::ds_controller::DSJointCarryControllerControlData::leftDesiredAngularError
Eigen::Vector3f leftDesiredAngularError
Definition: DSJointCarryController.h:159
armarx::control::ds_controller::DSJointCarryController::setRotationStiffness
void setRotationStiffness(const Ice::FloatSeq &rotationStiffness, const Ice::Current &)
Definition: DSJointCarryController.cpp:695
armarx::control::ds_controller::JointCarryGMMMotionGen::guard_target
Eigen::Vector3f guard_target
Definition: DSJointCarryController.h:71
armarx::control::ds_controller::DSJointCarryController::setGuardInHandPosition
void setGuardInHandPosition(const Ice::FloatSeq &guardPositionToHandInMeter, const Ice::Current &)
Definition: DSJointCarryController.cpp:667
armarx::control::ds_controller::DSJointCarryController::onDisconnectNJointController
void onDisconnectNJointController()
Definition: DSJointCarryController.cpp:64
GMRDynamics.h
armarx::DebugDrawerInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
Definition: JointController.h:40
armarx::control::ds_controller::JointCarryGMMPtr
boost::shared_ptr< GMRDynamics > JointCarryGMMPtr
Definition: DSJointCarryController.h:44
armarx::control::ds_controller::DSJointCarryControllerControlData
Definition: DSJointCarryController.h:155
SensorValue1DoFActuator.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
armarx::TripleBuffer< DSJointCarryControllerSensorData >