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:
63
64 JointCarryGMMMotionGen(const std::string& fileName)
65 {
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
94 guard_gmm.reset(new GMRDynamics(guard_gmmParas.K_gmm_,
95 guard_gmmParas.dim_,
97 guard_gmmParas.Priors_,
99 guard_gmmParas.Sigma_));
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 {
106 guard_target(i) = guard_gmmParas.attractor_.at(i);
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:
159 Eigen::Vector3f leftDesiredAngularError;
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:
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,
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
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
NJointControllerWithTripleBuffer(const DSJointCarryControllerControlData &initialCommands=DSJointCarryControllerControlData())
A simple triple buffer for lockfree comunication between a single writer and a single reader.
void rtPreActivateController()
This function is called before the controller is activated.
void setGuardObsAvoidVel(const Ice::FloatSeq &guardVel, const Ice::Current &)
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration)
TODO make protected and use attorneys.
void rtPostDeactivateController()
This function is called after the controller is deactivated.
void setGuardOrientation(const Ice::FloatSeq &guardOrientationInRobotBase, const Ice::Current &)
void setRotationStiffness(const Ice::FloatSeq &rotationStiffness, const Ice::Current &)
void setDesiredGuardOri(const Ice::FloatSeq &desiredOrientationInRobotBase, const Ice::Current &)
void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &)
DSJointCarryController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
void setGuardInHandPosition(const Ice::FloatSeq &guardPositionToHandInMeter, const Ice::Current &)
void updateDesiredVelocity(const Eigen::Vector3f &positionInMeter, float positionErrorToleranceInMeter)
Quaternion< float, 0 > Quaternionf
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
boost::shared_ptr< JointCarryGMMMotionGen > JointCarryGMMMotionGenPtr
boost::shared_ptr< GMRDynamics > JointCarryGMMPtr
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34
IceUtil::Handle< class RobotUnit > RobotUnitPtr
Definition FTSensor.h:34
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
detail::ControlThreadOutputBufferEntry SensorAndControl