接上篇,我们这里来调用定义好的protobuf消息,这里依然参考apollo的代码,https://github.com/ApolloAuto/apollo-platform仓库里面的ros下面的pb_msgs_example里面的代码 下面的内容也都是里面的代码粘贴复制,感兴趣的直接看源代码就可以
2.调用自定义的proto_msg
https://github.com/ApolloAuto/apollo-platform/blob/master/ros/pb_msgs_example
2.1 代码结构
2.2 代码
2.2.1 proto
chatter.proto
syntax
= "proto2";
package pb_msgs
;
import
"time.proto";
message ShortMessage
{
required Time stamp
= 1;
required string content
= 2;
}
message Counter
{
required uint64 count
= 1 [default
= 0];
}
2.2.1 scripts
listener.py
import rospy
import pb_msgs
.msg
def callback(data
):
rospy
.loginfo
(rospy
.get_caller_id
() + "I heard %s", data
.content
)
def listener():
rospy
.init_node
("listener", anonymous
= True)
rospy
.Subscriber
("pb_chatter", pb_msgs
.msg
.ShortMessage
, callback
)
rospy
.spin
()
if __name__
== "__main__":
listener
()
talker.py
import rospy
import pb_msgs
.msg
def talker():
pub
= rospy
.Publisher
('pb_chatter', pb_msgs
.msg
.ShortMessage
, queue_size
= 10)
rospy
.init_node
('talker', anonymous
= True)
rate
= rospy
.Rate
(10)
while not rospy
.is_shutdown
():
msg
= pb_msgs
.msg
.ShortMessage
()
now
= rospy
.get_rostime
()
msg
.stamp
.sec
= now
.secs
msg
.stamp
.nsec
= now
.nsecs
msg
.content
= "Hello world!"
pub
.publish
(msg
)
rate
.sleep
()
if __name__
== '__main__':
try:
talker
()
except rospy
.ROSInterruptException
:
pass
2.2.1 src
listener.cpp
#include "ros/ros.h"
#include "chatter.pb.h"
void pbChatterCallback(const boost
::shared_ptr
<pb_msgs
::ShortMessage
>& msg
)
{
ROS_INFO_STREAM("Time: " << msg
->stamp().sec() << "." << msg
->stamp().nsec());
ROS_INFO("I heard pb short message: [%s]", msg
->content().c_str());
}
void counterCallback(const boost
::shared_ptr
<pb_msgs
::Counter
>& msg
)
{
ROS_INFO("I get counter message: [%d]", (int)msg
->count());
}
int main(int argc
, char** argv
)
{
ros
::init(argc
, argv
, "listener");
ros
::NodeHandle n
;
ros
::Subscriber pb_sub
= n
.subscribe("pb_chatter", 1000, pbChatterCallback
);
ros
::Subscriber sub
= n
.subscribe("counter", 1000, counterCallback
);
ros
::spin();
return 0;
}
talker.cpp
#include "ros/ros.h"
#include "chatter.pb.h"
#include <sstream>
int main(int argc
, char** argv
)
{
ros
::init(argc
, argv
, "talker");
ros
::NodeHandle n
;
ros
::Publisher pb_chatter_pub
= n
.advertise
<pb_msgs
::ShortMessage
>("pb_chatter", 1000);
ros
::Publisher counter_pub
= n
.advertise
<pb_msgs
::Counter
>("counter", 1000);
ros
::Rate
loop_rate(10);
int count
= 0;
while (ros
::ok())
{
pb_msgs
::ShortMessage pb_msg
;
ros
::Time now
= ros
::Time
::now();
pb_msg
.mutable_stamp()->set_sec(now
.sec
);
pb_msg
.mutable_stamp()->set_nsec(now
.nsec
);
std
::stringstream ss
;
ss
<< "Hello world " << count
;
pb_msg
.set_content(ss
.str());
ROS_INFO("%s", pb_msg
.content().c_str());
pb_chatter_pub
.publish(pb_msg
);
pb_msgs
::Counter counter
;
counter
.set_count(count
);
counter_pub
.publish(counter
);
ros
::spinOnce();
loop_rate
.sleep();
++count
;
}
return 0;
}
2.2.1 CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(pb_msgs_example)
find_package(catkin REQUIRED COMPONENTS roscpp genmsg std_msgs pb_msgs)
add_proto_files(
DIRECTORY proto
FILES chatter.proto
)
generate_messages(DEPENDENCIES pb_msgs std_msgs)
catkin_package(
LIBRARIES pb_msgs_example_proto
CATKIN_DEPENDS roscpp
DEPENDS roscpp genmsg gencpp genpy pb_msgs std_msgs
)
include_directories($
{CATKIN_INCLUDE_DIRS
})
add_executable(pb_talker src/talker.cpp)
target_link_libraries(pb_talker $
{catkin_LIBRARIES
} pb_msgs_example_proto)
add_executable(pb_listener src/listener.cpp)
target_link_libraries(pb_listener $
{catkin_LIBRARIES
} pb_msgs_example_proto)
catkin_install_python(PROGRAMS
scripts/talker.py
scripts/listener.py
DESTINATION $
{CATKIN_PACKAGE_BIN_DESTINATION
}
)
install(TARGETS pb_talker pb_listener
ARCHIVE DESTINATION $
{CATKIN_PACKAGE_LIB_DESTINATION
}
LIBRARY DESTINATION $
{CATKIN_PACKAGE_LIB_DESTINATION
}
RUNTIME DESTINATION $
{CATKIN_PACKAGE_BIN_DESTINATION
}
)
2.2.1 package.xml
<package>
<name>pb_msgs_example
</name>
<version>1.0.0
</version>
<description>The pb_msgs_example package
</description>
<maintainer email="info@apollo.auto">Apollo Authors
</maintainer>
<license>BSD
</license>
<buildtool_depend>catkin
</buildtool_depend>
<build_depend>roscpp
</build_depend>
<build_depend>gencpp
</build_depend>
<build_depend>genmsg
</build_depend>
<build_depend>genpy
</build_depend>
<build_depend>std_msgs
</build_depend>
<build_depend>pb_msgs
</build_depend>
<run_depend>roscpp
</run_depend>
<run_depend>gencpp
</run_depend>
<run_depend>genmsg
</run_depend>
<run_depend>genpy
</run_depend>
<run_depend>std_msgs
</run_depend>
<run_depend>pb_msgs
</run_depend>
</package>