使用数组消息

消息构建

C++构建消息

Note

ROS中Msg的数组类型,对应C++中的Vector

Python构建消息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

team.members.append(Student(name="stu-1", age=1))
team.members.append(Student(name="stu-2", age=2))
team.members.append(Student(name="stu-3", age=3))

Tip

ROS中Msg的数组类型,对应Python中的List

完整示例代码

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
42
43
44
45
46
47
48
49
#include <iostream>
#include "ros/ros.h"

#include "demo_msgs/Team.h"

using namespace std;

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

  //通过节点创建发布者
  string topicName = "my_topic5";
  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;

    for (int i = 0; i < 3; ++i) {
      demo_msgs::Student stu;
      stu.name = "stu-" + to_string(index) + "-"+ to_string(i);
      stu.age = i;
      msg.members.push_back(stu);
    }

    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
34
35
36
37
38
39
#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;

  for (auto member : message->members) {
    cout << member.name << endl;
    cout << member.age << endl;
    cout << "##" << endl;
  }
  cout << "-----------------------" << endl;
}

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

  //创建订阅者
  string topicName = "my_topic5";
  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
35
36
37
38
39
#!/usr/bin/env python
# coding:utf-8

import rospy
from demo_msgs.msg import Team, Student

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

    # 创建发布者
    topicName = "my_topic5"
    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

        team.members.append(Student(name="stu-1", age=1))
        team.members.append(Student(name="stu-2", age=2))
        team.members.append(Student(name="stu-3", age=3))

        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
28
29
30
31
#!/usr/bin/env python
# coding:utf-8

import rospy
from demo_msgs.msg import Team, Student


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)

    for member in msg.members:
        print member

    print "--------------------"


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

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

    rospy.spin()