DataLogger.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "hrpsys/util/Hrpsys.h"
11 #include "hrpsys/idl/pointcloud.hh"
12 #include "hrpsys/idl/RobotHardwareService.hh"
13 #include "DataLogger.h"
14 
15 
17 
18 // Module specification
19 // <rtc-template block="module_spec">
20 static const char* nullcomponent_spec[] =
21  {
22  "implementation_id", "DataLogger",
23  "type_name", "DataLogger",
24  "description", "data logger component",
25  "version", HRPSYS_PACKAGE_VERSION,
26  "vendor", "AIST",
27  "category", "example",
28  "activity_type", "DataFlowComponent",
29  "max_instance", "10",
30  "language", "C++",
31  "lang_type", "compile",
32  // Configuration variables
33  "conf.default.log_precision", "0",
34  ""
35  };
36 // </rtc-template>
37 
38 #define LOG_SET_PRECISION(strm) \
39  int prc; \
40  if (precision != 0) { \
41  prc = os.precision(); \
42  os << std::scientific << std::setprecision(precision); \
43  } \
44 
45 #define LOG_UNSET_PRECISION(strm) \
46  if (precision != 0) \
47  os << std::fixed << std::setprecision(prc); \
48 
49 void printData(std::ostream& os, const RTC::Acceleration3D& data, unsigned int precision = 0)
50 {
52  os << data.ax << " " << data.ay << " " << data.az << " ";
54 }
55 
56 void printData(std::ostream& os, const RTC::Velocity2D& data, unsigned int precision = 0)
57 {
59  os << data.vx << " " << data.vy << " " << data.va << " ";
61 }
62 
63 void printData(std::ostream& os, const RTC::Pose3D& data, unsigned int precision = 0)
64 {
66  os << data.position.x << " " << data.position.y << " "
67  << data.position.z << " " << data.orientation.r << " "
68  << data.orientation.p << " " << data.orientation.y << " ";
70 }
71 
72 void printData(std::ostream& os, const RTC::AngularVelocity3D& data, unsigned int precision = 0)
73 {
75  os << data.avx << " " << data.avy << " " << data.avz << " ";
77 }
78 
79 void printData(std::ostream& os, const RTC::Point3D& data, unsigned int precision = 0)
80 {
82  os << data.x << " " << data.y << " " << data.z << " ";
84 }
85 
86 void printData(std::ostream& os, const RTC::Vector3D& data, unsigned int precision = 0)
87 {
89  os << data.x << " " << data.y << " " << data.z << " ";
91 }
92 
93 void printData(std::ostream& os, const RTC::Orientation3D& data, unsigned int precision = 0)
94 {
96  os << data.r << " " << data.p << " " << data.y << " ";
98 }
99 
100 void printData(std::ostream& os, const PointCloudTypes::PointCloud& data, unsigned int precision = 0)
101 {
102  uint npoint = data.data.length()/data.point_step;
103  os << data.width << " " << data.height << " " << data.type << " " << npoint;
104  float *ptr = (float *)data.data.get_buffer();
105  std::string type(data.type);
106  if (type != "xyz" && type != "xyzrgb"){
107  std::cerr << "point cloud type(" << type << ") is not supported"
108  << std::endl;
109  return;
110  }
111  for (uint i=0; i<npoint ;i++){
112  os << " " << *ptr++ << " " << *ptr++ << " " << *ptr++;
113  if (type == "xyzrgb"){
114  unsigned char *rgb = (unsigned char *)ptr;
115  os << " " << (int)rgb[0] << " " << (int)rgb[1] << " " << (int)rgb[2];
116  ptr++;
117  }
118  }
119 }
120 
121 template<class T>
122 std::ostream& operator<<(std::ostream& os, const _CORBA_Unbounded_Sequence<T > & data)
123 {
124  for (unsigned int j=0; j<data.length(); j++){
125  os << data[j] << " ";
126  }
127  return os;
128 }
129 
130 std::ostream& operator<<(std::ostream& os, const OpenHRP::RobotHardwareService::DblSequence6 & data)
131 {
132  for (unsigned int j=0; j<data.length(); j++){
133  os << data[j] << " ";
134  }
135  return os;
136 }
137 
138 std::ostream& operator<<(std::ostream& os, const OpenHRP::RobotHardwareService::DblSequence3 & data)
139 {
140  for (unsigned int j=0; j<data.length(); j++){
141  os << data[j] << " ";
142  }
143  return os;
144 }
145 
146 std::ostream& operator<<(std::ostream& os, const OpenHRP::RobotHardwareService::BatteryState & data)
147 {
148  os << data.voltage << " " << data.current << " " << data.soc << " ";
149  return os;
150 }
151 
152 template <class T>
153 void printData(std::ostream& os, const T& data, unsigned int precision = 0)
154 {
155  LOG_SET_PRECISION(os);
156  for (unsigned int j=0; j<data.length(); j++){
157  os << data[j] << " ";
158  }
160 }
161 
162 void printData(std::ostream& os, double data, unsigned int precision = 0)
163 {
164  LOG_SET_PRECISION(os);
165  os << data << " ";
167 }
168 
169 void printData(std::ostream& os, const OpenHRP::RobotHardwareService::RobotState2& data, unsigned int precision = 0)
170 {
171  printData(os, data.angle, precision);
172  printData(os, data.command, precision);
173  printData(os, data.torque, precision);
174  printData(os, data.servoState, precision);
175  printData(os, data.force, precision);
176  printData(os, data.rateGyro, precision);
177  printData(os, data.accel, precision);
178  printData(os, data.batteries, precision);
179  printData(os, data.voltage, precision);
180  printData(os, data.current, precision);
181  printData(os, data.temperature, precision);
182 }
183 
184 template <class T>
186 {
187 public:
188  LoggerPort(const char *name) : m_port(name, m_data) {}
189  const char *name(){
190  return m_port.name();
191  }
192  virtual void dumpLog(std::ostream& os, unsigned int precision = 0){
193  os.setf(std::ios::fixed, std::ios::floatfield);
194  for (unsigned int i=0; i<m_log.size(); i++){
195  printLog(os, m_log[i], precision);
196  }
197  }
198  void printLog(std::ostream& os, T &data, unsigned int precision = 0){
199  os << std::setprecision(6) << (data.tm.sec + data.tm.nsec/1e9) << " ";
200  // data
201  printData(os, data.data, precision);
202  os << std::endl;
203  }
205  return m_port;
206  }
207  void log(){
208  if (m_port.isNew()){
209  m_port.read();
210  m_log.push_back(m_data);
211  while (m_log.size() > m_maxLength){
212  m_log.pop_front();
213  }
214  }
215  }
216  void clear(){
217  m_log.clear();
218  }
219 protected:
222  std::deque<T> m_log;
223 };
224 
225 class LoggerPortForPointCloud : public LoggerPort<PointCloudTypes::PointCloud>
226 {
227 public:
228  LoggerPortForPointCloud(const char *name) : LoggerPort<PointCloudTypes::PointCloud>(name) {}
229  void dumpLog(std::ostream& os, unsigned int precision = 0){
230  os.setf(std::ios::fixed, std::ios::floatfield);
231  for (unsigned int i=0; i<m_log.size(); i++){
232  // time
233  os << std::setprecision(6) << (m_log[i].tm.sec + m_log[i].tm.nsec/1e9) << " ";
234  // data
235  printData(os, m_log[i], precision);
236  os << std::endl;
237  }
238  }
239 };
240 
242  : RTC::DataFlowComponentBase(manager),
243  // <rtc-template block="initializer">
244  m_emergencySignalIn("emergencySignal", m_emergencySignal),
245  m_DataLoggerServicePort("DataLoggerService"),
246  // </rtc-template>
247  m_suspendFlag(false),
248  m_log_precision(0),
249  dummy(0)
250 {
251  m_service0.setLogger(this);
252 }
253 
255 {
256 }
257 
258 
259 
260 RTC::ReturnCode_t DataLogger::onInitialize()
261 {
262  std::cerr << "[" << m_profile.instance_name << "] onInitialize()" << std::endl;
263  bindParameter("log_precision", m_log_precision, "0");
264 
265  // Registration: InPort/OutPort/Service
266  // <rtc-template block="registration">
267  // Set InPort buffers
268  addInPort("emergencySignal", m_emergencySignalIn);
269 
270  // Set OutPort buffer
271 
272  // Set service provider to Ports
273  m_DataLoggerServicePort.registerProvider("service0", "DataLoggerService", m_service0);
274 
275  // Set service consumers to Ports
276 
277  // Set CORBA Service Ports
279 
280  // </rtc-template>
281  // <rtc-template block="bind_config">
282  // Bind variables and configuration variable
283 
284  // </rtc-template>
285 
286  return RTC::RTC_OK;
287 }
288 
289 
290 
291 /*
292 RTC::ReturnCode_t DataLogger::onFinalize()
293 {
294  return RTC::RTC_OK;
295 }
296 */
297 
298 /*
299 RTC::ReturnCode_t DataLogger::onStartup(RTC::UniqueId ec_id)
300 {
301  return RTC::RTC_OK;
302 }
303 */
304 
305 /*
306 RTC::ReturnCode_t DataLogger::onShutdown(RTC::UniqueId ec_id)
307 {
308  return RTC::RTC_OK;
309 }
310 */
311 
312 RTC::ReturnCode_t DataLogger::onActivated(RTC::UniqueId ec_id)
313 {
314  return RTC::RTC_OK;
315 }
316 
317 RTC::ReturnCode_t DataLogger::onDeactivated(RTC::UniqueId ec_id)
318 {
319  return RTC::RTC_OK;
320 }
321 
322 RTC::ReturnCode_t DataLogger::onExecute(RTC::UniqueId ec_id)
323 {
324  if (ec_id == 0){
325  if (m_emergencySignalIn.isNew()){
327  time_t sec = time(NULL);
328  struct tm *tm_ = localtime(&sec);
329  char date[20];
330  strftime(date,20, "%Y-%m-%d", tm_);
331  char basename[30];
332  sprintf(basename, "emglog-%s-%02d%02d",
333  date, tm_->tm_hour, tm_->tm_min);
334  std::cout << "received emergency signal. saving log files("
335  << basename << ")" << std::endl;
336  save(basename);
337  while (m_emergencySignalIn.isNew()){
339  }
340  }
341  }else{
342  Guard guard(m_suspendFlagMutex);
343 
344  if (m_suspendFlag) return RTC::RTC_OK;
345 
346  for (unsigned int i=0; i<m_ports.size(); i++){
347  m_ports[i]->log();
348  }
349  }
350  return RTC::RTC_OK;
351 }
352 
353 /*
354 RTC::ReturnCode_t DataLogger::onAborting(RTC::UniqueId ec_id)
355 {
356  return RTC::RTC_OK;
357 }
358 */
359 
360 /*
361 RTC::ReturnCode_t DataLogger::onError(RTC::UniqueId ec_id)
362 {
363  return RTC::RTC_OK;
364 }
365 */
366 
367 /*
368 RTC::ReturnCode_t DataLogger::onReset(RTC::UniqueId ec_id)
369 {
370  return RTC::RTC_OK;
371 }
372 */
373 
374 /*
375 RTC::ReturnCode_t DataLogger::onStateUpdate(RTC::UniqueId ec_id)
376 {
377  return RTC::RTC_OK;
378 }
379 */
380 
381 /*
382 RTC::ReturnCode_t DataLogger::onRateChanged(RTC::UniqueId ec_id)
383 {
384  return RTC::RTC_OK;
385 }
386 */
387 
388 bool DataLogger::add(const char *i_type, const char *i_name)
389 {
390  suspendLogging();
391  for (unsigned int i=0; i<m_ports.size(); i++){
392  if (strcmp(m_ports[i]->name(),i_name) == 0){
393  std::cerr << "Logger port named \"" << i_name << "\" already exists"
394  << std::endl;
395  resumeLogging();
396  return false;
397  }
398  }
399 
400  LoggerPortBase *new_port=NULL;
401  if (strcmp(i_type, "TimedDoubleSeq")==0){
403  new_port = lp;
404  if (!addInPort(i_name, lp->port())) {
405  resumeLogging();
406  return false;
407  }
408  }else if (strcmp(i_type, "TimedLongSeq")==0){
410  new_port = lp;
411  if (!addInPort(i_name, lp->port())) {
412  resumeLogging();
413  return false;
414  }
415  }else if (strcmp(i_type, "TimedBooleanSeq")==0){
417  new_port = lp;
418  if (!addInPort(i_name, lp->port())) {
419  resumeLogging();
420  return false;
421  }
422  }else if (strcmp(i_type, "TimedLongSeqSeq")==0){
424  new_port = lp;
425  if (!addInPort(i_name, lp->port())) {
426  resumeLogging();
427  return false;
428  }
429  }else if (strcmp(i_type, "TimedPoint3D")==0){
431  new_port = lp;
432  if (!addInPort(i_name, lp->port())) {
433  resumeLogging();
434  return false;
435  }
436  }else if (strcmp(i_type, "TimedVector3D")==0){
438  new_port = lp;
439  if (!addInPort(i_name, lp->port())) {
440  resumeLogging();
441  return false;
442  }
443  }else if (strcmp(i_type, "TimedOrientation3D")==0){
445  new_port = lp;
446  if (!addInPort(i_name, lp->port())) {
447  resumeLogging();
448  return false;
449  }
450  }else if (strcmp(i_type, "TimedAcceleration3D")==0){
452  new_port = lp;
453  if (!addInPort(i_name, lp->port())) {
454  resumeLogging();
455  return false;
456  }
457  }else if (strcmp(i_type, "TimedAngularVelocity3D")==0){
459  new_port = lp;
460  if (!addInPort(i_name, lp->port())) {
461  resumeLogging();
462  return false;
463  }
464  }else if (strcmp(i_type, "TimedVelocity2D")==0){
466  new_port = lp;
467  if (!addInPort(i_name, lp->port())) {
468  resumeLogging();
469  return false;
470  }
471  }else if (strcmp(i_type, "TimedPose3D")==0){
473  new_port = lp;
474  if (!addInPort(i_name, lp->port())) {
475  resumeLogging();
476  return false;
477  }
478  }else if (strcmp(i_type, "PointCloud")==0){
480  new_port = lp;
481  if (!addInPort(i_name, lp->port())) {
482  resumeLogging();
483  return false;
484  }
485  }else if (strcmp(i_type, "TimedRobotState2")==0){
487  new_port = lp;
488  if (!addInPort(i_name, lp->port())) {
489  resumeLogging();
490  return false;
491  }
492  }else{
493  std::cout << "DataLogger: unsupported data type(" << i_type << ")"
494  << std::endl;
495  resumeLogging();
496  return false;
497  }
498  m_ports.push_back(new_port);
499  resumeLogging();
500  return true;
501 }
502 
503 bool DataLogger::save(const char *i_basename)
504 {
505  suspendLogging();
506  bool ret = true;
507  for (unsigned int i=0; i<m_ports.size(); i++){
508  std::string fname = i_basename;
509  fname.append(".");
510  fname.append(m_ports[i]->name());
511  std::ofstream ofs(fname.c_str());
512  if (ofs.is_open()){
513  m_ports[i]->dumpLog(ofs, m_log_precision);
514  }else{
515  std::cerr << "[" << m_profile.instance_name << "] failed to open(" << fname << ")" << std::endl;
516  ret = false;
517  }
518  }
519  if (ret) std::cerr << "[" << m_profile.instance_name << "] Save log to " << i_basename << ".*" << std::endl;
520  resumeLogging();
521  return ret;
522 }
523 
525 {
526  suspendLogging();
527  for (unsigned int i=0; i<m_ports.size(); i++){
528  m_ports[i]->clear();
529  }
530  std::cerr << "[" << m_profile.instance_name << "] Log cleared" << std::endl;
531  resumeLogging();
532  return true;
533 }
534 
536 {
537  Guard guard(m_suspendFlagMutex);
538  m_suspendFlag = true;
539 }
540 
542 {
543  Guard guard(m_suspendFlagMutex);
544  m_suspendFlag = false;
545 }
546 
547 void DataLogger::maxLength(unsigned int len)
548 {
549  suspendLogging();
550  for (unsigned int i=0; i<m_ports.size(); i++){
551  m_ports[i]->maxLength(len);
552  }
553  std::cerr << "[" << m_profile.instance_name << "] Log max length is set to " << len << std::endl;
554  resumeLogging();
555 }
556 
557 extern "C"
558 {
559 
561  {
563  manager->registerFactory(profile,
564  RTC::Create<DataLogger>,
565  RTC::Delete<DataLogger>);
566  }
567 
568 };
569 
570 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
void setLogger(DataLogger *i_logger)
const char * name()
Definition: DataLogger.cpp:189
png_infop png_charp png_int_32 png_int_32 int * type
#define LOG_SET_PRECISION(strm)
Definition: DataLogger.cpp:38
std::ostream & operator<<(std::ostream &os, const _CORBA_Unbounded_Sequence< T > &data)
Definition: DataLogger.cpp:122
png_voidp ptr
RTC::CorbaPort m_DataLoggerServicePort
Definition: DataLogger.h:149
png_infop png_charpp name
void printLog(std::ostream &os, T &data, unsigned int precision=0)
Definition: DataLogger.cpp:198
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
InPort< T > & port()
Definition: DataLogger.cpp:204
virtual RTC::ReturnCode_t onInitialize()
Definition: DataLogger.cpp:260
std::string basename(const std::string name)
virtual const char * name()
png_uint_32 i
void DataLoggerInit(RTC::Manager *manager)
Definition: DataLogger.cpp:560
void log()
Definition: DataLogger.cpp:207
DataLogger(RTC::Manager *manager)
Constructor.
Definition: DataLogger.cpp:241
LoggerPortForPointCloud(const char *name)
Definition: DataLogger.cpp:228
unsigned int uint
void maxLength(unsigned int len)
Definition: DataLogger.cpp:547
bool m_suspendFlag
Definition: DataLogger.h:165
bool add(const char *i_type, const char *i_name)
Definition: DataLogger.cpp:388
unsigned int m_maxLength
Definition: DataLogger.h:52
ExecutionContextHandle_t UniqueId
coil::Mutex m_suspendFlagMutex
Definition: DataLogger.h:166
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
unsigned int m_log_precision
Definition: DataLogger.h:167
def j(str, encoding="cp932")
DataLoggerService_impl m_service0
Definition: DataLogger.h:155
InPort< TimedLong > m_emergencySignalIn
Definition: DataLogger.h:138
coil::Guard< coil::Mutex > Guard
Definition: DataLogger.cpp:16
static const char * nullcomponent_spec[]
Definition: DataLogger.cpp:20
#define LOG_UNSET_PRECISION(strm)
Definition: DataLogger.cpp:45
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: DataLogger.cpp:312
void resumeLogging()
Definition: DataLogger.cpp:541
virtual void dumpLog(std::ostream &os, unsigned int precision=0)
Definition: DataLogger.cpp:192
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: DataLogger.cpp:322
typedef int
std::vector< LoggerPortBase * > m_ports
Definition: DataLogger.h:126
std::string sprintf(char const *__restrict fmt,...)
LoggerPort(const char *name)
Definition: DataLogger.cpp:188
virtual bool isNew()
logger component
bool addPort(PortBase &port)
void suspendLogging()
Definition: DataLogger.cpp:535
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: DataLogger.cpp:317
bool save(const char *i_basename)
Definition: DataLogger.cpp:503
JSAMPIMAGE data
InPort< T > m_port
Definition: DataLogger.cpp:220
bool addInPort(const char *name, InPortBase &inport)
bool clear()
Definition: DataLogger.cpp:524
void clear()
Definition: DataLogger.cpp:216
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void printData(std::ostream &os, const RTC::Acceleration3D &data, unsigned int precision=0)
Definition: DataLogger.cpp:49
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
virtual ~DataLogger()
Destructor.
Definition: DataLogger.cpp:254
void dumpLog(std::ostream &os, unsigned int precision=0)
Definition: DataLogger.cpp:229
std::deque< T > m_log
Definition: DataLogger.cpp:222


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