SimulatorUtil.h
Go to the documentation of this file.
1 #include <boost/bind.hpp>
2 #include <boost/function.hpp>
3 #include <rtm/CorbaNaming.h>
4 #include <hrpModel/World.h>
6 #include <hrpModel/ModelLoaderUtil.h>
7 #include <hrpUtil/TimeMeasure.h>
8 #include "Project.h"
9 #include "BodyRTC.h"
10 #include "ProjectUtil.h"
11 #include "GLmodel.h"
12 
13 
14 hrp::BodyPtr createBody(const std::string& name, const std::string& url,
15  std::vector<BodyRTCPtr> *bodies,
16  RTC::CorbaNaming *naming)
17 {
18  std::cout << "createBody(" << name << "," << url << ")" << std::endl;
20  std::string args = "BodyRTC?instance_name="+name;
21  BodyRTCPtr body = (BodyRTC *)manager.createComponent(args.c_str());
22  if (!loadBodyFromModelLoader(body, url.c_str(),
23  CosNaming::NamingContext::_duplicate(naming->getRootContext()),
24  true)){
25  std::cerr << "failed to load model[" << url << "]" << std::endl;
26  manager.deleteComponent(body.get());
27  return hrp::BodyPtr();
28  }else{
29  body->createDataPorts();
30  bodies->push_back(body);
31  return body;
32  }
33 }
34 
35 class Simulator
36 {
37 public:
38  void init(Project &prj, GLscene *i_scene, RTC::CorbaNaming &naming){
39  BodyFactory factory = boost::bind(createBody, _1, _2, &bodies, &naming);
40  initWorld(prj, factory, world);
41  initRTS(prj, receivers);
42  std::cout << "number of receivers:" << receivers.size() << std::endl;
44  totalTime = prj.totalTime();
45  scene = i_scene;
46  }
47 
48  bool oneStep(){
49  tm_control.begin();
50  for (unsigned int i=0; i<bodies.size(); i++){
51  bodies[i]->writeDataPorts();
52  }
53 
54  for (unsigned int i=0; i<bodies.size(); i++){
55  bodies[i]->readDataPorts();
56  }
57 
58  for (unsigned int i=0; i<receivers.size(); i++){
59  receivers[i].tick(world.timeStep());
60  }
61  tm_control.end();
62 
66 
68  scene->addState(state);
69  tm_dynamics.end();
70 
71  if (world.currentTime() > totalTime){
72  std::cout << "controller:" << tm_control.totalTime()
73  << "[s], " << tm_control.averageTime()*1000
74  << "[ms/frame]" << std::endl;
75  std::cout << "dynamics :" << tm_dynamics.totalTime()
76  << "[s], " << tm_dynamics.averageTime()*1000
77  << "[ms/frame]" << std::endl;
78  }
79 
80  return world.currentTime() <= totalTime;
81  }
82 
85  std::vector<BodyRTCPtr> bodies;
86  std::vector<ClockReceiver> receivers;
87  OpenHRP::WorldState state;
88  double totalTime;
90 };
RTObject_impl * createComponent(const char *comp_args)
void deleteComponent(RTObject_impl *comp)
bool oneStep()
Definition: SimulatorUtil.h:48
double timeStep(void) const
boost::intrusive_ptr< BodyRTC > BodyRTCPtr
Definition: BodyRTC.h:175
manager
sample RT component which has one data input port and one data output port
CosNaming::NamingContext_ptr getRootContext()
double totalTime
Definition: SimulatorUtil.h:88
std::vector< CollisionInfo > collisions
Definition: SceneState.h:22
png_uint_32 i
OpenHRP::WorldState state
Definition: SimulatorUtil.h:87
TimeMeasure tm_dynamics
void begin()
hrp::World< hrp::ConstraintForceSolver > world
Definition: SimulatorUtil.h:84
static Manager & instance()
std::vector< ClockReceiver > receivers
boost::shared_ptr< Body > BodyPtr
void getWorldState(WorldState &state, WorldBase &world)
void initWorldState(WorldState &state, WorldBase &world)
GLscene * scene
Definition: SimulatorUtil.h:89
double totalTime()
Definition: Project.h:106
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
double averageTime() const
virtual void calcNextState(OpenHRP::CollisionSequence &corbaCollisionSequence)
void initRTS(Project &prj, std::vector< ClockReceiver > &receivers)
TConstraintForceSolver constraintForceSolver
hrp::BodyPtr createBody(const std::string &name, const std::string &url, std::vector< BodyRTCPtr > *bodies, RTC::CorbaNaming *naming)
Definition: SimulatorUtil.h:14
TimeMeasure tm_collision
double totalTime()
void init(Project &prj, GLscene *i_scene, RTC::CorbaNaming &naming)
Definition: SimulatorUtil.h:38
double currentTime(void) const
boost::function2< hrp::BodyPtr, const std::string &, const ModelItem & > BodyFactory
Definition: ProjectUtil.h:9
TimeMeasure tm_control
std::vector< BodyRTCPtr > bodies
Definition: SimulatorUtil.h:85
void initWorld(Project &prj, BodyFactory &factory, hrp::World< hrp::ConstraintForceSolver > &world, std::vector< hrp::ColdetLinkPairPtr > &pairs)
Definition: ProjectUtil.cpp:9


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51