Skip to content

Commit e30fa1a

Browse files
committed
Implemented primitive conversions for floating point values
1 parent d90cf4b commit e30fa1a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

mqtt_client/src/MqttClient.ros2.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ const std::string MqttClient::kRosMsgTypeMqttTopicPrefix =
6161

6262
const std::string MqttClient::kLatencyRosTopicPrefix = "~/latencies/";
6363

64+
template <typename T>
65+
T mqtt2float(mqtt::const_message_ptr mqtt_msg) {
66+
auto str_msg = mqtt_msg->to_string ();
67+
std::size_t pos;
68+
const T v = std::stold(str_msg, &pos);
69+
70+
if (pos != str_msg.size())
71+
throw std::invalid_argument ("not all charaters processed");
72+
73+
return v;
74+
}
75+
6476
template <typename T>
6577
T mqtt2int(mqtt::const_message_ptr mqtt_msg) {
6678
auto str_msg = mqtt_msg->to_string ();
@@ -155,9 +167,17 @@ bool fixedMqtt2PrimitiveRos(mqtt::const_message_ptr mqtt_msg,
155167
serializeRosMessage(msg, serialized_msg);
156168
return true;
157169
} else if (msg_type == "std_msgs/msg/Float32") {
158-
/// TODO
170+
std_msgs::msg::Float32 msg;
171+
msg.data = mqtt2float<float>(mqtt_msg);
172+
173+
serializeRosMessage(msg, serialized_msg);
174+
return true;
159175
} else if (msg_type == "std_msgs/msg/Float64") {
160-
/// TODO
176+
std_msgs::msg::Float64 msg;
177+
msg.data = mqtt2float<double>(mqtt_msg);
178+
179+
serializeRosMessage(msg, serialized_msg);
180+
return true;
161181
}
162182
} catch (const std::exception &) {
163183
}

0 commit comments

Comments
 (0)