QtRosNode.cpp
Go to the documentation of this file.
00001 
00002 #include <tf/tf.h>
00003 #include <std_msgs/String.h>
00004 #include <sensor_msgs/LaserScan.h>
00005 #include <sensor_msgs/Image.h>
00006 #include <geometry_msgs/PoseStamped.h>
00007 #include <nav_msgs/OccupancyGrid.h>
00008 #include <map_messages/PointOfInterest.h>
00009 #include <map_messages/DeletePointOfInterest.h>
00010 #include <map_messages/PointsOfInterest.h>
00011 
00012 #include "QtRosNode.h"
00013 #include "Workers/PointOfInterest/PointOfInterest.h"
00014 
00015 QtRosNode::QtRosNode(int argc, char *argv[], const char* node_name, MainWindow* mainWindow)
00016 {
00017   ros::init(argc, argv, node_name);
00018   ros::start();
00019   m_NodeHandle = new ros::NodeHandle;
00020   m_MainWindow = mainWindow;
00021 
00022   //m_Application = app;
00023   quitfromgui = false;
00024 
00025 
00026   advertiseTopics();
00027   subscribeToTopics();
00028 
00029   start();
00030 
00031 
00032   ADD_MACHINE_STATE( m_ModuleMachine, IDLE );
00033   ADD_MACHINE_STATE( m_ModuleMachine, WAITING );
00034   ADD_MACHINE_STATE( m_ModuleMachine, DOOR_IS_OPEN );
00035 
00036 
00037   m_ModuleMachine.setName( "Module State" );
00038   m_ModuleMachine.setState( IDLE );
00039 
00040 }
00041 
00042 QtRosNode::~QtRosNode()
00043 {
00044     if( m_NodeHandle ) delete m_NodeHandle;
00045     if(ros::isStarted()) {
00046       ros::shutdown(); // explicitly needed since we use ros::start();
00047       ros::waitForShutdown();
00048     }
00049     wait();
00050 }
00051 
00052 
00053 void QtRosNode::compareRanges()
00054 {
00055   //switch (m_ModuleMachine.state() )
00056   //{
00057     //case WAITING:
00058     //{
00059       //int ranges_center = ( m_Ranges.size() / 2 ) + 1;
00061       //m_NewRange = (m_Ranges.at( ranges_center -1 )        // mittelwert der mittleren 3 laser, noiseresistent
00062           //+ m_Ranges.at( ranges_center )
00063           //+ m_Ranges.at( ranges_center +1 ))/3;
00064       //TRACE_INFO( "NewRange: " << m_NewRange );
00065       //m_CurrentTimestamp = m_NewTimestamp;
00066 
00067       //if (( m_NewRange > m_CurrentRange + 1000 ) && ( m_CurrentRange <= 2000)) // Tür nicht weiter als 2m weg, um hintergrundbewegungen zu ignorieren
00068       //{
00069         //usleep( 1000000 );
00070         //ROS_INFO_STREAM( "The door is open!" );
00073         //m_ModuleMachine.setState( DOOR_IS_OPEN );
00074       //}
00075       //else if (m_NewRange < m_CurrentRange)   // falls tür nach dem startbutton geschlossen wird
00076       //{
00077         //m_CurrentRange = m_NewRange;
00078       //}
00079       //break;
00080     //}
00081     //default:
00082       //break;
00083   //}
00084 }
00085 
00086 
00087 void QtRosNode::subscribeToTopics()
00088 {
00089 //    // subscribe to all topics here
00090     m_LaserSubscriber = m_NodeHandle->subscribe<sensor_msgs::LaserScan>("/scan", 10, &QtRosNode::callbackLaserScan, this);
00091 //   // m_RgbImageSubscriber = m_NodeHandle->subscribe<sensor_msgs::Image>("/camera/rgb/image_color", 10, &MainWindow::callbackRgbImage, m_MainWindow);
00092     m_PoseStampedSubscriber = m_NodeHandle->subscribe<geometry_msgs::PoseStamped>("/pose", 10, &QtRosNode::callbackPoseStamped, this);
00093     m_MapDataSubscriber = m_NodeHandle->subscribe<nav_msgs::OccupancyGrid>("/map", 10, &QtRosNode::callbackOccupancyGrid, this);
00094     m_POIsSubscriber = m_NodeHandle->subscribe<map_messages::PointsOfInterest>("/map_manager/poi_list", 10, &QtRosNode::callbackPOIList, this);
00095 
00096     //m_OLPrimaryImageSubscriber = m_NodeHandle->subscribe<map_messages::PointsOfInterest>("/or/obj_learn_primary", 10, &QtRosNode::callbackPOIList, this);
00097     //m_OLPrimaryImageSubscriber = m_NodeHandle->subscribe<sensor_msgs::Image>("/or/obj_learn_primary", 10, &QtRosNode::callbackGUIImage, this);
00098     //m_OLPrimaryImageSubscriber = m_NodeHandle->subscribe<or_msgs::OrImage>("/or/obj_learn_primary", 10, &QtRosNode::callbackPrimaryImage, this);
00099     m_ORLearningStatusSubscriber = m_NodeHandle->subscribe<or_msgs::OrLearningStatus>("/or/learning_status", 10, &QtRosNode::callbackLearningStatus, this);
00100     m_ORObjectNamesSubscriber = m_NodeHandle->subscribe<or_msgs::OrObjectNames>("or/obj_names", 10, &QtRosNode::callbackObjectNames, this);
00101 }
00102 
00103 void QtRosNode::advertiseTopics()
00104 {
00105 //    // TODO advertise topics here or find other solution
00106 //    ros::Publisher* text_publisher = new ros::Publisher(m_NodeHandle->advertise<std_msgs::String>("chatter", 10));
00107 //    m_MainWindow->setTextPublisher(text_publisher);
00108 //    ros::Publisher* pose2D_publisher = new ros::Publisher(m_NodeHandle->advertise<geometry_msgs::Pose2D>("/userdef_pose", 10));
00109 //    m_MainWindow->setPose2DPublisher(pose2D_publisher);
00110 //    ros::Publisher* addPointOfInterest_publisher = new ros::Publisher(m_NodeHandle->advertise<map_messages::PointOfInterest>("/add_POI", 10));
00111 //    m_MainWindow->setAddPOIPublisher(addPointOfInterest_publisher);
00112 //    ros::Publisher* modifyPointOfInterest_publisher = new ros::Publisher(m_NodeHandle->advertise<map_messages::PointOfInterest>("/modify_POI", 10));
00113 //    m_MainWindow->setModifyPOIPublisher(modifyPointOfInterest_publisher);
00114 //    ros::Publisher* deletePointOfInterest_publisher = new ros::Publisher(m_NodeHandle->advertise<map_messages::DeletePointOfInterest>("/delete_POI", 10));
00115 //    m_MainWindow->setDeletePOIPublisher(deletePointOfInterest_publisher);
00116 }
00117 
00118 
00119 void QtRosNode::quitNow()
00120 {
00121   quitfromgui = true;
00122 }
00123 
00124 
00125 void QtRosNode::run()
00126 {
00127   ros::Rate loop_rate(10);
00128 
00129   while (ros::ok())
00130   {
00131     ros::spinOnce();
00132     loop_rate.sleep();
00133   }
00134   emit rosShutdown();
00135 
00136 }
00137 
00138 void QtRosNode::callbackPoseStamped(const geometry_msgs::PoseStamped::ConstPtr &msg)
00139 {
00140     double theta = tf::getYaw(msg->pose.orientation);
00141     emit poseUpdated(msg->pose.position.x, msg->pose.position.y, theta);
00142 }
00143 
00144 void QtRosNode::callbackOccupancyGrid(const nav_msgs::OccupancyGrid::ConstPtr &msg)
00145 {
00146 
00147    unsigned char* map = new unsigned char[msg->data.size()];
00148     //generate exploredRegion
00149    // TODO bounding box should not be MIN/MAX in first round
00150     int minX = INT_MIN;
00151     int minY = INT_MIN;
00152     int maxX = INT_MAX;
00153     int maxY = INT_MAX;
00154     for(int x = 0; x < msg->info.height; x++)
00155     {
00156         int xOffset = msg->info.height * x;
00157         for(int y = 0; y < msg->info.width; y++)
00158         {
00159             int i = xOffset + y;
00160             map[i] = msg->data[i] == -1 ? 50 : msg->data[i];
00161             if(map[i]!=-1) {
00162                 if(minX==INT_MIN || minX > x)
00163                     minX = x;
00164                 if(minY==INT_MIN || minY > y)
00165                     minY = y;
00166                 if(maxX==INT_MAX || maxX < x)
00167                     maxX = x;
00168                 if(maxY==INT_MAX || maxY < y)
00169                     maxY = y;
00170             }
00171         }
00172     }
00173 
00174     emit mapUpdated(map, msg->info.width, msg->info.resolution, msg->info.origin, minX, minY, maxX, maxY);
00175 }
00176 
00177 void QtRosNode::callbackPOIList(const map_messages::PointsOfInterest::ConstPtr& msg)
00178 {
00179     emit poiListUpdated(msg->pois);
00180 
00181 }
00182 
00183 
00184 
00185 //void QtRosNode::callbackPrimaryImage(const or_msgs::OrImage::ConstPtr &msg ) {
00186 //    ROS_INFO_STREAM("callbackPrimaryImage");
00187 //}
00188 
00189 //void QtRosNode::callbackGUIImage(const sensor_msgs::Image::ConstPtr& msg)
00190 //{
00191 
00192 //}
00193 
00194 
00195 void QtRosNode::callbackLaserScan(const sensor_msgs::LaserScan::ConstPtr& msg)
00196 {
00197     //m_Ranges.clear();
00198     //switch ( m_ModuleMachine.state() )
00199     //{
00200         //case IDLE:
00201             //{
00202                 //if ( laserDataM->getScannerConfig()->getName() == "SickLMS100" )
00203                 //{
00204                     //m_Ranges = laserDataM->getRangeVector();
00205                     //int ranges_center = ( m_Ranges.size() / 2 ) + 1;
00207                     //m_CurrentRange = (m_Ranges.at( ranges_center -1 )        // mittelwert der mittleren 3 laser, noiseresistent
00208                             //+ m_Ranges.at( ranges_center )
00209                             //+ m_Ranges.at( ranges_center +1 ))/3;
00210 
00211                 //}
00212                 //break;
00213             //}
00214         //case WAITING:
00215             //{
00216                 //string LRFName = laserDataM->getScannerConfig()->getName();
00217                 //m_NewTimestamp = laserDataM->getTimestamp();
00218                 //if ( LRFName == "SickLMS100" && m_NewTimestamp > m_CurrentTimestamp + 500 )
00219                 //{
00220                     //m_Ranges = laserDataM->getRangeVector();
00221                     //compareRanges();
00222                 //}
00223                 //break;
00224             //}
00225         //default:
00226             //break;
00227     //}
00228 
00229 
00230 
00231 
00232 
00233     //    if(m_SensorDataDisplay)
00234     //    {
00235     //         NewLaserDataPainter* painter = dynamic_cast<NewLaserDataPainter*> (m_SensorDataDisplay->getSensorDataGLWidget()->getPainter("2D Laser Data"));
00236     //        // TODO convert laser data to world coordinates here or in painter
00237     //        if(painter)
00238     //        {
00239     //            painter->updateData(msg);
00240     //        }
00241     //        else
00242     //        {
00243     //            ROS_WARN_STREAM("Could not get Painter Object \"2D Laser Data\"");
00244     //        }
00245     //    }
00246 }
00247 
00248 void QtRosNode::callbackLearningStatus(const or_msgs::OrLearningStatusConstPtr &msg)
00249 {
00250     emit learningStatus(msg->image_names, msg->object_type);
00251 }
00252 
00253 void QtRosNode::callbackObjectNames(const or_msgs::OrObjectNamesConstPtr &msg)
00254 {
00255     emit objectNames(msg->object_names, msg->object_types);
00256 }


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43