Skip to content

Commit ba1c821

Browse files
committed
Fix typos, documentation links, Int64 handling and add test
1 parent 0649f99 commit ba1c821

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ bridge:
339339
340340
As seen in the [Quick Start](#quick-start), the *mqtt_client* can not only exchange arbitrary ROS messages with other *mqtt_clients*, but it can also exchange primitive message data with other non-*mqtt_client* MQTT clients. This allows ROS-based devices to exchange primitive messages with devices not based on ROS. The `primitive` parameter can be set for both ROS-to-MQTT (`bridge/ros2mqtt`) and for MQTT-to-ROS (`bridge/mqtt2ros`) transmissions.
341341

342-
If a ROS-to-MQTT transmission is configured as `primitive` and the ROS message type is one of the supported primitive ROS message types, the raw data is published as a string. The supported primitive ROS message types are [`std_msgs/String`](http://docs.ros.org/en/api/std_msgs/html/msg/String.html), [`std_msgs/Bool`](http://docs.ros.org/en/api/std_msgs/html/msg/Bool.html), [`std_msgs/Char`](http://docs.ros.org/en/api/std_msgs/html/msg/Char.html), [`std_msgs/UInt8`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt8.html), [`std_msgs/UInt16`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt16.html), [`std_msgs/UInt32`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt32.html), [`std_msgs/UInt64`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt16.html), [`std_msgs/Int8`](http://docs.ros.org/en/api/std_msgs/html/msg/Int8.html), [`std_msgs/Int16`](http://docs.ros.org/en/api/std_msgs/html/msg/Int16.html), [`std_msgs/Int32`](http://docs.ros.org/en/api/std_msgs/html/msg/Int32.html), [`std_msgs/Int64`](http://docs.ros.org/en/api/std_msgs/html/msg/Int64.html), [`std_msgs/Float32`](http://docs.ros.org/en/api/std_msgs/html/msg/Float32.html), [`std_msgs/Float32`](http://docs.ros.org/en/api/std_msgs/html/msg/Float64.html).
342+
If a ROS-to-MQTT transmission is configured as `primitive` and the ROS message type is one of the supported primitive ROS message types, the raw data is published as a string. The supported primitive ROS message types are [`std_msgs/String`](http://docs.ros.org/en/api/std_msgs/html/msg/String.html), [`std_msgs/Bool`](http://docs.ros.org/en/api/std_msgs/html/msg/Bool.html), [`std_msgs/Char`](http://docs.ros.org/en/api/std_msgs/html/msg/Char.html), [`std_msgs/UInt8`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt8.html), [`std_msgs/UInt16`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt16.html), [`std_msgs/UInt32`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt32.html), [`std_msgs/UInt64`](http://docs.ros.org/en/api/std_msgs/html/msg/UInt64.html), [`std_msgs/Int8`](http://docs.ros.org/en/api/std_msgs/html/msg/Int8.html), [`std_msgs/Int16`](http://docs.ros.org/en/api/std_msgs/html/msg/Int16.html), [`std_msgs/Int32`](http://docs.ros.org/en/api/std_msgs/html/msg/Int32.html), [`std_msgs/Int64`](http://docs.ros.org/en/api/std_msgs/html/msg/Int64.html), [`std_msgs/Float32`](http://docs.ros.org/en/api/std_msgs/html/msg/Float32.html), [`std_msgs/Float64`](http://docs.ros.org/en/api/std_msgs/html/msg/Float64.html).
343343

344344
If an MQTT-to-ROS transmission is configured as `primitive`, the MQTT message is interpreted and published as a primitive data type, if possible. The message is probed in the following order: `bool` ([`std_msgs/Bool`](http://docs.ros.org/en/api/std_msgs/html/msg/Bool.html)), `int` ([`std_msgs/Int32`](http://docs.ros.org/en/api/std_msgs/html/msg/Int32.html)), `float` ([`std_msgs/Float32`](http://docs.ros.org/en/api/std_msgs/html/msg/Float32.html)), `string` ([`std_msgs/String`](http://docs.ros.org/en/api/std_msgs/html/msg/String.html)).
345345

mqtt_client/CMakeLists.txt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,15 @@ if(${ROS_VERSION} EQUAL 2)
6969
FILES_MATCHING PATTERN "*ros2*"
7070
)
7171

72-
# if(BUILD_TESTING)
73-
# find_package(ament_lint_auto REQUIRED)
74-
# # the following line skips the linter which checks for copyrights
75-
# # comment the line when a copyright and license is added to all source files
76-
# set(ament_cmake_copyright_FOUND TRUE)
77-
# # the following line skips cpplint (only works in a git repo)
78-
# # comment the line when this package is in a git repo and when
79-
# # a copyright and license is added to all source files
80-
# set(ament_cmake_cpplint_FOUND TRUE)
81-
# ament_lint_auto_find_test_dependencies()
82-
# endif()
72+
if(BUILD_TESTING)
73+
find_package(ament_lint_auto REQUIRED)
74+
set(ament_cmake_cpplint_FOUND TRUE)
75+
ament_lint_auto_find_test_dependencies()
76+
77+
find_package(ament_cmake_gtest REQUIRED)
78+
ament_add_gtest(test_fixed_primitive test/test_fixed_primitive.cpp)
79+
target_link_libraries(test_fixed_primitive ${PROJECT_NAME}_lib)
80+
endif()
8381

8482
ament_package()
8583

mqtt_client/include/mqtt_client/MqttClient.ros2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class MqttClient : public rclcpp::Node,
199199
const Ros2MqttInterface& ros2mqtt) const;
200200

201201
/**
202-
* @brief Get the candiate topic endpoints for subscription matching
202+
* @brief Get the candidate topic endpoints for subscription matching
203203
*
204204
* @param ros2mqtt the ROS to MQTT interface spec
205205
*

mqtt_client/src/MqttClient.ros2.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ T mqtt2float(mqtt::const_message_ptr mqtt_msg) {
6767
std::size_t pos;
6868
const T v = std::stold(str_msg, &pos);
6969

70-
if (pos != str_msg.size())
71-
throw std::invalid_argument ("not all charaters processed");
70+
if (pos != str_msg.size())
71+
throw std::invalid_argument ("not all characters processed");
7272

7373
return v;
7474
}
@@ -79,8 +79,8 @@ T mqtt2int(mqtt::const_message_ptr mqtt_msg) {
7979
std::size_t pos;
8080
const T v = std::stoll(str_msg, &pos);
8181

82-
if (pos != str_msg.size())
83-
throw std::invalid_argument ("not all charaters processed");
82+
if (pos != str_msg.size())
83+
throw std::invalid_argument ("not all characters processed");
8484

8585
return v;
8686
}
@@ -150,9 +150,9 @@ bool fixedMqtt2PrimitiveRos(mqtt::const_message_ptr mqtt_msg,
150150
msg.data = mqtt2int<int32_t>(mqtt_msg);
151151

152152
serializeRosMessage(msg, serialized_msg);
153-
} else if (msg_type == "std_msgs/msg/Int64") {
154-
std_msgs::msg::Int32 msg;
155-
msg.data = mqtt2int<int32_t>(mqtt_msg);
153+
} else if (msg_type == "std_msgs/msg/Int64") {
154+
std_msgs::msg::Int64 msg;
155+
msg.data = mqtt2int<int64_t>(mqtt_msg);
156156

157157
serializeRosMessage(msg, serialized_msg);
158158
} else if (msg_type == "std_msgs/msg/Float32") {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <gtest/gtest.h>
2+
#include <mqtt/message.h>
3+
#include <std_msgs/msg/int64.hpp>
4+
#include <rclcpp/serialized_message.hpp>
5+
#include "mqtt_client/MqttClient.ros2.hpp"
6+
7+
namespace mqtt_client {
8+
// forward declaration of function under test
9+
bool fixedMqtt2PrimitiveRos(mqtt::const_message_ptr mqtt_msg,
10+
const std::string& msg_type,
11+
rclcpp::SerializedMessage &serialized_msg);
12+
}
13+
14+
TEST(FixedMqtt2PrimitiveRos, Int64Conversion) {
15+
auto mqtt_msg = mqtt::make_message("test", "42");
16+
rclcpp::SerializedMessage serialized;
17+
ASSERT_TRUE(mqtt_client::fixedMqtt2PrimitiveRos(mqtt_msg,
18+
"std_msgs/msg/Int64",
19+
serialized));
20+
std_msgs::msg::Int64 out;
21+
mqtt_client::deserializeRosMessage(serialized, out);
22+
EXPECT_EQ(out.data, 42);
23+
}
24+
25+
int main(int argc, char **argv) {
26+
testing::InitGoogleTest(&argc, argv);
27+
return RUN_ALL_TESTS();
28+
}

0 commit comments

Comments
 (0)