monitor/main.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <boost/bind.hpp>
4 #include <hrpModel/ModelLoaderUtil.h>
6 #ifdef __APPLE__
7 #include <GLUT/glut.h>
8 #else
9 #include <GL/glut.h>
10 #endif
11 #include "hrpsys/util/ProjectUtil.h"
12 #include "hrpsys/util/GLbody.h"
13 #include "hrpsys/util/GLlink.h"
14 #include "hrpsys/util/GLutil.h"
15 #include "hrpsys/util/SDLUtil.h"
16 #include "GLscene.h"
17 #include "Monitor.h"
18 
19 using namespace hrp;
20 using namespace OpenHRP;
21 
22 hrp::BodyPtr createBody(const std::string& name, const ModelItem& mitem,
23  ModelLoader_ptr modelloader)
24 {
25  BodyInfo_var binfo;
26  try{
27  OpenHRP::ModelLoader::ModelLoadOption opt;
28  opt.readImage = true;
29  opt.AABBdata.length(0);
30  opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
31  binfo = modelloader->getBodyInfoEx(mitem.url.c_str(), opt);
32  }catch(OpenHRP::ModelLoader::ModelLoaderException ex){
33  std::cerr << ex.description << std::endl;
34  return hrp::BodyPtr();
35  }
36  GLbody *glbody = new GLbody();
37  hrp::BodyPtr body(glbody);
38  hrp::loadBodyFromBodyInfo(body, binfo, true, GLlinkFactory);
39  loadShapeFromBodyInfo(glbody, binfo);
40  body->setName(name);
41  return body;
42 }
43 
44 int main(int argc, char* argv[])
45 {
46  if (argc < 2){
47  std::cerr << "Usage:" << argv[0] << " project.xml [-rh RobotHardwareComponent] [-sh StateHolder component] [-size size] [-bg r g b] [-host localhost] [-port 2809] [-interval 100] [-nogui]" << std::endl;
48  return 1;
49  }
50 
51  Project prj;
52  if (!prj.parse(argv[1])){
53  std::cerr << "failed to parse " << argv[1] << std::endl;
54  return 1;
55  }
56 
57  char *rhname = NULL, *shname = NULL, *hostname = NULL;
58  int wsize = 0, port=0, interval=0;
59  float bgColor[] = {0,0,0};
60  bool orbinitref = false, gui = true;
61  for (int i = 2; i<argc; i++){
62  if (strcmp(argv[i], "-rh")==0){
63  rhname = argv[++i];
64  }else if(strcmp(argv[i], "-sh")==0){
65  shname = argv[++i];
66  }else if(strcmp(argv[i], "-size")==0){
67  wsize = atoi(argv[++i]);
68  }else if(strcmp(argv[i], "-bg")==0){
69  bgColor[0] = atof(argv[++i]);
70  bgColor[1] = atof(argv[++i]);
71  bgColor[2] = atof(argv[++i]);
72  }else if(strcmp(argv[i], "-host")==0){
73  hostname = argv[++i];
74  }else if(strcmp(argv[i], "-port")==0){
75  port = atoi(argv[++i]);
76  }else if(strcmp(argv[i], "-interval")==0){
77  interval = atoi(argv[++i]);
78  }else if(strcmp(argv[i], "-nogui")==0){
79  gui = false;
80  }else if(strcmp(argv[i], "-ORBInitRef")==0){
81  orbinitref = true;
82  }
83  }
84  char **nargv = argv;
85  int nargc = argc;
86  if(orbinitref == false){
87  nargv = (char **)malloc((argc+2)*sizeof(char *));
88  for(int i = 0; i < nargc; i++) nargv[i] = argv[i];
89  char buf1[] = "-ORBInitRef";
90  char buf2[256];
91  sprintf(buf2, "NameService=corbaloc:iiop:%s:%d/NameService", hostname==NULL?"localhost":hostname, port==0?2809:port);
92  nargv[nargc++] = buf1;
93  nargv[nargc++] = buf2;
94  }
95 
96  //================= CORBA =========================
97  for(int i = 0; i < nargc-1; i++) {
98  if(strcmp(nargv[i], "-ORBInitRef")==0) {
99  std::cerr << "[monitor] Starting CORBA Client with " << nargv[i+1] << std::endl;
100  }
101  }
102  CORBA::ORB_var orb;
103  CosNaming::NamingContext_var namingContext;
104 
105  try {
106  orb = CORBA::ORB_init(nargc, nargv);
107 
108  CORBA::Object_var obj;
109  obj = orb->resolve_initial_references("RootPOA");
110  PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
111  if(CORBA::is_nil(poa)){
112  throw std::string("error: failed to narrow root POA.");
113  }
114 
115  PortableServer::POAManager_var poaManager = poa->the_POAManager();
116  if(CORBA::is_nil(poaManager)){
117  throw std::string("error: failed to narrow root POA manager.");
118  }
119 
120  obj = orb->resolve_initial_references("NameService");
121  namingContext = CosNaming::NamingContext::_narrow(obj);
122  if(CORBA::is_nil(namingContext)){
123  throw std::string("error: failed to narrow naming context.");
124  }
125 
126  poaManager->activate();
127  }catch (CORBA::SystemException& ex) {
128  std::cerr << "[monitor] Failed to initialize CORBA " << std::endl << ex._rep_id() << std::endl;
129  return -1;
130  }catch (const std::string& error){
131  std::cerr << "[monitor] Failed to initialize CORBA " << std::endl << error << std::endl;
132  return -1;
133  }
134 
135  //================= logger ======================
137  log.enableRingBuffer(5000);
138 
139  //================= monitor ======================
141  if ( hostname != NULL ) rhview.hostname = hostname;
142  if ( port != 0 ) rhview.port = port;
143  if ( interval != 0 ) rhview.interval = interval;
144  std::cerr << "[monitor] Monitor service with " << rhview.hostname << "@" << rhview.port << ", " << 1000/rhview.interval << "Hz" << std::endl;
145  Monitor monitor(orb,
146  rhview.hostname, rhview.port, rhview.interval,
147  &log);
148  if (rhname) {
149  monitor.setRobotHardwareName(rhname);
150  }else{
151  monitor.setRobotHardwareName(rhview.RobotHardwareName.c_str());
152  }
153  if (shname) {
154  monitor.setStateHolderName(shname);
155  }else{
156  monitor.setStateHolderName(rhview.StateHolderName.c_str());
157  }
158  //==================== viewer ===============
159  if ( gui ) {
160  GLscene scene(&log);
161  scene.setBackGroundColor(bgColor);
162  scene.showCoMonFloor(prj.view().showCoMonFloor);
163 
164  SDLwindow window(&scene, &log, &monitor);
165  window.init(wsize, wsize);
166 
167  std::vector<hrp::ColdetLinkPairPtr> pairs;
168  ModelLoader_var modelloader = getModelLoader(namingContext);
169  if (CORBA::is_nil(modelloader)){
170  std::cerr << "openhrp-model-loader is not running" << std::endl;
171  return 1;
172  }
173  BodyFactory factory = boost::bind(createBody, _1, _2, modelloader);
174 
175  initWorld(prj, factory, scene, pairs);
176  scene.setCollisionCheckPairs(pairs);
177 
178  monitor.start();
179  int cnt=0;
180  while(window.oneStep());
181  monitor.stop();
182  }
183  else
184  {
185  std::vector<hrp::ColdetLinkPairPtr> pairs;
186  ModelLoader_var modelloader = getModelLoader(namingContext);
187  if (CORBA::is_nil(modelloader)){
188  std::cerr << "openhrp-model-loader is not running" << std::endl;
189  return 1;
190  }
191  BodyFactory factory = boost::bind(createBody, _1, _2, modelloader);
192 
193  // add bodies
195  for (std::map<std::string, ModelItem>::iterator it=prj.models().begin();
196  it != prj.models().end(); it++){
197  const std::string name
198  = it->second.rtcName == "" ? it->first : it->second.rtcName;
199  hrp::BodyPtr tmp_body = factory(name, it->second);
200  if (tmp_body){
201  tmp_body->setName(name);
202  if(tmp_body->numJoints() > 0)
203  body = tmp_body;
204  else
205  std::cerr << "[monitor] Skipping non-robot model (" << name << ")" << std::endl;
206  }
207  }
208  if (!body) {
209  std::cerr << "[monitor] Monitoring " << body->name() << std::endl;
210  return -1;
211  }
212  monitor.start();
213  while(monitor.oneStep()){
214  log.updateIndex();
215  monitor.showStatus(body);
216  }
217  monitor.stop();
218  }
219  try {
220  orb->destroy();
221  }
222  catch(...){
223 
224  }
225 
226  return 0;
227 }
void enableRingBuffer(int len)
Definition: LogManager.h:157
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
bool parse(const std::string &filename)
Definition: Project.cpp:32
int main(int argc, char *argv[])
void setCollisionCheckPairs(const std::vector< hrp::ColdetLinkPairPtr > &i_pairs)
void setBackGroundColor(float rgb[3])
char * malloc()
png_infop png_charpp name
std::string hostname
Definition: Project.h:89
bool init(int w=0, int h=0, bool resizable=true)
Definition: SDLUtil.cpp:54
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
void showCoMonFloor(bool flag)
void error(char *msg) const
RobotHardwareClientView & RobotHardwareClient()
Definition: Project.h:118
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
Definition: GLutil.cpp:181
png_uint_32 i
boost::shared_ptr< Body > BodyPtr
Definition: GLbody.h:11
bool oneStep()
Definition: SDLUtil.cpp:342
void setStateHolderName(const char *i_name)
Definition: Monitor.cpp:283
std::string url
Definition: Project.h:62
void showStatus(hrp::BodyPtr &body)
Definition: Monitor.cpp:125
bool oneStep()
Definition: Monitor.cpp:28
int updateIndex()
Definition: LogManager.h:108
std::string RobotHardwareName
Definition: Project.h:89
Definition: Monitor.h:8
bool showCoMonFloor
Definition: Project.h:97
ThreeDView & view()
Definition: Project.h:119
std::map< std::string, ModelItem > & models()
Definition: Project.h:114
hrp::BodyPtr createBody(const std::string &name, const ModelItem &mitem, ModelLoader_ptr modelloader)
std::string sprintf(char const *__restrict fmt,...)
obj
void setRobotHardwareName(const char *i_name)
Definition: Monitor.cpp:278
boost::function2< hrp::BodyPtr, const std::string &, const ModelItem & > BodyFactory
Definition: ProjectUtil.h:9
void initWorld(Project &prj, BodyFactory &factory, hrp::World< hrp::ConstraintForceSolver > &world, std::vector< hrp::ColdetLinkPairPtr > &pairs)
Definition: ProjectUtil.cpp:9
std::string StateHolderName
Definition: Project.h:89


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