-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for publishing position data from MQTT #676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
303846a
Add support for publishing position data from MQTT
samuelthoren 8427619
Merge branch 'fix/format' into feature/publish_mqtt_data
samuelthoren 4ec5fd0
Merge remote-tracking branch 'origin/dev' into feature/publish_mqtt_data
samuelthoren 4c7e18a
Fix review comments
samuelthoren 31c8f56
Merge remote-tracking branch 'origin/dev' into feature/publish_mqtt_data
samuelthoren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,41 @@ | ||
| import os | ||
| import sys | ||
|
|
||
| from ament_index_python.packages import get_package_prefix | ||
|
|
||
| sys.path.insert( | ||
| 0, | ||
| os.path.join( # Need to modify the sys.path since we launch from the ros2 installed path | ||
| get_package_prefix("atos"), "share", "atos", "launch" | ||
| ), | ||
| ) | ||
|
|
||
| import launch_utils.launch_base as launch_base | ||
| from launch import LaunchDescription | ||
| from launch_ros.actions import Node | ||
|
|
||
|
|
||
| def get_mqtt_nodes(): | ||
| files = launch_base.get_files() | ||
| return [ | ||
| Node( | ||
| package="atos", | ||
| namespace="atos", | ||
| executable="mqtt_bridge", | ||
| name="mqtt_bridge", | ||
| # prefix=['gdbserver localhost:3000'], ## To use with VSC debugger | ||
| parameters=[files["params"]], | ||
| # arguments=["--ros-args", "--log-level", "debug"], # To get RCL_DEBUG prints | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| def generate_launch_description(): | ||
| base_nodes = launch_base.get_base_nodes() | ||
|
|
||
| mqtt_nodes = get_mqtt_nodes() | ||
|
|
||
| for node in mqtt_nodes: | ||
| base_nodes.append(node) | ||
|
|
||
| return LaunchDescription(base_nodes) |
This file contains hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <memory> | ||
| #include <regex> | ||
| #include <string> | ||
|
|
||
| #include <rclcpp/clock.hpp> | ||
| #include <rclcpp/logging.hpp> | ||
| #include <rclcpp/serialization.hpp> | ||
| #include <rclcpp/serialized_message.hpp> | ||
| #include <sensor_msgs/msg/nav_sat_fix.hpp> | ||
| #include <std_msgs/msg/empty.hpp> | ||
|
|
||
| class Mqtt2RosUtils { | ||
|
|
||
| public: | ||
| Mqtt2RosUtils() = delete; | ||
|
|
||
| static sensor_msgs::msg::NavSatFix mqtt2navsatfix(const std::string& payload) { | ||
| std::string payload_lowercase = payload; | ||
| std::transform(payload_lowercase.begin(), payload_lowercase.end(), payload_lowercase.begin(), ::tolower); | ||
|
|
||
| auto msg = sensor_msgs::msg::NavSatFix(); | ||
| msg.header.stamp = rclcpp::Clock(RCL_ROS_TIME).now(); | ||
| msg.latitude = extract_value_from_payload(payload_lowercase, {"lat", "latitude"}); | ||
| msg.longitude = extract_value_from_payload(payload_lowercase, {"lon", "lng", "long", "longitude"}); | ||
| msg.altitude = extract_value_from_payload(payload_lowercase, {"alt", "altitude"}); | ||
|
|
||
| return msg; | ||
| } | ||
|
|
||
| static rclcpp::SerializedMessage get_serialized_msg(const std::string& msg_type, const std::string& payload) { | ||
| if (msg_type == "std_msgs/msg/Empty") { | ||
| auto msg = std_msgs::msg::Empty(); | ||
| auto serialized = rclcpp::SerializedMessage(); | ||
| rclcpp::Serialization<std_msgs::msg::Empty> serializer; | ||
| serializer.serialize_message(&msg, &serialized); | ||
| return serialized; | ||
|
|
||
| } else if (msg_type == "sensor_msgs/msg/NavSatFix") { | ||
| auto msg = mqtt2navsatfix(payload); | ||
| auto serialized = rclcpp::SerializedMessage(); | ||
| rclcpp::Serialization<sensor_msgs::msg::NavSatFix> serializer; | ||
| serializer.serialize_message(&msg, &serialized); | ||
| return serialized; | ||
| } | ||
|
|
||
| RCLCPP_WARN(rclcpp::get_logger("mqtt2ros_utils"), | ||
| "Message type %s not supported. Returning empty serialized message", | ||
| msg_type.c_str()); | ||
| return rclcpp::SerializedMessage(); | ||
| } | ||
|
|
||
| private: | ||
| static double extract_value_from_payload(const std::string& input, std::vector<std::string> keys) { | ||
| // Lowercase input | ||
| std::string str = input; | ||
| std::transform(str.begin(), str.end(), str.begin(), ::tolower); | ||
|
|
||
| // Sort keys by length to avoid wrong match | ||
| std::sort( | ||
| keys.begin(), keys.end(), [](const std::string& a, const std::string& b) { return a.size() > b.size(); }); | ||
|
|
||
| // Build regex so we can match any of the keys (key1|key2|...) | ||
| std::string key_pattern; | ||
| for (size_t i{0}; i < keys.size(); ++i) { | ||
| key_pattern += keys.at(i); | ||
| if (i != keys.size() - 1) | ||
| key_pattern += "|"; | ||
| } | ||
|
|
||
| // Extract numeric value for any matching key (supports JSON or key: value formats) | ||
| std::regex re{"(?:^|[\\s,{])\"?(" + key_pattern + ")\"?\\s*[:=]\\s*([+-]?\\d+(\\.\\d+)?([eE][+-]?\\d+)?)"}; | ||
|
|
||
| std::smatch match; | ||
| double value{0.0}; | ||
|
|
||
| // Iterate through all matches, keep the last one in case inputs are duplicated | ||
| auto begin{str.cbegin()}; | ||
| auto end{str.cend()}; | ||
| while (std::regex_search(begin, end, match, re)) { | ||
| try { | ||
| value = std::stod(match[2].str()); | ||
| } catch (...) { | ||
| value = 0.0; | ||
| } | ||
| begin = match.suffix().first; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| }; | ||
This file contains hidden or 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 hidden or 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,114 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "mqtt2ros_utils.hpp" | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadStringFullKeys) { | ||
| std::string payload{"latitude: 1.0, longitude: 2.0, altitude: 3.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadStringShortKeys) { | ||
| std::string payload{"lat: 1.0, lon: 2.0, alt: 3.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadStringMixedKeys) { | ||
| std::string payload{"latitude: 1.0, longitude: 2.0, alt: 3.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadJsonFullKeys) { | ||
| std::string payload{"{\"latitude\": 1.0, \"longitude\": 2.0, \"altitude\": 3.0}"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadJsonShortKeys) { | ||
| std::string payload{"{\"lat\": 1.0, \"lon\": 2.0, \"alt\": 3.0}"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadJsonMixedKeys) { | ||
| std::string payload{"{\"latitude\": 1.0, \"longitude\": 2.0, \"alt\": 3.0}"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadValuesIsInt) { | ||
| std::string payload{"latitude: 1, longitude: 2, altitude: 3"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadHasSpaceSeparator) { | ||
| std::string payload{"latitude: 1 longitude: 2 altitude: 3 "}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadContainsNegativeValues) { | ||
| std::string payload{"lat: 1.0, lon: 2.0, alt: -3.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, -3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, PayloadMissingSomeKeys) { | ||
| std::string payload{"lat: 1.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 0.0); | ||
| EXPECT_EQ(msg.altitude, 0.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, MixedJsonAndKeyValue) { | ||
| std::string payload{"latitude: 1.0, \"longitude\": 2.0, alt: 3.0"}; | ||
|
|
||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } | ||
|
|
||
| TEST(Mqtt2RosNavSatParser, OrderIndependence) { | ||
| std::string payload{"alt: 3.0, lat: 1.0, lon: 2.0"}; | ||
| const auto msg{Mqtt2RosUtils::mqtt2navsatfix(payload)}; | ||
|
|
||
| EXPECT_EQ(msg.latitude, 1.0); | ||
| EXPECT_EQ(msg.longitude, 2.0); | ||
| EXPECT_EQ(msg.altitude, 3.0); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.