rbprmbuilder.impl.hh 16.8 KB
Newer Older
Steve Tonneau's avatar
Steve Tonneau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright (c) 2014 CNRS
// Author: Florent Lamiraux
//
// This file is part of hpp-manipulation-corba.
// hpp-manipulation-corba is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-manipulation-corba is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Lesser Public License for more details.  You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-manipulation-corba.  If not, see
// <http://www.gnu.org/licenses/>.

#ifndef HPP_RBPRM_CORBA_BUILDER_IMPL_HH
# define HPP_RBPRM_CORBA_BUILDER_IMPL_HH

# include <hpp/core/problem-solver.hh>
22
# include <hpp/core/path.hh>
Steve Tonneau's avatar
Steve Tonneau committed
23
# include "rbprmbuilder.hh"
Steve Tonneau's avatar
Steve Tonneau committed
24
# include <hpp/rbprm/rbprm-device.hh>
Steve Tonneau's avatar
Steve Tonneau committed
25
# include <hpp/rbprm/rbprm-fullbody.hh>
Steve Tonneau's avatar
Steve Tonneau committed
26
27
# include <hpp/rbprm/rbprm-shooter.hh>
# include <hpp/rbprm/rbprm-validation.hh>
28
# include <hpp/rbprm/sampling/analysis.hh>
29
30
31
32
# include <hpp/core/collision-path-validation-report.hh>
# include <hpp/core/problem-solver.hh>
# include <hpp/core/discretized-collision-checking.hh>
# include <hpp/core/straight-path.hh>
33
# include <hpp/rbprm/rbprm-path-validation.hh>
34
35
# include <hpp/fcl/BVH/BVH_model.h>
# include <hpp/core/config-validations.hh>
36
#include <hpp/rbprm/dynamic/dynamic-path-validation.hh>
37
38
# include "hpp/corbaserver/fwd.hh"

Steve Tonneau's avatar
Steve Tonneau committed
39
40
41
42
namespace hpp {
  namespace rbprm {
    namespace impl {
      using CORBA::Short;
43
			typedef std::map<std::string, std::vector<boost::shared_ptr<model::CollisionObject> > > affMap_t;
Steve Tonneau's avatar
Steve Tonneau committed
44

Steve Tonneau's avatar
Steve Tonneau committed
45
46
47
    struct BindShooter
    {
        BindShooter(const std::size_t shootLimit = 10000,
Steve Tonneau's avatar
Steve Tonneau committed
48
                    const std::size_t displacementLimit = 1000)
Steve Tonneau's avatar
Steve Tonneau committed
49
50
51
52
53
54
            : shootLimit_(shootLimit)
            , displacementLimit_(displacementLimit) {}

        hpp::rbprm::RbPrmShooterPtr_t create (const hpp::model::DevicePtr_t& robot)
        {
            hpp::model::RbPrmDevicePtr_t robotcast = boost::static_pointer_cast<hpp::model::RbPrmDevice>(robot);
55
		        if (affMap_.empty ()) {
56
57
    	        throw hpp::Error ("No affordances found. Unable to create shooter object.");
      		  }
Steve Tonneau's avatar
Steve Tonneau committed
58
            rbprm::RbPrmShooterPtr_t shooter = hpp::rbprm::RbPrmShooter::create
59
                    (robotcast, problemSolver_->problem ()->collisionObstacles(), affMap_,
60
										romFilter_,affFilter_,shootLimit_,displacementLimit_);
Steve Tonneau's avatar
Steve Tonneau committed
61
62
63
            if(!so3Bounds_.empty())
                shooter->BoundSO3(so3Bounds_);
            return shooter;
Steve Tonneau's avatar
Steve Tonneau committed
64
        }
65
66
67
68

