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

想想两个节点直接只需要通过话题通信就可以了,所以一个节点用C++写,一个节点用Python写似乎没有影响啊,只要保证两者之间的通信OK就好了,就有点像通信协议,一个STM32板子和一个FPGA板子,无所谓,只要遵循同一个通信协议就可以进行通信了呀对不对。

转载自:https://blog.csdn.net/qq_41816368/article/details/88046200

使用ros实现c++python通信

河北一帆 2019-02-28 22:29:28 2224 收藏 9

最后发布:2019-02-28 22:29:28首次发布:2019-02-28 22:29:28

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_41816368/article/details/88046200

版权

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

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

使用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

猜你喜欢

转载自blog.csdn.net/sinat_16643223/article/details/114382329