通过launch文件中参数设置先锋移动速度

钱欣然
2023-12-01

参考http://www.rosclub.cn/post-158.html
circle.cpp
change the topic name to your topic
or you can make a launch file to remap the topic name

<launch>
<node pkg="" type="" name="">
    <remap from="topic_now" to="topic_you_want"/> 
</node>
</launch>
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)
{
      ros::init(argc, argv, "circle");
      ros::NodeHandle nh;
      geometry_msgs::Twist T;
      if(!nh.getParam("/speed", T.linear.x))
      {
         ROS_ERROR("Get Speed ERROR");
         return 0;  
      }
         T.angular.z = T.linear.x / 2.0;
         ros::Publisher twist_pb = nh.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 100);

        ros::Rate rate(1);
        while(ros::ok())
       {
             twist_pb.publish(T);
             ros::spinOnce();
             rate.sleep();
      }
}

Cmakelist.txt

cmake_minimum_required(VERSION 2.8.3)
project(turtlesim_launch)

find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES turtlesim_launch
#  CATKIN_DEPENDS roscpp
#  DEPENDS system_lib
)
include_directories(
 include
  ${catkin_INCLUDE_DIRS}
)
#上面使 环境变量${catkin_INCLUDE_DIRS} 有效
MESSAGE(STATUS "THIS is ___ "  ${catkin_INCLUDE_DIRS})
add_executable(circle_node src/circle.cpp)
target_link_libraries(circle_node  ${catkin_LIBRARIES})

1.launch

<launch>
    <node pkg="turtlesim_launch" type="circle_node" name="circle_node" output="screen"/>
    <param name="speed" value="1.0"/>
</launch>

CMakeList.txt中添加一句MESSAGE 增加一下对catkin 的认识吧

MESSAGE(STATUS "This __ "${catkin_include_dirs})
MESSAGE(STATUS "This___library__"${catkin_LIBRARIES})

输出为:

robot@ubuntu:~/practice_ws$ catkin_make
Base path: /home/robot/practice_ws
Source space: /home/robot/practice_ws/src
Build space: /home/robot/practice_ws/build
Devel space: /home/robot/practice_ws/devel
Install space: /home/robot/practice_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/robot/practice_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/robot/practice_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/hydro
-- This workspace overlays: /opt/ros/hydro
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Python version: 2.7
-- Using Debian Python package layout
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/robot/practice_ws/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- catkin 0.5.90
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - turtlesim_launch
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'turtlesim_launch'
-- ==> add_subdirectory(turtlesim_launch)
-- THIS is ___ /opt/ros/hydro/include/usr/include
 This___library__/opt/ros/hydro/lib/libroscpp.so/usr/lib/libboost_signals-mt.so/usr/lib/libboost_filesystem-mt.so/opt/ros/hydro/lib/librosconsole.so/opt/ros/hydro/lib/librosconsole_log4cxx.so/opt/ros/hydro/lib/librosconsole_backend_interface.so/usr/lib/liblog4cxx.so/usr/lib/libboost_regex-mt.so/opt/ros/hydro/lib/libroscpp_serialization.so/opt/ros/hydro/lib/librostime.so/usr/lib/libboost_date_time-mt.so/usr/lib/libboost_system-mt.so/usr/lib/libboost_thread-mt.so/usr/lib/x86_64-linux-gnu/libpthread.so/opt/ros/hydro/lib/libxmlrpcpp.so/opt/ros/hydro/lib/libcpp_common.so/opt/ros/hydro/lib/libconsole_bridge.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/robot/practice_ws/build
####
#### Running command: "make -j1 -l1" in "/home/robot/practice_ws/build"
####
[100%] Built target circle_node

两种获取参数服务器参数的方法
ros::NodeHandle::getParam():

ros::NodeHandle nh;
std::string global_name, relative_name, default_param;
if (nh.getParam("/global_name", global_name))
{
  ...
}

// Default value version
nh.param<std::string>("default_param", default_param, "default_value");

第一个参数为参数名,第二个参数为接收参数值的变量名。如果获得参数值失败,则返回false。nh.param()函数可以设置一个缺省值,如果获取失败,则设置为缺省值。这种方式下如果想要获得私有参数,则需要声明一个私有NodeHandle。(如:ros::NodeHandle nh(~))

ros::param::get()

std::string global_name, relative_name, default_param;
if (ros::param::get("/global_name", global_name))
{
  ...
}

// Default value version
ros::param::param<std::string>("default_param", default_param, "default_value");

如果想获得私有参数则需要在参数名前面加上”~”。(例如:ros::param::get(“~private_name”, param);)

 类似资料: