A LCM interface for logging LCM messages to a file or playing back from a existing log.
Note the user is responsible for offsetting the clock used to generate the log and the clock used for playback. For example, if the log is generated by some external logger (the lcm-logger binary), which uses the unix epoch time clock to record message arrival time, the user needs to offset those timestamps properly to match and the clock used for playback.
#include <drake/lcm/drake_lcm_log.h>
Public Member Functions | |
| DrakeLcmLog (const std::string &file_name, bool is_write, bool overwrite_publish_time_with_system_clock=false) | |
| Constructs a DrakeLcmLog. More... | |
| ~DrakeLcmLog () override | |
| void | Publish (const std::string &channel, const void *data, int data_size, std::optional< double > time_sec) override |
Writes an entry occurred at timestamp with content data to the log file. More... | |
| std::shared_ptr< DrakeSubscriptionInterface > | Subscribe (const std::string &channel, HandlerFunction handler) override |
Subscribes handler to channel. More... | |
| std::shared_ptr< DrakeSubscriptionInterface > | SubscribeMultichannel (std::string_view regex, MultichannelHandlerFunction) override |
| This function is not yet supported for LCM logs, and will always throw. More... | |
| std::shared_ptr< DrakeSubscriptionInterface > | SubscribeAllChannels (MultichannelHandlerFunction) override |
| Subscribe to all channels; this is useful for logging and redirecting LCM traffic without regard to its content. More... | |
| int | HandleSubscriptions (int) override |
| This is a no-op for Read mode, and an exception in Write mode. More... | |
| double | GetNextMessageTime () const |
| Returns the time in seconds for the next logged message's occurrence time or infinity if there are no more messages in the current log. More... | |
| void | DispatchMessageAndAdvanceLog (double current_time) |
Let MSG be the next message event in the log, if current_time matches MSG's timestamp, for every DrakeLcmMessageHandlerInterface sub that's subscribed to MSG's channel, invoke sub's HandleMessage method. More... | |
| bool | is_write () const |
| Returns true if this instance is constructed in write-only mode. More... | |
| double | timestamp_to_second (uint64_t timestamp) const |
Converts timestamp (in microseconds) to time (in seconds) relative to the starting time passed to the constructor. More... | |
| uint64_t | second_to_timestamp (double sec) const |
| Converts time (in seconds) relative to the starting time passed to the constructor to a timestamp in microseconds. More... | |
| std::string | get_lcm_url () const override |
| Returns a URL describing the transport of this LCM interface. More... | |
Does not allow copy, move, or assignment | |
| DrakeLcmLog (const DrakeLcmLog &)=delete | |
| DrakeLcmLog & | operator= (const DrakeLcmLog &)=delete |
| DrakeLcmLog (DrakeLcmLog &&)=delete | |
| DrakeLcmLog & | operator= (DrakeLcmLog &&)=delete |
Public Member Functions inherited from DrakeLcmInterface | |
| virtual | ~DrakeLcmInterface () |
| DrakeLcmInterface (const DrakeLcmInterface &)=delete | |
| DrakeLcmInterface & | operator= (const DrakeLcmInterface &)=delete |
| DrakeLcmInterface (DrakeLcmInterface &&)=delete | |
| DrakeLcmInterface & | operator= (DrakeLcmInterface &&)=delete |
Static Public Member Functions | |
| static bool | available () |
| Returns true if the LCM runtime library is enabled in this build of Drake. More... | |
Additional Inherited Members | |
Public Types inherited from DrakeLcmInterface | |
| using | HandlerFunction = std::function< void(const void *, int)> |
| A callback used by DrakeLcmInterface::Subscribe(), with arguments: More... | |
| using | MultichannelHandlerFunction = std::function< void(std::string_view, const void *, int)> |
| A callback used by DrakeLcmInterface::SubscribeMultipleChannels (which therefore needs the receiving channel passed in). More... | |
Protected Member Functions inherited from DrakeLcmInterface | |
| DrakeLcmInterface () | |
|
delete |
|
delete |
| DrakeLcmLog | ( | const std::string & | file_name, |
| bool | is_write, | ||
| bool | overwrite_publish_time_with_system_clock = false |
||
| ) |
Constructs a DrakeLcmLog.
| file_name | Log's file name for reading or writing. |
| is_write | If false, this instance reads from the Lcm log identified by file_name. If true, this instance writes to the Lcm log whose name is given by file_name. |
| overwrite_publish_time_with_system_clock | This parameter only affects the Publish method in write-only mode. If true, override the second parameter passed to Publish method, and use host system's clock to generate the timestamp for the logged message. This is used to mimic lcm-logger's behavior. It also implicitly records how fast the messages are generated in real time. |
| std::exception | if unable to open file. |
|
override |
|
static |
Returns true if the LCM runtime library is enabled in this build of Drake.
When false, functions that require the runtime (which at the moment is all functions, including the constructor) will throw an error. See //tools/flags:with_lcm_runtime.
| void DispatchMessageAndAdvanceLog | ( | double | current_time | ) |
Let MSG be the next message event in the log, if current_time matches MSG's timestamp, for every DrakeLcmMessageHandlerInterface sub that's subscribed to MSG's channel, invoke sub's HandleMessage method.
Then, this function advances the log by exactly one message. This function does nothing if MSG is null (end of log) or current_time does not match MSG's timestamp.
| std::exception | if this instance is not constructed in read-only mode. |
|
overridevirtual |
Returns a URL describing the transport of this LCM interface.
When the URL refers to a transport offered by LCM itself (e.g., memq or udpm), then this function must return the conventional URL spelling. If the implementation of DrakeLcmInterface is using a non-standard back end, the result implementation-defined.
In either case, it is always formatted using URI syntax rules per the RFC(s).
Implements DrakeLcmInterface.
| double GetNextMessageTime | ( | ) | const |
Returns the time in seconds for the next logged message's occurrence time or infinity if there are no more messages in the current log.
| std::exception | if this instance is not constructed in read-only mode. |
This is a no-op for Read mode, and an exception in Write mode.
Implements DrakeLcmInterface.
| bool is_write | ( | ) | const |
Returns true if this instance is constructed in write-only mode.
|
delete |
|
delete |
|
overridevirtual |
Writes an entry occurred at timestamp with content data to the log file.
The current implementation blocks until writing is done.
| channel | Channel name. |
| data | Pointer to raw bytes. |
| data_size | Number of bytes in data. |
| time_sec | Time in seconds when the message is published. Since messages are save to the log file in the order of Publish calls, this function should only be called with non-decreasing second. Note that this parameter can be overwritten by the host system's clock if overwrite_publish_time_with_system_clock is true at construction time. |
| std::exception | if this instance is not constructed in write-only mode. |
Implements DrakeLcmInterface.
| uint64_t second_to_timestamp | ( | double | sec | ) | const |
Converts time (in seconds) relative to the starting time passed to the constructor to a timestamp in microseconds.
|
overridevirtual |
Subscribes handler to channel.
Multiple handlers can subscribe to the same channel.
| std::exception | if this instance is not constructed in read-only mode. |
Implements DrakeLcmInterface.
|
overridevirtual |
Subscribe to all channels; this is useful for logging and redirecting LCM traffic without regard to its content.
| std::exception | if this instance is not constructed in read-only mode. |
Implements DrakeLcmInterface.
|
overridevirtual |
This function is not yet supported for LCM logs, and will always throw.
Implements DrakeLcmInterface.
| double timestamp_to_second | ( | uint64_t | timestamp | ) | const |
Converts timestamp (in microseconds) to time (in seconds) relative to the starting time passed to the constructor.