Skip to content

Commit 3f8c624

Browse files
add ifle reset capabilities
1 parent 6b35e26 commit 3f8c624

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

data_tamer_cpp/include/data_tamer/sinks/mcap_sink.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class MCAPSink : public DataSinkBase
4444
/// WARNING: this can consume a large amount of disk space very quickly.
4545
void setMaxTimeBeforeReset(std::chrono::seconds reset_time);
4646

47+
/// When resetting the MCAP recording (see `setMaxTimeBeforeReset`),
48+
/// if `create_new_file` is true then the filename will be incremented
49+
/// and then saved instead of overwriting the previous file.
50+
void setCreateNewFileOnReset(bool create_new_file);
51+
4752
/// Stop recording and save the file
4853
void stopRecording();
4954

@@ -54,6 +59,7 @@ class MCAPSink : public DataSinkBase
5459
*
5560
* @param filepath file path of the new file (should be ".mcap" extension)
5661
* @param do_compression if true, compress the data on the fly.
62+
* WARNING: if this is called with the same filename as previously, the file counter will be reset, too.
5763
*/
5864
void restartRecording(std::string const& filepath, bool do_compression = false);
5965

@@ -65,13 +71,20 @@ class MCAPSink : public DataSinkBase
6571
std::unordered_map<uint64_t, uint16_t> hash_to_channel_id_;
6672
std::unordered_map<std::string, Schema> schemas_;
6773

74+
// file reset variables
75+
bool create_file_on_reset_ = false;
76+
std::string original_filepath_;
77+
size_t file_reset_counter_ = 1;
78+
6879
std::chrono::seconds reset_time_ = std::chrono::seconds(60 * 10);
6980
std::chrono::system_clock::time_point start_time_;
7081

7182
bool forced_stop_recording_ = false;
7283
std::recursive_mutex mutex_;
7384

7485
void openFile(std::string const& filepath);
86+
void restartRecordingImpl(std::string const& filepath, bool do_compression,
87+
bool new_file);
7588
};
7689

7790
} // namespace DataTamer

data_tamer_cpp/src/sinks/mcap_sink.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <chrono>
55
#include <sstream>
66
#include <mutex>
7+
#include <string>
78

89
#ifndef USING_ROS2
910
#define MCAP_IMPLEMENTATION
@@ -35,7 +36,7 @@ namespace DataTamer
3536
static constexpr char const* kDataTamer = "data_tamer";
3637

3738
MCAPSink::MCAPSink(const std::string& filepath, bool do_compression)
38-
: filepath_(filepath), compression_(do_compression)
39+
: filepath_(filepath), compression_(do_compression), original_filepath_(filepath)
3940
{
4041
openFile(filepath_);
4142
}
@@ -121,7 +122,13 @@ bool MCAPSink::storeSnapshot(const Snapshot& snapshot)
121122
auto const now = std::chrono::system_clock::now();
122123
if(reset_time_ != std::chrono::seconds(0) && now - start_time_ > reset_time_)
123124
{
124-
restartRecording(filepath_, compression_);
125+
if(create_file_on_reset_)
126+
{
127+
// change the current filepath to the original with "_[# resets]"" appended
128+
filepath_ = original_filepath_ + "_" + std::to_string(file_reset_counter_);
129+
++file_reset_counter_;
130+
}
131+
restartRecordingImpl(filepath_, compression_, false);
125132
}
126133
return true;
127134
}
@@ -131,6 +138,11 @@ void MCAPSink::setMaxTimeBeforeReset(std::chrono::seconds reset_time)
131138
reset_time_ = reset_time;
132139
}
133140

141+
void MCAPSink::setCreateNewFileOnReset(bool create_file_on_reset)
142+
{
143+
create_file_on_reset_ = create_file_on_reset;
144+
}
145+
134146
void MCAPSink::stopRecording()
135147
{
136148
std::scoped_lock lk(mutex_);
@@ -140,8 +152,20 @@ void MCAPSink::stopRecording()
140152
}
141153

142154
void MCAPSink::restartRecording(const std::string& filepath, bool do_compression)
155+
{
156+
restartRecordingImpl(filepath, do_compression, true);
157+
}
158+
159+
void MCAPSink::restartRecordingImpl(const std::string& filepath, bool do_compression,
160+
bool new_file)
143161
{
144162
std::scoped_lock lk(mutex_);
163+
if(new_file)
164+
{
165+
// if this was called by a user, we need to change the filepath that we will increment when reset
166+
file_reset_counter_ = 1;
167+
original_filepath_ = filepath;
168+
}
145169
filepath_ = filepath;
146170
compression_ = do_compression;
147171
openFile(filepath_);

0 commit comments

Comments
 (0)