Skip to content

Commit c7911ad

Browse files
committed
test
1 parent fe6ab9d commit c7911ad

File tree

4 files changed

+28
-48
lines changed

4 files changed

+28
-48
lines changed

src/stingray_core_communication/stingray_core_communication/src/link_node_base.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <array>
4-
#include <cstddef>
54
#include <cstdint>
65
#include <functional>
76
#include <memory>
@@ -116,11 +115,6 @@ class LinkNodeBase : public rclcpp::Node
116115
}
117116

118117
protected:
119-
virtual void onSerialBytes([[maybe_unused]] const uint8_t *data,
120-
[[maybe_unused]] size_t length)
121-
{
122-
}
123-
124118
void processIncoming()
125119
{
126120
stream_manager_.Process();
@@ -192,9 +186,8 @@ class LinkNodeBase : public rclcpp::Node
192186
return;
193187
}
194188

195-
const auto *data = msg->data.data();
189+
const void *data = msg->data.data();
196190
const auto length = static_cast<unsigned>(msg->data.size());
197-
onSerialBytes(data, static_cast<size_t>(length));
198191
hydrolib_RingQueue_Push(&tx_queue_, data, static_cast<uint16_t>(length));
199192
processIncoming();
200193
}

src/stingray_core_communication/stingray_core_communication/src/pressure_link_node.cpp

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <array>
2+
#include <bit>
3+
#include <cstdint>
24
#include <cstring>
35
#include <memory>
46
#include <string>
@@ -25,36 +27,20 @@ class PressureLinkNode : public baseLink::LinkNodeBase
2527
}
2628

2729
private:
28-
void onSerialBytes(const uint8_t *data, size_t length) override
30+
void publishRaw(const float value)
2931
{
30-
serial_line_buffer_.append(reinterpret_cast<const char *>(data), length);
31-
32-
size_t delimiter_pos = 0;
33-
while ((delimiter_pos = serial_line_buffer_.find_first_of("\r\n")) !=
34-
std::string::npos)
35-
{
36-
std::string line = serial_line_buffer_.substr(0, delimiter_pos);
37-
serial_line_buffer_.erase(0, delimiter_pos + 1);
38-
39-
while (!serial_line_buffer_.empty() &&
40-
(serial_line_buffer_.front() == '\r' ||
41-
serial_line_buffer_.front() == '\n'))
42-
{
43-
serial_line_buffer_.erase(0, 1);
44-
}
45-
46-
if (!line.empty())
47-
{
48-
publishRaw(line);
49-
}
50-
}
32+
auto msg = std_msgs::msg::String();
33+
msg.data = std::to_string(value);
34+
data_raw_pub_->publish(std::move(msg));
5135
}
5236

53-
void publishRaw(const std::string &raw_data)
37+
static float decodeFloatLE(const uint8_t *data)
5438
{
55-
auto msg = std_msgs::msg::String();
56-
msg.data = raw_data;
57-
data_raw_pub_->publish(std::move(msg));
39+
const uint32_t raw = static_cast<uint32_t>(data[0]) |
40+
(static_cast<uint32_t>(data[1]) << 8U) |
41+
(static_cast<uint32_t>(data[2]) << 16U) |
42+
(static_cast<uint32_t>(data[3]) << 24U);
43+
return std::bit_cast<float>(raw);
5844
}
5945

6046
hydrolib::ReturnCode memoryRead(void *buffer, unsigned address,
@@ -72,32 +58,33 @@ class PressureLinkNode : public baseLink::LinkNodeBase
7258
hydrolib::ReturnCode memoryWrite(const void *buffer, unsigned address,
7359
unsigned length)
7460
{
61+
RCLCPP_DEBUG(this->get_logger(),
62+
"Pressure slave write: addr=%u len=%u",
63+
address, length);
64+
7565
if (address + length > memory_.size())
7666
{
7767
return hydrolib::ReturnCode::FAIL;
7868
}
7969

8070
std::memcpy(memory_.data() + address, buffer, length);
8171

82-
if (address == 0U && length > 0U)
83-
{
84-
const auto *bytes = static_cast<const char *>(buffer);
85-
std::string raw_data(bytes, bytes + length);
86-
87-
const auto null_pos = raw_data.find('\0');
88-
if (null_pos != std::string::npos)
89-
{
90-
raw_data.resize(null_pos);
91-
}
72+
constexpr unsigned kValueAddress = 0U;
73+
constexpr unsigned kValueLength = sizeof(float);
74+
const bool value_is_written =
75+
(address <= kValueAddress) &&
76+
((address + length) >= (kValueAddress + kValueLength));
9277

93-
publishRaw(raw_data);
78+
if (value_is_written)
79+
{
80+
const float value = decodeFloatLE(memory_.data() + kValueAddress);
81+
publishRaw(value);
9482
}
9583

9684
return hydrolib::ReturnCode::OK;
9785
}
9886

9987
std::array<uint8_t, 256> memory_{};
100-
std::string serial_line_buffer_;
10188
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr data_raw_pub_;
10289
};
10390

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pressure_sensor:
22
ros__parameters:
33
dump_param: 1.0 # FIXME remove in the future
4-
data_topic: /pressure/data_raw
4+
data_topic: /data_raw

src/stingray_core_sensors/pressure_sensor/include/pressure_sensor/pressure_sensor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace stingray_core::pressure_sensor {
1212
struct PressureSensorConfig{
1313
PressureSensorConfig(const rclcpp::Node::SharedPtr& node)
1414
: dump_param(node->declare_parameter<double>("dump_param", DEFAULT_DUMP_PARAM)),
15-
data_topic(node->declare_parameter<std::string>("data_topic", "/pressure/data_raw"))
15+
data_topic(node->declare_parameter<std::string>("data_topic", "/data_raw"))
1616
{}
1717
const double dump_param;
1818
const std::string data_topic;

0 commit comments

Comments
 (0)