sr_ronex_read_analog_data.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00003 * 
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 3.0 of the License, or (at your option) any later version.
00008 * 
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012 * Lesser General Public License for more details.
00013 * 
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library.
00016 */
00017 
00024 //-------------------------------------------------------------------------------
00025 
00026 #include <string>
00027 #include <vector>
00028 #include <ros/ros.h>
00029 #include <ros/console.h>
00030 #include <boost/lexical_cast.hpp>
00031 
00032 #include "sr_ronex_msgs/GeneralIOState.h"
00033 #include "sr_ronex_utilities/sr_ronex_utilities.hpp"
00034 
00035 //-------------------------------------------------------------------------------
00036 
00040 class SrRonexFindGeneralIOModule
00041 {
00042 public:
00043   SrRonexFindGeneralIOModule() {}
00044   ~SrRonexFindGeneralIOModule() {}
00045 
00046 public:
00054   bool get_path_(const uint16_t& ronex_id_as_int, std::string& path)
00055   {
00056     std::string ronex_id = this->to_string_(ronex_id_as_int);
00057 
00058     // Wait until there's at least one General I/O module.
00059     ros::Rate loop_rate(10);
00060     std::string param;
00061     while ( ros::param::get("/ronex/devices/0/ronex_id", param ) == false )
00062     {
00063       ROS_INFO_STREAM("Waiting for General I/O module to be loaded properly.\n");
00064       loop_rate.sleep();
00065     }
00066 
00067     // When -1 is returned, the module with the given id is not present on the parameter server.
00068     // Note that ronex parameter id starts from zero.
00069     int ronex_parameter_id = ronex::get_ronex_param_id(ronex_id);
00070     if ( ronex_parameter_id == -1 )
00071     {
00072       ROS_ERROR_STREAM("Did not find the General I/O module with ronex_id " << ronex_id << ".\n");
00073       return false;  // Failed to set path.
00074     }
00075 
00076     // The module is present on the parameter server and ronex_parameter_id
00077     // contains the id on which the module is stored on the parameter server.
00078 
00079     std::string path_key = ronex::get_ronex_devices_string(ronex_parameter_id, std::string("path"));
00080     ros::param::get(path_key, path);
00081 
00082     return true;  // Path is set.
00083   }
00084 
00085 private:
00092   std::string to_string_(int d)
00093   {
00094     return boost::lexical_cast<std::string>(d);
00095   }
00096 };
00097 
00098 //-------------------------------------------------------------------------------
00099 
00105 void generalIOState_callback(const sr_ronex_msgs::GeneralIOState::ConstPtr& msg)
00106 {
00107   const std::vector<uint16_t > &analogue = msg->analogue;
00108   const size_t len = analogue.size();
00109   for (size_t k = 0; k < len; k++)
00110     ROS_INFO_STREAM("analogue[" << k << "] = " << analogue[k] << "\n");
00111 }
00112 
00113 //-------------------------------------------------------------------------------
00114 
00118 int main(int argc, char **argv)
00119 {
00120   // Initialize ROS with a unique node name.
00121   ros::init(argc, argv, "sr_ronex_read_analog_data");
00122 
00123   // Create a handle to this process' node.
00124   ros::NodeHandle n;
00125 
00126   // Get the path of the General I/O module with the given ronex id.
00127   // Note that you may have to set the value of ronex_id,
00128   // depending on which General I/O board the input device is connected to.
00129   uint16_t ronex_id;
00130   std::cout << "Please enter the ronex id: ";
00131   std::cin >> ronex_id;
00132   std::string path;
00133   SrRonexFindGeneralIOModule findModule;
00134   if ( findModule.get_path_( ronex_id, path ) )
00135   {
00141     // For example "/ronex/general_io/1" + "/state"
00142     std::string topic = path + "/state";
00143     ros::Subscriber sub = n.subscribe(topic.c_str(),
00144                                        1000,
00145                                        generalIOState_callback);
00146 
00152     ros::spin();
00153   }
00154 
00155   return 0;
00156 }
00157 
00158 //-------------------------------------------------------------------------------


sr_ronex_examples
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless, Yi Li
autogenerated on Thu Jun 6 2019 21:22:11