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

Refactoring basic ROS code into object-oriented one

asked 2011-10-19 00:52:14 -0600

alfa_80 gravatar image

updated 2014-01-28 17:10:36 -0600

ngrennan gravatar image

I was trying to refactor the existing basic ROS talker code into somewhat that of using OO paradigm. By the way, I am not a good object-oriented programmer but willing to practice a lot and apply in any projects that I am involved. Here I provide the code and there are currently 2 problems I am having. They are:

  1. Problem with the PointCloud constructor. I got an error of "extra qualification ‘PointCloud::’ on member ‘PointCloud’".
  2. I'm confused the way I should use loop_rate().

Hopefully, someone who are very good in OO programming can help or improve this little thing..The code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>


class PointCloud
{

private:
    int count;
    PointCloud();
    ros::NodeHandle n;
    ros::Publisher chatter_pub;
//  ros::Rate loop_rate(int);
    std_msgs::String msg;
    std::stringstream ss;

public:

PointCloud::PointCloud()
{
    chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
}

void display();
};

void PointCloud::display()
{
//      loop_rate(10);

    count = 0;
    while (ros::ok())
    {
        ss << "hello world " << count;
        msg.data = ss.str();

        ROS_INFO("%s", msg.data.c_str());

        chatter_pub.publish(msg);

        ros::spinOnce();

//          loop_rate.sleep();
        ++count;
    }
 }
int main(int argc, char **argv)
{
    ros::init(argc, argv, "talker");
    PointCloud pointCloud;
    ROS_INFO("Node started");
    ros::spin(); 
    ROS_INFO("Node finished");

   return 0;
}

The modified code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>


class PointCloud
{
private:
    ros::NodeHandle n;
    ros::Publisher chatter_pub;

    int count;

public:
    PointCloud();
    void display();
};

    PointCloud::PointCloud()
    {
        chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
            count = 0;
    }

    void PointCloud::display()
    {
        std_msgs::String msg;
        std::stringstream ss;
        ss << "hello world " << count;
        msg.data = ss.str();
        ROS_INFO("%s", msg.data.c_str());
        chatter_pub.publish(msg);
        ++count;
    }

int main(int argc, char **argv)
{
    ros::init(argc, argv, "talker");
    PointCloud pointCloud;
    ROS_INFO("Node started");

    ros::Rate loop_rate(10);

    while (ros::ok())
    {
        ros::spinOnce();
        pointCloud.display();
        loop_rate.sleep();
    }

    ROS_INFO("Node finished");

  return 0;
}
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2011-10-19 02:18:45 -0600

dornhege gravatar image
  1. Yes, just remove the extra PointCloud::
  2. Move the main loop functionality from display(). This should stay in main. You have a double main loop (ros::spin() implements one), but only one is called as no one calls display(). Put the main loop from display() where ros::spin() is and call pointCloud.display() from there. Move the loop_rate part also there and uncomment it, it should work.
edit flag offensive delete link more

Comments

I didn't get you. But I understood point No.1. As I tried further, I got a lot of bugs..Could you just show it by editing the code if possible, it's easier to view.
alfa_80 gravatar image alfa_80  ( 2011-10-19 03:55:06 -0600 )edit
Basically take those whole display() function and put its code in main in place of ros::spin. You only keep the chatter_pub.publish(msg) line (+ the initialization code) in display.
dornhege gravatar image dornhege  ( 2011-10-19 04:18:35 -0600 )edit
Thanks, I've just modified it differently, it somehow works even I've to modify it a little bit more..thanks anyway..
alfa_80 gravatar image alfa_80  ( 2011-10-19 05:31:28 -0600 )edit
You can add your modified code to the original post if you have some questions about that.
dornhege gravatar image dornhege  ( 2011-10-19 05:41:51 -0600 )edit
I've added the modified version of it. Kindly have a look. It works fine except it buffers every message to be sent, that means in message 1, it contains one message, in message 2, it contains previous message plus current message and so on..Please also comment from OO concept point of view as well.
alfa_80 gravatar image alfa_80  ( 2011-10-19 06:07:59 -0600 )edit
I've updated your modified code. It looks like it's supposed to now, but I didn't test it. The main loop goes in main() unless you have a good reason to outsource that. Regarding 2.: The stringstream was a member variable so you were accumulating messages there.
dornhege gravatar image dornhege  ( 2011-10-20 02:11:13 -0600 )edit
@dornhege: Thanks a lot for the code modification and sharing..
alfa_80 gravatar image alfa_80  ( 2011-10-20 02:44:01 -0600 )edit
@dornhege: Why when stringstream was declared as a member variable, the messages were accumulating? I didn't see any logic behind that..
alfa_80 gravatar image alfa_80  ( 2011-10-20 19:51:54 -0600 )edit
1

answered 2011-10-19 02:35:43 -0600

Thomas D gravatar image

You could try using this set of tutorials as your starting point. It extends the basic publisher and subscriber tutorials to use classes. Plus, it will show you how to use publishers and subscribers in C++ and Python, how to use the parameter server (with launch files and command line arguments), and how to implement a dynamic reconfigure server.

edit flag offensive delete link more
0

answered 2011-10-19 21:34:22 -0600

If you wish to improve your OO programming skills, you may be interested in the C++ FAQ Lite. You will find a lot of C++ related topics covered, and tons of hints and best practices for good C++ programming.

As books and Internet sites can only take you so far, I found the following advice from the Learning OO/C++ section very useful:

Object-oriented thinking is caught, not just taught. Get cozy with someone who really knows what they're talking about, and try to get inside their head and watch them solve problems. Listen. Learn by emulating.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2011-10-19 00:52:14 -0600

Seen: 3,108 times

Last updated: Oct 20 '11