## 安装ros1 运行一键安装脚本 ```shell wget http://fishros.com/install -O fishros && . fishros ``` 安装会有提示,这里不做过多阐述 ## 创建并编译工作空间 ```shell mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src catkin_init_workspace cd ~/catkin_ws/ catkin_make source ~/catkin_ws/devel/setup.bash #设置环境变量 echo $ROS_PACKAGE_PATH #检查环境变量是否正确;正确的形式:(/home/root1/catkin_ws/src:/opt/ros/noetic/share) ``` ## 创建并编译功能包 ```shell cd ~/catkin_ws/src catkin_create_pkg turtle_circle rospy std_msgs geometry_msgs #创建功能包 cd ~/catkin_ws catkin_make source ~/catkin_ws/devel/setup.bash ``` ## 创建脚本文件 ```shell cd ~/catkin_ws/src/turtle_circle mkdir scripts cd scripts touch draw_circle.py gedit draw_circle.py #粘贴下面的代码 chmod +x draw_circle.py ``` ### 画圆代码 ```python #!/usr/bin/env python3 import rospy from geometry_msgs.msg import Twist import math def draw_circle(): # 初始化ROS节点 rospy.init_node('turtle_circle_node', anonymous=True) # 创建Publisher,发布到turtle1的cmd_vel话题 pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) # 设置循环频率 rate = rospy.Rate(10) # 10Hz # 创建Twist消息对象 vel_msg = Twist() # 设置线速度和角速度 linear_speed = 1.0 # 线速度 angular_speed = 1.0 # 角速度 radius = 1.0 # 圆半径 # 计算合适的线速度和角速度比例,使乌龟画圆 vel_msg.linear.x = linear_speed vel_msg.linear.y = 0.0 vel_msg.linear.z = 0.0 vel_msg.angular.x = 0.0 vel_msg.angular.y = 0.0 vel_msg.angular.z = angular_speed rospy.loginfo("开始让乌龟画圆...") rospy.loginfo("按Ctrl+C停止") try: while not rospy.is_shutdown(): # 发布速度指令 pub.publish(vel_msg) # 按照设定频率循环 rate.sleep() except rospy.ROSInterruptException: rospy.loginfo("程序被用户中断") # 停止乌龟运动 vel_msg.linear.x = 0.0 vel_msg.angular.z = 0.0 pub.publish(vel_msg) rospy.loginfo("乌龟停止运动") if __name__ == '__main__': try: draw_circle() except rospy.ROSInterruptException: pass ``` ### 画正方形代码 ```python #!/usr/bin/env python3 import rospy from geometry_msgs.msg import Twist import math import time def draw_square(): # 初始化ROS节点 rospy.init_node('turtle_square_node', anonymous=True) # 创建Publisher,发布到turtle1的cmd_vel话题 pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) # 设置循环频率 rate = rospy.Rate(10) # 10Hz # 创建Twist消息对象 vel_msg = Twist() # 设置参数 linear_speed = 1.0 # 线速度 angular_speed = 1.0 # 角速度 side_length = 2.0 # 正方形边长 rospy.loginfo("开始让乌龟画正方形...") rospy.loginfo("按Ctrl+C停止") try: # 画4条边 for i in range(4): rospy.loginfo(f"画第 {i+1} 条边...") # 直走:根据边长计算需要的时间 move_time = side_length / linear_speed start_time = time.time() vel_msg.linear.x = linear_speed vel_msg.angular.z = 0.0 while (time.time() - start_time) < move_time and not rospy.is_shutdown(): pub.publish(vel_msg) rate.sleep() # 转向90度:计算需要的时间 # 角度 = 角速度 * 时间,所以 时间 = 角度 / 角速度 turn_time = (math.pi / 2) / angular_speed start_time = time.time() vel_msg.linear.x = 0.0 vel_msg.angular.z = angular_speed while (time.time() - start_time) < turn_time and not rospy.is_shutdown(): pub.publish(vel_msg) rate.sleep() rospy.loginfo("正方形绘制完成") except rospy.ROSInterruptException: rospy.loginfo("程序被用户中断") # 停止乌龟运动 vel_msg.linear.x = 0.0 vel_msg.angular.z = 0.0 pub.publish(vel_msg) rospy.loginfo("乌龟停止运动") if __name__ == '__main__': try: draw_square() except rospy.ROSInterruptException: pass ``` ## 使用键盘控制乌龟移动 注意这里需要开2个终端,第一个用来创建乌龟,第二个用来发送wsad的指令来控制乌龟移动 ```shell roscore & rosrun turtlesim turtlesim_node rosrun turtlesim turtle_teleop_key ``` ## 运行节点 1. 启动 turtlesim_node 2. 运行你的 draw_circle 节点 ```shell source ~/catkin_ws/devel/setup.bash roscore & rosrun turtlesim turtlesim_node rosrun turtle_circle draw_circle.py ``` ## 拓展内容 ### 双乌龟 #### 添加乌龟 ```shell rosservice call /spawn 3 3 0 turtle2 ``` #### 模仿上只运动 ```shell rosrun turtlesim mimic input:=turtle1 output:=turtle2 ``` #### 改变初始位置 ```shell rosservice call /turtle1/teleport_absolute 1 1 0 ``` 
没有评论