ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to read a topic for a external program?

asked 2014-04-11 02:36:24 -0500

alex.filgueira gravatar image

updated 2014-04-24 01:36:52 -0500

Hi and thanks for your attention. I want create a program in C++. This program is external to ROS. But I want that this program can be read and work with ROS TOPICS. For example, I have a node that calculate the distance that walk my robot, and publish a topic with this data. And now I want that my external-program read this topic. Any idea or example for resolve my question? Thanks for all.

EDIT------------------------------------- Ok, I can read topics out of ROS with client.http or client.python but now I want a client.c THIS is my client.c :

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>        
/* netbd.h es necesitada por la estructura hostent ;-) */

#define PORT 9090         
/* El Puerto Abierto del nodo remoto */

#define MAXDATASIZE 100   
/* El número máximo de datos en bytes */

int main(int argc, char *argv[])
{
   int fd, numbytes;       
   /* ficheros descriptores */

   char buf[MAXDATASIZE];  
   /* en donde es almacenará el texto recibido */

   struct hostent *he;         
   /* estructura que recibirá información sobre el nodo remoto */

   struct sockaddr_in server;  
   /* información sobre la dirección del servidor */

   if (argc !=2) { 
      /* esto es porque nuestro programa sólo necesitará un
      argumento, (la IP) */
      printf("Uso: %s <Dirección IP>\n",argv[0]);
      exit(-1);
   }

   if ((he=gethostbyname(argv[1]))==NULL){       
      /* llamada a gethostbyname() */
      printf("gethostbyname() error\n");
      exit(-1);
   }

   if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  
      /* llamada a socket() */
      printf("socket() error\n");
      exit(-1);
   }

   server.sin_family = AF_INET;
   server.sin_port = htons(PORT); 
   /* htons() es necesaria nuevamente ;-o */
   server.sin_addr = *((struct in_addr *)he->h_addr);  
   /*he->h_addr pasa la información de ``*he'' a "h_addr" */
   bzero(&(server.sin_zero),8);

   if(connect(fd, (struct sockaddr *)&server,
      sizeof(struct sockaddr))==-1){ 
      /* llamada a connect() */
      printf("connect() error\n");
      exit(-1);
   }
   else
   {
       printf("CONECT_ok!\n");
   }

   char* message = "raw\r\n\r\n";
   int bytes = send(fd,message, strlen(message),0 );
   printf("bytes = %d\n", bytes);
   if ((bytes) == -1)
   {
       printf("Error send \n");
   }


   if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){  
      /* llamada a recv() */
      printf("Error recv() \n");
      exit(-1);
   }

   buf[numbytes]='\0';

   printf("Mensaje del Servidor: %s\n",buf); 
   /* muestra el mensaje de bienvenida del servidor =) */

  while(1);

   close(fd);   /* cerramos fd =) */

}

I build with gcc. and I launch with ./client 127.0.0.1 or ./client localhost The steps that I do are:

1 console: roscore

2 console: roslaunch rosbridge_server rosbridge_websocket.launch

3 console: ./client

When I execute ./client, the printing of progrm:

CONECT_ok!

bytes = 7

Mensaje del Servidor:

But the console 2 no say nothing. I think that this is for the handshake or similar, but I don´t know that do now. Ps: I use Ubuntu 12.04, Ros Hydro and rosbridge 2.0

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2014-04-11 04:07:33 -0500

demmeln gravatar image

There is rosc, a dependecy free C implementation of the ROS middle ware: http://wiki.ros.org/rosc

Maybe even better suited for you could be ros_bridge, which exposes ros topics as a JSON API, so anything talking http and JSON can receive messages: http://wiki.ros.org/rosbridge_suite

edit flag offensive delete link more

Comments

Ok, thanks. I prove these method and coment the results.

alex.filgueira gravatar image alex.filgueira  ( 2014-04-13 22:01:22 -0500 )edit

Ok, ros_bridge can be a good idea for my problem, but I search and search and the all examples are in html. Someone have a example that can share in other language, example: c, c++, python? Thanks.

alex.filgueira gravatar image alex.filgueira  ( 2014-04-15 05:00:53 -0500 )edit

On the rosbridge webpackage there is a "simple python client" example: (http://rosbridge.org/doku.php?id=writing_a_rosbridge_client_in_python )

demmeln gravatar image demmeln  ( 2014-04-15 06:01:19 -0500 )edit

Also check out the answers to this question, for something in java http://answers.ros.org/question/152806/rosbridge-20-in-java/

demmeln gravatar image demmeln  ( 2014-04-15 07:33:10 -0500 )edit

Ok, I did a client.python, but now I want a client.c . I edit the first post with this question. Thanks!

alex.filgueira gravatar image alex.filgueira  ( 2014-04-24 01:33:44 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-04-11 02:36:24 -0500

Seen: 1,224 times

Last updated: Apr 24 '14