54 #include <Rcs_macros.h> 55 #include <Rcs_typedef.h> 56 #include <Rcs_Vec3d.h> 57 #include <TaskPosition3D.h> 58 #include <TaskVelocity1D.h> 59 #include <TaskDistance.h> 60 #include <TaskOmega1D.h> 61 #include <TaskEuler3D.h> 62 #include <TaskFactory.h> 64 #ifdef GRAPHICS_AVAILABLE 66 #include <RcsViewer.h> 80 RcsBody* rightGrasp = RcsGraph_getBodyByName(
graph,
"PowerGrasp_R");
82 RcsBody* box = RcsGraph_getBodyByName(
graph,
"Box");
84 RcsBody* basket = RcsGraph_getBodyByName(
graph,
"Basket");
88 std::string refFrameType =
"world";
90 RcsBody* refBody =
nullptr;
91 RcsBody* refFrame =
nullptr;
92 if (refFrameType ==
"world") {
95 else if (refFrameType ==
"box") {
99 else if (refFrameType ==
"basket") {
104 std::ostringstream os;
105 os <<
"Unsupported reference frame type: " << refFrame;
106 throw std::invalid_argument(os.str());
110 std::string actionModelType =
"unspecified";
114 std::string taskCombinationMethod =
"unspecified";
118 if (actionModelType ==
"ik_activation") {
121 std::vector<Task*> tasks;
125 RcsBody* tip1R = RcsGraph_getBodyByName(
graph,
"tip1_R");
128 tasks.emplace_back(
new TaskPosition1D(
"Y",
graph, rightGrasp, refBody, refFrame));
129 tasks.emplace_back(
new TaskPosition1D(
"Z",
graph, rightGrasp, refBody, refFrame));
131 tasks.emplace_back(
new TaskDistance(
graph, tip1R, box, 0.05));
136 tasks.emplace_back(
new TaskVelocity1D(
"Yd",
graph, rightGrasp, refBody, refFrame));
137 tasks.emplace_back(
new TaskVelocity1D(
"Zd",
graph, rightGrasp, refBody, refFrame));
141 for (
auto t : tasks) { amIK->addTask(t); }
144 amIK->addAlwaysActiveTask(
new TaskPosition1D(
"X",
graph, rightGrasp, basket, basket));
149 amIK->setXdesFromTaskSpec(taskSpec);
154 std::cout <<
"IK considers the provided collision model" << std::endl;
162 else if (actionModelType ==
"ds_activation") {
165 std::vector<std::unique_ptr<DynamicalSystem>> tasks;
170 innerAM->addTask(
new TaskPosition3D(
graph, rightGrasp, basket, basket));
171 innerAM->addTask(
new TaskEuler3D(
graph, rightGrasp, basket, basket));
172 innerAM->addTask(TaskFactory::createTask(
173 R
"(<Task name="Hand R Joints" controlVariable="Joints" jnts="fing1-knuck1_R tip1-fing1_R fing2-knuck2_R tip2-fing2_R fing3-knuck3_R tip3-fing3_R knuck1-base_R" tmc="0.1" vmax="1000" active="untrue"/>)", 184 std::vector<unsigned int> taskDimsRight{
187 std::vector<unsigned int> offsetsRight{
191 for (
auto tsk : tsRight) {
193 tasks.emplace_back(
new DSSlice(ds, offsetsRight[i], taskDimsRight[i]));
201 innerAM->addTask(
new TaskVelocity1D(
"Xd",
graph, rightGrasp, refBody, refFrame));
202 innerAM->addTask(
new TaskVelocity1D(
"Yd",
graph, rightGrasp, refBody, refFrame));
203 innerAM->addTask(
new TaskVelocity1D(
"Zd",
graph, rightGrasp, refBody, refFrame));
204 innerAM->addTask(
new TaskOmega1D(
"Ad",
graph, rightGrasp, refBody, refFrame));
205 innerAM->addTask(
new TaskOmega1D(
"Bd",
graph, rightGrasp, refBody, refFrame));
206 innerAM->addTask(
new TaskOmega1D(
"Cd",
graph, rightGrasp, refBody, refFrame));
208 innerAM->addTask(TaskFactory::createTask(
209 R
"(<Task name="Hand R Joints" controlVariable="Joints" jnts="fing1-knuck1_R tip1-fing1_R fing2-knuck2_R tip2-fing2_R fing3-knuck3_R tip3-fing3_R knuck1-base_R" tmc="0.1" vmax="1000" active="untrue"/>)", 216 std::vector<unsigned int> taskDimsRight{
219 std::vector<unsigned int> offsetsRight{
223 for (
auto tsk : tsRight) {
225 tasks.emplace_back(
new DSSlice(ds, offsetsRight[i], taskDimsRight[i]));
231 throw std::invalid_argument(
"No tasks specified!");
237 std::cout <<
"IK considers the provided collision model" << std::endl;
243 std::vector<DynamicalSystem*> taskRel;
244 for (
auto& task : tasks) {
245 taskRel.push_back(task.release());
252 std::ostringstream os;
253 os <<
"Unsupported action model type: " << actionModelType;
254 throw std::invalid_argument(os.str());
260 std::unique_ptr<OMCombined> fullState(
new OMCombined());
265 omRightLin->setMinState({0.4, -0.8, 0.6});
266 omRightLin->setMaxState({1.6, 0.8, 1.4});
267 omRightLin->setMaxVelocity(3.);
272 omRightLin->setMinState({0.4, -0.8, 0.6});
273 omRightLin->setMaxState({1.6, 0.8, 1.4});
281 omBoxLin->setMinState({0.4, -0.8, 0.6});
282 omBoxLin->setMaxState({1.6, 0.8, 1.4});
283 omBoxLin->setMaxVelocity(3.);
288 omBoxLin->setMinState({0.4, -0.8, 0.6});
289 omBoxLin->setMaxState({1.6, 0.8, 1.4});
296 omBoxAng->setMaxVelocity(RCS_DEG2RAD(720));
313 RcsSensor* ftsL = RcsGraph_getSensorByName(
graph,
"WristLoadCellLBR_L");
316 fullState->addPart(
OMPartial::fromMask(omForceTorque, {
false,
true,
true,
false,
false,
false}));
318 RcsSensor* ftsR = RcsGraph_getSensorByName(
graph,
"WristLoadCellLBR_R");
321 fullState->addPart(
OMPartial::fromMask(omForceTorque, {
false,
true,
true,
false,
false,
false}));
329 fullState->addPart(omColl);
339 fullState->addPart(omCollPred);
354 fullState->addPart(omDSDescr);
357 throw std::invalid_argument(
"The action model needs to be of type AMDynamicalSystemActivation!");
369 throw std::invalid_argument(
"The action model needs to be of type ActionModelIK!");
373 return fullState.release();
392 RcsBody* box = RcsGraph_getBodyByName(
graph,
"Box");
399 #ifdef GRAPHICS_AVAILABLE 401 RcsBody* table = RcsGraph_getBodyByName(
graph,
"Table");
403 std::string cameraView =
"egoView";
407 double cameraCenter[3];
408 Vec3d_copy(cameraCenter, table->A_BI->org);
409 cameraCenter[0] -= 0.2;
412 double cameraLocation[3];
413 Vec3d_setZero(cameraLocation);
417 Vec3d_setUnitVector(cameraUp, 2);
419 if (cameraView ==
"egoView") {
420 RcsBody* railBot = RcsGraph_getBodyByName(
graph,
"RailBot");
424 Vec3d_transformSelf(cameraLocation, table->A_BI);
427 cameraLocation[0] = railBot->A_BI->org[0] + 2.3;
428 cameraLocation[1] = 0;
429 cameraLocation[2] = railBot->A_BI->org[2] + 1.5;
432 RMSG(
"Unsupported camera view: %s", cameraView.c_str());
437 viewer->setCameraHomePosition(osg::Vec3d(cameraLocation[0], cameraLocation[1], cameraLocation[2]),
438 osg::Vec3d(cameraCenter[0], cameraCenter[1], cameraCenter[2]),
439 osg::Vec3d(cameraUp[0], cameraUp[1], cameraUp[2]));
445 std::vector<std::string>& linesOut,
446 double currentTime,
const MatNd* obs,
447 const MatNd* currentAction,
448 PhysicsBase* simulator,
453 const char* simName =
"None";
454 if (simulator !=
nullptr) {
455 simName = simulator->getClassName();
457 linesOut.emplace_back(
458 string_format(
"physics engine: %s sim time: %2.3f s", simName, currentTime));
460 unsigned int numPosCtrlJoints = 0;
461 unsigned int numTrqCtrlJoints = 0;
463 RCSGRAPH_TRAVERSE_JOINTS(
graph) {
464 if (JNT->jacobiIndex != -1) {
465 if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_POSITION) {
468 else if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_TORQUE) {
473 linesOut.emplace_back(
474 string_format(
"num joints: %d total, %d pos ctrl, %d trq ctrl",
graph->nJ, numPosCtrlJoints,
481 linesOut.emplace_back(
482 string_format(
"right hand pg: [% 1.3f,% 1.3f,% 1.3f] m [% 1.3f,% 1.3f,% 1.3f] m/s",
483 obs->ele[omRightLin.pos], obs->ele[omRightLin.pos + 1], obs->ele[omRightLin.pos + 2],
484 obs->ele[sd + omRightLin.vel], obs->ele[sd + omRightLin.vel + 1],
485 obs->ele[sd + omRightLin.vel + 2]));
489 if (omBoxAng && omRightLin) {
490 linesOut.emplace_back(
491 string_format(
"box absolute: [% 1.3f,% 1.3f,% 1.3f] m [% 3.0f,% 3.0f,% 3.0f] deg",
492 obs->ele[omRightLin.pos + 3], obs->ele[omRightLin.pos + 4], obs->ele[omRightLin.pos + 5],
493 RCS_RAD2DEG(obs->ele[omBoxAng.pos]), RCS_RAD2DEG(obs->ele[omBoxAng.pos + 1]),
494 RCS_RAD2DEG(obs->ele[omBoxAng.pos + 2])));
496 else if (omRightLin) {
497 linesOut.emplace_back(
499 obs->ele[omRightLin.pos + 3], obs->ele[omRightLin.pos + 4],
500 obs->ele[omRightLin.pos + 5]));
505 linesOut.emplace_back(
507 obs->ele[omFTS.pos], obs->ele[omFTS.pos + 1], obs->ele[omFTS.pos + 2]));
512 if (omColl && omCollPred) {
513 linesOut.emplace_back(
515 obs->ele[omColl.pos], obs->ele[omCollPred.pos]));
519 linesOut.emplace_back(
string_format(
"coll cost: %3.2f", obs->ele[omColl.pos]));
521 else if (omCollPred) {
522 linesOut.emplace_back(
string_format(
"pred coll cost: %3.2f", obs->ele[omCollPred.pos]));
527 linesOut.emplace_back(
string_format(
"manip idx: %1.3f", obs->ele[omMI.pos]));
533 linesOut.emplace_back(
534 string_format(
"goal distance: [% 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f,\n" 535 " % 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f]",
536 obs->ele[omGD.pos], obs->ele[omGD.pos + 1], obs->ele[omGD.pos + 2],
537 obs->ele[omGD.pos + 3], obs->ele[omGD.pos + 4],
538 obs->ele[omGD.pos + 5], obs->ele[omGD.pos + 6], obs->ele[omGD.pos + 7],
539 obs->ele[omGD.pos + 8], obs->ele[omGD.pos + 9], obs->ele[omGD.pos + 10]));
545 linesOut.emplace_back(
547 obs->ele[omTSD.pos], obs->ele[omTSD.pos + 1], obs->ele[omTSD.pos + 2]));
550 std::stringstream ss;
552 for (
unsigned int i = 0; i < currentAction->m - 1; i++) {
553 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, i, 0) <<
", ";
558 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, currentAction->m - 1, 0) <<
"]";
563 std::stringstream ss;
564 ss <<
"activations: [";
565 for (
unsigned int i = 0; i < castedAM->getDim() - 1; i++) {
566 ss << std::fixed << std::setprecision(2) << MatNd_get(castedAM->getActivation(), i, 0) <<
", ";
571 ss << std::fixed << std::setprecision(2) <<
572 MatNd_get(castedAM->getActivation(), castedAM->getDim() - 1, 0) <<
"]";
575 linesOut.emplace_back(
string_format(
"tcm: %s", castedAM->getTaskCombinationMethodName()));
578 if (physicsManager !=
nullptr) {
583 linesOut.emplace_back(
585 box_bpi->
body->shape[0]->extents[0], box_bpi->
body->shape[0]->extents[1]));
586 linesOut.emplace_back(
588 box_bpi->
body->m, box_bpi->
material.getFrictionCoefficient()));
589 linesOut.emplace_back(
590 string_format(
"basket mass: %1.2f kg basket frict coeff: %1.3f ",
591 basket_bpi->
body->m, basket_bpi->
material.getFrictionCoefficient()));
virtual bool getProperty(std::string &out, const char *property)=0
virtual bool getPropertyBool(const char *property, bool def=false)=0
virtual void populatePhysicsParameters(PhysicsParameterManager *manager)
virtual void initViewer(Rcs::Viewer *viewer)
virtual ActionModel * createActionModel()
virtual ForceDisturber * createForceDisturber()
virtual PropertySource * getChild(const char *prefix)=0
void addParam(const char *bodyName, PhysicsParameterDescriptor *desc)
static TaskCombinationMethod checkTaskCombinationMethod(std::string tcmName)
virtual ObservationModel * createObservationModel()
virtual InitStateSetter * createInitStateSetter()
virtual const std::vector< PropertySource * > & getChildList(const char *prefix)=0
virtual unsigned int getStateDim() const =0
void getHUDText(std::vector< std::string > &linesOut, double currentTime, const MatNd *obs, const MatNd *currentAction, PhysicsBase *simulator, PhysicsParameterManager *physicsManager, ForceDisturber *forceDisturber) override
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)
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
ActionModel * actionModel
Action model (pluggable)
RcsCollisionMdl * collisionMdl
Collision model to use. Is based on the graph, so take care when using with IK etc.
static ExperimentConfigRegistration< ECBoxLifting > RegBoxLifting("BoxLifting")
PhysicsMaterial material
Material of the body's first shape.
RcsGraph * graph
Graph description.