Fix compilation errors in ydlidar_ros2_driver_node.cpp by adding defa…#62
Open
myselfbasil wants to merge 1 commit intoYDLIDAR:humblefrom
Open
Fix compilation errors in ydlidar_ros2_driver_node.cpp by adding defa…#62myselfbasil wants to merge 1 commit intoYDLIDAR:humblefrom
myselfbasil wants to merge 1 commit intoYDLIDAR:humblefrom
Conversation
…ult values to declare_parameter calls
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix Compilation Errors in ydlidar_ros2_driver_node.cpp
Description
This pull request addresses compilation errors in the
ydlidar_ros2_driver_node.cppfile of the YDLIDAR ROS 2 driver package, which prevented successful building of the ROS 2 workspace. The errors were caused by incorrect usage of therclcpp::Node::declare_parameterAPI in ROS 2 Humble, along with several compiler warnings. This PR fixes the errors, resolves the warnings, and ensures the driver compiles successfully while maintaining its functionality.Problem
The
colcon buildcommand failed when building theydlidar_ros2_driverpackage, producing errors in theydlidar_ros2_driver_node.cppfile. The primary issues were:Compilation Errors:
rclcpp::Node::declare_parametercalls (e.g.,node->declare_parameter("port")) were missing required default values. In ROS 2 Humble, thedeclare_parameterfunction requires at least a parameter name and a default value to infer the parameter type. This led to errors like:port,ignore_array,frame_id,baudrate,lidar_type,device_type,sample_rate,abnormal_check_count,intensity_bit,fixed_resolution,reversion,inverted,auto_reconnect,isSingleChannel,intensity,support_motor_dtr,debug,angle_max,angle_min,range_max,range_min,frequency, andinvalid_range_is_inf.Compiler Warnings:
p: At line 235, the variableconst auto& p = scan.points.at(i);was declared but unused, triggering a-Wunused-variablewarning.stop_scan_serviceandstart_scan_servicelambda functions had unused parameters (request_header,req,response), triggering-Wunused-parameterwarnings.Solution
The solution involves:
declare_parametercalls, using the values already assigned to variables (e.g.,str_optvalue,optval,b_optvalue,f_optvalue,invalid_range_is_inf) before the correspondingget_parametercalls, as these represent the intended defaults.std::string,int,bool,float) in thedeclare_parametercalls to ensure compatibility with the ROS 2 Humble API.pand marking unused lambda parameters with[[maybe_unused]]to suppress warnings.m1_mode,m2_mode, andm3_modeparameters, which were missing in the original code, to align with their usage inlaser.setWorkMode.Changes Made
src/ydlidar_ros2_driver/src/ydlidar_ros2_driver_node.cppdeclare_parameterCalls:node->declare_parametercalls to include default values and explicit type specifications. For example:params/ydlidar.yaml).m1_mode,m2_mode, andm3_modeasboolparameters, with defaults offalse,false, andtrue, respectively, to match their usage inlaser.setWorkMode. Note: These parameters are retrieved into anint i_v, which may require further review for type consistency.const auto& p = scan.points.at(i);at line 235, as the loop already usesscan.points[i]directly.[[maybe_unused]]to the lambda parameters instop_scan_serviceandstart_scan_service:Testing
The changes were tested in a ROS 2 Humble workspace with the following steps:
src/ydlidar_ros2_driver/src/ydlidar_ros2_driver_node.cppwith the updated code.source install/setup.bash ros2 run ydlidar_ros2_driver ydlidar_ros2_driver_nodescantopic (assuming a connected YDLIDAR device).Notes for Reviewers
"/dev/ydlidar",230400,180.0f) are based on the original variable assignments in the code. Please verify these against the YDLIDAR model in use (e.g., X4, S4, G4) and theparams/ydlidar.yamlfile, if available.m1_mode,m2_mode,m3_mode: These parameters are declared asboolbut retrieved into anint i_vforlaser.setWorkMode. This works due to implicit conversion (false→0,true→1), but please confirm ifsetWorkModeexpects specific integer values beyond0or1. If so, we may need to change these tointparameters (e.g.,node->declare_parameter<int>("m1_mode", 0);).params/ydlidar.yamlfile exists, ensure the defaults in this PR align with it or document any discrepancies.Checklist
colcon build.