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 <ros/ros.h>
00028 #include <ros/console.h>
00029 #include <boost/lexical_cast.hpp>
00030 
00031 #include "sr_ronex_msgs/GeneralIOState.h"
00032 #include "sr_ronex_utilities/sr_ronex_utilities.hpp"
00033 
00034 //-------------------------------------------------------------------------------
00035 
00039 class SrRonexFindGeneralIOModule
00040 {
00041 public:
00042   SrRonexFindGeneralIOModule() {}
00043   ~SrRonexFindGeneralIOModule() {}
00044   
00045 public:
00053   bool get_path_( const short unsigned int& ronex_id_as_int, std::string& path )
00054   {
00055     std::string ronex_id = this->to_string_(ronex_id_as_int);
00056     
00057     // Wait until there's at least one General I/O module.
00058     ros::Rate loop_rate(10);
00059     std::string param;
00060     while ( ros::param::get("/ronex/devices/0/ronex_id", param ) == false )
00061     {
00062       ROS_INFO_STREAM( "Waiting for General I/O module to be loaded properly.\n" );
00063       loop_rate.sleep();
00064     }
00065 
00066     // When -1 is returned, the module with the given id is not present on the parameter server. 
00067     // Note that ronex parameter id starts from zero.
00068     int ronex_parameter_id = ronex::get_ronex_param_id(ronex_id);
00069     if ( ronex_parameter_id == -1 )
00070     {
00071       ROS_ERROR_STREAM( "Did not find the General I/O module with ronex_id " << ronex_id << ".\n" );
00072       return false; // Failed to set path.
00073     }
00074     
00075     // The module is present on the parameter server and ronex_parameter_id
00076     // contains the id on which the module is stored on the parameter server.
00077     
00078     std::string path_key = ronex::get_ronex_devices_string( ronex_parameter_id, std::string("path") );
00079     ros::param::get( path_key, path );
00080 
00081     return true; // Path is set.
00082   }
00083   
00084 private:
00091   std::string to_string_(int d)
00092   {
00093     return boost::lexical_cast<std::string>(d);
00094   }
00095 };
00096 
00097 //-------------------------------------------------------------------------------
00098 
00104 void generalIOState_callback(const sr_ronex_msgs::GeneralIOState::ConstPtr& msg)
00105 {
00106   const std::vector<short unsigned int> &analogue = msg->analogue;
00107   const size_t len = analogue.size();
00108   for (size_t k = 0; k < len; k++)
00109     ROS_INFO_STREAM( "analogue[" << k << "] = " << analogue[k] << "\n" );
00110 }
00111 
00112 //-------------------------------------------------------------------------------
00113 
00117 int main(int argc, char **argv)
00118 {
00119   // Initialize ROS with a unique node name.
00120   ros::init(argc, argv, "sr_ronex_read_analog_data");
00121   
00122   // Create a handle to this process' node. 
00123   ros::NodeHandle n;
00124 
00125   // Get the path of the General I/O module with the given ronex id.
00126   // Note that you may have to set the value of ronex_id,
00127   // depending on which General I/O board the input device is connected to.
00128   short unsigned int ronex_id; 
00129   std::cout << "Please enter the ronex id: ";
00130   std::cin >> ronex_id;
00131   std::string path;
00132   SrRonexFindGeneralIOModule findModule;
00133   if ( findModule.get_path_( ronex_id, path ) )
00134   {
00140     // For example "/ronex/general_io/1" + "/state"
00141     std::string topic = path + "/state"; 
00142     ros::Subscriber sub = n.subscribe( topic.c_str(), 
00143                                        1000,
00144                                        generalIOState_callback);
00145     
00151     ros::spin();
00152   }
00153   
00154   return 0;
00155 }
00156 
00157 //-------------------------------------------------------------------------------


sr_ronex_examples
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless, Yi Li
autogenerated on Fri Aug 28 2015 13:12:34