0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by Oportoz on ROS Answers with karma: 1 on 2014-06-26

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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);

Originally posted by joq with karma: 25443 on 2014-06-26

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Oportoz on 2014-06-26:
Thank you joq. I will try this way and then I post here the news.

Regards

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.