        hpp::core::PathValidationPtr_t createPathValidation (const hpp::model::DevicePtr_t& robot, const hpp::model::value_type& val)
        {
            hpp::model::RbPrmDevicePtr_t robotcast = boost::static_pointer_cast<hpp::model::RbPrmDevice>(robot);
69
70
71
72
73
            affMap_ = problemSolver_->map
              <std::vector<boost::shared_ptr<model::CollisionObject> > > ();
            if (affMap_.empty ()) {
              throw hpp::Error ("No affordances found. Unable to create Path Validaton object.");
            }
74
            hpp::rbprm::RbPrmValidationPtr_t validation
75
              (hpp::rbprm::RbPrmValidation::create(robotcast, romFilter_, affFilter_, affMap_));
76
            hpp::rbprm::RbPrmPathValidationPtr_t collisionChecking = hpp::rbprm::RbPrmPathValidation::create(robot,val);
77
            collisionChecking->add (validation);
78
79
            problemSolver_->problem()->configValidation(core::ConfigValidations::create ());
            problemSolver_->problem()->configValidations()->add(validation);
80
            return collisionChecking;
81
82
        }

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        hpp::core::PathValidationPtr_t createDynamicPathValidation (const hpp::model::DevicePtr_t& robot, const hpp::model::value_type& val)
        {
          hpp::model::RbPrmDevicePtr_t robotcast = boost::static_pointer_cast<hpp::model::RbPrmDevice>(robot);
          affMap_ = problemSolver_->map
            <std::vector<boost::shared_ptr<model::CollisionObject> > > ();
          if (affMap_.empty ()) {
            throw hpp::Error ("No affordances found. Unable to create Path Validaton object.");
          }
          hpp::rbprm::RbPrmValidationPtr_t validation
            (hpp::rbprm::RbPrmValidation::create(robotcast, romFilter_, affFilter_, affMap_));
          hpp::rbprm::DynamicPathValidationPtr_t collisionChecking = hpp::rbprm::DynamicPathValidation::create(robot,val);
          collisionChecking->add (validation);
          problemSolver_->problem()->configValidation(core::ConfigValidations::create ());
          problemSolver_->problem()->configValidations()->add(validation);
          // build the dynamicValidation :
          double sizeFootX,sizeFootY,mass,mu;
          bool rectangularContact;
          try {
101
102
103
104
            boost::any value_x = problemSolver_->problem()->get<boost::any> (std::string("sizeFootX"));
            boost::any value_y = problemSolver_->problem()->get<boost::any> (std::string("sizeFootY"));
            sizeFootX = boost::any_cast<double>(value_x)/2.;
            sizeFootY = boost::any_cast<double>(value_y)/2.;
105
106
107
108
109
110
111
112
            rectangularContact = 1;
          } catch (const std::exception& e) {
            hppDout(warning,"Warning : size of foot not definied, use 0 (contact point)");
            sizeFootX =0;
            sizeFootY =0;
            rectangularContact = 0;
          }
          mass = robot->mass();
113
114
115
116
117
118
119
120
          try {
            boost::any value = problemSolver_->problem()->get<boost::any> (std::string("friction"));
            mu = boost::any_cast<double>(value);
            hppDout(notice,"dynamic val : mu define in python : "<<mu);
          } catch (const std::exception& e) {
            mu= 0.5;
            hppDout(notice,"dynamic val : mu not defined, take : "<<mu<<" as default.");
          }
121
122
123
124
125
126
          DynamicValidationPtr_t dynamicVal = DynamicValidation::create(rectangularContact,sizeFootX,sizeFootY,mass,mu);
          collisionChecking->addDynamicValidator(dynamicVal);

          return collisionChecking;
        }

Steve Tonneau's avatar
Steve Tonneau committed
127
        hpp::core::ProblemSolverPtr_t problemSolver_;
Steve Tonneau's avatar
Steve Tonneau committed
128
        std::vector<std::string> romFilter_;
129
        std::map<std::string, std::vector<std::string> > affFilter_;
Steve Tonneau's avatar
Steve Tonneau committed
130
131
        std::size_t shootLimit_;
        std::size_t displacementLimit_;
Steve Tonneau's avatar
Steve Tonneau committed
132
        std::vector<double> so3Bounds_;
133
				affMap_t affMap_;
Steve Tonneau's avatar
Steve Tonneau committed
134
135
    };

Steve Tonneau's avatar
Steve Tonneau committed
136
137
138
      class RbprmBuilder : public virtual POA_hpp::corbaserver::rbprm::RbprmBuilder
      {
        public:
Steve Tonneau's avatar
Steve Tonneau committed
139
        RbprmBuilder ();
Steve Tonneau's avatar
Steve Tonneau committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

