53 #include <Rcs_macros.h> 54 #include <Rcs_typedef.h> 55 #include <Rcs_Vec3d.h> 56 #include <TaskPosition3D.h> 57 #include <TaskVelocity1D.h> 58 #include <TaskOmega1D.h> 59 #include <TaskEuler3D.h> 60 #include <TaskFactory.h> 62 #ifdef GRAPHICS_AVAILABLE 64 #include <RcsViewer.h> 78 RcsBody* leftGrasp = RcsGraph_getBodyByName(
graph,
"PowerGrasp_L");
82 std::string refFrameType =
"world";
84 RcsBody* refBody =
nullptr;
85 RcsBody* refFrame =
nullptr;
86 if (refFrameType ==
"world") {
89 else if (refFrameType ==
"box") {
90 RcsBody* box = RcsGraph_getBodyByName(
graph,
"Box");
95 else if (refFrameType ==
"upperGoal") {
96 RcsBody* goalUpperShelve = RcsGraph_getBodyByName(
graph,
"GoalUpperShelve");
97 RCHECK(goalUpperShelve);
98 refBody = goalUpperShelve;
99 refFrame = goalUpperShelve;
102 std::ostringstream os;
103 os <<
"Unsupported reference frame type: " << refFrame;
104 throw std::invalid_argument(os.str());
109 std::vector<std::unique_ptr<DynamicalSystem>> tasks;
114 innerAM->addTask(
new TaskPosition3D(
graph, leftGrasp, refBody, refFrame));
115 innerAM->addTask(
new TaskEuler3D(
graph, leftGrasp, refBody, refFrame));
116 innerAM->addTask(TaskFactory::createTask(
117 R
"(<Task name="Hand L Joints" controlVariable="Joints" jnts="fing1-knuck1_L tip1-fing1_L fing2-knuck2_L tip2-fing2_L fing3-knuck3_L tip3-fing3_L knuck1-base_L" tmc="0.1" vmax="1000" active="untrue"/>)", 123 std::vector<unsigned int> taskDimsLeft{3, 3, 3, 3, 3, 7};
124 std::vector<unsigned int> offsetsLeft{0, 0, 0, 3, 3, 6};
126 for (
auto tsk : tsLeft) {
128 tasks.emplace_back(
new DSSlice(ds, offsetsLeft[i], taskDimsLeft[i]));
135 innerAM->addTask(
new TaskVelocity1D(
"Xd",
graph, leftGrasp, refBody, refFrame));
136 innerAM->addTask(
new TaskVelocity1D(
"Yd",
graph, leftGrasp, refBody, refFrame));
137 innerAM->addTask(
new TaskVelocity1D(
"Zd",
graph, leftGrasp, refBody, refFrame));
138 innerAM->addTask(
new TaskOmega1D(
"Ad",
graph, leftGrasp,
nullptr,
nullptr));
139 innerAM->addTask(
new TaskOmega1D(
"Bd",
graph, leftGrasp,
nullptr,
nullptr));
140 innerAM->addTask(TaskFactory::createTask(
141 R
"(<Task name="Hand L Joints" controlVariable="Joints" jnts="fing1-knuck1_L tip1-fing1_L fing2-knuck2_L tip2-fing2_L fing3-knuck3_L tip3-fing3_L knuck1-base_L" tmc="0.1" vmax="1000" active="untrue"/>)", 147 std::vector<unsigned int> taskDimsLeft;
148 std::vector<unsigned int> offsetsLeft;
150 taskDimsLeft = {1, 1, 1, 1, 1, 7};
151 offsetsLeft = {0, 1, 2, 3, 4, 5};
154 taskDimsLeft = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7};
155 offsetsLeft = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5};
158 for (
auto tsk : tsLeft) {
160 tasks.emplace_back(
new DSSlice(ds, offsetsLeft[i], taskDimsLeft[i]));
166 throw std::invalid_argument(
"No tasks specified!");
172 std::cout <<
"IK considers the provided collision model" << std::endl;
178 std::vector<DynamicalSystem*> taskRel;
179 for (
auto& task : tasks) {
180 taskRel.push_back(task.release());
184 std::string taskCombinationMethod =
"unspecified";
194 std::unique_ptr<OMCombined> fullState(
new OMCombined());
198 omLeftLin->setMinState({-0.2, -0.5, 0.7});
199 omLeftLin->setMaxState({2.1, 0.8, 2.0});
200 omLeftLin->setMaxVelocity(3.);
201 fullState->addPart(omLeftLin);
205 omLeftLin->setMinState({-0.2, -0.5, 0.7});
206 omLeftLin->setMaxState({2.1, 0.8, 2.0});
207 fullState->addPart(omLeftLin);
214 omBoxLin->setMinState({-0.2, -0.6, -0.5});
215 omBoxLin->setMaxState({1.8, 0.6, 1.0});
216 omBoxLin->setMaxVelocity(5.);
217 fullState->addPart(omBoxLin);
222 omBoxLin->setMinState({-0.2, -0.6, -0.5});
223 omBoxLin->setMaxState({1.8, 0.6, 1.0});
224 fullState->addPart(omBoxLin);
230 omBoxAng->setMaxVelocity(RCS_DEG2RAD(360));
231 fullState->addPart(omBoxAng);
235 fullState->addPart(omBoxAng);
247 RcsSensor* ftsL = RcsGraph_getSensorByName(
graph,
"WristLoadCellLBR_L");
250 fullState->addPart(
OMPartial::fromMask(omForceTorque, {
true,
true,
true,
false,
false,
false}));
258 fullState->addPart(omColl);
268 fullState->addPart(omCollPred);
283 fullState->addPart(omDSDescr);
286 throw std::invalid_argument(
"The action model needs to be of type AMDynamicalSystemActivation!");
295 fullState->addPart(omTSDescr);
298 throw std::invalid_argument(
"The action model needs to be of type ActionModelIK!");
302 return fullState.release();
319 RcsBody* box = RcsGraph_getBodyByName(
graph,
"Box");
326 #ifdef GRAPHICS_AVAILABLE 328 RcsBody* table = RcsGraph_getBodyByName(
graph,
"Table");
330 std::string cameraView =
"overLeftShoulder";
334 double cameraCenter[3];
335 Vec3d_copy(cameraCenter, table->A_BI->org);
336 cameraCenter[2] += 0.1;
339 double cameraLocation[3];
340 Vec3d_setZero(cameraLocation);
344 Vec3d_setUnitVector(cameraUp, 2);
346 if (cameraView ==
"overLeftShoulder") {
347 RcsBody* leftShoulder = RcsGraph_getBodyByName(
graph,
"lbr_link_0_L");
349 cameraLocation[0] = leftShoulder->A_BI->org[0] - 2.2;
350 cameraLocation[1] = leftShoulder->A_BI->org[1] + 1.2;
351 cameraLocation[2] = leftShoulder->A_BI->org[2] + 0.5;
354 Vec3d_transformSelf(cameraLocation, table->A_BI);
358 RMSG(
"Unsupported camera view: %s", cameraView.c_str());
363 viewer->setCameraHomePosition(osg::Vec3d(cameraLocation[0], cameraLocation[1], cameraLocation[2]),
364 osg::Vec3d(cameraCenter[0], cameraCenter[1], cameraCenter[2]),
365 osg::Vec3d(cameraUp[0], cameraUp[1], cameraUp[2]));
371 std::vector<std::string>& linesOut,
374 const MatNd* currentAction,
375 PhysicsBase* simulator,
380 const char* simName =
"None";
381 if (simulator !=
nullptr) {
382 simName = simulator->getClassName();
384 linesOut.emplace_back(
385 string_format(
"physics engine: %s sim time: %2.3f s", simName, currentTime));
387 unsigned int numPosCtrlJoints = 0;
388 unsigned int numTrqCtrlJoints = 0;
390 RCSGRAPH_TRAVERSE_JOINTS(
graph) {
391 if (JNT->jacobiIndex != -1) {
392 if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_POSITION) {
395 else if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_TORQUE) {
400 linesOut.emplace_back(
401 string_format(
"num joints: %d total, %d pos ctrl, %d trq ctrl",
graph->nJ, numPosCtrlJoints,
408 linesOut.emplace_back(
409 string_format(
"left hand pg: [% 1.3f,% 1.3f,% 1.3f] m [% 1.3f,% 1.3f,% 1.3f] m/s",
410 obs->ele[omLeftLin.pos], obs->ele[omLeftLin.pos + 1], obs->ele[omLeftLin.pos + 2],
411 obs->ele[sd + omLeftLin.vel], obs->ele[sd + omLeftLin.vel + 1],
412 obs->ele[sd + omLeftLin.vel + 2]));
416 if (omBoxAng && omLeftLin) {
417 linesOut.emplace_back(
418 string_format(
"box relative: [% 1.3f,% 1.3f,% 1.3f] m [% 3.0f,% 3.0f,% 3.0f] deg",
419 obs->ele[omLeftLin.pos + 3], obs->ele[omLeftLin.pos + 4], obs->ele[omLeftLin.pos + 5],
420 RCS_RAD2DEG(obs->ele[omBoxAng.pos]), RCS_RAD2DEG(obs->ele[omBoxAng.pos + 1]),
421 RCS_RAD2DEG(obs->ele[omBoxAng.pos + 2])));
423 else if (omLeftLin) {
424 linesOut.emplace_back(
426 obs->ele[omLeftLin.pos + 3], obs->ele[omLeftLin.pos + 4], obs->ele[omLeftLin.pos + 5]));
431 linesOut.emplace_back(
432 string_format(
"forces: [% 3.1f, % 3.1f, % 3.1f] N ", obs->ele[omFTS.pos], obs->ele[omFTS.pos + 1],
433 obs->ele[omFTS.pos + 2]));
438 if (omColl && omCollPred) {
439 linesOut.emplace_back(
440 string_format(
"coll cost: %3.2f pred coll cost: %3.2f", obs->ele[omColl.pos],
441 obs->ele[omCollPred.pos]));
445 linesOut.emplace_back(
string_format(
"coll cost: %3.2f", obs->ele[omColl.pos]));
447 else if (omCollPred) {
448 linesOut.emplace_back(
string_format(
"pred coll cost: %3.2f", obs->ele[omCollPred.pos]));
453 linesOut.emplace_back(
string_format(
"manip idx: %1.3f", obs->ele[omMI.pos]));
458 linesOut.emplace_back(
459 string_format(
"ts delta: [% 1.3f,% 1.3f,% 1.3f] m", obs->ele[omTSD.pos], obs->ele[omTSD.pos + 1],
460 obs->ele[omTSD.pos + 2]));
463 std::stringstream ss;
465 for (
unsigned int i = 0; i < currentAction->m - 1; i++) {
466 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, i, 0) <<
", ";
471 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, currentAction->m - 1, 0) <<
"]";
476 std::stringstream ss;
477 ss <<
"activations: [";
478 for (
unsigned int i = 0; i < castedAM->getDim() - 1; i++) {
479 ss << std::fixed << std::setprecision(2) << MatNd_get(castedAM->getActivation(), i, 0) <<
", ";
484 ss << std::fixed << std::setprecision(2) <<
485 MatNd_get(castedAM->getActivation(), castedAM->getDim() - 1, 0) <<
"]";
488 linesOut.emplace_back(
string_format(
"tcm: %s", castedAM->getTaskCombinationMethodName()));
491 if (physicsManager !=
nullptr) {
495 linesOut.emplace_back(
string_format(
"box mass: %1.2f kg box frict coeff: %1.3f ",
496 box_bpi->
body->m, box_bpi->
material.getFrictionCoefficient()));
virtual bool getProperty(std::string &out, const char *property)=0
virtual void initViewer(Rcs::Viewer *viewer)
virtual bool getPropertyBool(const char *property, bool def=false)=0
virtual InitStateSetter * createInitStateSetter()
virtual PropertySource * getChild(const char *prefix)=0
void addParam(const char *bodyName, PhysicsParameterDescriptor *desc)
static TaskCombinationMethod checkTaskCombinationMethod(std::string tcmName)
void getHUDText(std::vector< std::string > &linesOut, double currentTime, const MatNd *obs, const MatNd *currentAction, PhysicsBase *simulator, PhysicsParameterManager *physicsManager, ForceDisturber *forceDisturber) override
virtual ObservationModel * createObservationModel()
virtual const std::vector< PropertySource * > & getChildList(const char *prefix)=0
virtual unsigned int getStateDim() const =0
BodyParamInfo * getBodyInfo(const char *bodyName)
static DynamicalSystem * create(PropertySource *properties, unsigned int innerTaskDim)
static OMPartial * fromMask(ObservationModel *wrapped, const std::vector< bool > &mask, bool exclude=false)
std::string string_format(const std::string &format, Args ... args)
virtual void populatePhysicsParameters(PhysicsParameterManager *manager)
ObservationModel * observationModel
Observation model (pluggable) used to create the observation which will be returned from step() ...
PropertySource * properties
Property source (owned)
const AM * unwrap() const
virtual ForceDisturber * createForceDisturber()
static ExperimentConfigRegistration< ECBoxShelving > RegBoxShelving("BoxShelving")
virtual ActionModel * createActionModel()
ActionModel * actionModel
Action model (pluggable)
RcsCollisionMdl * collisionMdl
Collision model to use. Is based on the graph, so take care when using with IK etc.
PhysicsMaterial material
Material of the body's first shape.
RcsGraph * graph
Graph description.