4 #include <hrpCorba/DynamicsSimulator.hh> 5 #include <hrpCorba/Controller.hh> 13 template <
typename X,
typename X_ptr>
16 CosNaming::Name ncName;
18 ncName[0].id = CORBA::string_dup(n.c_str());
19 ncName[0].kind = CORBA::string_dup(
"");
22 srv = X::_narrow(cxt->resolve(ncName));
24 std::cerr << n <<
" not found: ";
26 case CosNaming::NamingContext::missing_node:
27 std::cerr <<
"Missing Node" << std::endl;
28 case CosNaming::NamingContext::not_context:
29 std::cerr <<
"Not Context" << std::endl;
31 case CosNaming::NamingContext::not_object:
32 std::cerr <<
"Not Object" << std::endl;
36 }
catch(CosNaming::NamingContext::CannotProceed &exc) {
37 std::cerr <<
"Resolve " << n <<
" CannotProceed" << std::endl;
38 }
catch(CosNaming::NamingContext::AlreadyBound &exc) {
39 std::cerr <<
"Resolve " << n <<
" InvalidName" << std::endl;
44 int main(
int argc,
char* argv[])
46 double timeStep = 0.001;
47 double controlTimeStep = 0.002;
48 double EndTime = 13.0;
50 string Model[2], ModelFname[2];
51 double world_gravity = 9.8;
52 double statFric,slipFric;
53 statFric = slipFric = 0.5;
54 double culling_thresh = 0.01;
56 for (
int i=1;
i<argc;
i++){
57 if (strcmp(
"-ORBconfig", argv[
i])==0 || strcmp(
"-ORBInitRef", argv[i])==0 ){
59 }
else if (strcmp(
"-url", argv[i])==0){
60 for(
int j = 0;
j < 2;
j++){
61 ModelFname[
j] = argv[++
i];
63 }
else if (strcmp(
"-timeStep", argv[i])==0){
64 timeStep = atof(argv[++i]);
68 for(
int j = 0;
j < 2;
j++){
69 Model[
j] =
"file://" + ModelFname[
j];
70 cout <<
"Model: " << Model[
j] << endl;
74 BodyInfo_var floor =
loadBodyInfo(Model[0].c_str(), argc, argv);
76 cerr <<
"ModelLoader: " << Model[0] <<
" cannot be loaded" << endl;
79 BodyInfo_var body =
loadBodyInfo(Model[1].c_str(), argc, argv);
81 cerr <<
"ModelLoader: " << Model[1] <<
" cannot be loaded" << endl;
88 orb = CORBA::ORB_init(argc, argv);
91 CORBA::Object_var poaObj = orb -> resolve_initial_references(
"RootPOA");
92 PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(poaObj);
95 PortableServer::POAManager_var
manager = rootPOA -> the_POAManager();
97 CosNaming::NamingContext_var cxt;
99 CORBA::Object_var nS = orb->resolve_initial_references(
"NameService");
100 cxt = CosNaming::NamingContext::_narrow(nS);
106 if (CORBA::is_nil( olv )) {
107 std::cerr <<
"OnlineViewer not found" << std::endl;
111 olv->load(floor->name(), Model[0].c_str());
112 olv->load(body->name(), Model[1].c_str());
114 }
catch (CORBA::SystemException& ex) {
115 cerr <<
"Failed to connect GrxUI." << endl;
120 DynamicsSimulatorFactory_var dynamicsSimulatorFactory;
121 dynamicsSimulatorFactory =
122 checkCorbaServer <DynamicsSimulatorFactory, DynamicsSimulatorFactory_var> (
"DynamicsSimulatorFactory", cxt);
124 if (CORBA::is_nil(dynamicsSimulatorFactory)) {
125 std::cerr <<
"DynamicsSimulatorFactory not found" << std::endl;
127 DynamicsSimulator_var dynamicsSimulator = dynamicsSimulatorFactory->create();
129 cout <<
"** Dynamics server setup ** " << endl;
130 cout <<
"Character :" << floor->name() << endl;
131 dynamicsSimulator->registerCharacter(floor->name(), floor);
132 cout <<
"Character :" << body->name() << endl;
133 dynamicsSimulator->registerCharacter(body->name(), body);
135 dynamicsSimulator->init(timeStep, DynamicsSimulator::RUNGE_KUTTA, DynamicsSimulator::ENABLE_SENSOR);
140 g[2] = world_gravity;
141 dynamicsSimulator->setGVector(g);
146 waist_p << 0, 0, 0.7135;
147 waist_R = Matrix33::Identity();
151 for(
int i=0;
i<3;
i++) trans[
i] = waist_p(
i);
152 for(
int i=0;
i<3;
i++){
153 for(
int j=0;
j<3;
j++) trans[3+3*
i+
j] = waist_R(
i,
j);
158 angle[0] = 0.0; angle[1] = -0.0360373; angle[2] = 0.0; angle[3] = 0.0785047;
159 angle[4] = -0.0424675; angle[5] = 0.0; angle[6] = 0.174533; angle[7] = -0.00349066;
160 angle[8] = 0.0; angle[9] = -1.5708; angle[10] = 0.0; angle[11] = 0.0;
161 angle[12] = 0.0; angle[13] = 0.0; angle[14] = -0.0360373; angle[15] = 0.0;
162 angle[16] = 0.0785047; angle[17] = -0.0424675; angle[18] = 0.0; angle[19] = 0.174533;
163 angle[20] = -0.00349066;angle[21] = 0.0; angle[22] = -1.5708; angle[23] = 0.0;
164 angle[24] = 0.0; angle[25] = 0.0; angle[26] = 0.0; angle[27] = 0.0;
167 dynamicsSimulator->calcWorldForwardKinematics();
172 dynamicsSimulator->registerCollisionCheckPair(floor->name(),
"", body->name() ,
"",
173 statFric,slipFric,K,C,culling_thresh,0.0);
174 dynamicsSimulator->initSimulation();
177 Controller_var controller;
178 controller = checkCorbaServer <Controller, Controller_var> (
"SamplePDController", cxt);
180 if (CORBA::is_nil(controller)) {
181 std::cerr <<
"Controller not found" << std::endl;
184 controller->setModelName(body->name());
185 controller->setDynamicsSimulator(dynamicsSimulator);
186 controller->initialize();
187 controller->setTimeStep(controlTimeStep);
191 static ofstream log_file;
192 log_file.open(
"samplePD.log");
195 WorldState_var
state;
199 double controlTime=0.0;
202 if(controlTime <= time){
212 controlTime = controlTimeStep * j;
215 controller->control();
218 dynamicsSimulator->stepSimulation();
223 olv->update( state );
224 }
catch (CORBA::SystemException& ex) {
229 DblSequence_var waist_pR;
230 DblSequence_var waist_vw;
235 log_file << time <<
" ";
236 log_file << waist_vw[2] <<
" ";
240 controller->output();
242 if( time > EndTime )
break;
248 dynamicsSimulator->destroy();
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
X_ptr checkCorbaServer(std::string n, CosNaming::NamingContext_var &cxt)
png_infop png_bytep * trans
void getWorldState(WorldState &state, WorldBase &world)
def j(str, encoding="cp932")
int main(int argc, char *argv[])
HRP_UTIL_EXPORT OpenHRP::OnlineViewer_var getOnlineViewer(int argc, char **argv)