Guest User

Untitled

a guest
May 1st, 2015
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include "ros/ros.h"
  2. #include "sensor_msgs/JointState.h"
  3. #include <vector>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. /**
  9.  * This tutorial demonstrates simple sending of messages over the ROS system.
  10.  */
  11. int main(int argc, char **argv)
  12. {
  13.   /**
  14.    * The ros::init() function needs to see argc and argv so that it can perform
  15.    * any ROS arguments and name remapping that were provided at the command line. For programmatic
  16.    * remappings you can use a different version of init() which takes remappings
  17.    * directly, but for most command-line programs, passing argc and argv is the easiest
  18.    * way to do it.  The third argument to init() is the name of the node.
  19.    *
  20.    * You must call one of the versions of ros::init() before using any other
  21.    * part of the ROS system.
  22.    */
  23.    vector<double> values;
  24.    ifstream infile ;
  25.    infile.open("/home/x/catkin_ws_PTU/src/flir_ptu/flir_ptu_driver/src/data1.csv");
  26.    while(infile)
  27.    {
  28.         string s;
  29.         if(getline(infile,s)) break;
  30.         istringstream ss( s );
  31.         vector <string> record;
  32.         while (ss)
  33.         {
  34.             string s;
  35.             if (!getline( ss, s, ',' )) break;
  36.             values.push_back( atof(s.c_str()));
  37.         }
  38.  
  39.     }
  40.  
  41.    for(int i = 0;i<values.size();i++ )
  42.    {
  43.        cout << values[i] << endl;
  44.     }
  45.   ros::init(argc, argv, "talker");
  46.  
  47.   /**
  48.    * NodeHandle is the main access point to communications with the ROS system.
  49.    * The first NodeHandle constructed will fully initialize this node, and the last
  50.    * NodeHandle destructed will close down the node.
  51.    */
  52.   ros::NodeHandle n;
  53.  
  54.   /**
  55.    * The advertise() function is how you tell ROS that you want to
  56.    * publish on a given topic name. This invokes a call to the ROS
  57.    * master node, which keeps a registry of who is publishing and who
  58.    * is subscribing. After this advertise() call is made, the master
  59.    * node will notify anyone who is trying to subscribe to this topic name,
  60.    * and they will in turn negotiate a peer-to-peer connection with this
  61.    * node.  advertise() returns a Publisher object which allows you to
  62.    * publis messages on that topic through a call to publish().  Once
  63.    * all copies of the returned Publisher object are destroyed, the topic
  64.    * will be automatically unadvertised.
  65.    *
  66.    * The second parameter to advertise() is the size of the message queue
  67.    * used for publishing messages.  If messages are published more quickly
  68.    * than we can send them, the number here specifies how many messages to
  69.    * buffer up before throwing some away.
  70.    */
  71.   ros::Publisher command_pub = n.advertise<sensor_msgs::JointState>("/ptu/cmd", 1);
  72.  
  73.   ros::Rate loop_rate(10);
  74.  
  75.   /**
  76.    * A count of how many messages we have sent. This is used to create
  77.    * a unique string for each message.
  78.    */
  79.   int i = 0;
  80.   while (ros::ok())
  81.   {
  82.     /**
  83.      * This is a message object. You stuff it with data, and then publish it.
  84.      */
  85.     sensor_msgs::JointState msg;
  86.  
  87.     double pan_vel = 0.0;
  88.     double tilt_vel = 0.0;
  89.  
  90.  
  91.     std::vector<double> vel(2);
  92.     vel[0] = pan_vel;
  93.     vel[1] = values[i];
  94.     cout << "vel: " << vel[i] << endl;
  95.  
  96.     msg.velocity = vel;
  97.  
  98.  
  99.     /**
  100.      * The publish() function is how you send messages. The parameter
  101.      * is the message object. The type of this object must agree with the type
  102.      * given as a template parameter to the advertise<>() call, as was done
  103.      * in the constructor above.
  104.      */
  105.     command_pub.publish(msg);
  106.     if(i<100)
  107.     {
  108.         i++;
  109.     }
  110.     else
  111.     {  
  112.         cout << "re" << endl;
  113.     }
  114.     //ros::Duration(1).sleep();
  115.     ros::spinOnce();
  116.  
  117.     loop_rate.sleep();
  118.   }
  119.  
  120.  
  121.   return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment