A question about eclipse and c++ in ROS

asked 2013-12-13 00:23:25 -0500

domikilo gravatar image

updated 2014-04-20 14:09:45 -0500

ngrennan gravatar image

I am trying to use eclipse for programming c++ in ROS, I have one question about run node topic in ROS without using launch file. Example I want to write a program for receive data from Asus xtion Pro live, for receive data, I must run 3dsensor.launch before run and debug. So are there any solution for me to solve this issue my code is

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/highgui/highgui.hpp>

static const std::string OPENCV_WINDOW ="image window";
class ImageConverter
{
    ros::NodeHandle nh_;
    image_transport::ImageTransport it_;
    image_transport::Subscriber image_sub_;
    image_transport::Publisher image_pub_;
public:
    ImageConverter()
    : it_(nh_)
    {
        image_sub_ = it_.subscribe("/camera/rgb/image_raw",1,&ImageConverter::imageCb,this);
        image_pub_ = it_.advertise("image_converter/output_video",1);
        cv::namedWindow(OPENCV_WINDOW);

    }
    ~ImageConverter()
    {
        cv::destroyWindow(OPENCV_WINDOW);
    }
    void imageCb(const sensor_msgs::ImageConstPtr & msg)
    {
        cv_bridge::CvImagePtr cv_ptr;
        try
        {
            cv_ptr = cv_bridge::toCvCopy(msg,sensor_msgs::image_encodings::BGR8);
        }
        catch (cv_bridge::Exception & e)
        {
            ROS_ERROR("cv_bridge exception : %s",e.what());
            return;
        }
        //draw a circle
        if(cv_ptr->image.rows >60 && cv_ptr->image.cols > 60)
            cv::circle(cv_ptr->image,cv::Point(50,50),10,CV_RGB(255,0,0));
        //update UI Window
        cv::imshow(OPENCV_WINDOW,cv_ptr->image);
        cv::waitKey(3);

        image_pub_.publish(cv_ptr->toImageMsg());

    }
};

int main(int argc , char** argv)
{
    ros::init(argc,argv,"image_converter");
    ImageConverter ic;
    ros::spin();
    return 0;
}
edit retag flag offensive close merge delete

Comments

I'm not sure I fully understand your question, but I think you're asking "is there any way to turn on my camera from my ros node", in which case the answer is "not really" since this is exactly what roslaunch was designed to do. ROS+Eclipse in terms of building and running your project is a pain, it'd be easier to just do your programming in Eclipse then use catkin_make and roslaunch to actually build/run the project (in my opinion)

Tim Sweet gravatar image Tim Sweet  ( 2013-12-18 07:33:35 -0500 )edit

My meaning is "could I use ros command in C++?" I tried successfully with 'system("/opt/ros/hydro/bin/roscore &");', but It didnt work with roslaunch, I dont want to use terminal to type roslaunch 3dsensor.launch , I want to add this command in my c++ code.

domikilo gravatar image domikilo  ( 2013-12-18 21:25:40 -0500 )edit

It is technically possible, but an analogy: it is possible to fit a round peg into a square hole, but that doesn't mean you should. The extra few seconds it takes to launch from the command line are worth it because it allows deterministic startup/shutdown behavior, platform independence, etc.

Tim Sweet gravatar image Tim Sweet  ( 2013-12-18 23:53:02 -0500 )edit

But if you are set on using the system call: it is probably not working because you have to source your environment before running roslaunch. This is done for you in your ~/.bashrc file (ie you'll see in that file: source /opt/ros/hydro/setup.bash). You could make that a system call as well.

Tim Sweet gravatar image Tim Sweet  ( 2013-12-18 23:55:57 -0500 )edit

Hi I know that I added "source /opt/ros/hydro/setup.bash" in starup application

domikilo gravatar image domikilo  ( 2013-12-21 17:52:54 -0500 )edit

That's not how bash files work, unfortunately. Every time you `source` something it only is set up for that particular environment, as in one particular "window" or process. You'd still need to source that bash file from your program

Tim Sweet gravatar image Tim Sweet  ( 2013-12-30 04:59:32 -0500 )edit

Or just directly refer to the correct processes (ie in /opt/ros/hydro), since that bash file mostly just adds to your system PATH and sets environmental variables.

Tim Sweet gravatar image Tim Sweet  ( 2013-12-30 05:00:18 -0500 )edit