Ask Your Question
0

ros node comined with opengl

asked 2012-10-17 05:15:56 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi,

i have a problem with combining both.

my code is:

#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "laser_geometry/laser_geometry.h"
#include "tf/transform_listener.h"
#include <iostream>
#include <GL/glut.h>
using namespace std;

int camPosY=10;
namespace gui
    {

    class gui{
    public:
          gui();
          void scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan);

      private:
          ros::NodeHandle node;
          laser_geometry::LaserProjection projector_;
          tf::TransformListener tfListener_;
          ros::Subscriber scan_sub;
    };

    gui::gui(){
        scan_sub=node.subscribe("/scan", 100, &gui::scanCallback, this);
    }

    void gui::scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan){

        ROS_INFO("hi");
        sensor_msgs::PointCloud cloud;
        projector_.transformLaserScanToPointCloud("laser", *scan, cloud, tfListener_);
        for(unsigned int i=0;i<cloud.points.size();i++){
            cout<<cloud.points[i].x<<endl;
        }
    }
    };

void display(void)
{
    glLoadIdentity();
    gluLookAt(0,camPosY,-10,0,camPosY,0,0,1,0);
    glClear( GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(-2.0, -4.0, 0.0);
    glVertex3f(2.0, -4.0, 0.0);
    glVertex3f(2.0, 4.0, 0.0);
    glVertex3f(-2.0, 4.0, 0.0);
    glEnd();
    glFlush();

}

void keyboard(int key,int x,int y){

if(key == GLUT_KEY_DOWN)camPosY-=1;
if(key == GLUT_KEY_UP)camPosY+=1;

glutPostRedisplay();
}

int main(int argc, char ** argv)
{
 ros::init(argc, argv, "GUI");
 gui::gui gui;
 glutInit(&argc, argv);
 glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

 glutInitWindowPosition(100,100);
 glutInitWindowSize(800,600);
 glutCreateWindow ("Roboter");

 glClearColor(0.0, 0.0, 0.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(60,1,0.1,1000);
 glMatrixMode(GL_MODELVIEW);
 gluLookAt(0,10,-10,0,0,0,0,1,0);
 glutDisplayFunc(display);
 glutSpecialFunc(keyboard);
 glutMainLoop();

 return 0;
}

here i want to show the scan data later but how can i show the display and updating it and use ros::spin()? Do i need a second thread only for ros::spin()?

thx

edit retag flag offensive close merge delete

Comments

Please format your code properly. Mark the code and press Ctrl-K.

Lorenz gravatar image Lorenz  ( 2012-10-17 22:37:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-10-17 05:27:19 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

You can call ros::spinOnce() from glutIdleFunc to facilitate spinning.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-10-17 05:15:56 -0500

Seen: 1,502 times

Last updated: Oct 18 '12