40 #include <EntityBase.h> 42 #include <Rcs_macros.h> 43 #include <Rcs_typedef.h> 44 #include <Rcs_cmdLine.h> 49 Rcs::EntityBase* entity,
51 bool computeJointVelocities) :
52 ComponentBase(entity), computeJointVelocities(computeJointVelocities)
62 std::string policyFile;
63 if (argP.getArgument(
"-policy", &policyFile,
"Policy file to use")) {
64 std::string policyType;
65 if (!argP.getArgument(
"-policyType", &policyType,
"Type of policy to load")) {
67 if (policyFile.size() >= 4 && policyFile.substr(policyFile.size() - 4) ==
".pth") {
71 RFATAL(
"Cannot determine policy type from policy file %s.", policyFile.c_str());
74 RCHECK(!policyType.empty());
79 auto policyConfig = settings->
getChild(
"policy");
80 if (policyConfig->exists()) {
108 std::unique_ptr<ActionModel> goHomeWrappedAM;
116 RLOG(0,
"Couldn't find IK definition in action model, using joint position control for go home.");
119 goHomeWrappedAM->reset();
122 Eigen::VectorXd homePos;
123 homePos.setZero(goHomeWrappedAM->getDim());
125 goHomeWrappedAM->getStableAction(&hpMat);
137 Eigen::MatrixXd::Identity(goHomeWrappedAM->getDim(), goHomeWrappedAM->getDim()), homePos);
173 #define SUBSCRIBE(name) ComponentBase::subscribe(#name, &PolicyComponent::on##name) 234 RcsGraph_computeForwardKinematics(
desiredGraph, NULL, NULL);
238 double goHomeAct = 1;
239 MatNd ghaMat = MatNd_fromPtr(1, 1, &goHomeAct);
242 RcsGraph_computeForwardKinematics(
desiredGraph, NULL, NULL);
247 unsigned int aor = RcsGraph_numJointLimitsViolated(
desiredGraph,
true);
249 RLOG(0,
"%d joint limit violations - triggering emergency stop", aor);
250 getEntity()->publish(
"EmergencyStop");
260 const double distLimit = 0.001;
261 double minDist = RcsCollisionMdl_getMinDist(
collisionMdl);
262 if (minDist < distLimit) {
263 RLOG(0,
"Found collision distance of %f (must be >%f) - triggering emergency stop",
265 RcsCollisionModel_fprintCollisions(stdout,
collisionMdl, distLimit);
266 getEntity()->publish(
"EmergencyStop");
276 RLOG(0,
"PolicyComponent::onInitFromState()");
309 RLOG(0,
"Not in emergency stop, ignored.");
320 RLOG(0,
"Emergency stop is active, cannot start policy.");
347 RLOG(0,
"Emergency stop is active, cannot start go home policy.");
363 getEntity()->publish<std::string,
const RcsGraph*>(
"RenderGraph",
"IK",
desiredGraph);
365 getEntity()->publish<
const MatNd*>(
"RenderLines",
collisionMdl->cp);
370 getEntity()->publish<std::string, std::string>(
"RenderCommand",
"IK",
"setGhostMode");
379 return "PolicyComponent: emergency stop!";
382 return "PolicyComponent: policy active";
385 return "PolicyComponent: go home";
388 return "PolicyComponent: inactive";
394 printf(
"Desired state from action model:%n");
ControlPolicy * getPolicy() const
bool renderingInitialized
void setCollisionCheck(bool collisionCheck)
MatNd * createValueMatrix() const
std::string getStateText() const
virtual PropertySource * getChild(const char *prefix)=0
PolicyComponent(EntityBase *entity, PropertySource *settings, bool computeJointVelocities=false)
const BoxSpace * getSpace() const
void onEmergencyRecover()
double dt
The time step [s].
virtual void computeCommand(MatNd *q_des, MatNd *q_dot_des, MatNd *T_des, const MatNd *action, double dt)=0
MatNd * computeObservation(const MatNd *currentAction, double dt) const
const MatNd * getObservation() const
virtual void computeAction(MatNd *action, const MatNd *observation)=0
virtual ~PolicyComponent()
DynamicalSystem * goHomeDS
RcsGraph * getDesiredGraph() const
static DynamicalSystem * create(PropertySource *properties, unsigned int innerTaskDim)
void onInitFromState(const RcsGraph *target)
virtual void setGoal(const Eigen::VectorXd &x_des)=0
RcsCollisionMdl * collisionMdl
static ExperimentConfig * create(PropertySource *properties)
const MatNd * getAction() const
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
void setJointLimitCheck(bool jointLimitCheck)
static ControlPolicy * create(const char *name, const char *dataFile)
ActionModel * actionModel
Action model (pluggable)
RcsCollisionMdl * collisionMdl
Collision model to use. Is based on the graph, so take care when using with IK etc.
bool computeJointVelocities
const MatNd * getJointCommandPtr() const
ExperimentConfig * experiment
void onUpdatePolicy(const RcsGraph *state)
MatNd viewEigen2MatNd(M &src)
RcsGraph * graph
Graph description.