Skip to content

[Apollo] how to use protobuf msg in CMake program

Robin edited this page Jan 11, 2020 · 1 revision

How to use protobuf msg in CMake program

1. ros package:

/apollo/ros_pkgs_ws/src/
/apollo/ros_pkgs_ws/src/test/
/apollo/ros_pkgs_ws/src/test/src/test_proto.cpp
/apollo/ros_pkgs_ws/src/test/CMakeList.txt
/apollo/ros_pkgs_ws/src/test/package.xml

2. files content:

In /apollo/ros_pkgs_ws/src/test/src/test_proto.cpp

#include <iostream>
#include <ros/ros.h>
#include "modules/canbus/proto/chassis.pb.h"


class TestProto {
public:
    TestProto();
    void OnChassis(const apollo::canbus::Chassis& msg);

private:
    ros::NodeHandle _nh;
    ros::Subscriber chassis_sub_;
};

TestProto::TestProto() {
     chassis_sub_ = _nh.subscribe("/apollo/canbus/chassis", 1, &TestProto::OnChassis, this);
}

void TestProto::OnChassis(const apollo::canbus::Chassis& msg){
    std::cout << "=======================" << std::endl;
}

int main(int argc,char **argv)
{
    ros::init(argc,argv,"test_proto");
    TestProto test_proto;
    ros::spin();
}

In /apollo/ros_pkgs_ws/src/test/CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(test)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

find_package(catkin REQUIRED COMPONENTS
  pcl_conversions
  pcl_ros
  visualization_msgs
  roscpp
  rospy
  std_msgs
)


###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)


set(MODULE_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}  ${MODULE_ROOT_DIR})
set(COMMON_PROTO_INCLUDE_DIR "${MODULE_ROOT_DIR}/modules/common/proto/")
set(CHASSIS_PROTO_INCLUDE_DIR "${MODULE_ROOT_DIR}/modules/canbus/proto/")
include_directories(${COMMON_PROTO_INCLUDE_DIR} ${CHASSIS_PROTO_INCLUDE_DIR})
set(CHASSIS_PB_SRCS "${MODULE_ROOT_DIR}/modules/canbus/proto/chassis.pb.cc")
set(ERROR_CODE_SRCS "${MODULE_ROOT_DIR}/modules/common/proto/error_code.pb.cc")
set(HEADER_PB_SRCS "${MODULE_ROOT_DIR}/modules/common/proto/header.pb.cc")
set(SIGNAL_PB_SRCS "${MODULE_ROOT_DIR}/modules/common/proto/vehicle_signal.pb.cc")
set(GEOMETRY_PB_SRCS "${MODULE_ROOT_DIR}/modules/common/proto/geometry.pb.cc")
set(STATE_PB_SRCS "${MODULE_ROOT_DIR}/modules/common/proto/drive_state.pb.cc")
set(LIB_PB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../devel/lib")
if (NOT TARGET chassis_pb)
  add_library(chassis_pb SHARED ${CHASSIS_PB_SRCS} ${SIGNAL_PB_SRCS} ${HEADER_PB_SRCS} ${GEOMETRY_PB_SRCS} ${STATE_PB_SRCS} ${ERROR_CODE_SRCS})
  target_link_libraries(chassis_pb ${catkin_LIBRARIES} ${PROTOBUF_LIBRARIES})
else()
  link_directories(${LIB_PB_PATH})
endif()


# test_proto
add_executable(test_proto src/test_proto.cpp)
target_link_libraries(test_proto ${catkin_LIBRARIES} chassis_pb)

...

Outline

[dev-env] Set-up dev-env

[cv] cv notes

[NN] Neural-Network

[auto-drive] automatic drive

[auto-drive] apollo auto & qtruck

Clone this wiki locally