PX4 Firmware
PX4 Autopilot Software http://px4.io
px4::Replay Class Reference

Parses an ULog file and replays it in 'real-time'. More...

#include <Replay.hpp>

Inheritance diagram for px4::Replay:
Collaboration diagram for px4::Replay:

Classes

class  CompatBase
 
class  CompatSensorCombinedDtType
 
struct  Subscription
 

Public Member Functions

 Replay ()=default
 
virtual ~Replay ()
 
void run () override
 

Static Public Member Functions

static int task_spawn (int argc, char *argv[])
 
static Replayinstantiate (int argc, char *argv[])
 
static int custom_command (int argc, char *argv[])
 
static int print_usage (const char *reason=nullptr)
 
static int applyParams (bool quiet)
 Apply the parameters from the log. More...
 
static void setupReplayFile (const char *file_name)
 Tell the replay module that we want to use replay mode. More...
 
static bool isSetup ()
 

Protected Member Functions

bool publishTopic (Subscription &sub, void *data)
 publish an orb topic More...
 
virtual void onEnterMainLoop ()
 called when entering the main replay loop More...
 
virtual void onExitMainLoop ()
 called when exiting the main replay loop More...
 
virtual void onSubscriptionAdded (Subscription &sub, uint16_t msg_id)
 called when a new subscription is added More...
 
virtual uint64_t handleTopicDelay (uint64_t next_file_time, uint64_t timestamp_offset)
 handle delay until topic can be published. More...
 
virtual bool handleTopicUpdate (Subscription &sub, void *data, std::ifstream &replay_file)
 handle the publication of a topic update More...
 
void readTopicDataToBuffer (const Subscription &sub, std::ifstream &replay_file)
 read a topic from the file (offset given by the subscription) into _read_buffer More...
 
bool nextDataMessage (std::ifstream &file, Subscription &subscription, int msg_id)
 Find next data message for this subscription, starting with the stored file offset. More...
 

Static Protected Member Functions

static bool findFieldOffset (const std::string &format, const std::string &field_name, int &offset, int &field_size)
 Find the offset & field size in bytes for a given field name. More...
 

Protected Attributes

std::vector< Subscription * > _subscriptions
 
std::vector< uint8_t > _read_buffer
 

Private Member Functions

bool readFileHeader (std::ifstream &file)
 
bool readFileDefinitions (std::ifstream &file)
 Read definitions section: check formats, apply parameters and store the start of the data section. More...
 
bool readFormat (std::ifstream &file, uint16_t msg_size)
 file parsing methods. They return false, when further parsing should be aborted. More...
 
bool readAndAddSubscription (std::ifstream &file, uint16_t msg_size)
 
bool readFlagBits (std::ifstream &file, uint16_t msg_size)
 
bool readDefinitionsAndApplyParams (std::ifstream &file)
 Read the file header and definitions sections. More...
 
bool readAndHandleAdditionalMessages (std::ifstream &file, std::streampos end_position)
 Read and handle additional messages starting at current file position, while position < end_position. More...
 
bool readDropout (std::ifstream &file, uint16_t msg_size)
 
bool readAndApplyParameter (std::ifstream &file, uint16_t msg_size)
 
void setUserParams (const char *filename)
 

Static Private Member Functions

static const orb_metadatafindTopic (const std::string &name)
 
static std::string extractArraySize (const std::string &type_name_full, int &array_size)
 get the array size from a type. More...
 
static size_t sizeOfType (const std::string &type_name)
 get the size of a type that is not an array More...
 
static size_t sizeOfFullType (const std::string &type_name_full)
 get the size of a type that can be an array More...
 

Private Attributes

std::set< std::string > _overridden_params
 
std::map< std::string, std::string > _file_formats
 all formats we read from the file More...
 
uint64_t _file_start_time
 
uint64_t _replay_start_time
 
std::streampos _data_section_start
 first ADD_LOGGED_MSG message More...
 
std::streampos _subscription_file_pos = 0
 keep track of file position to avoid adding a subscription multiple times. More...
 
int64_t _read_until_file_position = 1ULL << 60
 read limit if log contains appended data More...
 

Static Private Attributes

static char * _replay_file = nullptr
 

Detailed Description

Parses an ULog file and replays it in 'real-time'.

The timestamp of each replayed message is offset to match the starting time of replay. It keeps a stream for each subscription to find the next message to replay. This is necessary because data messages from different subscriptions don't need to be in monotonic increasing order.

Definition at line 58 of file Replay.hpp.

Constructor & Destructor Documentation

◆ Replay()

px4::Replay::Replay ( )
default

Referenced by applyParams(), and instantiate().

Here is the caller graph for this function:

◆ ~Replay()

px4::Replay::~Replay ( )
virtual

Definition at line 87 of file Replay.cpp.

References _subscriptions.

Member Function Documentation

