4 #include <hrpCorba/DynamicsSimulator.hh> 5 #include <hrpCorba/Controller.hh> 6 #include <hrpCorba/ViewSimulator.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.002;
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);
103 ViewSimulator_var viewSimulator;
104 viewSimulator = checkCorbaServer <ViewSimulator, ViewSimulator_var> (
"ViewSimulator", cxt);
106 if (CORBA::is_nil(viewSimulator)) {
107 std::cerr <<
"viewSimulator not found" << std::endl;
110 viewSimulator->registerCharacter(floor->name(), floor);
111 viewSimulator->registerCharacter(body->name(), body);
114 DynamicsSimulatorFactory_var dynamicsSimulatorFactory;
115 dynamicsSimulatorFactory =
116 checkCorbaServer <DynamicsSimulatorFactory, DynamicsSimulatorFactory_var> (
"DynamicsSimulatorFactory", cxt);
118 if (CORBA::is_nil(dynamicsSimulatorFactory)) {
119 std::cerr <<
"DynamicsSimulatorFactory not found" << std::endl;
121 DynamicsSimulator_var dynamicsSimulator = dynamicsSimulatorFactory->create();
123 cout <<
"** Dynamics server setup ** " << endl;
124 cout <<
"Character :" << floor->name() << endl;
125 dynamicsSimulator->registerCharacter(floor->name(), floor);
126 cout <<
"Character :" << body->name() << endl;
127 dynamicsSimulator->registerCharacter(body->name(), body);
129 dynamicsSimulator->init(timeStep, DynamicsSimulator::RUNGE_KUTTA, DynamicsSimulator::ENABLE_SENSOR);
134 g[2] = world_gravity;
135 dynamicsSimulator->setGVector(g);
140 waist_p << 0, 0, 0.7135;
141 waist_R = Matrix33::Identity();
145 for(
int i=0;
i<3;
i++) trans[
i] = waist_p(
i);
146 for(
int i=0;
i<3;
i++){
147 for(
int j=0;
j<3;
j++) trans[3+3*
i+
j] = waist_R(
i,
j);
152 angle[0] = 0.0; angle[1] = -0.0360373; angle[2] = 0.0; angle[3] = 0.0785047;
153 angle[4] = -0.0424675; angle[5] = 0.0; angle[6] = 0.174533; angle[7] = -0.00349066;
154 angle[8] = 0.0; angle[9] = -1.5708; angle[10] = 0.0; angle[11] = 0.0;
155 angle[12] = 0.0; angle[13] = 0.0; angle[14] = -0.0360373; angle[15] = 0.0;
156 angle[16] = 0.0785047; angle[17] = -0.0424675; angle[18] = 0.0; angle[19] = 0.174533;
157 angle[20] = -0.00349066;angle[21] = 0.0; angle[22] = -1.5708; angle[23] = 0.0;
158 angle[24] = 0.0; angle[25] = 0.0; angle[26] = 0.0; angle[27] = 0.0;
161 dynamicsSimulator->calcWorldForwardKinematics();
166 dynamicsSimulator->registerCollisionCheckPair(floor->name(),
"", body->name() ,
"",
167 statFric,slipFric,K,C,culling_thresh,0.0);
168 dynamicsSimulator->initSimulation();
171 Controller_var controller;
172 controller = checkCorbaServer <Controller, Controller_var> (
"SamplePDController", cxt);
174 if (CORBA::is_nil(controller)) {
175 std::cerr <<
"Controller not found" << std::endl;
178 controller->setModelName(body->name());
179 controller->setDynamicsSimulator(dynamicsSimulator);
180 controller->initialize();
181 controller->setTimeStep(controlTimeStep);
185 WorldState_var
state;
189 double controlTime=0.0;
192 if(controlTime <= time){
202 controlTime = controlTimeStep * j;
205 controller->control();
208 dynamicsSimulator->stepSimulation();
213 if((
int)(time*500)%30==0){
214 viewSimulator->updateScene( state );
215 CameraSequence_var cameras;
216 viewSimulator->getCameraSequence(cameras);
217 for(
int k=0; k<cameras->length(); k++){
218 ImageData_var imageData = cameras[k]->getImageData();
219 std::cout <<
"camera(" << k <<
") ";
220 for(
int l=0; l<imageData->floatData.length(); l+=100)
221 std::cout <<imageData->floatData[l] <<
" " ;
222 std::cout << std::endl;
225 }
catch (CORBA::SystemException& ex) {
230 DblSequence_var waist_pR;
231 DblSequence_var waist_vw;
236 controller->output();
238 if( time > EndTime )
break;
242 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[])