作者: Sam (甄峰) sam_code@hotmail.com
ROS学习过程中,Sam准备学习的第一个例子是:从头建立一个catkin package, 并创建一个发布者(Publisher)和订阅者(Subscriber)。
1. catkin package的创建:
Sam在 catkin_ws/src/Test_Source目录下,运行:
$catkin_create_pkg Message_Source std_msgs roscpp
创建了Message_Source这个catkin Package. 它依赖于std_msgs, roscpp等Package.
(这里是有问题的,Package Name应该以小写字母为开头)
所以修改为:
$catkin_create_pkg messager std_msgs roscpp
可以看到如下结果:
在catkin_ws/src/Test_Source目录下,可以看到messager目录。
而messager目录内又包含:
CMakeLists.txt include package.xml src
可以看到,它既包含CMakeLists.txt, 又包含package.xml. 所以,它正是一个catkin Package.
修改package.xml:
exbot
BSD
2. 创建一个Publisher Node.
在catkin_ws/src/Test_Source/Message_Source/src目录内,创建一个文件,talker_sam.cpp.
// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/String.h"
// %EndTag(MSG_HEADER)%
#include
int main(int argc, char **argv)
{
// %Tag(INIT)%
ros::init(argc, argv, "talker");
// %EndTag(INIT)%
// %Tag(NODEHANDLE)%
ros::NodeHandle n;
// %EndTag(NODEHANDLE)%
// %Tag(PUBLISHER)%
ros::Publisher chatter_pub = n.advertise("chatter", 1000);
// %EndTag(PUBLISHER)%
// %Tag(LOOP_RATE)%
ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%
// %Tag(ROS_OK)%
int count = 0;
while (ros::ok())
{
// %EndTag(ROS_OK)%
// %Tag(FILL_MESSAGE)%
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%
// %Tag(ROSCONSOLE)%
ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%
// %Tag(PUBLISH)%
chatter_pub.publish(msg);
// %EndTag(PUBLISH)%
// %Tag(SPINONCE)%
ros::spinOnce();
// %EndTag(SPINONCE)%
// %Tag(RATE_SLEEP)%
loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
++count;
}
return 0;
}
// %EndTag(FULLTEXT)%
3. 创建一个Subscriber:
在catkin_ws/src/Test_Source/Message_Source/src目录内,创建一个文件,listener_sam.cpp
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
return 0;
}
// %EndTag(FULLTEXT)%
owing disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// %Tag(FULLTEXT)%
#include "ros/ros.h"
#include "std_msgs/String.h"
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
return 0;
}
// %EndTag(FULLTEXT)%
4. 修改CMakeLists.txt
把talker_sam.cpp, listener.cpp添加入Makefile.
add_executable(talker_sam src/talker_sam.cpp)
target_link_libraries(talker_sam ${catkin_LIBRARIES})
add_executable(listener_sam src/listener_sam.cpp)
target_link_libraries(listener_sam ${catkin_LIBRARIES})
5. 运行与验证:
5.1:首先运行所有程序:
$roscore
$rosrun messager takler_sam
$rosrun messager listener_sam
可以看到,终端上各自打印出各类信息。
5.2:验证:
$rosrun rqt_graph rqt_graph
/talker---->/listener
Topic名为:/chatter
5.3:查看Topic内容:
前面可以看到Topic的名子为:/chatter
$rostopic echo /chatter
可以看到Topic内容。
$rostopic type /chatter
可以看到Topic Message内容。
ROS学习过程中,Sam准备学习的第一个例子是:从头建立一个catkin package, 并创建一个发布者(Publisher)和订阅者(Subscriber)。
1. catkin package的创建:
Sam在 catkin_ws/src/Test_Source目录下,运行:
$catkin_create_pkg Message_Source std_msgs roscpp
创建了Message_Source这个catkin Package. 它依赖于std_msgs, roscpp等Package.
(这里是有问题的,Package Name应该以小写字母为开头)
所以修改为:
$catkin_create_pkg messager std_msgs roscpp
可以看到如下结果:
在catkin_ws/src/Test_Source目录下,可以看到messager目录。
而messager目录内又包含:
CMakeLists.txt include package.xml src
可以看到,它既包含CMakeLists.txt, 又包含package.xml. 所以,它正是一个catkin Package.
修改package.xml:
exbot
BSD
2. 创建一个Publisher Node.
在catkin_ws/src/Test_Source/Message_Source/src目录内,创建一个文件,talker_sam.cpp.
// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/String.h"
// %EndTag(MSG_HEADER)%
#include
int main(int argc, char **argv)
{
// %Tag(INIT)%
ros::init(argc, argv, "talker");
// %EndTag(INIT)%
// %Tag(NODEHANDLE)%
ros::NodeHandle n;
// %EndTag(NODEHANDLE)%
// %Tag(PUBLISHER)%
ros::Publisher chatter_pub = n.advertise("chatter", 1000);
// %EndTag(PUBLISHER)%
// %Tag(LOOP_RATE)%
ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%
// %Tag(ROS_OK)%
int count = 0;
while (ros::ok())
{
// %EndTag(ROS_OK)%
// %Tag(FILL_MESSAGE)%
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%
// %Tag(ROSCONSOLE)%
ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%
// %Tag(PUBLISH)%
chatter_pub.publish(msg);
// %EndTag(PUBLISH)%
// %Tag(SPINONCE)%
ros::spinOnce();
// %EndTag(SPINONCE)%
// %Tag(RATE_SLEEP)%
loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
++count;
}
return 0;
}
// %EndTag(FULLTEXT)%
3. 创建一个Subscriber:
在catkin_ws/src/Test_Source/Message_Source/src目录内,创建一个文件,listener_sam.cpp
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
return 0;
}
// %EndTag(FULLTEXT)%
owing disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// %Tag(FULLTEXT)%
#include "ros/ros.h"
#include "std_msgs/String.h"
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
return 0;
}
// %EndTag(FULLTEXT)%
4. 修改CMakeLists.txt
把talker_sam.cpp, listener.cpp添加入Makefile.
add_executable(talker_sam src/talker_sam.cpp)
target_link_libraries(talker_sam ${catkin_LIBRARIES})
add_executable(listener_sam src/listener_sam.cpp)
target_link_libraries(listener_sam ${catkin_LIBRARIES})
5. 运行与验证:
5.1:首先运行所有程序:
$roscore
$rosrun messager takler_sam
$rosrun messager listener_sam
可以看到,终端上各自打印出各类信息。
5.2:验证:
$rosrun rqt_graph rqt_graph
/talker---->/listener
Topic名为:/chatter
5.3:查看Topic内容:
前面可以看到Topic的名子为:/chatter
$rostopic echo /chatter
可以看到Topic内容。
$rostopic type /chatter
可以看到Topic Message内容。