# Edite configuration file: CamkeList.txt
catkin_install_python(PROGRAMS scripts/自定义文件名(hello_world).py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
# Build
# 1.command line method:
cd tutu_ws; catkin_make
# 2.vsc method: ctrl + shift + B
cd tutu_ws; code .
# Run
roscore; cd tutu_ws; source ./devel/setup.bash; rosrun helloworld hello_world.py
hello_world.py 的具体内容如下所示:
#! /usr/bin/env python
import rospy
# sys.path.insert(0,path + "/src/pkg/scripts") # 如果需要导入自己的包
# import your_py
if __name__ == "__main__":
rospy.init_node("Hello")
'''
在ROS msater中注册节点
@param name: 节点名称(唯一),节点名称中不能使用命名空间(不能包含 '/')
@type name: str
@param argv: 封装节点调用时传递的参数,存到参数服务器
@type argv: None
@param anonymous: 取值为 true 时,为节点名称后缀随机编号可以解决重名
@type anonymous: bool
'''
rospy.loginfo("Hello World!!!!") # 打印
也使用 launch
来实现 Run
步骤: 【可以摆脱需要多个 terminal 的问题】
cd tutu_ws; mkdir src/helloworld/launch; > new.launch
roslaunch helloworld new.launch
new.launch 的具体内容如下所示:
<launch>
<node pkg="helloworld" type="hello_world.py" name="hello" output="screen" />
</launch>
For C++:
- Modify Package Configuration File (package.xml)
<build_depend>message_generation</build_depend> <build_export_depend>message_runtime</build_export_depend> <exec_depend>message_runtime</exec_depend> <exec_depend>roscpp</exec_depend> <depend>std_msgs</depend>
- Modify Configuration File (CMakeLists.txt)
find_package(roscpp, ...) add_message_files(xxx.msg, ...) add_service_files(xxx.srv, ...) add_action_files(xxx.action, ...) generate_messages(std_msgs, ...) add_executable(X__ xxx.cpp) ## HELLOWORLD 基本步骤 1/2, rosrun helloworld X add_dependencies(xxx ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) target_link_libraries(X__ ${catkin_LIBRARIES}) ## HELLOWORLD 基本步骤 2/2
3. ROS 计算图 rqt_graph
启动:rosrun rqt_graph rqt_graph
参考资料如下: ROS理论与实践