Ask Your Question

Revision history [back]

Here is a simple example that subscribes to a geometry_msgs/Pose2D topic, toggles a LED every time it receives a message and stores the data in separate variables.

#include <ros.h>
#include <geometry_msgs/Pose2D.h>

ros::NodeHandle  nh;

void poseCb( const geometry_msgs::Pose2D& pose_msg){
  digitalWrite(13, HIGH-digitalRead(13));   // blink the led
  float x = pose_msg.x;
  float y = pose_msg.y;
  float th = pose_msg.theta;
}

ros::Subscriber<geometry_msgs::Pose2D> sub("pose", &poseCb );

void setup()
{ 
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.subscribe(sub);
}

void loop()
{  
  nh.spinOnce();
  delay(1);
}