Apollo: Communicate between ROS1 outside docker and apollo modules

Created on 9 Jul 2018  路  8Comments  路  Source: ApolloAuto/apollo

I can't find any issue or example for communication between ROS1 outside docker and apollo modules. I mean:

  1. inside docker I launch bootstrap

  2. outside from docker I start rostopic pub -r 10 /test/tf1 geometry_msgs/TransformStamped '{header:{}, child_frame_id: "", transform: {}}'

  3. inside docker I have code in apollo/modules/localization/my_localization/my_localization.cc

void MY_tf_Callback(const geometry_msgs::TransformStamped& msg)
{
  AINFO << "I heard TransformStamped" ;
}
ros::Subscriber sub;
ros::Publisher pub;

Status MyLocalization::Start() {
  std::shared_ptr<ros::NodeHandle> nh = AdapterManager::node_handle();
  // std::shared_ptr<ros::NodeHandle> nh = std::make_shared<ros::NodeHandle>();
  sub = nh->subscribe("/test/tf1", 32, MY_tf_Callback);
  pub = nh->advertise<geometry_msgs::TransformStamped>("/test/tf2", 1000);
}

void CARLALocalization::OnTimer(const ros::TimerEvent &event) {

  geometry_msgs::TransformStamped msg;

  pub.publish(msg);
}

Expected behavior:

  1. inside docker I'll have topics /test/tf1 and /test/tf2.

  2. outside docker I'll have topics /test/tf1 and /test/tf2.

  3. In file /apollo/data/log/localization.INFO I'll see "I heard TransformStamped"

  4. inside docker rostopic echo -c /test/tf1 will print some messages.

  5. outside docker rostopic echo -c /test/tf2 will print some messages.

Real behaviour:

  1. inside docker I'll have only topic /test/tf1.

  2. outside docker I'll have only topic /test/tf1.

  3. In file /apollo/data/log/localization.INFO I don't see "I heard TransformStamped"

  4. inside docker rostopic echo -c /test/tf1 print

/use_sim_time is not set, will not subscribe to simulated time [/clock] topic

  1. outside docker topic /test/tf2 does not exist.
CarOS Question

Most helpful comment

My example will show resolving this issue with new localization module. You can change any other or write your own new module.

  1. apollo/modules/localization/my_localization/my_localization.h
    class MyLocalization
    {
    ros::Subscriber sub;
    ros::Publisher pub;
    std::shared_ptr nh;
    }

  2. apollo/modules/localization/my_localization/my_localization.cc

void MY_tf_Callback(const tf2_msgs::TFMessage & msg)
{
AINFO << "I heard TransformStamped" ;
}

Status MyLocalization::Start()
{
nh = std::make_shared();
sub = nh->subscribe("/test/tf1", 32, MY_tf_Callback);
pub = nh->advertise("/test/tf2", 1000);
}

void MyLocalization::OnTimer(const ros::TimerEvent &event)
{
geometry_msgs::TransformStamped msg;
pub.publish(msg);
}

  1. You should start your apollo module (that includes regular ros subscribers and publishers) earlier than you will start ros publisher and subscriber outside from apollo system/docker.

P.S. I think point 3 is a bug but I'm not sure that I make everything the best way.
P.P.S. I didn't try organize communication between several apollo modules that contains regular ros subscribers and publishers and also I didn't try use regular ros services inside apollo modules. I think it can be useful experience.

Thanks everyone for attention and help!

All 8 comments

add those lines to dev_start.sh


      --env ROS_MASTER_URI=http://172.17.0.1:11311 \
      --env ROS_IP=172.17.0.1 \

then you can connect to apollo's rosmaster outside docker

Update. Second try.
I has one simulation environment that can also provide /tf and /clock topics.
I modified my code :
sub = nh->subscribe("/tf", 32, MY_tf_Callback);

And right now i have:

  1. inside docker I'll have topics /tf /clock.

  2. outside docker I'll have topics /tf /clock.

  3. In file /apollo/data/log/localization.INFO I don't see "I heard TransformStamped"

  4. inside docker rostopic echo -c /tf print

