什么是ROS?
ROS (Robot Operating System) 机器人操作系统,实际上是一个分布式计算框架,为机器人软件开发提供了一整套工具、库和约定。
为什么选择ROS?
- 模块化设计:每个功能可以独立开发和测试
- 丰富的生态:海量开源软件包
- 跨平台:支持Ubuntu、Windows、macOS
- 社区活跃:全球数十万开发者
环境搭建
安装Ubuntu
ROS推荐在Ubuntu上运行,以ROS Noetic为例(Ubuntu 20.04):
1 2 3 4 5 6 7 8 9 10
| sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt install curl curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update sudo apt install ros-noetic-desktop-full
|
初始化rosdep
1 2
| sudo rosdep init rosdep update
|
配置环境变量
1 2
| echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc source ~/.bashrc
|
核心概念
1. Node (节点)
Node是ROS中最小的计算单元,每个节点负责一个独立的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import rospy
def talker(): rospy.init_node('talker', anonymous=True) pub = rospy.Publisher('chatter', String, queue_size=10) rate = rospy.Rate(10) while not rospy.is_shutdown(): hello_str = "hello world %s" % rospy.get_time() pub.publish(hello_str) rate.sleep()
if __name__ == '__main__': talker()
|
2. Topic (话题)
Topic是节点间的通信管道,基于发布-订阅模式。
1 2 3 4 5 6 7 8
| def callback(data): rospy.loginfo("I heard: %s", data.data)
def listener(): rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) rospy.spin()
|
3. Message (消息)
消息是传输的数据结构:
1 2 3 4 5
| rosmsg show std_msgs/String
|
4. Service (服务)
提供请求-响应式通信:
1 2 3 4 5 6 7 8
| def handle_add_two_ints(req): return AddTwoIntsResponse(req.a + req.b)
def add_two_ints_server(): rospy.init_node('add_two_ints_server') s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints) rospy.spin()
|
创建工作空间
1 2 3 4 5 6 7 8
| mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc source ~/.bashrc
|
创建自己的包
1 2
| cd ~/catkin_ws/src catkin_create_pkg my_robot rospy std_msgs
|
包结构:
1 2 3 4
| my_robot/ ├── CMakeLists.txt ├── package.xml └── src/
|
常用命令
启动ROS Master
查看节点
1 2
| rosnode list rosnode info /node_name
|
查看话题
1 2 3
| rostopic list rostopic echo /topic_name rostopic hz /topic_name
|
可视化工具
rqt_graph:查看节点关系图
1
| rosrun rqt_graph rqt_graph
|
rviz:3D可视化
实战项目:控制小海龟
1 2 3 4 5
| rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
|
编写自己的控制节点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import rospy from geometry_msgs.msg import Twist
def move_turtle(): rospy.init_node('turtle_controller') pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) rate = rospy.Rate(10) vel = Twist() vel.linear.x = 2.0 vel.angular.z = 1.0 while not rospy.is_shutdown(): pub.publish(vel) rate.sleep()
if __name__ == '__main__': move_turtle()
|
调试技巧
1. 使用rostopic pub
手动发布消息测试:
1 2 3 4
| rostopic pub /turtle1/cmd_vel geometry_msgs/Twist "linear: x: 2.0 angular: z: 1.0"
|
2. 录制和回放
1 2 3 4 5
| rosbag record -a
rosbag play <bagfile>
|
3. 日志输出
1 2 3
| rospy.loginfo("Info message") rospy.logwarn("Warning message") rospy.logerr("Error message")
|
进阶学习路线
- TF变换:坐标系管理
- Navigation:导航框架
- MoveIt:运动规划
- Gazebo:仿真环境
总结
ROS为机器人开发提供了强大的工具链,虽然学习曲线陡峭,但掌握后能大幅提高开发效率。建议从小项目开始,逐步深入。
参考资源: