ros wiki learning

来源:互联网 发布:java线程之间的通信 编辑:程序博客网 时间:2024/05/16 12:35

mo1、ROS now includes software for tasks ranging from navigation and localization (SLAM),3D object recognition, action planning, motion control for multi-jointed arms, machine learning and even playing billiards.

2、第一部分的学习按照ros wiki http://wiki.ros.org/ROS/Tutorials
2.1ROS filesystem
rospack allows you to get information about packages.
这里写图片描述
2.2Creating a ROS Package创建一个ROS包
2.2.1创建一个catkin包

(1)首先创建一个catkin工作空间
http://wiki.ros.org/catkin/Tutorials/create_a_workspace
source /opt/ros/indigo/setup.bash
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
source devel/setup.bash
(2)其次创建catkin包
cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
(3)Building a catkin workspace and sourcing the setup file
cd ~/catkin_ws
catkin_make
. ~/catkin_ws/devel/setup.bash(这是必须的一步add the workspace to your ROS environment you need to source the generated setup file:)

rospack depends1 rospy(std_msgs roscpp rospy创建的时候的第一个)
rospack depends rospy

2.3 Build a ROS package
Before continuing remember to source your environment setup file if you have not already每次创建ROS包的时候,要先改变一下环境变量。
source /opt/ros/indigo/setup.bash
cd ~/catkin_ws
mkdir build
cd build
cmake ..
make
sudo make install
或者catkin_make

2.4理解ROS节点和roscore, rosnode, and rosrun 等命令行的使用
这里写图片描述

Nodes can publish or subscribe to a Topic. Nodes can also provide or use a Service.
2.4.1常用命令
1、roscore is the first thing you should run when using ROS.
2、rosnode displays information about the ROS nodes that are currently running.
2.1 The rosnode list command lists these active nodes:
rosnode list
2.2 The rosnode info command returns information about a specific node.
rosnode info /rosout
这里写图片描述
3、rosrun allows you to use the package name to directly run a node within a package (without having to know the package path).

$ rosrun [package_name] [node_name]

rosrun turtlesim turtlesim_node
这里写图片描述

这里写图片描述
按Ctrl+c结束进程。

http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics
4、理解ROS的主题(ROS topic)(rqt-graph)
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
这里写图片描述
The turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesim subscribes to the same topic to receive the key strokes.

rosrun rqt_graph rqt_graph

rosrun rqt_graph rqt_graph

这里写图片描述
当我们把鼠标放在/turtle1/cmd_vel上时,会发现ROS节点(/turtlesim和/teleop_turtle)通过主题(turtle1/cmd_vel)通讯。

5、ROS messages
Communication on topics happens by sending ROS messages between nodes.

rostopic type /turtle1/cmd_vel [rostopic type [topic]] 话题的类型
rosmsg show geometry_msgs/Twist 【rosmsg show [type]】具体的信息
这里写图片描述
用法:rostopic pub [topic] [msg_type] [args]
rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist – ‘[2.0, 0.0, 0.0]’ ‘[0.0, 0.0, 1.8]’
这里写图片描述
这个是执行一次的结果

执行四次之后会形成一个圆形

The previous command will send a single message to turtlesim telling it to move with an linear velocity of 2.0, and an angular velocity of 1.8 .

6、rosservice的应用(http://wiki.ros.org/ROS/Tutorials/UnderstandingServicesParams)

Services are another way that nodes can communicate with each other. Services allow nodes to send a request and receive a response.

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
rosservice list
rosservice type /clear
rosservice call /clear(清除turtlesim之前的轨迹)

这里写图片描述

rosservice type /spawn |rossrv show
rosservice call /spawn 2 2 0.2 ” ”
这里写图片描述

6.1 rosparam的应用
(1)rosparam list (list parameter names)
这里写图片描述
(2)rosparam set [param_name]
rosparam get [param_name]
rosparam set /background_r 150
rosservice call /clear(This changes the parameter value, now we have to call the clear service for the parameter change to take effect: )
执行完之后会有背景颜色的变化。
这个是background_r为0的时候

这个是background_r为150的时候。很明显颜色发生了很大的变化

7、rqt_console和roslaunch
we use rqt_console and rqt_logger_level for debugging and roslaunch for starting many nodes at once。
我们使用rqt_console和rqt_logger_level来做调试。
使用roslaunch来一次性的启动多个节点。
7.1第一步:在两个terminal里面分别输入
rosrun rqt_console rqt_console
rosrun rqt_logger_level rqt_logger_level
rqt
rqt_logger

这个时候启用turtlesim节点,rosrun turtlesim turtlesim_node
这里写图片描述
执行完这一句会发现rqt_console上一直在报警。rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 – ‘{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0,y: 0.0,z: 0.0}}’
(注意点:在rqt_loggers上显示出新的节点,最右端warn下refresh)