        virtual void loadRobotRomModel (const char* robotName,
                 const char* rootJointType,
                 const char* packageName,
                 const char* modelName,
                 const char* urdfSuffix,
                 const char* srdfSuffix) throw (hpp::Error);

        virtual void loadRobotCompleteModel (const char* robotName,
                 const char* rootJointType,
                 const char* packageName,
                 const char* modelName,
                 const char* urdfSuffix,
                 const char* srdfSuffix) throw (hpp::Error);

Steve Tonneau's avatar
Steve Tonneau committed
155
156
157
158
159
160
161
162

        virtual void loadFullBodyRobot (const char* robotName,
                 const char* rootJointType,
                 const char* packageName,
                 const char* modelName,
                 const char* urdfSuffix,
                 const char* srdfSuffix) throw (hpp::Error);

163
164
        virtual void loadFullBodyRobotFromExistingRobot () throw (hpp::Error);

165
        void setStaticStability(const bool staticStability) throw (hpp::Error);
166

Steve Tonneau's avatar
Steve Tonneau committed
167
        virtual void setFilter(const hpp::Names_t& roms) throw (hpp::Error);
168
				virtual void setAffordanceFilter(const char* romName, const hpp::Names_t& affordances) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
169
170
        virtual void boundSO3(const hpp::floatSeq& limitszyx) throw (hpp::Error);

Steve Tonneau's avatar
Steve Tonneau committed
171

Steve Tonneau's avatar
Steve Tonneau committed
172
        virtual hpp::floatSeq* getSampleConfig(const char* limb, unsigned short sampleId) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
173
        virtual hpp::floatSeq* getSamplePosition(const char* limb, unsigned short sampleId) throw (hpp::Error);
174
        virtual hpp::floatSeqSeq* getEffectorPosition(const char* limb, const hpp::floatSeq& configuration) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
175
        virtual CORBA::UShort getNumSamples(const char* limb) throw (hpp::Error);
176
        virtual hpp::floatSeq* getOctreeNodeIds(const char* limb) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
177
        virtual double getSampleValue(const char* limb, const char* valueName, unsigned short sampleId) throw (hpp::Error);
178
        virtual double getEffectorDistance(unsigned short  state1, unsigned short  state2) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
179

Steve Tonneau's avatar
Steve Tonneau committed
180
        virtual hpp::floatSeq* generateContacts(const hpp::floatSeq& configuration,
181
                                                const hpp::floatSeq& direction, const hpp::floatSeq& acceleration, const double robustnessThreshold) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
182

183
184
        virtual hpp::floatSeq* generateGroundContact(const hpp::Names_t& contactLimbs) throw (hpp::Error);

185
186
187
188
        virtual hpp::floatSeq* getContactSamplesIds(const char* limb,
                                                   const hpp::floatSeq& configuration,
                                                   const hpp::floatSeq& direction) throw (hpp::Error);

Steve Tonneau's avatar
Steve Tonneau committed
189
        virtual hpp::floatSeqSeq* getContactSamplesProjected(const char* limb,
Steve Tonneau's avatar
Steve Tonneau committed
190
191
192
193
                                                   const hpp::floatSeq& configuration,
                                                   const hpp::floatSeq& direction,
                                                   unsigned short numSamples) throw (hpp::Error);

194
195
196
        virtual hpp::floatSeq* getSamplesIdsInOctreeNode(const char* limb,
                                                   double octreeNodeId) throw (hpp::Error);

197
        virtual void addLimb(const char* id, const char* limb, const char* effector, const hpp::floatSeq& offset, const hpp::floatSeq& normal, double x, double y,
198
199
200
201
                             unsigned short samples, const char *heuristicName, double resolution, const char *contactType,
                             double disableEffectorCollision) throw (hpp::Error);
        virtual void addLimbDatabase(const char* databasePath, const char* id, const char* heuristicName, double loadValues,
                                     double disableEffectorCollision) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
202

Steve Tonneau's avatar
Steve Tonneau committed
203
204
        virtual void setStartState(const hpp::floatSeq& configuration, const hpp::Names_t& contactLimbs) throw (hpp::Error);
        virtual void setEndState(const hpp::floatSeq& configuration, const hpp::Names_t& contactLimbs) throw (hpp::Error);
205
        virtual hpp::floatSeq*  computeContactForConfig(const hpp::floatSeq& configuration, const char* limbNam) throw (hpp::Error);
206
        virtual hpp::floatSeqSeq* computeContactPoints(unsigned short cId) throw (hpp::Error);
207
        virtual hpp::floatSeqSeq* computeContactPointsForLimb(unsigned short cId, const char* limbName) throw (hpp::Error);
208
209
        virtual hpp::floatSeqSeq* interpolate(double timestep, double path, double robustnessTreshold, unsigned short filterStates) throw (hpp::Error);
        virtual hpp::floatSeqSeq* interpolateConfigs(const hpp::floatSeqSeq& configs, double robustnessTreshold, unsigned short filterStates) throw (hpp::Error);
210
211
        virtual hpp::floatSeqSeq* getContactCone(unsigned short stateId, double friction) throw (hpp::Error);
        virtual hpp::floatSeqSeq* getContactIntermediateCone(unsigned short stateId, double friction) throw (hpp::Error);
212
213
        virtual CORBA::Short generateComTraj(const hpp::floatSeqSeq& positions, const hpp::floatSeqSeq& velocities,
                                             const hpp::floatSeqSeq& accelerations, const double dt) throw (hpp::Error);
214
        virtual CORBA::Short generateRootPath(const hpp::floatSeqSeq& rootPositions,
215
                                      const hpp::floatSeq& q1, const hpp::floatSeq& q2) throw (hpp::Error);
216
217
        virtual CORBA::Short limbRRT(double state1, double state2, unsigned short numOptimizations) throw (hpp::Error);
        virtual CORBA::Short limbRRTFromRootPath(double state1, double state2, unsigned short path, unsigned short numOptimizations) throw (hpp::Error);
218
        virtual CORBA::Short configToPath(const hpp::floatSeqSeq& configs) throw (hpp::Error);
219
        virtual CORBA::Short comRRT(double state1, double state2, unsigned short path, unsigned short numOptimizations) throw (hpp::Error);
220
221
222
223
224