◆ applyParams()

int px4::Replay::applyParams ( bool  quiet)
static

Apply the parameters from the log.

Parameters
quietdo not print an error if true and no log file given via ENV
Returns
0 on success

Definition at line 986 of file Replay.cpp.

References _replay_file, isSetup(), readDefinitionsAndApplyParams(), and Replay().

Referenced by custom_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ custom_command()

int px4::Replay::custom_command ( int  argc,
char *  argv[] 
)
static
See also
ModuleBase

Definition at line 944 of file Replay.cpp.

References applyParams(), print_usage(), and task_spawn().

Here is the call graph for this function:

◆ extractArraySize()

std::string px4::Replay::extractArraySize ( const std::string &  type_name_full,
int &  array_size 
)
staticprivate

get the array size from a type.

eg. float[3] -> return float

Definition at line 656 of file Replay.cpp.

Referenced by sizeOfFullType().

Here is the caller graph for this function:

◆ findFieldOffset()

bool px4::Replay::findFieldOffset ( const std::string &  format,
const std::string &  field_name,
int &  offset,
int &  field_size 
)
staticprotected

Find the offset & field size in bytes for a given field name.

Parameters
formatformat string, as specified by ULog
field_namesearch for this field
offsetreturned offset
field_sizereturned field size
Returns
true if found, false otherwise

Definition at line 441 of file Replay.cpp.

References sizeOfFullType().

Referenced by readAndAddSubscription().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findTopic()

const orb_metadata * px4::Replay::findTopic ( const std::string &  name)
staticprivate

Definition at line 642 of file Replay.cpp.

References orb_get_topics(), and orb_topics_count().

Referenced by readAndAddSubscription(), and sizeOfType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleTopicDelay()

uint64_t px4::Replay::handleTopicDelay ( uint64_t  next_file_time,
uint64_t  timestamp_offset 
)
protectedvirtual

handle delay until topic can be published.

Parameters
next_file_timestamptimestamp of next message to publish
timestamp_offsetoffset between file start time and replay start time
Returns
timestamp that the message to publish should have

Reimplemented in px4::ReplayEkf2.

Definition at line 878 of file Replay.cpp.

References _file_start_time, and hrt_absolute_time().

