StateHolder.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "StateHolder.h"
11 #include <rtm/CorbaNaming.h>
12 #include <hrpUtil/Eigen3d.h>
13 #include <hrpModel/ModelLoaderUtil.h>
14 
15 // Module specification
16 // <rtc-template block="module_spec">
17 static const char* stateholder_spec[] =
18  {
19  "implementation_id", "StateHolder",
20  "type_name", "StateHolder",
21  "description", "state holder",
22  "version", HRPSYS_PACKAGE_VERSION,
23  "vendor", "AIST",
24  "category", "example",
25  "activity_type", "DataFlowComponent",
26  "max_instance", "10",
27  "language", "C++",
28  "lang_type", "compile",
29  // Configuration variables
30 
31  ""
32  };
33 // </rtc-template>
34 
36  : RTC::DataFlowComponentBase(manager),
37  // <rtc-template block="initializer">
38  m_currentQIn("currentQIn", m_currentQ),
39  m_qIn("qIn", m_q),
40  m_tqIn("tqIn", m_tq),
41  m_basePosIn("basePosIn", m_basePos),
42  m_baseRpyIn("baseRpyIn", m_baseRpy),
43  m_zmpIn("zmpIn", m_zmp),
44  m_optionalDataIn("optionalDataIn", m_optionalData),
45  m_qOut("qOut", m_q),
46  m_tqOut("tqOut", m_tq),
47  m_basePosOut("basePosOut", m_basePos),
48  m_baseRpyOut("baseRpyOut", m_baseRpy),
49  m_baseTformOut("baseTformOut", m_baseTform),
50  m_basePoseOut("basePoseOut", m_basePose),
51  m_zmpOut("zmpOut", m_zmp),
52  m_optionalDataOut("optionalDataOut", m_optionalData),
53  m_StateHolderServicePort("StateHolderService"),
54  m_TimeKeeperServicePort("TimeKeeperService"),
55  // </rtc-template>
56  m_timeCount(0),
57  dummy(0)
58 {
59 
62  m_requestGoActual = false;
63 
64  sem_init(&m_waitSem, 0, 0);
65  sem_init(&m_timeSem, 0, 0);
66 }
67 
69 {
70 }
71 
72 
73 
74 RTC::ReturnCode_t StateHolder::onInitialize()
75 {
76  std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
77  // <rtc-template block="bind_config">
78  // Bind variables and configuration variable
79 
80  // </rtc-template>
81 
82  // Registration: InPort/OutPort/Service
83  // <rtc-template block="registration">
84  // Set InPort buffers
85  addInPort("currentQIn", m_currentQIn);
86  addInPort("qIn", m_qIn);
87  addInPort("tqIn", m_tqIn);
88  addInPort("basePosIn", m_basePosIn);
89  addInPort("baseRpyIn", m_baseRpyIn);
90  addInPort("zmpIn", m_zmpIn);
91  addInPort("optionalDataIn", m_optionalDataIn);
92 
93  // Set OutPort buffer
94  addOutPort("qOut", m_qOut);
95  addOutPort("tqOut", m_tqOut);
96  addOutPort("basePosOut", m_basePosOut);
97  addOutPort("baseRpyOut", m_baseRpyOut);
98  addOutPort("baseTformOut", m_baseTformOut);
99  addOutPort("basePoseOut", m_basePoseOut);
100  addOutPort("zmpOut", m_zmpOut);
101  addOutPort("optionalDataOut", m_optionalDataOut);
102 
103  // Set service provider to Ports
104  m_StateHolderServicePort.registerProvider("service0", "StateHolderService", m_service0);
105  m_TimeKeeperServicePort.registerProvider("service1", "TimeKeeperService", m_service1);
106 
107  // Set service consumers to Ports
108 
109  // Set CORBA Service Ports
112 
113  // </rtc-template>
114 
116  coil::stringTo(m_dt, prop["dt"].c_str());
117  std::cout << "StateHolder: dt = " << m_dt << std::endl;
118  RTC::Manager& rtcManager = RTC::Manager::instance();
119  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
120  int comPos = nameServer.find(",");
121  if (comPos < 0){
122  comPos = nameServer.length();
123  }
124  nameServer = nameServer.substr(0, comPos);
125  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
126  OpenHRP::BodyInfo_var binfo;
127  binfo = hrp::loadBodyInfo(prop["model"].c_str(),
128  CosNaming::NamingContext::_duplicate(naming.getRootContext()));
129  OpenHRP::LinkInfoSequence_var lis = binfo->links();
130  const OpenHRP::LinkInfo& li = lis[0];
131  hrp::Vector3 p, axis;
132  p << li.translation[0], li.translation[1], li.translation[2];
133  axis << li.rotation[0], li.rotation[1], li.rotation[2];
134  hrp::Matrix33 R = hrp::rodrigues(axis, li.rotation[3]);
135  hrp::Vector3 rpy = hrp::rpyFromRot(R);
136 
137  m_baseTform.data.length(12);
138  double *T = m_baseTform.data.get_buffer();
139  T[0] = R(0,0); T[1] = R(0,1); T[ 2] = R(0,2); T[ 3] = p[0];
140  T[4] = R(0,0); T[5] = R(0,1); T[ 6] = R(0,2); T[ 7] = p[1];
141  T[8] = R(0,0); T[9] = R(0,1); T[10] = R(0,2); T[11] = p[2];
142  m_basePos.data.x = m_basePose.data.position.x = p[0];
143  m_basePos.data.y = m_basePose.data.position.y = p[1];
144  m_basePos.data.z = m_basePose.data.position.z = p[2];
145  m_baseRpy.data.r = m_basePose.data.orientation.r = rpy[0];
146  m_baseRpy.data.p = m_basePose.data.orientation.p = rpy[1];
147  m_baseRpy.data.y = m_basePose.data.orientation.y = rpy[2];
148  m_zmp.data.x = m_zmp.data.y = m_zmp.data.z = 0.0;
149 
150  // Setting for wrench data ports (real + virtual)
151  std::vector<std::string> fsensor_names;
152  // find names for real force sensors
153  for ( unsigned int k = 0; k < lis->length(); k++ ) {
154  OpenHRP::SensorInfoSequence& sensors = lis[k].sensors;
155  for ( unsigned int l = 0; l < sensors.length(); l++ ) {
156  if ( std::string(sensors[l].type) == "Force" ) {
157  fsensor_names.push_back(std::string(sensors[l].name));
158  }
159  }
160  }
161  int npforce = fsensor_names.size();
162  // find names for virtual force sensors
163  coil::vstring virtual_force_sensor = coil::split(prop["virtual_force_sensor"], ",");
164  unsigned int nvforce = virtual_force_sensor.size()/10;
165  for (unsigned int i=0; i<nvforce; i++){
166  fsensor_names.push_back(virtual_force_sensor[i*10+0]);
167  }
168  // add ports for all force sensors
169  unsigned int nforce = npforce + nvforce;
170  m_wrenches.resize(nforce);
171  m_wrenchesIn.resize(nforce);
172  m_wrenchesOut.resize(nforce);
173  for (unsigned int i=0; i<nforce; i++){
174  m_wrenchesIn[i] = new InPort<TimedDoubleSeq>(std::string(fsensor_names[i]+"In").c_str(), m_wrenches[i]);
175  m_wrenchesOut[i] = new OutPort<TimedDoubleSeq>(std::string(fsensor_names[i]+"Out").c_str(), m_wrenches[i]);
176  m_wrenches[i].data.length(6);
177  m_wrenches[i].data[0] = m_wrenches[i].data[1] = m_wrenches[i].data[2] = 0.0;
178  m_wrenches[i].data[3] = m_wrenches[i].data[4] = m_wrenches[i].data[5] = 0.0;
179  registerInPort(std::string(fsensor_names[i]+"In").c_str(), *m_wrenchesIn[i]);
180  registerOutPort(std::string(fsensor_names[i]+"Out").c_str(), *m_wrenchesOut[i]);
181  }
182 
183 
184  return RTC::RTC_OK;
185 }
186 
187 
188 
189 /*
190 RTC::ReturnCode_t StateHolder::onFinalize()
191 {
192  return RTC::RTC_OK;
193 }
194 */
195 
196 /*
197 RTC::ReturnCode_t StateHolder::onStartup(RTC::UniqueId ec_id)
198 {
199  return RTC::RTC_OK;
200 }
201 */
202 
203 /*
204 RTC::ReturnCode_t StateHolder::onShutdown(RTC::UniqueId ec_id)
205 {
206  return RTC::RTC_OK;
207 }
208 */
209 
210 /*
211 RTC::ReturnCode_t StateHolder::onActivated(RTC::UniqueId ec_id)
212 {
213  return RTC::RTC_OK;
214 }
215 */
216 
217 /*
218 RTC::ReturnCode_t StateHolder::onDeactivated(RTC::UniqueId ec_id)
219 {
220  return RTC::RTC_OK;
221 }
222 */
223 
224 RTC::ReturnCode_t StateHolder::onExecute(RTC::UniqueId ec_id)
225 {
226  //std::cout << "StateHolder::onExecute(" << ec_id << ")" << std::endl;
227  RTC::Time tm;
228 
229  if (m_currentQIn.isNew()){
230  m_currentQIn.read();
231  tm = m_currentQ.tm;
232  }else{
234  tm.sec = coiltm.sec();
235  tm.nsec = coiltm.usec()*1000;
236  }
237 
238  if (m_qIn.isNew()){
239  m_qIn.read();
240  }
241  if (m_tqIn.isNew()){
242  m_tqIn.read();
243  }
244  if (m_requestGoActual || (m_q.data.length() == 0 && m_currentQ.data.length() > 0)){
245  m_q = m_currentQ;
246  if (m_q.data.length() != m_tq.data.length()) m_tq.data.length(m_q.data.length());
247  // Reset reference wrenches to zero
248  for (unsigned int i=0; i<m_wrenchesIn.size(); i++){
249  m_wrenches[i].data[0] = m_wrenches[i].data[1] = m_wrenches[i].data[2] = 0.0;
250  m_wrenches[i].data[3] = m_wrenches[i].data[4] = m_wrenches[i].data[5] = 0.0;
251  }
252  }
253 
254  if (m_requestGoActual){
255  m_requestGoActual = false;
256  sem_post(&m_waitSem);
257  }
258 
259  if (m_basePosIn.isNew()){
260  m_basePosIn.read();
261  }
262 
263  if (m_baseRpyIn.isNew()){
264  m_baseRpyIn.read();
265  }
266 
267  if (m_zmpIn.isNew()){
268  m_zmpIn.read();
269  }
270 
271  if (m_optionalDataIn.isNew()){
273  }
274 
275  for (size_t i = 0; i < m_wrenchesIn.size(); i++) {
276  if ( m_wrenchesIn[i]->isNew() ) {
277  m_wrenchesIn[i]->read();
278  }
279  }
280 
281  double *a = m_baseTform.data.get_buffer();
282  a[0] = m_basePos.data.x;
283  a[1] = m_basePos.data.y;
284  a[2] = m_basePos.data.z;
286  m_baseRpy.data.p,
287  m_baseRpy.data.y);
289 
290  m_basePose.data.position = m_basePos.data;
291  m_basePose.data.orientation = m_baseRpy.data;
292 
293  // put timestamps
294  m_q.tm = tm;
295  m_tq.tm = tm;
296  m_baseTform.tm = tm;
297  m_basePos.tm = tm;
298  m_baseRpy.tm = tm;
299  m_zmp.tm = tm;
300  m_basePose.tm = tm;
301  for (size_t i = 0; i < m_wrenches.size(); i++) {
302  m_wrenches[i].tm = tm;
303  }
304 
305  // write
306  if (m_q.data.length() > 0){
307  m_qOut.write();
308  }
309  if (m_tq.data.length() > 0){
310  m_tqOut.write();
311  }
315  m_zmpOut.write();
318  for (size_t i = 0; i < m_wrenchesOut.size(); i++) {
319  m_wrenchesOut[i]->write();
320  }
321 
322  if (m_timeCount > 0){
323  m_timeCount--;
324  if (m_timeCount == 0) sem_post(&m_timeSem);
325  }
326 
327  return RTC::RTC_OK;
328 }
329 
330 /*
331 RTC::ReturnCode_t StateHolder::onAborting(RTC::UniqueId ec_id)
332 {
333  return RTC::RTC_OK;
334 }
335 */
336 
337 /*
338 RTC::ReturnCode_t StateHolder::onError(RTC::UniqueId ec_id)
339 {
340  return RTC::RTC_OK;
341 }
342 */
343 
344 /*
345 RTC::ReturnCode_t StateHolder::onReset(RTC::UniqueId ec_id)
346 {
347  return RTC::RTC_OK;
348 }
349 */
350 
351 /*
352 RTC::ReturnCode_t StateHolder::onStateUpdate(RTC::UniqueId ec_id)
353 {
354  return RTC::RTC_OK;
355 }
356 */
357 
358 /*
359 RTC::ReturnCode_t StateHolder::onRateChanged(RTC::UniqueId ec_id)
360 {
361  return RTC::RTC_OK;
362 }
363 */
364 
365 
367 {
368  std::cout << "StateHolder::goActual()" << std::endl;
369  m_requestGoActual = true;
370  sem_wait(&m_waitSem);
371 }
372 
373 void StateHolder::getCommand(StateHolderService::Command &com)
374 {
375  com.jointRefs.length(m_q.data.length());
376  memcpy(com.jointRefs.get_buffer(), m_q.data.get_buffer(), sizeof(double)*m_q.data.length());
377  com.baseTransform.length(12);
378  com.baseTransform[0] = m_basePos.data.x;
379  com.baseTransform[1] = m_basePos.data.y;
380  com.baseTransform[2] = m_basePos.data.z;
381  hrp::Matrix33 R = hrp::rotFromRpy(m_baseRpy.data.r, m_baseRpy.data.p, m_baseRpy.data.y);
382  double *a = com.baseTransform.get_buffer();
384  com.zmp.length(3);
385  com.zmp[0] = m_zmp.data.x; com.zmp[1] = m_zmp.data.y; com.zmp[2] = m_zmp.data.z;
386 }
387 
388 void StateHolder::wait(CORBA::Double tm)
389 {
390  m_timeCount = tm/m_dt;
391  sem_wait(&m_timeSem);
392 }
393 
394 extern "C"
395 {
396 
398  {
400  manager->registerFactory(profile,
401  RTC::Create<StateHolder>,
402  RTC::Delete<StateHolder>);
403  }
404 
405 };
406 
407 
ComponentProfile m_profile
TimeKeeperService_impl m_service1
Definition: StateHolder.h:159
png_infop png_charpp int png_charpp profile
StateHolderService_impl m_service0
Definition: StateHolder.h:158
sem_t m_waitSem
Definition: StateHolder.h:170
RTC::CorbaPort m_StateHolderServicePort
Definition: StateHolder.h:151
InPort< TimedDoubleSeq > m_tqIn
Definition: StateHolder.h:114
state holder component
OutPort< TimedPoint3D > m_basePosOut
Definition: StateHolder.h:139
png_infop png_charp png_int_32 png_int_32 int * type
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
long int sec() const
bool stringTo(To &val, const char *str)
InPort< TimedPoint3D > m_basePosIn
Definition: StateHolder.h:115
png_infop png_charpp name
OutPort< TimedDoubleSeq > m_tqOut
Definition: StateHolder.h:138
void setComponent(StateHolder *i_comp)
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
std::vector< InPort< TimedDoubleSeq > * > m_wrenchesIn
Definition: StateHolder.h:118
bool m_requestGoActual
Definition: StateHolder.h:171
CORBA::ORB_ptr getORB()
TimedOrientation3D m_baseRpy
Definition: StateHolder.h:132
png_uint_32 i
void setComponent(StateHolder *i_comp)
coil::Properties & getProperties()
static Manager & instance()
TimedPoint3D m_zmp
Definition: StateHolder.h:135
OutPort< TimedOrientation3D > m_baseRpyOut
Definition: StateHolder.h:140
bool addOutPort(const char *name, OutPortBase &outport)
RTC::CorbaPort m_TimeKeeperServicePort
Definition: StateHolder.h:152
double m_dt
Definition: StateHolder.h:172
OutPort< TimedDoubleSeq > m_qOut
Definition: StateHolder.h:137
Eigen::Vector3d Vector3
Matrix33 rotFromRpy(const Vector3 &rpy)
Eigen::Matrix3d Matrix33
std::vector< std::string > vstring
void setMatrix33ToRowMajorArray(const Matrix33 &m33, Array &a, size_t top=0)
int gettimeofday(struct timeval *tv, struct timezone *tz)
coil::Properties & getConfig()
OutPort< TimedDoubleSeq > m_optionalDataOut
Definition: StateHolder.h:145
ExecutionContextHandle_t UniqueId
InPort< TimedDoubleSeq > m_optionalDataIn
Definition: StateHolder.h:120
void getCommand(StateHolderService::Command &com)
TimedPoint3D m_basePos
Definition: StateHolder.h:131
Matrix33 rodrigues(const Vector3 &axis, double q)
virtual ~StateHolder()
Destructor.
Definition: StateHolder.cpp:68
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
void goActual()
void wait(CORBA::Double tm)
prop
TimedPose3D m_basePose
Definition: StateHolder.h:134
HRP_UTIL_EXPORT Vector3 rpyFromRot(const Matrix33 &m)
InPort< TimedOrientation3D > m_baseRpyIn
Definition: StateHolder.h:116
naming
OutPort< TimedPose3D > m_basePoseOut
Definition: StateHolder.h:142
void registerInPort(const char *name, InPortBase &inport)
std::vector< OutPort< TimedDoubleSeq > * > m_wrenchesOut
Definition: StateHolder.h:144
virtual bool isNew()
void StateHolderInit(RTC::Manager *manager)
InPort< TimedDoubleSeq > m_qIn
Definition: StateHolder.h:113
bool addPort(PortBase &port)
virtual bool write(DataType &value)
TimedDoubleSeq m_tq
Definition: StateHolder.h:130
sem_t m_timeSem
Definition: StateHolder.h:170
TimedDoubleSeq m_baseTform
Definition: StateHolder.h:133
long int usec() const
InPort< TimedPoint3D > m_zmpIn
Definition: StateHolder.h:117
OutPort< TimedPoint3D > m_zmpOut
Definition: StateHolder.h:143
bool addInPort(const char *name, InPortBase &inport)
StateHolder(RTC::Manager *manager)
Constructor.
Definition: StateHolder.cpp:35
OutPort< TimedDoubleSeq > m_baseTformOut
Definition: StateHolder.h:141
TimedDoubleSeq m_currentQ
Definition: StateHolder.h:111
void registerOutPort(const char *name, OutPortBase &outport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
virtual RTC::ReturnCode_t onInitialize()
Definition: StateHolder.cpp:74
TimedDoubleSeq m_q
Definition: StateHolder.h:129
static const char * stateholder_spec[]
Definition: StateHolder.cpp:17
InPort< TimedDoubleSeq > m_currentQIn
Definition: StateHolder.h:112
std::vector< TimedDoubleSeq > m_wrenches
Definition: StateHolder.h:136
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)


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