simulator/main.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <boost/bind.hpp>
3 #include <boost/function.hpp>
4 #include <rtm/Manager.h>
5 #include <rtm/CorbaNaming.h>
6 #include <hrpModel/ModelLoaderUtil.h>
7 #ifdef __APPLE__
8 #include <GLUT/glut.h>
9 #else
10 #include <GL/glut.h>
11 #endif
12 #include <SDL_thread.h>
13 #include "hrpsys/util/GLbodyRTC.h"
14 #include "hrpsys/util/GLlink.h"
15 #include "hrpsys/util/GLutil.h"
16 #include "hrpsys/util/Project.h"
17 #include "hrpsys/util/OpenRTMUtil.h"
18 #include "hrpsys/util/SDLUtil.h"
19 #include "hrpsys/util/BVutil.h"
20 #include "Simulator.h"
21 #include "GLscene.h"
22 
23 using namespace std;
24 using namespace hrp;
25 using namespace OpenHRP;
26 
27 hrp::BodyPtr createBody(const std::string& name, const ModelItem& mitem,
28  ModelLoader_ptr modelloader, GLscene *scene,
29  bool usebbox)
30 {
31  std::cout << "createBody(" << name << "," << mitem.url << ")" << std::endl;
33  std::string args = "GLbodyRTC?instance_name="+name;
34  GLbodyRTC *glbodyrtc = (GLbodyRTC *)manager.createComponent(args.c_str());
35  hrp::BodyPtr body = hrp::BodyPtr(glbodyrtc);
36  BodyInfo_var binfo;
37  try{
38  OpenHRP::ModelLoader::ModelLoadOption opt;
39  opt.readImage = true;
40  opt.AABBdata.length(0);
41  opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
42  binfo = modelloader->getBodyInfoEx(mitem.url.c_str(), opt);
43  }catch(OpenHRP::ModelLoader::ModelLoaderException ex){
44  std::cerr << ex.description << std::endl;
45  return hrp::BodyPtr();
46  }
47  if (!loadBodyFromBodyInfo(body, binfo, true, GLlinkFactory)){
48  std::cerr << "failed to load model[" << mitem.url << "]" << std::endl;
49  manager.deleteComponent(glbodyrtc);
50  return hrp::BodyPtr();
51  }else{
52  for (std::map<std::string, JointItem>::const_iterator it2=mitem.joint.begin();
53  it2 != mitem.joint.end(); it2++){
54  hrp::Link *link = body->link(it2->first);
55  if (!link) continue;
56  if (link) link->isHighGainMode = it2->second.isHighGain;
57  if (it2->second.collisionShape == ""){
58  // do nothing
59  }else if (it2->second.collisionShape == "convex hull"){
60  convertToConvexHull(link);
61  }else if (it2->second.collisionShape == "AABB"){
62  convertToAABB(link);
63  }else{
64  std::cerr << "unknown value of collisionShape property:"
65  << it2->second.collisionShape << std::endl;
66  }
67  }
68  glbodyrtc->setup();
69  if (usebbox) convertToAABB(body);
70  for (size_t i=0; i<mitem.inports.size(); i++){
71  glbodyrtc->createInPort(mitem.inports[i]);
72  }
73  for (size_t i=0; i<mitem.outports.size(); i++){
74  glbodyrtc->createOutPort(mitem.outports[i]);
75  }
76  loadShapeFromBodyInfo(glbodyrtc, binfo);
77  body->setName(name);
78  scene->addBody(body);
79  return body;
80  }
81 }
82 
83 void print_usage(char* progname)
84 {
85  std::cerr << "Usage:" << progname << " [project file] [options]" << std::endl;
86  std::cerr << "Options:" << std::endl;
87  std::cerr << " -nodisplay : headless mode" << std::endl;
88  std::cerr << " -realtime : syncronize to real world time" << std::endl;
89  std::cerr << " -usebbox : use bounding box for collision detection" << std::endl;
90  std::cerr << " -endless : endless mode" << std::endl;
91  std::cerr << " -showsensors : visualize sensors" << std::endl;
92  std::cerr << " -size [pixels] : specify window size in pixels" << std::endl;
93  std::cerr << " -no-default-lights : disable ambient light (simulation environment will be dark)" << std::endl;
94  std::cerr << " -max-edge-length [value] : specify maximum length of polygon edge (if exceed, polygon will be divided to improve rendering quality)" << std::endl;
95  std::cerr << " -max-log-length [value] : specify maximum size of the log" << std::endl;
96  std::cerr << " -exit-on-finish : exit the program when the simulation finish" << std::endl;
97  std::cerr << " -record : record the simulation as movie" << std::endl;
98  std::cerr << " -bg [r] [g] [b] : specify background color" << std::endl;
99  std::cerr << " -h --help : show this help message" << std::endl;
100 }
101 
102 int main(int argc, char* argv[])
103 {
104  bool display = true, usebbox=false;
105  bool showsensors = false;
106  int wsize = 0;
107  bool useDefaultLights = true;
108  double maxEdgeLen = 0;
109  bool exitOnFinish = false;
110  bool record = false;
111  double maxLogLen = 60;
112  bool realtime = false;
113  bool endless = false;
114 
115  if (argc <= 1){
116  print_usage(argv[0]);
117  return 1;
118  }
119 
120  float bgColor[]={0,0,0};
121  for (int i=1; i<argc; i++){
122  if (strcmp("-nodisplay",argv[i])==0){
123  display = false;
124  }else if(strcmp("-realtime", argv[i])==0){
125  realtime = true;
126  }else if(strcmp("-usebbox", argv[i])==0){
127  usebbox = true;
128  }else if(strcmp("-endless", argv[i])==0){
129  endless = true;
130  }else if(strcmp("-showsensors", argv[i])==0){
131  showsensors = true;
132  }else if(strcmp("-size", argv[i])==0){
133  wsize = atoi(argv[++i]);
134  }else if(strcmp("-no-default-lights", argv[i])==0){
135  useDefaultLights = false;
136  }else if(strcmp("-max-edge-length", argv[i])==0){
137  maxEdgeLen = atof(argv[++i]);
138  }else if(strcmp("-max-log-length", argv[i])==0){
139  maxLogLen = atof(argv[++i]);
140  }else if(strcmp("-exit-on-finish", argv[i])==0){
141  exitOnFinish = true;
142  }else if(strcmp("-record", argv[i])==0){
143  record = true;
144  exitOnFinish = true;
145  }else if(strcmp("-bg", argv[i])==0){
146  bgColor[0] = atof(argv[++i]);
147  bgColor[1] = atof(argv[++i]);
148  bgColor[2] = atof(argv[++i]);
149  }else if(strcmp("-h", argv[i])==0 || strcmp("--help", argv[i])==0){
150  print_usage(argv[0]);
151  return 1;
152  }
153  }
154 
155  Project prj;
156  if (!prj.parse(argv[1])){
157  std::cerr << "failed to parse " << argv[1] << std::endl;
158  return 1;
159  }
160  if (realtime){
161  prj.realTime(true);
162  }
163  if (endless){
164  prj.totalTime(0);
165  }
166 
167  //================= OpenRTM =========================
169  int rtmargc=0;
170  std::vector<char *> rtmargv;
171  for (int i=1; i<argc; i++){
172  if (strcmp(argv[i], "-nodisplay")
173  && strcmp(argv[i], "-realtime")
174  && strcmp(argv[i], "-usebbox")
175  && strcmp(argv[i], "-endless")
176  && strcmp(argv[i], "-showsensors")
177  && strcmp(argv[i], "-size")
178  && strcmp(argv[i], "-no-default-lights")
179  && strcmp(argv[i], "-max-edge-length")
180  && strcmp(argv[i], "-max-log-length")
181  && strcmp(argv[i], "-exit-on-finish")
182  && strcmp(argv[i], "-record")
183  && strcmp(argv[i], "-bg")
184  ){
185  rtmargv.push_back(argv[i]);
186  rtmargc++;
187  }
188  }
189  manager = RTC::Manager::init(rtmargc, rtmargv.data());
190  manager->init(rtmargc, rtmargv.data());
191  GLbodyRTC::moduleInit(manager);
192  manager->activateManager();
193  manager->runManager(true);
194 
195  std::string nameServer = manager->getConfig()["corba.nameservers"];
196  int comPos = nameServer.find(",");
197  if (comPos < 0){
198  comPos = nameServer.length();
199  }
200  nameServer = nameServer.substr(0, comPos);
201  RTC::CorbaNaming naming(manager->getORB(), nameServer.c_str());
202 
203  ModelLoader_var modelloader = getModelLoader(CosNaming::NamingContext::_duplicate(naming.getRootContext()));
204  if (CORBA::is_nil(modelloader)){
205  std::cerr << "openhrp-model-loader is not running" << std::endl;
206  return 1;
207  }
208  //==================== Viewer setup ===============
210  GLscene scene(&log);
211  scene.setBackGroundColor(bgColor);
212  scene.showSensors(showsensors);
213  scene.maxEdgeLen(maxEdgeLen);
214  scene.showCollision(prj.view().showCollision);
215  Simulator simulator(&log);
216 
217  SDLwindow window(&scene, &log, &simulator);
218  if (display){
219  window.init(wsize, wsize);
220  if (!useDefaultLights) scene.defaultLights(false);
221  window.setView(prj.view().T);
222  scene.showFloorGrid(prj.view().showScale);
223  }
224 
225  //================= setup Simulator ======================
226  BodyFactory factory = boost::bind(createBody, _1, _2, modelloader, &scene, usebbox);
227  simulator.init(prj, factory);
228  if (!prj.totalTime()){
229  log.enableRingBuffer(maxLogLen/prj.timeStep());
230  }
231 
232  std::cout << "timestep = " << prj.timeStep() << ", total time = "
233  << prj.totalTime() << std::endl;
234 
235  if (display){
236  simulator.start();
237  while(window.oneStep()){
238  if (exitOnFinish && !simulator.isRunning()) break;
239  };
240  simulator.stop();
241  if (record){
242  log.record(10);
243  while(window.oneStep()){
244  if (!log.isRecording()) break;
245  }
246  }
247  }else{
248  while (simulator.oneStep());
249  }
250 
251  manager->shutdown();
252 
253  return 0;
254 }
void convertToAABB(hrp::BodyPtr i_body)
Definition: BVutil.cpp:14
void createInPort(const std::string &config)
Definition: BodyRTC.cpp:140
void enableRingBuffer(int len)
Definition: LogManager.h:157
bool showScale
Definition: Project.h:97
void print_usage(char *progname)
RTObject_impl * createComponent(const char *comp_args)
void deleteComponent(RTObject_impl *comp)
void showFloorGrid(bool flag)
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
bool parse(const std::string &filename)
Definition: Project.cpp:32
void addBody(GLbody *i_body)
Definition: GLmodel.cpp:293
int main(int argc, char *argv[])
void setBackGroundColor(float rgb[3])
void runManager(bool no_block=false)
RTC::ReturnCode_t setup()
Definition: BodyRTC.cpp:65
void shutdown()
bool init(int w=0, int h=0, bool resizable=true)
Definition: SDLUtil.cpp:54
manager
void showSensors(bool flag)
sample RT component which has one data input port and one data output port
CORBA::ORB_ptr getORB()
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
Definition: GLutil.cpp:181
png_uint_32 i
std::vector< std::string > outports
Definition: Project.h:66
static Manager & instance()
boost::shared_ptr< Body > BodyPtr
void setView(double T[16])
Definition: SDLUtil.cpp:359
static void moduleInit(RTC::Manager *)
Definition: GLbodyRTC.cpp:29
bool oneStep()
Definition: SDLUtil.cpp:342
void init(Project &prj, BodyFactory &factory)
std::string url
Definition: Project.h:62
coil::Properties & getConfig()
void convertToConvexHull(hrp::BodyPtr i_body)
Definition: BVutil.cpp:79
static Manager * init(int argc, char **argv)
double totalTime()
Definition: Project.h:106
bool realTime()
Definition: Project.h:112
void defaultLights(bool flag)
ThreeDView & view()
Definition: Project.h:119
void showCollision(bool flag)
bool record(double i_fps)
Definition: LogManager.h:84
bool activateManager()
std::vector< std::string > inports
Definition: Project.h:65
naming
std::map< std::string, JointItem > joint
Definition: Project.h:63
double T[16]
Definition: Project.h:98
void maxEdgeLen(double i_len)
bool showCollision
Definition: Project.h:97
int loadBodyFromBodyInfo(::World *world, const char *_name, BodyInfo_ptr bodyInfo)
double timeStep()
Definition: Project.h:105
bool isRecording()
Definition: LogManager.h:94
boost::function2< hrp::BodyPtr, const std::string &, const ModelItem & > BodyFactory
Definition: ProjectUtil.h:9
hrp::BodyPtr createBody(const std::string &name, const ModelItem &mitem, ModelLoader_ptr modelloader, GLscene *scene, bool usebbox)
void createOutPort(const std::string &config)
Definition: BodyRTC.cpp:219


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