Referenced by onSubscriptionAdded(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleTopicUpdate()

bool px4::Replay::handleTopicUpdate ( Subscription sub,
void *  data,
std::ifstream &  replay_file 
)
protectedvirtual

handle the publication of a topic update

Returns
true if published, false otherwise

Reimplemented in px4::ReplayEkf2.

Definition at line 872 of file Replay.cpp.

References publishTopic().

Referenced by onSubscriptionAdded(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ instantiate()

Replay * px4::Replay::instantiate ( int  argc,
char *  argv[] 
)
static
See also
ModuleBase

Definition at line 1017 of file Replay.cpp.

References ll40ls::instance, and Replay().

Here is the call graph for this function:

◆ isSetup()

static bool px4::Replay::isSetup ( )
inlinestatic

Definition at line 94 of file Replay.hpp.

References _replay_file.

Referenced by applyParams(), replay_main(), and task_spawn().

Here is the caller graph for this function:

◆ nextDataMessage()

bool px4::Replay::nextDataMessage ( std::ifstream &  file,
Subscription subscription,
int  msg_id 
)
protected

Find next data message for this subscription, starting with the stored file offset.

Skip the first message, and if found, read the timestamp and store the new file offset. This also takes care of new subscriptions and parameter updates. When reaching EOF, the subscription is set to invalid. File seek position is arbitrary after this call.

Returns
false on file error

Definition at line 557 of file Replay.cpp.

References _read_until_file_position, ADD_LOGGED_MSG, DATA, DROPOUT, INFO, INFO_MULTIPLE, LOGGING, px4::Replay::Subscription::next_read_pos, px4::Replay::Subscription::next_timestamp, orb_metadata::o_name, orb_metadata::o_size_no_padding, px4::Replay::Subscription::orb_meta, PARAMETER, readAndAddSubscription(), REMOVE_LOGGED_MSG, SYNC, px4::Replay::Subscription::timestamp_offset, and ULOG_MSG_HEADER_LEN.

Referenced by px4::ReplayEkf2::findTimestampAndPublish(), onSubscriptionAdded(), readAndAddSubscription(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onEnterMainLoop()

virtual void px4::Replay::onEnterMainLoop ( )
inlineprotectedvirtual

called when entering the main replay loop

Reimplemented in px4::ReplayEkf2.

Definition at line 168 of file Replay.hpp.

Referenced by run().

Here is the caller graph for this function:

◆ onExitMainLoop()

virtual void px4::Replay::onExitMainLoop ( )
inlineprotectedvirtual

called when exiting the main replay loop

Reimplemented in px4::ReplayEkf2.

Definition at line 173 of file Replay.hpp.

Referenced by run().

Here is the caller graph for this function:

◆ onSubscriptionAdded()

virtual void px4::Replay::onSubscriptionAdded ( Subscription sub,
uint16_t  msg_id 
)
inlineprotectedvirtual

called when a new subscription is added

Reimplemented in px4::ReplayEkf2.

Definition at line 178 of file Replay.hpp.

References data, file, handleTopicDelay(), handleTopicUpdate(), nextDataMessage(), and readTopicDataToBuffer().

Referenced by readAndAddSubscription().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_usage()

int px4::Replay::print_usage ( const char *  reason = nullptr)
static
See also
ModuleBase

Definition at line 1036 of file Replay.cpp.

Referenced by custom_command().

Here is the caller graph for this function:

◆ publishTopic()

bool px4::Replay::publishTopic ( Subscription sub,
void *  data 
)
protected

publish an orb topic

Parameters
sub
data
Returns
true if published, false otherwise

Definition at line 894 of file Replay.cpp.

References _subscriptions, px4::Replay::CompatBase::apply(), px4::Replay::Subscription::compat, ll40ls::instance, px4::Replay::Subscription::multi_id, orb_metadata::o_name, px4::Replay::Subscription::orb_advert, orb_advertise(), orb_advertise_multi(), px4::Replay::Subscription::orb_meta, ORB_PRIO_DEFAULT, orb_publish(), and px4::Replay::Subscription::publication_counter.

Referenced by px4::ReplayEkf2::findTimestampAndPublish(), px4::ReplayEkf2::handleTopicUpdate(), handleTopicUpdate(), and px4::ReplayEkf2::publishEkf2Topics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAndAddSubscription()

bool px4::Replay::readAndAddSubscription ( std::ifstream &  file,
uint16_t  msg_size 
)
private

Definition at line 324 of file Replay.cpp.

References _file_formats, _read_buffer, _subscription_file_pos, _subscriptions, px4::Replay::Subscription::compat, px4::Replay::CompatSensorCombinedDtType::CompatSensorCombinedDtType(), findFieldOffset(), findTopic(), px4::Replay::Subscription::multi_id, px4::Replay::Subscription::next_read_pos, nextDataMessage(), orb_metadata::o_fields, orb_metadata::o_name, onSubscriptionAdded(), px4::Replay::Subscription::orb_meta, px4::Replay::Subscription::timestamp_offset, and ULOG_MSG_HEADER_LEN.

Referenced by nextDataMessage(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAndApplyParameter()

bool px4::Replay::readAndApplyParameter ( std::ifstream &  file,
uint16_t  msg_size 
)
private

Definition at line 505 of file Replay.cpp.

References _overridden_params, _read_buffer, param_find(), PARAM_INVALID, param_name(), and param_set().

Referenced by readAndHandleAdditionalMessages(), and readFileDefinitions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readAndHandleAdditionalMessages()

bool px4::Replay::readAndHandleAdditionalMessages ( std::ifstream &  file,
std::streampos  end_position 
)
private

Read and handle additional messages starting at current file position, while position < end_position.

This handles dropout and parameter update messages. We need to handle these separately, because they have no timestamp. We look at the file position instead.

Returns
false on file error

Definition at line 472 of file Replay.cpp.

References DROPOUT, PARAMETER, readAndApplyParameter(), readDropout(), and ULOG_MSG_HEADER_LEN.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readDefinitionsAndApplyParams()

bool px4::Replay::readDefinitionsAndApplyParams ( std::ifstream &  file)
private

Read the file header and definitions sections.

Apply the parameters from this section and apply user-defined overridden parameters.

Returns
true on success

Definition at line 714 of file Replay.cpp.

References PARAMS_OVERRIDE_FILE, readFileDefinitions(), readFileHeader(), and setUserParams().

Referenced by applyParams(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readDropout()

bool px4::Replay::readDropout ( std::ifstream &  file,
uint16_t  msg_size 
)
private

Definition at line 547 of file Replay.cpp.

Referenced by readAndHandleAdditionalMessages().

Here is the caller graph for this function:

◆ readFileDefinitions()

bool px4::Replay::readFileDefinitions ( std::ifstream &  file)
private

Read definitions section: check formats, apply parameters and store the start of the data section.

Returns
true on success

Definition at line 194 of file Replay.cpp.

References _data_section_start, ADD_LOGGED_MSG, FLAG_BITS, FORMAT, INFO, INFO_MULTIPLE, PARAMETER, readAndApplyParameter(), readFlagBits(), readFormat(), and ULOG_MSG_HEADER_LEN.

Referenced by readDefinitionsAndApplyParams().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readFileHeader()

bool px4::Replay::readFileHeader ( std::ifstream &  file)
private

Definition at line 170 of file Replay.cpp.

References _file_start_time.

Referenced by readDefinitionsAndApplyParams().

Here is the caller graph for this function:

◆ readFlagBits()

bool px4::Replay::readFlagBits ( std::ifstream &  file,
uint16_t  msg_size 
)
private

Definition at line 251 of file Replay.cpp.

References _read_buffer, _read_until_file_position, and ULOG_INCOMPAT_FLAG0_DATA_APPENDED_MASK.

Referenced by readFileDefinitions().

Here is the caller graph for this function:

◆ readFormat()

bool px4::Replay::readFormat ( std::ifstream &  file,
uint16_t  msg_size 
)
private

file parsing methods. They return false, when further parsing should be aborted.

Definition at line 298 of file Replay.cpp.

References _file_formats, _read_buffer, and name.

Referenced by readFileDefinitions().

Here is the caller graph for this function:

◆ readTopicDataToBuffer()

void px4::Replay::readTopicDataToBuffer ( const Subscription sub,
std::ifstream &  replay_file 
)
protected

read a topic from the file (offset given by the subscription) into _read_buffer

Definition at line 862 of file Replay.cpp.

References _read_buffer, px4::Replay::Subscription::next_read_pos, orb_metadata::o_size, orb_metadata::o_size_no_padding, px4::Replay::Subscription::orb_meta, and ULOG_MSG_HEADER_LEN.

Referenced by px4::ReplayEkf2::findTimestampAndPublish(), onSubscriptionAdded(), px4::ReplayEkf2::publishEkf2Topics(), and run().

Here is the caller graph for this function:

◆ run()

◆ setupReplayFile()

void px4::Replay::setupReplayFile ( const char *  file_name)
static

Tell the replay module that we want to use replay mode.

After that, only 'replay start' must be executed (typically the last step after startup).

Parameters
file_namefile name of the used log replay file. Will be copied.

Definition at line 118 of file Replay.cpp.

References _replay_file.

Referenced by replay_main().

Here is the caller graph for this function:

◆ setUserParams()

void px4::Replay::setUserParams ( const char *  filename)
private

Definition at line 128 of file Replay.cpp.

References _overridden_params, param_find(), param_name(), param_set(), param_type(), PARAM_TYPE_FLOAT, and PARAM_TYPE_INT32.

Referenced by readDefinitionsAndApplyParams().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeOfFullType()

size_t px4::Replay::sizeOfFullType ( const std::string &  type_name_full)
staticprivate

get the size of a type that can be an array

Definition at line 706 of file Replay.cpp.

References extractArraySize(), and sizeOfType().

Referenced by findFieldOffset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeOfType()

size_t px4::Replay::sizeOfType ( const std::string &  type_name)
staticprivate

get the size of a type that is not an array

Definition at line 671 of file Replay.cpp.

References findTopic(), and orb_metadata::o_size.

Referenced by sizeOfFullType().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ task_spawn()

int px4::Replay::task_spawn ( int  argc,
char *  argv[] 
)
static
See also
ModuleBase

Definition at line 958 of file Replay.cpp.

References isSetup().

Referenced by custom_command().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _data_section_start

std::streampos px4::Replay::_data_section_start
private

first ADD_LOGGED_MSG message

Definition at line 218 of file Replay.hpp.

Referenced by readFileDefinitions(), and run().

◆ _file_formats

std::map<std::string, std::string> px4::Replay::_file_formats
private

all formats we read from the file

Definition at line 214 of file Replay.hpp.

Referenced by readAndAddSubscription(), and readFormat().

◆ _file_start_time

uint64_t px4::Replay::_file_start_time
private

Definition at line 216 of file Replay.hpp.

Referenced by handleTopicDelay(), readFileHeader(), and run().

◆ _overridden_params

std::set<std::string> px4::Replay::_overridden_params
private

Definition at line 213 of file Replay.hpp.

Referenced by readAndApplyParameter(), and setUserParams().

◆ _read_buffer

◆ _read_until_file_position

int64_t px4::Replay::_read_until_file_position = 1ULL << 60
private

read limit if log contains appended data

Definition at line 223 of file Replay.hpp.

Referenced by nextDataMessage(), and readFlagBits().

◆ _replay_file

char * px4::Replay::_replay_file = nullptr
staticprivate

Definition at line 269 of file Replay.hpp.

Referenced by applyParams(), isSetup(), run(), and setupReplayFile().

◆ _replay_start_time

uint64_t px4::Replay::_replay_start_time
private

Definition at line 217 of file Replay.hpp.

Referenced by run().

◆ _subscription_file_pos

std::streampos px4::Replay::_subscription_file_pos = 0
private

keep track of file position to avoid adding a subscription multiple times.

Definition at line 221 of file Replay.hpp.

Referenced by readAndAddSubscription().

◆ _subscriptions


The documentation for this class was generated from the following files: