Skip to content

Instantly share code, notes, and snippets.

@hamzamerzic
Last active December 15, 2023 02:48
Show Gist options
  • Save hamzamerzic/4704bbdc2e0cd46f70e6ebe188ab51ac to your computer and use it in GitHub Desktop.
Save hamzamerzic/4704bbdc2e0cd46f70e6ebe188ab51ac to your computer and use it in GitHub Desktop.

Revisions

  1. hamzamerzic renamed this gist Mar 6, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. hamzamerzic renamed this gist Mar 6, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. hamzamerzic revised this gist Dec 29, 2016. 1 changed file with 26 additions and 6 deletions.
    32 changes: 26 additions & 6 deletions ros_spinner.cpp
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,21 @@
    #include <ros/callback_queue.h>
    #include <ros/ros.h>
    #include <boost/thread.hpp>

    /*
    * @brief A spinner thread to force ros to spinOnce() and therefore ros services/callbacks to
    * get invoked etc.
    */
    class Spinner {
    public:
    Spinner() : _run(true), _thread(std::bind(&Spinner::spin, this)) {
    Spinner() : _run(true), _thread(boost::bind(&Spinner::spin, this)) {
    }

    ~Spinner() {
    _run = false;
    _thread.join();
    }

    void spin() {
    ros::NodeHandle nh;
    ros::WallDuration timeout(0.1f);
    @@ -24,8 +26,26 @@ class Spinner {

    ros::spinOnce(); // For proper services cleanup.
    }

    private:
    bool _run;
    std::thread _thread;
    };
    boost::thread _thread;
    };

    /*----- Different way -----*/
    #include <ros/ros.h>
    #include <boost/thread.hpp>

    void spinThread() {
    ros::spin();
    }

    int main(int argc, char** argv) {
    boost::thread spin_thread(&spinThread);

    // Some work...

    ros::shutdown();
    spin_thread.join();
    return 0;
    }
  4. hamzamerzic revised this gist Dec 29, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions ros_spinner.cpp
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,12 @@ class Spinner {
    public:
    Spinner() : _run(true), _thread(std::bind(&Spinner::spin, this)) {
    }


    ~Spinner() {
    _run = false;
    _thread.join();
    }

    void spin() {
    ros::NodeHandle nh;
    ros::WallDuration timeout(0.1f);
    @@ -19,12 +24,7 @@ class Spinner {

    ros::spinOnce(); // For proper services cleanup.
    }

    void terminate() {
    _run = false;
    _thread.join();
    }


    private:
    bool _run;
    std::thread _thread;
  5. hamzamerzic revised this gist Dec 29, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ros_spinner.cpp
    Original file line number Diff line number Diff line change
    @@ -6,26 +6,26 @@
    */
    class Spinner {
    public:
    Spinner() : run(true), _thread(std::bind(&Spinner::spin, this)) {
    Spinner() : _run(true), _thread(std::bind(&Spinner::spin, this)) {
    }

    void spin() {
    ros::NodeHandle nh;
    ros::WallDuration timeout(0.1f);

    ros::CallbackQueue* queue = ros::getGlobalCallbackQueue();
    while (nh.ok() && run)
    while (nh.ok() && _run)
    queue->callAvailable(timeout);

    ros::spinOnce(); // For proper services cleanup.
    }

    void terminate() {
    run = false;
    _run = false;
    _thread.join();
    }

    private:
    bool run;
    bool _run;
    std::thread _thread;
    };
  6. hamzamerzic revised this gist Dec 29, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ros_spinner.cpp
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,8 @@
    * @brief A spinner thread to force ros to spinOnce() and therefore ros services/callbacks to
    * get invoked etc.
    */
    struct Spinner {
    class Spinner {
    public:
    Spinner() : run(true), _thread(std::bind(&Spinner::spin, this)) {
    }

  7. hamzamerzic created this gist Dec 29, 2016.
    30 changes: 30 additions & 0 deletions ros_spinner.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #include <ros/callback_queue.h>

    /*
    * @brief A spinner thread to force ros to spinOnce() and therefore ros services/callbacks to
    * get invoked etc.
    */
    struct Spinner {
    Spinner() : run(true), _thread(std::bind(&Spinner::spin, this)) {
    }

    void spin() {
    ros::NodeHandle nh;
    ros::WallDuration timeout(0.1f);

    ros::CallbackQueue* queue = ros::getGlobalCallbackQueue();
    while (nh.ok() && run)
    queue->callAvailable(timeout);

    ros::spinOnce(); // For proper services cleanup.
    }

    void terminate() {
    run = false;
    _thread.join();
    }

    private:
    bool run;
    std::thread _thread;
    };