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()) {
247  m_tq.data.length(m_q.data.length());
248  for(size_t i=0;i<m_tq.data.length();i++){
249  m_tq.data[i] = 0;
250  }
251  }
252  // Reset reference wrenches to zero
253  for (unsigned int i=0; i<m_wrenchesIn.size(); i++){
254  m_wrenches[i].data[0] = m_wrenches[i].data[1] = m_wrenches[i].data[2] = 0.0;
255  m_wrenches[i].data[3] = m_wrenches[i].data[4] = m_wrenches[i].data[5] = 0.0;
256  }
257  }
258 
259  if (m_requestGoActual){
260  m_requestGoActual = false;
261  sem_post(&m_waitSem);
262  }
263 
264  if (m_basePosIn.isNew()){
265  m_basePosIn.read();
266  }
267 
268  if (m_baseRpyIn.isNew()){
269  m_baseRpyIn.read();
270  }
271 
272  if (m_zmpIn.isNew()){
273  m_zmpIn.read();
274  }
275 
276  if (m_optionalDataIn.isNew()){
278  }
279 
280  for (size_t i = 0; i < m_wrenchesIn.size(); i++) {
281  if ( m_wrenchesIn[i]->isNew() ) {
282  m_wrenchesIn[i]->read();
283  }
284  }
285 
286  double *a = m_baseTform.data.get_buffer();
287  a[0] = m_basePos.data.x;
288  a[1] = m_basePos.data.y;
289  a[2] = m_basePos.data.z;
291  m_baseRpy.data.p,
292  m_baseRpy.data.y);
294 
295  m_basePose.data.position = m_basePos.data;
296  m_basePose.data.orientation = m_baseRpy.data;
297 
298  // put timestamps
299  m_q.tm = tm;
300  m_tq.tm = tm;
301  m_baseTform.tm = tm;
302  m_basePos.tm = tm;
303  m_baseRpy.tm = tm;
304  m_zmp.tm = tm;
305  m_basePose.tm = tm;
306  for (size_t i = 0; i < m_wrenches.size(); i++) {
307  m_wrenches[i].tm = tm;
308  }
309 
310  // write
311  if (m_q.data.length() > 0){
312  m_qOut.write();
313  }
314  if (m_tq.data.length() > 0){
315  m_tqOut.write();
316  }
320  m_zmpOut.write();
323  for (size_t i = 0; i < m_wrenchesOut.size(); i++) {
324  m_wrenchesOut[i]->write();
325  }
326 
327  if (m_timeCount > 0){
328  m_timeCount--;
329  if (m_timeCount == 0) sem_post(&m_timeSem);
330  }
331 
332  return RTC::RTC_OK;
333 }
334 
335 /*
336 RTC::ReturnCode_t StateHolder::onAborting(RTC::UniqueId ec_id)
337 {
338  return RTC::RTC_OK;
339 }
340 */
341 
342 /*
343 RTC::ReturnCode_t StateHolder::onError(RTC::UniqueId ec_id)
344 {
345  return RTC::RTC_OK;
346 }
347 */
348 
349 /*
350 RTC::ReturnCode_t StateHolder::onReset(RTC::UniqueId ec_id)
351 {
352  return RTC::RTC_OK;
353 }
354 */
355 
356 /*
357 RTC::ReturnCode_t StateHolder::onStateUpdate(RTC::UniqueId ec_id)
358 {
359  return RTC::RTC_OK;
360 }
361 */
362 
363 /*
364 RTC::ReturnCode_t StateHolder::onRateChanged(RTC::UniqueId ec_id)
365 {
366  return RTC::RTC_OK;
367 }
368 */
369 
370 
372 {
373  std::cout << "StateHolder::goActual()" << std::endl;
374  m_requestGoActual = true;
375  sem_wait(&m_waitSem);
376 }
377 
378 void StateHolder::getCommand(StateHolderService::Command &com)
379 {
380  com.jointRefs.length(m_q.data.length());
381  memcpy(com.jointRefs.get_buffer(), m_q.data.get_buffer(), sizeof(double)*m_q.data.length());
382  com.baseTransform.length(12);
383  com.baseTransform[0] = m_basePos.data.x;
384  com.baseTransform[1] = m_basePos.data.y;
385  com.baseTransform[2] = m_basePos.data.z;
386  hrp::Matrix33 R = hrp::rotFromRpy(m_baseRpy.data.r, m_baseRpy.data.p, m_baseRpy.data.y);
387  double *a = com.baseTransform.get_buffer();
389  com.zmp.length(3);
390  com.zmp[0] = m_zmp.data.x; com.zmp[1] = m_zmp.data.y; com.zmp[2] = m_zmp.data.z;
391 }
392 
393 void StateHolder::wait(CORBA::Double tm)
394 {
395  m_timeCount = tm/m_dt;
396  sem_wait(&m_timeSem);
397 }
398 
399 extern "C"
400 {
401 
403  {
405  manager->registerFactory(profile,
406  RTC::Create<StateHolder>,
407  RTC::Delete<StateHolder>);
408  }
409 
410 };
411 
412 
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[])
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
long int sec() const
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)
long int usec() const
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
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 Sat Dec 17 2022 03:52:21