RcsPySim
A robot control and simulation library
PolicyComponent.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #ifndef RCSPYSIM_POLICYCOMPONENT_H
32 #define RCSPYSIM_POLICYCOMPONENT_H
33 
34 #include <config/PropertySource.h>
35 #include <ExperimentConfig.h>
36 #include <control/ControlPolicy.h>
37 
38 #include <ComponentBase.h>
39 #include <Rcs_MatNd.h>
40 
41 namespace Rcs
42 {
43 
44 class DynamicalSystem;
45 
46 /**
47  * Wraps RcsPySim Experiment and ControlPolicy for use in ECS.
48  */
49 class PolicyComponent : public ComponentBase
50 {
51 public:
52  PolicyComponent(EntityBase* entity, PropertySource* settings, bool computeJointVelocities = false);
53 
54  virtual ~PolicyComponent();
55 
56  // not copy- or movable
58 
59  ExperimentConfig* getExperiment() const;
60 
61  ControlPolicy* getPolicy() const;
62 
63  const MatNd* getObservation() const;
64 
65  const MatNd* getAction() const;
66 
67  const MatNd* getJointCommandPtr() const;
68 
69  RcsGraph* getDesiredGraph() const;
70 
71  // get a text describing the current state of the command fsm
72  std::string getStateText() const;
73 
75 
77 
78 private:
79  // event handlers
80  void subscribe();
81 
82 // void onStart();
83 // void onStop();
84  void onUpdatePolicy(const RcsGraph* state);
85 
86  void onInitFromState(const RcsGraph* target);
87 
88  void onEmergencyStop();
89 
90  void onEmergencyRecover();
91 
92  void onRender();
93 
94  void onPrint();
95 
96  void onPolicyStart();
97 
98  void onPolicyPause();
99 
100  void onPolicyReset();
101 
102  void onGoHome();
103 
104 
105  // experiment to use
107  // policy to use
109 
110  // the robot doesn't provide joint velocities. to work around that, compute them using finite differences
112 
113  // true to check joint limits
115  // true to check for collisions
117 
118  // collision model for collision check
119  RcsCollisionMdl* collisionMdl;
120 
121  // true if the policy should be active
123  // true while in emergency stop
124  bool eStop;
125  // true in the first onUpdatePolicy call after onEmergencyRecover
126  bool eRec;
127  // allows to catch the first render call
129 
130 
131  // Temporary matrices
132  MatNd* observation;
133  MatNd* action;
134 
135  // graph containing the desired states
136  RcsGraph* desiredGraph;
137 
138  // go home policy
139  bool goHome;
142 
143  // dummy, might be filled but is not used
144  MatNd* T_ctrl;
145 
146 };
147 
148 }
149 
150 #endif //RCSPYSIM_POLICYCOMPONENT_H
ControlPolicy * getPolicy() const
void setCollisionCheck(bool collisionCheck)
std::string getStateText() const
PolicyComponent(EntityBase *entity, PropertySource *settings, bool computeJointVelocities=false)
ControlPolicy * policy
const MatNd * getObservation() const
ActionModel * goHomeAM
DynamicalSystem * goHomeDS
RcsGraph * getDesiredGraph() const
void onInitFromState(const RcsGraph *target)
RcsCollisionMdl * collisionMdl
RCSPYSIM_NOCOPY_NOMOVE(PolicyComponent) ExperimentConfig *getExperiment() const
const MatNd * getAction() const
void setJointLimitCheck(bool jointLimitCheck)
const MatNd * getJointCommandPtr() const
ExperimentConfig * experiment
void onUpdatePolicy(const RcsGraph *state)