RcsPySim
A robot control and simulation library
DataLogger.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (c) 2020, Fabio Muratore, Honda Research Institute Europe GmbH, and
3  Technical University of Darmstadt.
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8  1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  3. Neither the name of Fabio Muratore, Honda Research Institute Europe GmbH,
14  or Technical University of Darmstadt, nor the names of its contributors may
15  be used to endorse or promote products derived from this software without
16  specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL FABIO MURATORE, HONDA RESEARCH INSTITUTE EUROPE GMBH,
22  OR TECHNICAL UNIVERSITY OF DARMSTADT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE.
29 *******************************************************************************/
30 
31 #ifndef _DATALOGGER_H_
32 #define _DATALOGGER_H_
33 
34 #include "util/BoxSpace.h"
35 
36 #include <Rcs_MatNd.h>
37 
38 #include <mutex>
39 #include <fstream>
40 #include <string>
41 
42 namespace Rcs
43 {
44 
45 /**
46  * Logs experiment data to a csv file.
47  * For every time step, it records the observations and the actions.
48  * The reward can be computed later on the Python side.
49  */
51 {
52 public:
53 
54  /**
55  * Constructor. Automatically names files like `TIME_FILENUM`.csv on start
56  */
57  DataLogger();
58 
59  virtual ~DataLogger();
60 
61  /**
62  * Start logging for at most stepCount steps.
63  *
64  * @param[in] observationSpace environment's observation space
65  * @param[in] actionSpace environment's action space
66  * @param[in] maxStepCount maximum number of time steps
67  */
68  void start(const BoxSpace* observationSpace, const BoxSpace* actionSpace, unsigned int maxStepCount);
69 
70  /**
71  * Stop logging and flush data to file.
72  */
73  void stop();
74 
75  /**
76  * Record data for the current step.
77  */
78  void record(const MatNd* observation, const MatNd* action);
79 
80  /**
81  * Return true if running.
82  */
83  bool isRunning() const
84  {
85  return running;
86  }
87 
88 private:
89  std::recursive_mutex mutex;
90 
91  //! Automatic log file naming
92  unsigned int fileCounter;
93 
94  //! Flag if the logger is currently recording
95  volatile bool running;
96 
97  //! Buffer to avoid writing on realtime main thread
98  MatNd* buffer;
99 
100  //! Step counter in current logging run
101  unsigned int currentStep;
102 
103  //! Current output stream
104  std::ofstream output;
105 };
106 
107 } /* namespace Rcs */
108 
109 #endif /* _DATALOGGER_H_ */
unsigned int currentStep
Step counter in current logging run.
Definition: DataLogger.h:101
std::recursive_mutex mutex
Definition: DataLogger.h:89
void start(const BoxSpace *observationSpace, const BoxSpace *actionSpace, unsigned int maxStepCount)
Definition: DataLogger.cpp:59
MatNd * buffer
Buffer to avoid writing on realtime main thread.
Definition: DataLogger.h:98
bool isRunning() const
Definition: DataLogger.h:83
unsigned int fileCounter
Automatic log file naming.
Definition: DataLogger.h:92
virtual ~DataLogger()
Definition: DataLogger.cpp:53
std::ofstream output
Current output stream.
Definition: DataLogger.h:104
volatile bool running
Flag if the logger is currently recording.
Definition: DataLogger.h:95
void record(const MatNd *observation, const MatNd *action)
Definition: DataLogger.cpp:129