transforms:


  • header:
    seq: 0
    stamp:
    secs: 0
    nsecs: 0
    frame_id: world
    child_frame_id: localization
    transform:
    translation:
    x: 0.0
    y: 0.0
    z: 0.0
    rotation:
    x: 0.0
    y: 0.0
    z: 0.0

w: 0.0

4.1outside docker

transforms:


  • header:
    seq: 0
    stamp:
    secs: 974
    nsecs: 447999999
    frame_id: "map"
    child_frame_id: "base_link"
    transform:
    translation:
    x: 314.974945068
    y: -330.60748291
    z: 38.1035308838
    rotation:
    x: 8.41813594205e-10
    y: 3.45706910466e-06
    z: -0.000243504987536
    w: 0.999999970347
  1. outside docker topic /test/tf2 still does not exist.

@yonhdee Thanks for advice!
I think you mean add
-e ROS_MASTER_URI=http://172.17.0.1:11311 \
-e ROS_IP=172.17.0.1 \
into docker run command.
If Im right. Here are results:

  1. outside docker nothing changes.
  2. inside docker nothing changes.

P.S. I'll do more separated test and will provide results as soon as I can.

First of all I think some troubles can exists in getting and using ros::NodeHandle.
I got it by custom method .
in /apollo/modules/common/adapters/adapter_manager.h I add
class AdapterManager {
...
std::shared_ptr<ros::NodeHandle> node_handle__;
static std::shared_ptr<ros::NodeHandle> node_handle() {return instance()->node_handle__;};
...
}
in /apollo/modules/common/adapters/adapter_manager.cc I add
void AdapterManager::Init(const AdapterManagerConfig &configs) {
...
if (configs.is_ros()) {
instance()->node_handle__.reset(new ros::NodeHandle());
instance()->node_handle_.reset(instance()->node_handle__.get());
}
...
}

I make new ros::NodeHandle inside my localization module by this:
std::shared_ptr<ros::NodeHandle> nh;
nh = std::make_shared<ros::NodeHandle>();

But nothing changes.
I think I have notice that I use apollo 2.5. I start my project then 3.0 does not exist. And I has not read out any important for my projects notes from 'Release notes'. Have I install and start use v.3.0?

My example will show resolving this issue with new localization module. You can change any other or write your own new module.

  1. apollo/modules/localization/my_localization/my_localization.h
    class MyLocalization
    {
    ros::Subscriber sub;
    ros::Publisher pub;
    std::shared_ptr nh;
    }

  2. apollo/modules/localization/my_localization/my_localization.cc

void MY_tf_Callback(const tf2_msgs::TFMessage & msg)
{
AINFO << "I heard TransformStamped" ;
}

Status MyLocalization::Start()
{
nh = std::make_shared();
sub = nh->subscribe("/test/tf1", 32, MY_tf_Callback);
pub = nh->advertise("/test/tf2", 1000);
}

void MyLocalization::OnTimer(const ros::TimerEvent &event)
{
geometry_msgs::TransformStamped msg;
pub.publish(msg);
}

  1. You should start your apollo module (that includes regular ros subscribers and publishers) earlier than you will start ros publisher and subscriber outside from apollo system/docker.

P.S. I think point 3 is a bug but I'm not sure that I make everything the best way.
P.P.S. I didn't try organize communication between several apollo modules that contains regular ros subscribers and publishers and also I didn't try use regular ros services inside apollo modules. I think it can be useful experience.

Thanks everyone for attention and help!

@gladijos

Thank you so much!!! This helped us big time! thank you for making this guide!

@mickeyouyou @quning78

This should be one of the Apollo guides in docs

closing the issue.

@snuffysasa Thanks, welcome if you do want ,you can pull a request for that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lesun90 picture lesun90  路  3Comments

freeclouds picture freeclouds  路  3Comments

Triangle001 picture Triangle001  路  3Comments

chilihua picture chilihua  路  3Comments

YaoQii picture YaoQii  路  3Comments