rtc/Simulator/Simulator.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <rtm/CorbaNaming.h>
11 
12 #include "Simulator.h"
13 #include <hrpCorba/OpenHRPCommon.hh>
14 #include <hrpModel/ModelLoaderUtil.h>
15 #include <hrpModel/OnlineViewerUtil.h>
16 #include <hrpModel/Link.h>
17 #include "hrpsys/util/Project.h"
18 
19 // Module specification
20 // <rtc-template block="module_spec">
21 static const char* component_spec[] =
22 {
23  "implementation_id", "Simulator",
24  "type_name", "Simulator",
25  "description", "dynamics simulator component",
26  "version", HRPSYS_PACKAGE_VERSION,
27  "vendor", "AIST",
28  "category", "example",
29  "activity_type", "DataFlowComponent",
30  "max_instance", "10",
31  "language", "C++",
32  "lang_type", "compile",
33  // Configuration variables
34  "conf.default.project", "",
35  "conf.default.kinematics_only", "0",
36  "conf.default.useOLV", "0",
37  ""
38 };
39 // </rtc-template>
40 
42  : RTC::DataFlowComponentBase(manager),
43  // <rtc-template block="initializer">
44  m_sceneStateOut("state", m_sceneState),
45  // </rtc-template>
46  dummy(0)
47 {
48 }
49 
51 {
52 }
53 
54 
55 
56 RTC::ReturnCode_t Simulator::onInitialize()
57 {
58  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
59  // <rtc-template block="bind_config">
60  // Bind variables and configuration variable
61  bindParameter("project", m_project, "");
62  bindParameter("kinematics_only", m_kinematicsOnly, "0");
63  bindParameter("useOLV", m_useOLV, "0");
64 
65  // </rtc-template>
66 
67  // Registration: InPort/OutPort/Service
68  // <rtc-template block="registration">
69  // Set InPort buffers
70 
71  // Set OutPort buffer
72  addOutPort("state", m_sceneStateOut);
73 
74  // Set service provider to Ports
75 
76  // Set service consumers to Ports
77 
78  // Set CORBA Service Ports
79 
80  // </rtc-template>
81 
82  //RTC::Properties& prop = getProperties();
83 
84  return RTC::RTC_OK;
85 }
86 
87 
88 
89 /*
90  RTC::ReturnCode_t Simulator::onFinalize()
91  {
92  return RTC::RTC_OK;
93  }
94 */
95 
96 /*
97  RTC::ReturnCode_t Simulator::onStartup(RTC::UniqueId ec_id)
98  {
99  return RTC::RTC_OK;
100  }
101 */
102 
103 /*
104  RTC::ReturnCode_t Simulator::onShutdown(RTC::UniqueId ec_id)
105  {
106  return RTC::RTC_OK;
107  }
108 */
109 
110 RTC::ReturnCode_t Simulator::onActivated(RTC::UniqueId ec_id)
111 {
112  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
113 
114  if (m_project == ""){
115  std::cerr << "Project file is not specified." << std::endl;
116  return RTC::RTC_ERROR;
117  }
118 
119  Project prj;
120  if (!prj.parse(m_project)) return RTC::RTC_ERROR;
121 
122  if ( m_kinematicsOnly == false ) {
124  }
125  std::cout << "kinematics_only : " << m_kinematicsOnly << std::endl;
126 
129  m_world.setCurrentTime(0.0);
131  std::cout << "time step = " << prj.timeStep() << std::endl;
132  if(prj.isEuler()){
134  } else {
136  }
137 
138  RTC::Manager& rtcManager = RTC::Manager::instance();
139  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
140  int comPos = nameServer.find(",");
141  if (comPos < 0){
142  comPos = nameServer.length();
143  }
144  nameServer = nameServer.substr(0, comPos);
145  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
146 
147  std::cout << "m_useOLV:" << m_useOLV << std::endl;
148  if (m_useOLV){
149  m_olv = hrp::getOnlineViewer(CosNaming::NamingContext::_duplicate(naming.getRootContext()));
150  }
151 
152  for (std::map<std::string, ModelItem>::iterator it=prj.models().begin();
153  it != prj.models().end(); it++){
154  RTCBodyPtr body(new RTCBody());
155  if (!loadBodyFromModelLoader(body, it->second.url.c_str(),
156  CosNaming::NamingContext::_duplicate(naming.getRootContext()),
157  true)){
158  std::cerr << "failed to load model[" << it->second.url << "]" << std::endl;
159  }else{
160  body->setName(it->first);
161  for (std::map<std::string, JointItem>::iterator it2=it->second.joint.begin();
162  it2 != it->second.joint.end(); it2++){
163  hrp::Link *link = body->link(it2->first);
164  if (link) link->isHighGainMode = it2->second.isHighGain;
165  }
166  m_world.addBody(body);
167  body->createPorts(this);
168  m_bodies.push_back(body);
169  }
170  if (m_useOLV){
171  m_olv->load(it->first.c_str(), it->second.url.c_str());
172  }
173  }
174  if (m_useOLV){
175  m_olv->clearLog();
177  }
178 
179 
180  for (unsigned int i=0; i<prj.collisionPairs().size(); i++){
181  const CollisionPairItem &cpi = prj.collisionPairs()[i];
182  int bodyIndex1 = m_world.bodyIndex(cpi.objectName1);
183  int bodyIndex2 = m_world.bodyIndex(cpi.objectName2);
184 
185  if(bodyIndex1 >= 0 && bodyIndex2 >= 0){
186  hrp::BodyPtr bodyPtr1 = m_world.body(bodyIndex1);
187  hrp::BodyPtr bodyPtr2 = m_world.body(bodyIndex2);
188 
189  std::vector<hrp::Link*> links1;
190  if(cpi.jointName1.empty()){
191  const hrp::LinkTraverse& traverse = bodyPtr1->linkTraverse();
192  links1.resize(traverse.numLinks());
193  std::copy(traverse.begin(), traverse.end(), links1.begin());
194  } else {
195  links1.push_back(bodyPtr1->link(cpi.jointName1));
196  }
197 
198  std::vector<hrp::Link*> links2;
199  if(cpi.jointName2.empty()){
200  const hrp::LinkTraverse& traverse = bodyPtr2->linkTraverse();
201  links2.resize(traverse.numLinks());
202  std::copy(traverse.begin(), traverse.end(), links2.begin());
203  } else {
204  links2.push_back(bodyPtr2->link(cpi.jointName2));
205  }
206 
207  for(size_t j=0; j < links1.size(); ++j){
208  for(size_t k=0; k < links2.size(); ++k){
209  hrp::Link* link1 = links1[j];
210  hrp::Link* link2 = links2[k];
211 
212  if(link1 && link2 && link1 != link2){
214  (bodyIndex1, link1, bodyIndex2, link2,
215  cpi.staticFriction, cpi.slidingFriction, 0.01, 0.0, 0.0);
216  }
217  }
218  }
219  }
220  }
221 
222  m_world.enableSensors(false);
223 
224  int nBodies = m_world.numBodies();
225  for(int i=0; i < nBodies; ++i){
226  hrp::BodyPtr bodyPtr = m_world.body(i);
227  bodyPtr->initializeConfiguration();
228  }
229 
230  for (std::map<std::string, ModelItem>::iterator it=prj.models().begin();
231  it != prj.models().end(); it++){
232  hrp::BodyPtr body = m_world.body(it->first);
233  for (std::map<std::string, JointItem>::iterator it2=it->second.joint.begin();
234  it2 != it->second.joint.end(); it2++){
235  hrp::Link *link = body->link(it2->first);
236  if (!link) continue;
237  if (link->isRoot()){
238  link->p = it2->second.translation;
239  link->setAttitude(it2->second.rotation);
240  }else{
241  link->q = it2->second.angle;
242  }
243  }
244  body->calcForwardKinematics();
245  }
246 
250 
251  m_sceneState.states.length(m_bodies.size());
252  for (unsigned int i=0; i<m_bodies.size(); i++){
253  m_sceneState.states[i].name = CORBA::string_dup(m_bodies[i]->name().c_str());
254  m_sceneState.states[i].q.length(m_bodies[i]->numJoints());
255  }
256 
257 
258  return RTC::RTC_OK;
259 }
260 
261 RTC::ReturnCode_t Simulator::onDeactivated(RTC::UniqueId ec_id)
262 {
263  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
264  return RTC::RTC_OK;
265 }
266 
267 RTC::ReturnCode_t Simulator::onExecute(RTC::UniqueId ec_id)
268 {
269  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
270  // output current state
272  for (unsigned int i=0; i<m_bodies.size(); i++){
273  m_bodies[i]->output(m_sceneState.states[i]);
274  }
275  m_sceneStateOut.write();
276 
277  // input command
278  for (unsigned int i=0; i<m_bodies.size(); i++) m_bodies[i]->input();
279 
280  if (m_kinematicsOnly){
281  for(unsigned int i=0; i < m_world.numBodies(); ++i){
282  m_world.body(i)->calcForwardKinematics();
283  }
285  }else{
287 
288  OpenHRP::CollisionSequence collision;
289  m_world.calcNextState(collision);
290  }
291 
292  if (m_useOLV){
294  m_olv->update( m_state );
295  }
296  return RTC::RTC_OK;
297 }
298 
299 /*
300  RTC::ReturnCode_t Simulator::onAborting(RTC::UniqueId ec_id)
301  {
302  return RTC::RTC_OK;
303  }
304 */
305 
306 /*
307  RTC::ReturnCode_t Simulator::onError(RTC::UniqueId ec_id)
308  {
309  return RTC::RTC_OK;
310  }
311 */
312 
313 /*
314  RTC::ReturnCode_t Simulator::onReset(RTC::UniqueId ec_id)
315  {
316  return RTC::RTC_OK;
317  }
318 */
319 
320 /*
321  RTC::ReturnCode_t Simulator::onStateUpdate(RTC::UniqueId ec_id)
322  {
323  return RTC::RTC_OK;
324  }
325 */
326 
327 /*
328  RTC::ReturnCode_t Simulator::onRateChanged(RTC::UniqueId ec_id)
329  {
330  return RTC::RTC_OK;
331  }
332 */
333 
334 
335 
336 extern "C"
337 {
338 
340  {
342  manager->registerFactory(profile,
343  RTC::Create<Simulator>,
344  RTC::Delete<Simulator>);
345  }
346 
347 };
348 
349 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual ~Simulator()
Destructor.
int bodyIndex(const std::string &name)
double timeStep(void) const
void setEulerMethod()
std::vector< Link * >::const_iterator end() const
bool parse(const std::string &filename)
Definition: Project.cpp:32
int addBody(BodyPtr body)
void useBuiltinCollisionDetector(bool on)
virtual void initialize()
double slidingFriction
Definition: Project.h:15
png_infop png_charpp name
void setCurrentTime(double tm)
Simulator(RTC::Manager *manager)
Constructor.
CORBA::ORB_ptr getORB()
OpenHRP::SceneState m_sceneState
std::string objectName1
Definition: Project.h:11
png_uint_32 i
std::vector< Link * >::const_iterator begin() const
static Manager & instance()
void setRungeKuttaMethod()
std::string jointName2
Definition: Project.h:14
bool addOutPort(const char *name, OutPortBase &outport)
void getWorldState(WorldState &state, WorldBase &world)
void enableConstraintForceOutput(bool on)
OpenHRP::WorldState m_state
BodyPtr body(int index)
void initWorldState(WorldState &state, WorldBase &world)
coil::Properties & getConfig()
unsigned int numLinks() const
ExecutionContextHandle_t UniqueId
void SimulatorInit(RTC::Manager *manager)
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
def j(str, encoding="cp932")
std::vector< RTCBodyPtr > m_bodies
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
double staticFriction
Definition: Project.h:16
std::map< std::string, ModelItem > & models()
Definition: Project.h:114
bool addCollisionCheckLinkPair(int bodyIndex1, Link *link1, int bodyIndex2, Link *link2, double muStatic, double muDynamic, double culling_thresh, double restitution, double epsilon)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
OutPort< OpenHRP::SceneState > m_sceneStateOut
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
std::string jointName1
Definition: Project.h:13
bool kinematicsOnly()
Definition: Project.h:111
virtual void calcNextState(OpenHRP::CollisionSequence &corbaCollisionSequence)
naming
std::string m_project
std::string objectName2
Definition: Project.h:12
TConstraintForceSolver constraintForceSolver
bool isEuler()
Definition: Project.h:110
virtual RTC::ReturnCode_t onInitialize()
static const char * component_spec[]
void setTimeStep(double dt)
hrp::World< hrp::ConstraintForceSolver > m_world
OpenHRP::OnlineViewer_var m_olv
HRP_UTIL_EXPORT OpenHRP::OnlineViewer_var getOnlineViewer(int argc, char **argv)
double currentTime(void) const
double timeStep()
Definition: Project.h:105
void clearBodies()
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
unsigned int numBodies()
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void enableSensors(bool on)
std::vector< CollisionPairItem > & collisionPairs()
Definition: Project.h:115


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