使用三方库消息

消息构建

C++构建消息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
demo_msgs::Team msg;
msg.name = "xxx";
msg.leader.name = "xxx";
msg.leader.age = 0;
msg.intro.data = "xxx";
msg.location.linear.x = 0;
msg.location.linear.y = 0;
msg.location.linear.z = 0;
msg.location.angular.x = 0;
msg.location.angular.y = 0;
msg.location.angular.z = 0;

Python构建消息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
team = Team()
team.name = "xxx"
team.leader.name = "xxx"
team.leader.age = 0
team.intro.data = "xxx"
team.location.linear.x = 0
team.location.linear.y = 0
team.location.linear.z = 0
team.location.angular.x = 0
team.location.angular.y = 0
team.location.angular.z = 0

完整示例代码

C++ Publisher实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "ros/ros.h"

#include "demo_msgs/Team.h"

using namespace std;

int main(int argc, char **argv) {
  // 创建节点
  string nodeName = "my_publisher4";
  ros::init(argc, argv, nodeName);
  ros::NodeHandle node;

  //通过节点创建发布者
  string topicName = "my_topic4";
  const ros::Publisher &publisher = node.advertise<demo_msgs::Team>(topicName, 1000);

  int index = 0;
  ros::Rate rate(1);
  while (ros::ok()) {
    //通过publisher发布消息
    demo_msgs::Team msg;
    msg.name = "name-" + to_string(index);
    msg.leader.name = "name-" + to_string(index);
    msg.leader.age = index;
    msg.intro.data = "intro-" + to_string(index);
    msg.location.linear.x = index;
    msg.location.linear.y = index;
    msg.location.linear.z = index;
    msg.location.angular.x = index;
    msg.location.angular.y = index;
    msg.location.angular.z = index;
    publisher.publish(msg);

    index++;
    rate.sleep();
  }

  ros::spin();
  return 0;
}

C++ Subscriber实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include "ros/ros.h"
#include "demo_msgs/Team.h"

using namespace std;

void callback(const demo_msgs::Team::ConstPtr &message) {
  cout << message->name << endl;
  cout << message->leader.name << endl;
  cout << message->leader.age << endl;
  cout << message->intro.data << endl;
  cout << message->location.linear.x << endl;
  cout << message->location.linear.y << endl;
  cout << message->location.linear.z << endl;
  cout << message->location.angular.x << endl;
  cout << message->location.angular.y << endl;
  cout << message->location.angular.z << endl;
  cout << "-----------------------" << endl;
}

int main(int argc, char **argv) {
  // 创建节点
  string nodeName = "my_subscriber4";
  ros::init(argc, argv, nodeName);
  ros::NodeHandle node;

  //创建订阅者
  string topicName = "my_topic4";
  const ros::Subscriber &subscriber = node.subscribe(topicName, 1000, callback);

  ros::spin();
  return 0;
}

Python Publisher实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
# coding:utf-8

import rospy
from demo_msgs.msg import Team

if __name__ == '__main__':
    nodeName = "topic_py_publisher4"
    rospy.init_node(nodeName)

    # 创建发布者
    topicName = "my_topic4"
    publisher = rospy.Publisher(topicName, Team, queue_size=1000)

    rate = rospy.Rate(1)
    index = 0
    while not rospy.is_shutdown():
        team = Team()
        team.name = "name-%d" % index
        team.leader.name = "name-%d" % index
        team.leader.age = index
        team.intro.data = "intro-%d" % index
        team.location.linear.x = index
        team.location.linear.y = index
        team.location.linear.z = index
        team.location.angular.x = index
        team.location.angular.y = index
        team.location.angular.z = index
        publisher.publish(team)

        rate.sleep()
        index += 1

    rospy.spin()

Python Subscriber实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# coding:utf-8

import rospy
from demo_msgs.msg import Team


def callback(msg):
    if not isinstance(msg, Team):
        return
    print "name: %s" % msg.name
    print "leader name: %s" % msg.leader.name
    print "leader age: %d" % msg.leader.age
    print "intro: %s" % msg.intro.data
    print "location: {}".format(msg.location)
    print "--------------------"


if __name__ == '__main__':
    nodeName = "topic_py_subscriber3"
    rospy.init_node(nodeName)

    # 创建订阅者
    topicName = "my_topic4"
    rospy.Subscriber(topicName, Team, callback)

    rospy.spin()