viewSimulator.cpp
Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: t; tab-width: 4; c-basic-offset: 8; -*-
00002 #include <iostream>
00003 #include <hrpModel/ModelLoaderUtil.h>
00004 #include <hrpCorba/DynamicsSimulator.hh>
00005 #include <hrpCorba/Controller.hh>
00006 #include <hrpCorba/ViewSimulator.hh>
00007 #include <hrpUtil/Eigen3d.h>
00008 
00009 using namespace std;
00010 using namespace hrp;
00011 using namespace OpenHRP;
00012 
00013 template <typename X, typename X_ptr>
00014 X_ptr checkCorbaServer(std::string n, CosNaming::NamingContext_var &cxt)
00015 {
00016         CosNaming::Name ncName;
00017         ncName.length(1);
00018         ncName[0].id = CORBA::string_dup(n.c_str());
00019         ncName[0].kind = CORBA::string_dup("");
00020         X_ptr srv = NULL;
00021         try {
00022                 srv = X::_narrow(cxt->resolve(ncName));
00023         } catch(const CosNaming::NamingContext::NotFound &exc) {
00024                 std::cerr << n << " not found: ";
00025                 switch(exc.why) {
00026                 case CosNaming::NamingContext::missing_node:
00027                         std::cerr << "Missing Node" << std::endl;
00028                 case CosNaming::NamingContext::not_context:
00029                         std::cerr << "Not Context" << std::endl;
00030                         break;
00031                 case CosNaming::NamingContext::not_object:
00032                         std::cerr << "Not Object" << std::endl;
00033                         break;
00034                 }
00035                 return (X_ptr)NULL;
00036         } catch(CosNaming::NamingContext::CannotProceed &exc) {
00037                 std::cerr << "Resolve " << n << " CannotProceed" << std::endl;
00038         } catch(CosNaming::NamingContext::AlreadyBound &exc) {
00039                 std::cerr << "Resolve " << n << " InvalidName" << std::endl;
00040         }
00041         return srv;
00042 }
00043 
00044 int main(int argc, char* argv[]) 
00045 {
00046         double timeStep = 0.002;  // (s)
00047     double controlTimeStep = 0.002;
00048         double EndTime = 13.0;
00049 
00050         string Model[2], ModelFname[2];
00051         double world_gravity = 9.8;  // default gravity acceleration [m/s^2]
00052     double statFric,slipFric;
00053         statFric = slipFric = 0.5;   // static/slip friction coefficient 
00054     double culling_thresh = 0.01;
00055 
00056         for (int i=1; i<argc; i++){
00057         if (strcmp("-ORBconfig", argv[i])==0 || strcmp("-ORBInitRef", argv[i])==0 ){
00058             argv[++i];  // skip ORB parameter
00059         }else if (strcmp("-url", argv[i])==0){
00060             for(int j = 0; j < 2; j++){
00061                 ModelFname[j] = argv[++i];
00062             }
00063                 }else if (strcmp("-timeStep", argv[i])==0){
00064                         timeStep = atof(argv[++i]);
00065         }
00066         }
00067 
00068     for(int j = 0; j < 2; j++){
00069         Model[j] = "file://" + ModelFname[j];
00070         cout << "Model: " << Model[j] << endl;
00071     }
00072 
00073     //================== Model Load ===============================
00074     BodyInfo_var floor = loadBodyInfo(Model[0].c_str(), argc, argv);
00075     if(!floor){
00076         cerr << "ModelLoader: " << Model[0] << " cannot be loaded" << endl;
00077         return 1;
00078     }
00079     BodyInfo_var body = loadBodyInfo(Model[1].c_str(), argc, argv);
00080     if(!body){
00081         cerr << "ModelLoader: " << Model[1] << " cannot be loaded" << endl;
00082         return 1;
00083     }
00084     
00085     //================== CORBA init ===============================
00086         // initialize CORBA 
00087         CORBA::ORB_var orb;
00088         orb = CORBA::ORB_init(argc, argv);
00089 
00090     // ROOT POA
00091         CORBA::Object_var poaObj = orb -> resolve_initial_references("RootPOA");
00092         PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(poaObj);
00093                 
00094         // get reference to POA manager
00095         PortableServer::POAManager_var manager = rootPOA -> the_POAManager();
00096 
00097     CosNaming::NamingContext_var cxt;
00098         {
00099                 CORBA::Object_var       nS = orb->resolve_initial_references("NameService");
00100         cxt = CosNaming::NamingContext::_narrow(nS);
00101         }
00102 
00103     ViewSimulator_var viewSimulator;
00104         viewSimulator = checkCorbaServer <ViewSimulator, ViewSimulator_var> ("ViewSimulator", cxt);
00105                 
00106         if (CORBA::is_nil(viewSimulator)) {
00107                 std::cerr << "viewSimulator not found" << std::endl;
00108         return 1;
00109         }
00110     viewSimulator->registerCharacter(floor->name(), floor);                         
00111     viewSimulator->registerCharacter(body->name(), body);
00112 
00113         //================= DynamicsSimulator setup ======================
00114         DynamicsSimulatorFactory_var dynamicsSimulatorFactory;
00115         dynamicsSimulatorFactory =
00116                 checkCorbaServer <DynamicsSimulatorFactory, DynamicsSimulatorFactory_var> ("DynamicsSimulatorFactory", cxt);
00117                 
00118         if (CORBA::is_nil(dynamicsSimulatorFactory)) {
00119                 std::cerr << "DynamicsSimulatorFactory not found" << std::endl;
00120         }
00121         DynamicsSimulator_var dynamicsSimulator = dynamicsSimulatorFactory->create();
00122 
00123         cout << "** Dynamics server setup ** " << endl;
00124     cout << "Character  :" << floor->name() << endl;
00125         dynamicsSimulator->registerCharacter(floor->name(), floor);                         
00126         cout << "Character  :" << body->name() << endl;
00127     dynamicsSimulator->registerCharacter(body->name(), body);
00128 
00129     dynamicsSimulator->init(timeStep, DynamicsSimulator::RUNGE_KUTTA, DynamicsSimulator::ENABLE_SENSOR);
00130     DblSequence3 g;
00131     g.length(3);
00132     g[0] = 0.0;
00133     g[1] = 0.0;
00134     g[2] = world_gravity;
00135     dynamicsSimulator->setGVector(g);
00136 
00137     // initial position and orientation
00138     Vector3  waist_p;
00139     Matrix33 waist_R;
00140     waist_p << 0, 0, 0.7135;
00141         waist_R = Matrix33::Identity();
00142 
00143         DblSequence trans;
00144         trans.length(12);
00145         for(int i=0; i<3; i++) trans[i]   = waist_p(i);
00146         for(int i=0; i<3; i++){
00147                 for(int j=0; j<3; j++) trans[3+3*i+j] = waist_R(i,j);
00148         }
00149         dynamicsSimulator->setCharacterLinkData( body->name(), "WAIST", DynamicsSimulator::ABS_TRANSFORM, trans );
00150     DblSequence angle;
00151     angle.length(29);
00152     angle[0] = 0.0;         angle[1] = -0.0360373;  angle[2] = 0.0;         angle[3] = 0.0785047;
00153     angle[4] = -0.0424675;  angle[5] = 0.0;         angle[6] = 0.174533;    angle[7] = -0.00349066;
00154     angle[8] = 0.0;         angle[9] = -1.5708;     angle[10] = 0.0;        angle[11] = 0.0;
00155     angle[12] = 0.0;        angle[13] = 0.0;        angle[14] = -0.0360373; angle[15] = 0.0;
00156     angle[16] = 0.0785047;  angle[17] = -0.0424675; angle[18] = 0.0;        angle[19] = 0.174533;
00157     angle[20] = -0.00349066;angle[21] = 0.0;        angle[22] = -1.5708;    angle[23] = 0.0;
00158     angle[24] = 0.0;        angle[25] = 0.0;        angle[26] = 0.0;        angle[27] = 0.0;
00159     angle[28] = 0.0;
00160     dynamicsSimulator->setCharacterAllLinkData( body->name(), DynamicsSimulator::JOINT_VALUE, angle );
00161     dynamicsSimulator->calcWorldForwardKinematics();
00162 
00163     DblSequence6 K, C;    // spring-damper parameters are not used now
00164     K.length(0);
00165     C.length(0);
00166     dynamicsSimulator->registerCollisionCheckPair(floor->name(),"", body->name() ,"",
00167                                                             statFric,slipFric,K,C,culling_thresh,0.0);
00168     dynamicsSimulator->initSimulation();
00169         
00170     // ==================  Controller setup ==========================
00171     Controller_var controller;
00172     controller = checkCorbaServer <Controller, Controller_var> ("SamplePDController", cxt);
00173 
00174     if (CORBA::is_nil(controller)) {
00175         std::cerr << "Controller not found" << std::endl;
00176     }
00177 
00178     controller->setModelName(body->name());
00179     controller->setDynamicsSimulator(dynamicsSimulator);
00180     controller->initialize();
00181     controller->setTimeStep(controlTimeStep);
00182     controller->start();
00183 
00184     // ==================  main loop   ======================
00185     WorldState_var state;
00186         int i=0;
00187     int j = 0;
00188         double time=0.0;
00189     double controlTime=0.0;
00190         while ( 1 ) {
00191         bool control=false;
00192         if(controlTime <= time){
00193             control=true;
00194             j++;
00195         }
00196 
00197         if(control)
00198             controller->input();
00199 
00200         i++;
00201         time = timeStep * i;
00202                 controlTime = controlTimeStep * j;
00203 
00204         if(control)
00205             controller->control();
00206                
00207         // ================== simulate one step ==============
00208                 dynamicsSimulator->stepSimulation();                                        
00209                
00210         // ================== viewer update ====================
00211         try {
00212                     dynamicsSimulator -> getWorldState( state );
00213             if((int)(time*500)%30==0){
00214                             viewSimulator->updateScene( state );
00215                 CameraSequence_var cameras;
00216                 viewSimulator->getCameraSequence(cameras);
00217                 for(int k=0; k<cameras->length(); k++){
00218                     ImageData_var imageData = cameras[k]->getImageData();
00219                     std::cout << "camera(" << k << ") ";
00220                     for(int l=0; l<imageData->floatData.length(); l+=100)
00221                         std::cout <<imageData->floatData[l] << " " ;
00222                     std::cout << std::endl;
00223                 }
00224             }
00225                 } catch (CORBA::SystemException& ex) {
00226                     return 1;
00227         }
00228         
00229         // ===================== get robot status ===================
00230                 DblSequence_var waist_pR;  // position and attitude
00231                 DblSequence_var waist_vw;  // linear and angular velocities
00232                 dynamicsSimulator->getCharacterLinkData(body->name(), "WAIST", DynamicsSimulator::ABS_TRANSFORM, waist_pR);
00233                 dynamicsSimulator->getCharacterLinkData(body->name(), "WAIST", DynamicsSimulator::ABS_VELOCITY,  waist_vw);
00234 
00235         if(control)
00236             controller->output();
00237 
00238                 if( time > EndTime ) break;
00239 
00240         }
00241 
00242         dynamicsSimulator->destroy();
00243 
00244         return 0;
00245 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:57