        typedef core::PathPtr_t (*t_rrt)
            (RbPrmFullBodyPtr_t, core::ProblemPtr_t, const core::PathPtr_t,
             const  State &, const State &, const  std::size_t, const bool);

225
        hpp::floatSeq* rrt(t_rrt functor , double state1,
226
227
228
                           unsigned short comTraj1, unsigned short comTraj2, unsigned short comTraj3,
                           unsigned short numOptimizations) throw (hpp::Error);

229
        virtual hpp::floatSeq* comRRTFromPos(double state1,
230
231
232
                                           unsigned short comTraj1,
                                           unsigned short comTraj2,
                                           unsigned short comTraj3,
Steve Tonneau's avatar
Steve Tonneau committed
233
                                           unsigned short numOptimizations) throw (hpp::Error);
234
        virtual hpp::floatSeq* effectorRRT(double state1,
235
236
237
                                           unsigned short comTraj1,
                                           unsigned short comTraj2,
                                           unsigned short comTraj3,
238
                                           unsigned short numOptimizations) throw (hpp::Error);
239
240
241
242
        virtual hpp::floatSeq* effectorRRTFromPath(double state1,
                                           unsigned short path,
                                           double path_from,
                                           double path_to,
243
244
245
                                           unsigned short comTraj1,
                                           unsigned short comTraj2,
                                           unsigned short comTraj3,
246
247
                                           unsigned short numOptimizations,
                                           const hpp::Names_t& trackedEffectors) throw (hpp::Error);
248
        virtual hpp::floatSeq* projectToCom(double state, const hpp::floatSeq& targetCom) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
249
        virtual CORBA::Short createState(const hpp::floatSeq& configuration, const hpp::Names_t& contactLimbs) throw (hpp::Error);
250
        virtual hpp::floatSeq* getConfigAtState(unsigned short stateId) throw (hpp::Error);
251
        double projectStateToCOMEigen(unsigned short stateId, const model::Configuration_t& com_target)throw (hpp::Error);
252
        virtual double projectStateToCOM(unsigned short stateId, const hpp::floatSeq& com) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
253
        virtual void saveComputedStates(const char* filepath) throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
254
        virtual void saveLimbDatabase(const char* limbname,const char* filepath) throw (hpp::Error);
255
        virtual hpp::floatSeq* getOctreeBox(const char* limbName, double sampleId) throw (hpp::Error);
256
257
        virtual CORBA::Short  isLimbInContact(const char* limbName, double state) throw (hpp::Error);
        virtual CORBA::Short  isLimbInContactIntermediary(const char* limbName, double state) throw (hpp::Error);
258
        virtual hpp::floatSeqSeq* getOctreeBoxes(const char* limbName, const hpp::floatSeq& configuration) throw (hpp::Error);
259
        virtual hpp::floatSeq* getOctreeTransform(const char* limbName, const hpp::floatSeq& configuration) throw (hpp::Error);
260
        virtual CORBA::Short isConfigBalanced(const hpp::floatSeq& config, const hpp::Names_t& contactLimbs, double robustnessTreshold) throw (hpp::Error);
261
        virtual void runSampleAnalysis(const char* analysis, double isstatic) throw (hpp::Error);
262
        virtual hpp::floatSeq* runLimbSampleAnalysis(const char* limbname, const char* analysis, double isstatic) throw (hpp::Error);
263
        virtual void dumpProfile(const char* logFile) throw (hpp::Error);
264
        virtual double getTimeAtState(unsigned short stateId)throw (hpp::Error);
265
        virtual Names_t* getContactsVariations(unsigned short stateIdFrom,unsigned short stateIdTo )throw (hpp::Error);
266
        virtual Names_t* getAllLimbsNames()throw (hpp::Error);
Steve Tonneau's avatar
Steve Tonneau committed
267
        public:
Steve Tonneau's avatar
Steve Tonneau committed
268
269
270
        void SetProblemSolver (hpp::core::ProblemSolverPtr_t problemSolver);

