ros_thread.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE 
00021 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00023 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00024 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00027 // DAMAGE.
00028 //
00029 // *****************************************************************************
00030 
00031 #include <QCoreApplication>
00032 #include "include/swri_console/ros_thread.h"
00033 
00034 using namespace swri_console;
00035 
00036 RosThread::RosThread(int argc, char** argv) :
00037   is_connected_(false),
00038   is_running_(true)
00039 {
00040   ros::init(argc, argv, "swri_console",
00041             ros::init_options::AnonymousName |
00042             ros::init_options::NoRosout);
00043 }
00044 
00045 void RosThread::run()
00046 {
00047   while (is_running_)
00048   {
00049     bool master_status = ros::master::check();
00050 
00051     if (!is_connected_ && master_status) {
00052       startRos();
00053     } else if (is_connected_ && !master_status) {
00054       stopRos();
00055     } else if (is_connected_ && master_status) {
00056       ros::spinOnce();
00057       Q_EMIT spun();
00058     }
00059     msleep(50);
00060   }
00061 }
00062 
00063 
00064 void RosThread::shutdown()
00065 {
00066   is_running_ = false;
00067   if (ros::isStarted())
00068   {
00069     ros::shutdown();
00070     ros::waitForShutdown();
00071   }
00072 }
00073 
00074 void RosThread::startRos()
00075 {
00076   ros::start();
00077   is_connected_ = true;
00078 
00079   ros::NodeHandle nh;
00080   rosout_sub_ = nh.subscribe("/rosout_agg", 10000,
00081                              &RosThread::handleRosout,
00082                              this);
00083   Q_EMIT connected(true);
00084 }
00085 
00086 void RosThread::stopRos()
00087 {
00088   ros::shutdown();
00089   is_connected_ = false;
00090   Q_EMIT connected(false);
00091 }
00092 
00093 void RosThread::handleRosout(const rosgraph_msgs::LogConstPtr &msg)
00094 {
00095   Q_EMIT logReceived(msg);
00096 }


swri_console
Author(s): Elliot Johnson
autogenerated on Tue Sep 12 2017 03:09:35