53 #include <Rcs_macros.h> 54 #include <Rcs_typedef.h> 55 #include <Rcs_Vec3d.h> 56 #include <TaskPosition3D.h> 57 #include <TaskPosition1D.h> 58 #include <TaskVelocity1D.h> 59 #include <TaskPositionForce1D.h> 60 #include <TaskOmega1D.h> 61 #include <TaskEuler3D.h> 63 #ifdef GRAPHICS_AVAILABLE 65 #include <RcsViewer.h> 79 RcsBody* leftEffector = RcsGraph_getBodyByName(
graph,
"Effector_L");
81 RcsBody* rightEffector = RcsGraph_getBodyByName(
graph,
"Effector_R");
82 RCHECK(rightEffector);
85 std::string refFrameType =
"world";
87 RcsBody* refBody =
nullptr;
88 RcsBody* refFrame =
nullptr;
89 if (refFrameType ==
"world") {
92 else if (refFrameType ==
"table") {
93 RcsBody* table = RcsGraph_getBodyByName(
graph,
"Table");
98 else if (refFrameType ==
"slider") {
99 RcsBody* slider = RcsGraph_getBodyByName(
graph,
"Slider");
105 std::ostringstream os;
106 os <<
"Unsupported reference frame type: " << refFrame;
107 throw std::invalid_argument(os.str());
111 std::string taskCombinationMethod =
"unspecified";
115 std::string actionModelType =
"unspecified";
118 if (actionModelType ==
"ik") {
121 std::vector<Task*> tasks;
124 throw std::invalid_argument(
"Position tasks are not implemented for AMIKGeneric in this environment.");
128 tasks.emplace_back(
new TaskVelocity1D(
"Xd",
graph, leftEffector, refBody, refFrame));
129 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Xd Left [m/s]"));
130 tasks.emplace_back(
new TaskVelocity1D(
"Yd",
graph, leftEffector, refBody, refFrame));
131 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Yd Left [m/s]"));
132 tasks.emplace_back(
new TaskVelocity1D(
"Zd",
graph, leftEffector, refBody, refFrame));
133 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Zd Left [m/s]"));
134 tasks.emplace_back(
new TaskOmega1D(
"Ad",
graph, leftEffector, refBody, refFrame));
135 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Ad Left [deg/s]"));
136 tasks.emplace_back(
new TaskOmega1D(
"Bd",
graph, leftEffector, refBody, refFrame));
137 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Bd Left [deg/s]"));
138 tasks.emplace_back(
new TaskOmega1D(
"Cd",
graph, leftEffector, refBody, refFrame));
139 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Cd Left [deg/s]"));
142 tasks.emplace_back(
new TaskVelocity1D(
"Xd",
graph, rightEffector, refBody, refFrame));
143 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Xd Right [m/s]"));
144 tasks.emplace_back(
new TaskVelocity1D(
"Yd",
graph, rightEffector, refBody, refFrame));
145 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Yd Right [m/s]"));
146 tasks.emplace_back(
new TaskVelocity1D(
"Zd",
graph, rightEffector, refBody, refFrame));
147 tasks.back()->resetParameter(Task::Parameters(-
dt,
dt, 1.0,
"Zd Right [m/s]"));
148 tasks.emplace_back(
new TaskOmega1D(
"Ad",
graph, rightEffector, refBody, refFrame));
149 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Ad Right [deg/s]"));
150 tasks.emplace_back(
new TaskOmega1D(
"Bd",
graph, rightEffector, refBody, refFrame));
151 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Bd Right [deg/s]"));
152 tasks.emplace_back(
new TaskOmega1D(
"Cd",
graph, rightEffector, refBody, refFrame));
153 tasks.back()->resetParameter(Task::Parameters(-
dt*M_PI_2,
dt*M_PI_2, 1.0,
"Cd Right [deg/s]"));
156 for (
auto t : tasks) { amIK->addTask(t); }
162 else if (actionModelType ==
"ik_activation") {
165 std::vector<Task*> tasks;
168 RcsBody* ball = RcsGraph_getBodyByName(
graph,
"Ball");
169 RcsBody* table = RcsGraph_getBodyByName(
graph,
"Table");
170 RcsBody* slider = RcsGraph_getBodyByName(
graph,
"Slider");
174 std::string taskName;
177 auto tl0 =
new TaskPosition3D(
graph, leftEffector,
nullptr,
nullptr);
178 taskName =
" Position Home [m]";
179 tl0->resetParameter(Task::Parameters(0., 2., 1.0,
"X" + taskName));
180 tl0->addParameter(Task::Parameters(-1., 1., 1.0,
"Y" + taskName));
181 tl0->addParameter(Task::Parameters(0., 1.7, 1.0,
"Z" + taskName));
182 tasks.emplace_back(tl0);
183 auto tl1 =
new TaskPosition3D(
graph, leftEffector, ball,
nullptr);
184 taskName =
" Position rel Ball [m]";
185 tl1->resetParameter(Task::Parameters(-2., 2., 1.0,
"X" + taskName));
186 tl1->addParameter(Task::Parameters(-1., 1., 1.0,
"Y" + taskName));
187 tl1->addParameter(Task::Parameters(
188 -ball->shape[0]->extents[0], 1., 1.0,
"Z" + taskName));
189 tasks.emplace_back(tl1);
193 auto tl3 =
new TaskEuler3D(
graph, leftEffector, table,
nullptr);
194 tasks.emplace_back(tl3);
196 auto tr0 =
new TaskPosition3D(
graph, rightEffector,
nullptr,
nullptr);
197 taskName =
" Position Home [m]";
198 tr0->resetParameter(Task::Parameters(0., 2., 1.0,
"X" + taskName));
199 tr0->addParameter(Task::Parameters(-1., 1., 1.0,
"Y" + taskName));
200 tr0->addParameter(Task::Parameters(0., 1.7, 1.0,
"Z" + taskName));
201 tasks.emplace_back(tr0);
202 auto tr1 =
new TaskPosition3D(
graph, rightEffector, slider,
nullptr);
203 taskName =
" Position rel Slider [m]";
204 tr1->resetParameter(Task::Parameters(-2., 2., 1.0,
"X" + taskName));
205 tr1->addParameter(Task::Parameters(-1., 1., 1.0,
"Y" + taskName));
206 tr1->addParameter(Task::Parameters(-0.5, 1., 1.0,
"Z" + taskName));
207 tasks.emplace_back(tr1);
208 auto tr2 =
new TaskPosition1D(
"Y",
graph, rightEffector, table,
nullptr);
209 tr2->resetParameter(Task::Parameters(-1., 1., 1.0,
"Y Position [m]"));
210 tasks.emplace_back(tr2);
211 auto tr3 =
new TaskEuler3D(
graph, rightEffector, slider,
nullptr);
212 tasks.emplace_back(tr3);
215 for (
auto t : tasks) { amIK->addTask(t); }
219 amIK->setXdesFromTaskSpec(taskSpec);
224 std::cout <<
"IK considers the provided collision model" << std::endl;
230 throw std::invalid_argument(
"Velocity tasks are not supported for AMIKControllerActivation.");
236 else if (actionModelType ==
"ds_activation") {
239 std::vector<std::unique_ptr<DynamicalSystem>> tasks;
243 RcsBody* slider = RcsGraph_getBodyByName(
graph,
"Slider");
246 innerAM->addTask(
new TaskPosition3D(
graph, leftEffector, slider, slider));
247 innerAM->addTask(
new TaskEuler3D(
graph, leftEffector, slider, slider));
249 innerAM->addTask(
new TaskPosition3D(
graph, rightEffector, slider, slider));
250 innerAM->addTask(
new TaskEuler3D(
graph, rightEffector, slider, slider));
255 std::vector<unsigned int> taskDimsLeft{
258 std::vector<unsigned int> offsetsLeft{
262 for (
auto tsk : tsLeft) {
264 tasks.emplace_back(
new DSSlice(ds, offsetsLeft[i], taskDimsLeft[i]));
268 std::vector<unsigned int> taskDimsRight{
271 unsigned int oL = offsetsLeft.back() + taskDimsLeft.back();
272 std::vector<unsigned int> offsetsRight{
273 oL, oL, oL + 3, oL + 3
277 for (
auto tsk : tsRight) {
279 tasks.emplace_back(
new DSSlice(ds, offsetsRight[i], taskDimsRight[i]));
287 innerAM->addTask(
new TaskVelocity1D(
"Xd",
graph, leftEffector, refBody, refFrame));
288 innerAM->addTask(
new TaskVelocity1D(
"Yd",
graph, leftEffector, refBody, refFrame));
289 innerAM->addTask(
new TaskVelocity1D(
"Zd",
graph, leftEffector, refBody, refFrame));
290 innerAM->addTask(
new TaskOmega1D(
"Ad",
graph, leftEffector, refBody, refFrame));
291 innerAM->addTask(
new TaskOmega1D(
"Bd",
graph, leftEffector, refBody, refFrame));
292 innerAM->addTask(
new TaskOmega1D(
"Cd",
graph, leftEffector, refBody, refFrame));
294 innerAM->addTask(
new TaskVelocity1D(
"Xd",
graph, rightEffector, refBody, refFrame));
295 innerAM->addTask(
new TaskVelocity1D(
"Yd",
graph, rightEffector, refBody, refFrame));
296 innerAM->addTask(
new TaskVelocity1D(
"Zd",
graph, rightEffector, refBody, refFrame));
297 innerAM->addTask(
new TaskOmega1D(
"Ad",
graph, rightEffector, refBody, refFrame));
298 innerAM->addTask(
new TaskOmega1D(
"Bd",
graph, rightEffector, refBody, refFrame));
299 innerAM->addTask(
new TaskOmega1D(
"Cd",
graph, rightEffector, refBody, refFrame));
304 std::vector<unsigned int> taskDimsLeft{
307 std::vector<unsigned int> offsetsLeft{
311 for (
auto tsk : tsLeft) {
313 tasks.emplace_back(
new DSSlice(ds, offsetsLeft[i], taskDimsLeft[i]));
317 std::vector<unsigned int> taskDimsRight{
320 unsigned int oL = offsetsLeft.back() + taskDimsLeft.back();
321 std::vector<unsigned int> offsetsRight{
322 oL, oL + 1, oL + 2, oL + 3, oL + 4, oL + 5
326 for (
auto tsk : tsRight) {
328 tasks.emplace_back(
new DSSlice(ds, offsetsRight[i], taskDimsRight[i]));
334 throw std::invalid_argument(
"No tasks specified!");
339 std::cout <<
"IK considers the provided collision model" << std::endl;
344 std::vector<DynamicalSystem*> taskRel;
345 for (
auto& task : tasks) {
346 taskRel.push_back(task.release());
354 std::ostringstream os;
355 os <<
"Unsupported action model type: " << actionModelType;
356 throw std::invalid_argument(os.str());
363 std::unique_ptr<OMCombined> fullState(
new OMCombined());
367 omLeftLin->setMinState({0.2, -1., 0.74});
368 omLeftLin->setMaxState({1.8, 1., 1.5});
369 omLeftLin->setMaxVelocity(3.);
370 fullState->addPart(omLeftLin);
373 omRightLin->setMinState({0.2, -1., 0.74});
374 omRightLin->setMaxState({1.8, 1., 1.5});
375 omRightLin->setMaxVelocity(3.);
376 fullState->addPart(omRightLin);
380 omLeftLin->setMinState({0.2, -1., 0.74});
381 omLeftLin->setMaxState({1.8, 1., 1.5});
382 fullState->addPart(omLeftLin);
385 omRightLin->setMinState({0.2, -1., 0.74});
386 omRightLin->setMaxState({1.8, 1., 1.5});
387 fullState->addPart(omRightLin);
393 omBallLin->setMinState({0.6, -1., 0.74});
394 omBallLin->setMaxState({1.6, 1., 0.88});
395 omBallLin->setMaxVelocity(5.);
400 omBallLin->setMinState({0.6, -1., 0.7});
401 omBallLin->setMaxState({1.6, 1., 0.9});
408 omSliderLin->setMinState({0.9, -1., 0.7});
409 omSliderLin->setMaxState({1.3, 0., 0.9});
410 omSliderLin->setMaxVelocity(5.);
416 omSliderLin->setMinState({0.9, -1., 0.7});
417 omSliderLin->setMaxState({1.3, 0., 0.9});
423 RcsSensor* ftsL = RcsGraph_getSensorByName(
graph,
"WristLoadCellLBR_L");
426 fullState->addPart(
OMPartial::fromMask(omForceTorque, {
true,
true,
true,
false,
false,
false}));
428 RcsSensor* ftsR = RcsGraph_getSensorByName(
graph,
"WristLoadCellLBR_R");
431 fullState->addPart(
OMPartial::fromMask(omForceTorque, {
true,
true,
true,
false,
false,
false}));
439 fullState->addPart(omColl);
449 fullState->addPart(omCollPred);
464 fullState->addPart(omTSDescrL);
466 fullState->addPart(omTSDescrR);
469 throw std::invalid_argument(
"The action model needs to be of type ActionModelIK!");
473 std::string actionModelType =
"unspecified";
475 if (actionModelType ==
"ds_activation") {
488 fullState->addPart(omDSDescr);
491 throw std::invalid_argument(
"The action model needs to be of type AMDynamicalSystemActivation!");
496 return fullState.release();
514 RcsBody* box = RcsGraph_getBodyByName(
graph,
"Ball");
521 #ifdef GRAPHICS_AVAILABLE 523 RcsBody* table = RcsGraph_getBodyByName(
graph,
"Table");
525 std::string cameraView =
"egoView";
529 double cameraCenter[3];
530 Vec3d_copy(cameraCenter, table->A_BI->org);
531 cameraCenter[0] -= 0.2;
534 double cameraLocation[3];
535 Vec3d_setZero(cameraLocation);
539 Vec3d_setUnitVector(cameraUp, 2);
541 if (cameraView ==
"egoView") {
542 RcsBody* railBot = RcsGraph_getBodyByName(
graph,
"RailBot");
546 Vec3d_transformSelf(cameraLocation, table->A_BI);
549 cameraLocation[0] = railBot->A_BI->org[0] - 0.5;
550 cameraLocation[1] = railBot->A_BI->org[1];
551 cameraLocation[2] = railBot->A_BI->org[2] + 1.5;
554 RMSG(
"Unsupported camera view: %s", cameraView.c_str());
559 viewer->setCameraHomePosition(osg::Vec3d(cameraLocation[0], cameraLocation[1], cameraLocation[2]),
560 osg::Vec3d(cameraCenter[0], cameraCenter[1], cameraCenter[2]),
561 osg::Vec3d(cameraUp[0], cameraUp[1], cameraUp[2]));
567 std::vector<std::string>& linesOut,
568 double currentTime,
const MatNd* obs,
569 const MatNd* currentAction,
570 PhysicsBase* simulator,
575 const char* simName =
"None";
576 if (simulator !=
nullptr) {
577 simName = simulator->getClassName();
579 linesOut.emplace_back(
580 string_format(
"physics engine: %s sim time: %2.3f s", simName, currentTime));
582 unsigned int numPosCtrlJoints = 0;
583 unsigned int numTrqCtrlJoints = 0;
585 RCSGRAPH_TRAVERSE_JOINTS(
graph) {
586 if (JNT->jacobiIndex != -1) {
587 if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_POSITION) {
590 else if (JNT->ctrlType == RCSJOINT_CTRL_TYPE::RCSJOINT_CTRL_TORQUE) {
595 linesOut.emplace_back(
596 string_format(
"num joints: %d total, %d pos ctrl, %d trq ctrl",
graph->nJ, numPosCtrlJoints,
603 linesOut.emplace_back(
604 string_format(
"left hand pg: [% 1.3f,% 1.3f,% 1.3f] m [% 1.3f,% 1.3f,% 1.3f] m/s",
605 obs->ele[omLeftLin.pos], obs->ele[omLeftLin.pos + 1], obs->ele[omLeftLin.pos + 2],
606 obs->ele[sd + omLeftLin.vel], obs->ele[sd + omLeftLin.vel + 1],
607 obs->ele[sd + omLeftLin.vel + 2]));
608 linesOut.emplace_back(
609 string_format(
"right hand pg: [% 1.3f,% 1.3f,% 1.3f] m [% 1.3f,% 1.3f,% 1.3f] m/s",
610 obs->ele[omLeftLin.pos + 3], obs->ele[omLeftLin.pos + 4], obs->ele[omLeftLin.pos + 5],
611 obs->ele[sd + omLeftLin.vel + 3], obs->ele[sd + omLeftLin.vel + 4],
612 obs->ele[sd + omLeftLin.vel + 5]));
615 else if (omLeftLin) {
616 linesOut.emplace_back(
618 obs->ele[omLeftLin.pos + 6], obs->ele[omLeftLin.pos + 7], obs->ele[omLeftLin.pos + 8]));
623 linesOut.emplace_back(
624 string_format(
"forces left: [% 3.1f, % 3.1f, % 3.1f] N right: [% 3.1f, % 3.1f, % 3.1f] N",
625 obs->ele[omFTS.pos], obs->ele[omFTS.pos + 1], obs->ele[omFTS.pos + 2],
626 obs->ele[omFTS.pos + 3], obs->ele[omFTS.pos + 4], obs->ele[omFTS.pos + 5]));
631 if (omColl && omCollPred) {
632 linesOut.emplace_back(
634 obs->ele[omColl.pos], obs->ele[omCollPred.pos]));
638 linesOut.emplace_back(
string_format(
"coll cost: %3.2f", obs->ele[omColl.pos]));
640 else if (omCollPred) {
641 linesOut.emplace_back(
string_format(
"pred coll cost: %3.2f", obs->ele[omCollPred.pos]));
646 linesOut.emplace_back(
string_format(
"manip idx: %1.3f", obs->ele[omMI.pos]));
652 linesOut.emplace_back(
653 string_format(
"goal distance: [% 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f,\n" 654 " % 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f,% 1.2f]",
655 obs->ele[omGD.pos], obs->ele[omGD.pos + 1], obs->ele[omGD.pos + 2],
656 obs->ele[omGD.pos + 3], obs->ele[omGD.pos + 4],
657 obs->ele[omGD.pos + 5], obs->ele[omGD.pos + 6], obs->ele[omGD.pos + 7],
658 obs->ele[omGD.pos + 8], obs->ele[omGD.pos + 9], obs->ele[omGD.pos + 10]));
664 linesOut.emplace_back(
666 obs->ele[omTSD.pos], obs->ele[omTSD.pos + 1], obs->ele[omTSD.pos + 2]));
669 std::stringstream ss;
671 for (
unsigned int i = 0; i < currentAction->m - 1; i++) {
672 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, i, 0) <<
", ";
677 ss << std::fixed << std::setprecision(2) << MatNd_get(currentAction, currentAction->m - 1, 0) <<
"]";
680 if (physicsManager !=
nullptr) {
683 double* com = ball_bpi->
body->Inertia->org;
684 double ball_radius = ball_bpi->
body->shape[0]->extents[0];
686 ball_bpi->
material.getDouble(
"slip", slip);
689 "ball mass: %2.2f kg ball radius: %2.3f cm",
690 ball_bpi->
body->m, ball_radius*100));
693 "ball friction: %1.2f ball rolling friction: %1.3f",
694 ball_bpi->
material.getFrictionCoefficient(),
695 ball_bpi->
material.getRollingFrictionCoefficient()/ball_radius));
698 "ball slip: %3.1f rad/(Ns) CoM offset:[% 2.1f, % 2.1f, % 2.1f] mm",
699 slip, com[0]*1000, com[1]*1000, com[2]*1000));
virtual bool getProperty(std::string &out, const char *property)=0
virtual bool getPropertyBool(const char *property, bool def=false)=0
virtual void initViewer(Rcs::Viewer *viewer)
virtual PropertySource * getChild(const char *prefix)=0
void addParam(const char *bodyName, PhysicsParameterDescriptor *desc)
double dt
The time step [s].
static TaskCombinationMethod checkTaskCombinationMethod(std::string tcmName)
virtual InitStateSetter * createInitStateSetter()
virtual ActionModel * createActionModel()
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)
virtual ForceDisturber * createForceDisturber()
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
static ExperimentConfigRegistration< ECBallInTube > RegBallInTube("BallInTube")
virtual ObservationModel * createObservationModel()
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.
virtual void populatePhysicsParameters(PhysicsParameterManager *manager)
RcsGraph * graph
Graph description.