Skip to content

Commit 58c3265

Browse files
Fixed the rotation bug; maybe (#93)
1 parent 235bc80 commit 58c3265

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/sweepcpp/src/sweep_scan_node.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <thread>
66

77
#include "rclcpp/rclcpp.hpp"
8+
#include "rclcpp/qos.hpp"
89
#include "sensor_msgs/msg/laser_scan.hpp"
910
#include "sweep/sweep.hpp"
1011

@@ -19,9 +20,9 @@ class SweepScannerNode : public rclcpp::Node
1920
{
2021
// Declare parameters
2122
this->declare_parameter("topic", "scan");
22-
this->declare_parameter("serial_port", "/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DM00L4EB-if00-port0"); // usbport /dev/ttyUSB0
23-
this->declare_parameter("rotation_speed", 10); // 1-10
24-
this->declare_parameter("sample_rate", 1000); //500-1750
23+
this->declare_parameter("serial_port", "/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DM00L4EB-if00-port0"); // usbport /dev/ttyUSB0
24+
this->declare_parameter("rotation_speed", 3); // 1-10
25+
this->declare_parameter("sample_rate", 750); //500-1750
2526
this->declare_parameter("frame_id", "lidar");
2627

2728
// Get parameters
@@ -39,7 +40,10 @@ class SweepScannerNode : public rclcpp::Node
3940
_scanner = std::make_shared<sweep::sweep>(serial_port.c_str());
4041

4142
// Create publisher
42-
_lidar_pub = this->create_publisher<sensor_msgs::msg::LaserScan>(topic, 10);
43+
rclcpp::QoS qos(100); // Increased from 10 to handle TF delays
44+
qos.reliability(rclcpp::ReliabilityPolicy::Reliable);
45+
qos.durability(rclcpp::DurabilityPolicy::Volatile);
46+
_lidar_pub = this->create_publisher<sensor_msgs::msg::LaserScan>(topic, qos);
4347

4448
// Start scanner thread
4549
_scanner_thread_active = true;
@@ -78,18 +82,16 @@ class SweepScannerNode : public rclcpp::Node
7882
laser_scan_msg.header.frame_id = _frame_id;
7983
laser_scan_msg.header.stamp = this->now();
8084

81-
// Calculate samples per rotation
82-
float samples_per_rotation = static_cast<float>(_sample_rate) / static_cast<float>(_rotation_speed);
83-
85+
// Set up LaserScan with actual sample count, not expected
8486
laser_scan_msg.angle_min = 0.0;
8587
laser_scan_msg.angle_max = 2.0 * M_PI;
86-
laser_scan_msg.angle_increment = laser_scan_msg.angle_max / samples_per_rotation;
88+
laser_scan_msg.angle_increment = (2.0 * M_PI) / static_cast<float>(scan.samples.size());
8789
laser_scan_msg.time_increment = 1.0f / static_cast<float>(_sample_rate);
8890
laser_scan_msg.range_min = 0.0f;
8991
laser_scan_msg.range_max = 25.0f;
9092

91-
// Pre-fill ranges with infinity
92-
laser_scan_msg.ranges.assign(scan.samples.size(), std::numeric_limits<float>::infinity());
93+
// Allocate array for actual number of samples received
94+
laser_scan_msg.ranges.resize(scan.samples.size());
9395

9496
size_t idx = 0;
9597
for (auto [angle_milli_deg, distance_cm, signal_strength] : scan.samples)

0 commit comments

Comments
 (0)