Skip to content

[Apollo] How to use custom ros msg in bazel program

Robin edited this page Jan 13, 2020 · 3 revisions

How to use custom ros msg in bazel program

1. custom ros msg

Step a: Add folder apollo/modules/custom_msg and create rospkg perception_msgs

Files:

apollo/modules/custom_msgs/  
apollo/modules/perception_msgs/  
apollo/modules/perception_msgs/msg/  
apollo/modules/perception_msgs/msg/PerceptionObstacles.msg  
apollo/modules/perception_msgs/CMakeLists.txt  
apollo/modules/perception_msgs/package.xml 

In apollo/modules/perception_msgs/msg/PerceptionObstacles.msg

string testfield

In apollo/modules/perception_msgs/CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(perception_msgs)

add_compile_options(-std=c++11)

find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  message_generation
  sensor_msgs
  std_msgs
)

add_message_files(
  FILES
  PerceptionObstacles.msg
)

generate_messages(
  DEPENDENCIES
  geometry_msgs   
  sensor_msgs  
  std_msgs
)

catkin_package(
 CATKIN_DEPENDS roscpp rospy  geometry_msgs message_runtime sensor_msgs std_msgs
)

include_directories(
  # include
  ${catkin_INCLUDE_DIRS}
)

In apollo/modules/perception_msgs/package.xml

<?xml version="1.0"?>
<package format="2">
  <name>perception_msgs</name>
  <version>0.0.0</version>
  <description>The perception_msgs package</description>
  <maintainer email="rr@qomolo.com">rr</maintainer>
  <license>TODO</license>
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>geometry_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>geometry_msgs</build_export_depend>
  <build_export_depend>sensor_msgs</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <build_export_depend>message_runtime</build_export_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>geometry_msgs</exec_depend>
  <exec_depend>sensor_msgs</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>message_runtime</exec_depend>
  <export>
  </export>
</package>
Step 2: build the perception_msgs rospkg

In apollo.sh

function build_custom_msg() {
  CURRENT_PATH=$(pwd)
  if [ -d "${ROS_ROOT}" ]; then
    ROS_PATH="${ROS_ROOT}/../.."
  else
    warning "ROS not found. Run apolllo.sh build first."
    exit 1
  fi

  source "${ROS_PATH}/setup.bash"

  cd modules
  catkin_make_isolated --install --source custom_msgs \
    --install-space "${ROS_PATH}" -DCMAKE_BUILD_TYPE=Release \
    --cmake-args --no-warn-unused-cli
  find "${ROS_PATH}" -name "*.pyc" -print0 | xargs -0 rm -rf
  cd -

  rm -rf modules/.catkin_workspace
  rm -rf modules/build_isolated/
  rm -rf modules/devel_isolated/
}
in_dev_docker:/apollo$ ./apollo.sh build_custom_msg 
robin@in_dev_docker:/apollo$ rosmsg show perception_msgs/PerceptionObstacles  
string testfield
Step 3: use perception_msgs in bazel program
apollo/common/protomsg_rosmsg_bridge/
apollo/common/protomsg_rosmsg_bridge/BUILD
apollo/common/protomsg_rosmsg_bridge/protomsg_to_rosmsg.cc

In apollo/common/protomsg_rosmsg_bridge/protomsg_to_rosmsg.cc

#include "ros/include/ros/ros.h"
# include "perception_msgs/PerceptionObstacles.h" 
#include "modules/perception/proto/perception_obstacle.pb.h"


namespace apollo {
namespace common {

class ProtomsgToRosmsg
{
    ros::NodeHandle nh_;
    std::string perception_obstacle_protomsg_name_, perception_obstacle_rosmsg_name_;
    ros::Publisher perception_obstacle_rosmsg_pub_;
    ros::Subscriber perception_obstacle_protomsg_sub_;
public:
    ProtomsgToRosmsg();
    ~ProtomsgToRosmsg();
    void Run();
    void OnPerceptionObstacles(const perception::PerceptionObstacles msg);
};


ProtomsgToRosmsg::ProtomsgToRosmsg() :
nh_("~"),
perception_obstacle_protomsg_name_("/apollo/perception/obstacles"), perception_obstacle_rosmsg_name_("/apollo/perception/obstaclesstd")
{
    perception_obstacle_protomsg_sub_ = nh_.subscribe(perception_obstacle_protomsg_name_, 1, &ProtomsgToRosmsg::OnPerceptionObstacles, this);  //perception::PerceptionObstacles
    perception_obstacle_rosmsg_pub_ = nh_.advertise<perception_msgs::PerceptionObstacles>(perception_obstacle_rosmsg_name_, 1);
}

ProtomsgToRosmsg::~ProtomsgToRosmsg() {

}

void ProtomsgToRosmsg::OnPerceptionObstacles(const perception::PerceptionObstacles msg) {
    std::cout << "perception obstacles gotten." << std::endl;
    perception_msgs::PerceptionObstacles newmsg;
    newmsg.testfield = "hello testfield!";
    perception_obstacle_rosmsg_pub_.publish(newmsg);
}

void ProtomsgToRosmsg::Run() {
    std::cout << "Hello world" << std::endl;
}

}  // namespace perception
}  // namespace apollo


int main(int argc, char* argv[]) {
    ros::init(argc, argv, "protomsg_to_rosmsg");

    apollo::common::ProtomsgToRosmsg app;
    app.Run();
    ros::spin();
    return 0;
}

In apollo/common/protomsg_rosmsg_bridge/BUILD

load("//tools:cpplint.bzl", "cpplint")

package(default_visibility = ["//visibility:public"])


cc_binary(
    name = "protomsg_to_rosmsg",
    srcs = ["protomsg_to_rosmsg.cc"],
    deps = [
        "//modules/perception/proto:perception_proto",
        "//external:gflags",
        "//modules/common:log",
        "@ros//:ros_common",
    ],
    linkopts = [
        "-ljansson",
        "-lcurl",
    ],
)

cpplint()

Build

in_dev_docker:/apollo$ bazel build -c opt modules/common/protomsg_rosmsg_bridge:protomsg_to_rosmsg

Run

in_dev_docker:/apollo$ ./bazel-bin/modules/common/protomsg_rosmsg_bridge/protomsg_to_rosmsg

Ref.
CreatingMsgAndSrv
Ros custom msg

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