roslaunch to bring up multiple turtlesim nodes and a mimicking node to cause one turtlesim to mimic(模仿) another.下面来学习roslaunch.
7.2roslaunch(http://wiki.ros.org/ROS/Tutorials/UsingRqtconsoleRoslaunch)
这里写图片描述
这里写图片描述
这里是一定要注意的,在没有 source the environment 的情况下是不可能roscd beginner_tutorials的,下次一定要注意这个问题。

cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials
这里写图片描述
mkdir launch
touch turtlemimic.launch
gedit turtlemimic.launch

这时候编辑launch文件
这里写图片描述
Here we start two groups with a namespace tag of turtlesim1 and turtlesim2 with a turtlesim node with a name of sim. This allows us to start two simulators without having name conflicts. (group部分是给两个turtlesim定义两个名字,这样他们模仿的时候不会出现命名冲突。)

Here we start the mimic node with the topics input and output renamed to turtlesim1 and turtlesim2. This renaming will cause turtlesim2 to mimic turtlesim1. (把主题的输入改成turtlesim1,输出改成turtlesim2,可以让那个turtlesim2模仿turtlesim1)

roslaunch beginner_tutorials turtlemimic.launch

rostopic pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 – ‘[2.0, 0.0, 0.0]’ ‘[0.0, 0.0, -1.8]’
这个指令是对turtle1来实现的,但是turtle2也跟着一起运动。
这里写图片描述

这个时候在终端输入rqt_graph可得:
这里写图片描述

8、创建一个ROS的信息和服务(Creating a ROS msg and srv)
msg: msg files are simple text files that describe the fields of a ROS message.
srv: an srv file describes a service. It is composed of two parts: a request and a response.
http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv

9、Writing a Simple Publisher and Subscriber (python)
用python写一个简单的发布者和订阅者程序。
http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials
mkdir scripts
cd scripts
wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py
chmod +x talker.py

1 #!/usr/bin/env python (每个Python的ROS节点都要在开头声明这一句,这一句保证了是以python脚本来实现的)
2 # license removed for brevity
3 import rospy
4 from std_msgs.msg import String
5
6 def talker():
7 pub = rospy.Publisher(‘chatter’, String, queue_size=10)
8 rospy.init_node(‘talker’, anonymous=True)
9 rate = rospy.Rate(10) # 10hz
10 while not rospy.is_shutdown():
11 hello_str = “hello world %s” % rospy.get_time()
12 rospy.loginfo(hello_str)
13 pub.publish(hello_str)
14 rate.sleep()
15
16 if name == ‘main‘:
17 try:
18 talker()
19 except rospy.ROSInterruptException:
20 pass

用python订阅节点。
cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials/scripts/
wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py
chmod +x listener.py
下面两部是一定要执行的,因为这在空间里面build your nodes.
cd ~/catkin_ws
catkin_make

代码详细片段:
1 #!/usr/bin/env python
2 import rospy
3 from std_msgs.msg import String
4
5 def callback(data):
6 rospy.loginfo(rospy.get_caller_id() + “I heard %s”, data.data)
7
8 def listener():
9
10 # In ROS, nodes are uniquely named. If two nodes with the same
11 # node are launched, the previous one is kicked off. The
12 # anonymous=True flag means that rospy will choose a unique
13 # name for our ‘listener’ node so that multiple listeners can
14 # run simultaneously.
15 rospy.init_node(‘listener’, anonymous=True)
16
17 rospy.Subscriber(“chatter”, String, callback)
18
19 # spin() simply keeps python from exiting until this node is stopped
20 rospy.spin()
21
22 if name == ‘main‘:
23 listener()

这里写图片描述

这里写图片描述

10、Writing a Simple Service and Client (Python)
10.1 Service
cd ~/catkin_ws
source devel/setup.bash
roscd beginner_tutorials/scripts
touch add_two_ints_server.py
gedit add_two_ints_server.py
cd ..
chmod +x scripts/add_two_ints_server.py(make the node executable)

这里写图片描述
这里我们声明了一个名为add_Two_ints_server的Service, 类型为AddTwoInts,所有的处理都交给handle_add_two_ints.

10.2 Clients
接着server的那部分,
这里写图片描述
cd scripts/
touch add_two_ints_client.py
gedit add_two_ints_client.py
这里写图片描述

cd ..
chmod +x scripts/add_two_ints_client.py

move_base运行之前需要4个配置文件,
这里写图片描述

0 0
原创粉丝点击