        private:
Steve Tonneau's avatar
Steve Tonneau committed
271
272
273
274
        /// \brief Pointer to hppPlanner object of hpp::corbaServer::Server.
        core::ProblemSolverPtr_t problemSolver_;

        private:
Steve Tonneau's avatar
Steve Tonneau committed
275
        model::T_Rom romDevices_;
Steve Tonneau's avatar
Steve Tonneau committed
276
        rbprm::RbPrmFullBodyPtr_t fullBody_;
Steve Tonneau's avatar
Steve Tonneau committed
277
        bool romLoaded_;
Steve Tonneau's avatar
Steve Tonneau committed
278
        bool fullBodyLoaded_;
Steve Tonneau's avatar
Steve Tonneau committed
279
        BindShooter bindShooter_;
Steve Tonneau's avatar
Steve Tonneau committed
280
281
        rbprm::State startState_;
        rbprm::State endState_;
Steve Tonneau's avatar
Steve Tonneau committed
282
        std::vector<rbprm::State> lastStatesComputed_;
283
        rbprm::T_StateFrame lastStatesComputedTime_;
Steve Tonneau's avatar
Steve Tonneau committed
284
        sampling::AnalysisFactory* analysisFactory_;
Steve Tonneau's avatar
Steve Tonneau committed
285
286
287
288
289
290
      }; // class RobotBuilder
    } // namespace impl
  } // namespace manipulation
} // namespace hpp

#endif // HPP_RBPRM_CORBA_BUILDER_IMPL_HH