Advertisement
Guest User

Untitled

a guest
May 2nd, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include "ros/ros.h"
  2. #include "std_msgs/String.h"
  3.  
  4. /**
  5.  * This tutorial demonstrates simple receipt of messages over the ROS system.
  6.  */
  7. void ChatterCallback(const sensor_msgs::JointState::ConstPtr& msg)
  8. {
  9.       ROS_INFO("i've heard: [%f]  " ,msg->position[0]);
  10.      vel = msg->velocity[0];
  11.     pos = msg->position[0];
  12.    if(msg->velocity[0] != vel) 
  13.     {
  14.         //Write to file
  15.         pos =- msg->position[0]
  16.     }
  17. }
  18. double vel;
  19. double pos;
  20. int main(int argc, char **argv)
  21. {
  22.   /**
  23.    * The ros::init() function needs to see argc and argv so that it can perform
  24.    * any ROS arguments and name remapping that were provided at the command line. For programmatic
  25.    * remappings you can use a different version of init() which takes remappings
  26.    * directly, but for most command-line programs, passing argc and argv is the easiest
  27.    * way to do it.  The third argument to init() is the name of the node.
  28.    *
  29.    * You must call one of the versions of ros::init() before using any other
  30.    * part of the ROS system.
  31.    */
  32.   ros::init(argc, argv, "listener");
  33.  
  34.   /**
  35.    * NodeHandle is the main access point to communications with the ROS system.
  36.    * The first NodeHandle constructed will fully initialize this node, and the last
  37.    * NodeHandle destructed will close down the node.
  38.    */
  39.   ros::NodeHandle n;
  40.  
  41.   /**
  42.    * The subscribe() call is how you tell ROS that you want to receive messages
  43.    * on a given topic.  This invokes a call to the ROS
  44.    * master node, which keeps a registry of who is publishing and who
  45.    * is subscribing.  Messages are passed to a callback function, here
  46.    * called chatterCallback.  subscribe() returns a Subscriber object that you
  47.    * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
  48.    * object go out of scope, this callback will automatically be unsubscribed from
  49.    * this topic.
  50.    *
  51.    * The second parameter to the subscribe() function is the size of the message
  52.    * queue.  If messages are arriving faster than they are being processed, this
  53.    * is the number of messages that will be buffered up before beginning to throw
  54.    * away the oldest ones.
  55.    */
  56.   ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
  57.  
  58.   /**
  59.    * ros::spin() will enter a loop, pumping callbacks.  With this version, all
  60.    * callbacks will be called from within this thread (the main one).  ros::spin()
  61.    * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
  62.    */
  63.   ros::spin();
  64.  
  65.   return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement