AlejandroSilvestri / os1

Doxygen para os1: OrbSlam2 comentado y modificado para pruebas de monocular slam.
20 stars 26 forks source link

my ROS Example error #9

Closed ghost closed 7 years ago

ghost commented 7 years ago

I have began to implement monocular ROS code..

my code:

#include<iostream>
#include<algorithm>
#include<fstream>
#include<chrono>

#include<ros/ros.h>
#include <cv_bridge/cv_bridge.h>

#include<opencv2/core/core.hpp>

#include"../../../include/System.h"
#include <Viewer.h>

using namespace std;

class ImageGrabber
{
public:
    ImageGrabber(ORB_SLAM2::System* pSLAM):mpSLAM(pSLAM){}

    void GrabImage(const sensor_msgs::ImageConstPtr& msg);

    ORB_SLAM2::System* mpSLAM;
};

ORB_SLAM2::System *Sistema;

int main(int argc, char **argv)
{
    ros::init(argc, argv, "Mono");
    ros::start();

    if(argc != 3)
    {
        cerr << endl << "Usage: rosrun ORB_SLAM2 Mono path_to_vocabulary path_to_settings" << endl;        
        ros::shutdown();
        return 1;
    }    

    // Create SLAM system. It initializes all system threads and gets ready to process frames.
    ORB_SLAM2::System SLAM(argv[1],argv[2],ORB_SLAM2::System::MONOCULAR,true,NULL);
    Sistema = &SLAM;
    ORB_SLAM2::Viewer* visor = SLAM.mpViewer;
    visor->duracion = false;

    ImageGrabber igb(&SLAM);

    ros::NodeHandle nodeHandler;
    //ros::Subscriber sub = nodeHandler.subscribe("/camera/left/image_rect", 1, &ImageGrabber::GrabImage,&igb);
    ros::Subscriber sub = nodeHandler.subscribe("/usb_cam/image_raw", 1, &ImageGrabber::GrabImage,&igb);
    ros::spin();

    // Stop all threads
    //visor->RequestStop();
    SLAM.Shutdown();

    // Save camera trajectory
    SLAM.SaveKeyFrameTrajectoryTUM("KeyFrameTrajectory.txt");

    ros::shutdown();

    return 0;
}

void ImageGrabber::GrabImage(const sensor_msgs::ImageConstPtr& msg)
{
    // Copy the ros image message to cv::Mat.
    cv_bridge::CvImageConstPtr cv_ptr;
    try
    {
        cv_ptr = cv_bridge::toCvShare(msg);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

    mpSLAM->TrackMonocular(cv_ptr->image,cv_ptr->header.stamp.toSec());
}

then when press ctrl+c,

Finishing the SLAM builder.

Saving keyframe trajectory to KeyFrameTrajectory.txt ...

trajectory saved!
QObject::~QObject: Timers cannot be stopped from another thread

Destruct?? How should I implement monocular ROS code??

AlejandroSilvestri commented 7 years ago

@takuhara ,

I'm not familiar with ROS, but I appreciate you're trying to implement it.

All I can say is main() has a while(true) as main loop, where image are taken from source and passed to System::TrackMonocular.

When your ROS code works, I'll add to os1 code, if you're ok with it.

ghost commented 7 years ago

(I am using Google translation.)

The above code is a slight modification of raulmur's original ROS code. https://github.com/raulmur/ORB_SLAM2/blob/master/Examples/ROS/ORB_SLAM2/src/ros_mono.cc SLAM works properly.

In order to stop execution, when ctcl + c is pressed on the terminal, ros :: spin () catches the signal and SLAM.Shutdown () or SLAM.SaveKeyFrameTrajectoryTUM ("KeyFrameTrajectory.txt") is executed. However, the terminal freezes at the end or an error "QObject :: ~ QObject: Timers can not be stopped from another thread" occurs.

From now on, I would like to explore the cause.

AlejandroSilvestri commented 7 years ago

@takuhara

I don't use that code in this project, but I can tell you same happened to me when using orb-slam2.

QObject is a class from Qt, which opencv uses to display frames. Perhaps Qt has its own timers, and I don't remember orb-slam2 System::Shutdown asking Qt for shutdown, so I believe that's the cause, but it shouldn't affect orb-slam2 ending properly.

Orb-slam2 uses opencv window to display frames in DrawFrame. OpenCV window uses Qt if it is available. Perhaps properly closing that window during shutdown prevents that error.

ghost commented 7 years ago

@AlejandroSilvestri

As you say, if I delete cv :: namedWindow ("ORB - SLAM 2: Current Frame") and later code in Viewer :: Run (), no error occurred. Later, I will insert destroyAllWindows etc. and try to verify.

necusjz commented 7 years ago

@AlejandroSilvestri Hi, I had the same happend when using orb-slam2. I want to know the details about how to peoperly closing that window except "ctrl+c", and how to prevent that error.

AlejandroSilvestri commented 7 years ago

@ichigoi7e

You mean Qt error at closing?

Do as @takahura said, delete opencv window. I'm not doing it, perhaps opencv documentation can help you to close the window.

I can add Qt is not mandatory in orb-slam2 nor os1. When installing opencv, if qt is installed opencv uses it. You can disable qt in cmake during opencv installation.