ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

read from nodelet

asked 2014-06-26 08:57:02 -0500

Oportoz gravatar image

updated 2014-06-26 10:15:19 -0500

Hello,

I'm working with velodyne 32E and I was subscribing a node from "velodyne_driver" to read data from the topic "velodyne_packets" but the code in the callback wasn't executed everytime the topic was published by the LIDAR. (In callback it just had a counter incrementing, only printing the counter out of callback when the bag file ends or if press Ctrl+C)

I thought if I could change my code to use a nodelet instead of a node to direct access to data but i didn't find how to make it.

To subscribe a node it very easy because then is "sends me" to a callback just like an interruption (I think) and I could do whatever I wanted with the data. But with nodelets it is different and I didn't realized yet how could I get the data in real time.

I want to read directly from nodelet, not to transform a node into a nodelet!

Could you help me?

Thank you, Oportoz


CODE:

#includes.........

int i;

void callback(const velodyne_msgs::VelodyneScan::ConstPtr& msg)
{
 i++; 
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "subscribe_pack");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe<velodyne_msgs::VelodyneScan>("/velodyne_packets", 1, callback);

  while(ros::ok())
    {
        ros::spin(); //
    }
  std::cout << i << std::endl;
return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-06-26 10:20:07 -0500

joq gravatar image

updated 2014-06-26 10:37:41 -0500

Untested approximation to your node:

#include <ros/ros.h>
#include <pluginlib/class_list_macros.h>
#include <nodelet/nodelet.h>

namespace your_package
{
  class YourNodelet: public nodelet::Nodelet
  {
  public:

    YourNodelet() {}
    ~YourNodelet() 
    {
      std::cout << i_ << std::endl;
    }

  private:

    virtual void onInit();
    void callback(const velodyne_msgs::VelodyneScan::ConstPtr& msg)
    int i_;
    ros::Subscriber sub_;
  };

  /** @brief Nodelet initialization. */
  void YourNodelet::onInit()
  {
    i_ = 0;
    nh = getNodeHandle()
    sub_ =  nh.subscribe("velodyne_packets", 10, &YourNodelet::callback, 
                         (YourNodelet *) this, ros::TransportHints().tcpNoDelay(true));
  }

  void YourNodelet::callback(const velodyne_msgs::VelodyneScan::ConstPtr &scanMsg)
  {
    ++i_;
  }

} // namespace your_package

// Register this plugin with pluginlib.  Names must match nodelet_velodyne.xml.
//
// parameters: package, class name, class type, base class type
PLUGINLIB_DECLARE_CLASS(your_package, YourNodelet,
                        your_package::YourNodelet, nodelet::Nodelet);
edit flag offensive delete link more

Comments

Thank you joq. I will try this way and then I post here the news. Regards

Oportoz gravatar image Oportoz  ( 2014-06-26 12:33:47 -0500 )edit

Question Tools

Stats

Asked: 2014-06-26 08:57:02 -0500

Seen: 582 times

Last updated: Jul 02 '14