forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99aa834
commit 7fc6472
Showing
5 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "drake/lcm/lcm_messages.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include "drake/common/drake_throw.h" | ||
#include "drake/common/nice_type_name.h" | ||
|
||
namespace drake { | ||
namespace lcm { | ||
|
||
/** | ||
Encodes an LCM message to a series of bytes. | ||
*/ | ||
template <typename Message> | ||
std::vector<uint8_t> EncodeLcmMessage(const Message& message) { | ||
const int num_bytes = message.getEncodedSize(); | ||
DRAKE_THROW_UNLESS(num_bytes >= 0); | ||
const size_t size_bytes = static_cast<size_t>(num_bytes); | ||
std::vector<uint8_t> bytes(size_bytes); | ||
message.encode(bytes.data(), 0, num_bytes); | ||
return bytes; | ||
} | ||
|
||
/** | ||
Decodes an LCM message from a series of bytes. | ||
@throws std::runtime_error if decoding fails. | ||
*/ | ||
template <typename Message> | ||
Message DecodeLcmMessage(const std::vector<uint8_t>& bytes) { | ||
Message message{}; | ||
const size_t size_decoded = message.decode(bytes.data(), 0, bytes.size()); | ||
if (size_decoded != bytes.size()) { | ||
throw std::runtime_error( | ||
"Error decoding message of type '" + NiceTypeName::Get(message) + "'"); | ||
} | ||
return message; | ||
} | ||
|
||
/** | ||
Compares two LCM messages of the same type to see if they are equal. | ||
*/ | ||
template <typename Message> | ||
bool AreLcmMessagesEqual(const Message& a, const Message& b) { | ||
return EncodeLcmMessage(a) == EncodeLcmMessage(b); | ||
} | ||
|
||
} // namespace lcm | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "drake/lcm/lcm_messages.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "drake/common/test_utilities/expect_throws_message.h" | ||
#include "drake/lcm/lcmt_drake_signal_utils.h" | ||
#include "drake/lcmt_drake_signal.hpp" | ||
|
||
namespace drake { | ||
namespace lcm { | ||
namespace { | ||
|
||
lcmt_drake_signal ModelMessage() { | ||
lcmt_drake_signal message{}; | ||
message.timestamp = 2; | ||
return message; | ||
} | ||
|
||
GTEST_TEST(LcmMessagesTest, EncodeDecodeLcmMessage) { | ||
lcmt_drake_signal message{}; | ||
EXPECT_FALSE( | ||
CompareLcmtDrakeSignalMessages(message, ModelMessage())); | ||
const std::vector<uint8_t> bytes = EncodeLcmMessage(ModelMessage()); | ||
EXPECT_GT(bytes.size(), 0); | ||
message = DecodeLcmMessage<lcmt_drake_signal>(bytes); | ||
EXPECT_TRUE( | ||
CompareLcmtDrakeSignalMessages(message, ModelMessage())); | ||
} | ||
|
||
GTEST_TEST(LcmMessagesTest, DecodeLcmMessageError) { | ||
const std::vector<uint8_t> bytes = EncodeLcmMessage(ModelMessage()); | ||
std::vector<uint8_t> corrupt_bytes = bytes; | ||
corrupt_bytes.at(0) = 0; | ||
DRAKE_EXPECT_THROWS_MESSAGE( | ||
DecodeLcmMessage<lcmt_drake_signal>(corrupt_bytes), | ||
std::runtime_error, | ||
"Error decoding message of type 'drake::lcmt_drake_signal'"); | ||
} | ||
|
||
GTEST_TEST(LcmMessagesTest, AreLcmMessagesEqual) { | ||
lcmt_drake_signal message{}; | ||
EXPECT_FALSE(AreLcmMessagesEqual(message, ModelMessage())); | ||
EXPECT_TRUE(AreLcmMessagesEqual(ModelMessage(), ModelMessage())); | ||
} | ||
|
||
} // namespace | ||
} // namespace lcm | ||
} // namespace drake |