使用ros实现c++与python通信

创建工作空间(选择在桌面创建)

cd ~
$ mkdir -p my_workspace/src

编译工作空间

$ cd my_workspace
$ catkin_make

source一下新生成的setup.bash文件:

$ source devel/setup.bash

每次打开一个终端都要source一下

要想保证工作空间已配置正确需确保ROS_PACKAGE_PATH环境变量包含你的工作空间目录,采用以下命令查看:

$ echo $ROS_PACKAGE_PATH

进入到src文件夹中,使用catkin_create_pkg命令来创建一个新的程序包

$ catkin_create_pkg my_pkg std_msgs rospy roscpp

查看程序包依赖关系

$ rospack depends1 beginner_tutorials
~/catkin_test/src$ rospack depends1 beginner_tutorials 
roscpp
rospy
std_msgs

返回工作空间目录

cd ~/my_workspace

编译工作空间

catkin_make

在程序包中的src文件夹中创建talker.py和listener.cpp文件

talker.py中内容为:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

listener.cpp中内容为:

#include "ros/ros.h"
#include "std_msgs/String.h"

/**
 * This tutorial demonstrates simple receipt of messages over the ROS system.
 */
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
	ROS_INFO("I lol heard: [%s]", msg->data.c_str());
}

int main(int argc, char *argv[])
{
	/**
	 * The ros::init() function needs to see argc and argv so that it can perform
	 * any ROS arguments and name remapping that were provided at the command line.
	 * For programmatic remappings you can use a different version of init() which takes
	 * remappings directly, but for most command-line programs, passing argc and argv is
	 * the easiest way to do it.  The third argument to init() is the name of the node.
	 *
	 * You must call one of the versions of ros::init() before using any other
	 * part of the ROS system.
	 */
	ros::init(argc, argv, "listener");

	/**
	 * NodeHandle is the main access point to communications with the ROS system.
	 * The first NodeHandle constructed will fully initialize this node, and the last
	 * NodeHandle destructed will close down the node.
	 */
	ros::NodeHandle n;

	/**
	 * The subscribe() call is how you tell ROS that you want to receive messages
	 * on a given topic.  This invokes a call to the ROS
	 * master node, which keeps a registry of who is publishing and who
	 * is subscribing.  Messages are passed to a callback function, here
	 * called chatterCallback.  subscribe() returns a Subscriber object that you
	 * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
	 * object go out of scope, this callback will automatically be unsubscribed from
	 * this topic.
	 *
	 * The second parameter to the subscribe() function is the size of the message
	 * queue.  If messages are arriving faster than they are being processed, this
	 * is the number of messages that will be buffered up before beginning to throw
	 * away the oldest ones.
	 */
	ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

	/**
	 * ros::spin() will enter a loop, pumping callbacks.  With this version, all
	 * callbacks will be called from within this thread (the main one).  ros::spin()
	 * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
	 */
	ros::spin();

	return 0;
}

将talker.py和listener.cpp转化为可执行文件

chmod +x talker.py
chmod +x listener.cpp

输入ls查看目录中文件talker.py和listener.cpp变为绿色,即转化为可执行文件。

打开my_pkg程序包中的CMakeLists.txt,将以下代码加入:

include_directories(include ${catkin_INCLUDE_DIRS}) 

add_executable(listener src/listener.cpp) 
target_link_libraries(listener ${catkin_LIBRARIES}) 
add_dependencies(listener my_pkg_generate_messages_cpp)

进入工作空间目录

cd ~/my_workspace

编译工作空间

catkin_make

打开新的终端

roscore

打开新的终端,进入工作空间目录

cd ~/my_workspace

source一下

source devel/setup.bash

运行talker节点

rosrun my_pkg talker.py

格式为: rosrun 程序包名 节点文件

打开新的终端,进入工作空间,source一下,运行listener节点

rosrun my_pkg listener

listener后面不需要加后缀.cpp

  • 2
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值