sr_ronex_read_analog_data.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # ####################################################################
00004 # Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00005 #
00006 # This library is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU Lesser General Public
00008 # License as published by the Free Software Foundation; either
00009 # version 3.0 of the License, or (at your option) any later version.
00010 #
00011 # This library is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014 # Lesser General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU Lesser General Public
00017 # License along with this library.
00018 # ####################################################################
00019 
00020 import roslib
00021 import rospy
00022 from time import sleep
00023 from sr_ronex_msgs.msg import GeneralIOState
00024 
00025 roslib.load_manifest('sr_ronex_examples')
00026 
00027 # --------------------------------------------------------------------------------
00028 
00029 """
00030 The callback function given to the subscribe() call.
00031 """
00032 
00033 
00034 def generalIOState_callback(data):
00035     # Note that the type of data.analogue is tuple.
00036     analogue = data.analogue
00037     rospy.loginfo("analogue = %s", analogue)
00038 
00039 # --------------------------------------------------------------------------------
00040 
00041 """
00042 This class demonstrates how to find the General I/O module with the given ronex_id.
00043 """
00044 
00045 
00046 class SrRonexFindGeneralIOModule(object):
00047 
00048     def __init__(self, ronex_id):
00049         self.ronex_id = ronex_id
00050 
00051     """
00052     Get the path of the General I/O module with the given ronex_id.
00053     Note that None is returned if the module is not found.
00054     """
00055     def get_path(self):
00056         """
00057         Find the ronexes present on the system.
00058         """
00059         # Wait until there's one ronex.
00060         while True:
00061             try:
00062                 rospy.get_param("/ronex/devices/0/ronex_id")
00063                 break
00064             except:
00065                 rospy.loginfo("Waiting for the ronex to be loaded properly.")
00066                 sleep(0.1)
00067 
00068         """
00069         Retrieve all the ronex parameter ids from the parameter server.
00070         If there are three General I/O modules, then ronex_param_ids is [0,1,2].
00071         Note that the id starts from zero. And the size of the returned variable
00072         is equal to the number of General I/O modules.
00073         """
00074         ronex_param_ids = rospy.get_param("/ronex/devices")
00075         for key in ronex_param_ids:
00076             if self.ronex_id == ronex_param_ids[key]["ronex_id"]:
00077                 path = ronex_param_ids[key]["path"]
00078                 return path
00079 
00080 # --------------------------------------------------------------------------------
00081 
00082 """
00083 Assume that your RoNeX consists of a Bridge (IN) module, and one
00084 or multiple General I/O module(s). This example demonstrates how
00085 to read analogue data with RoNeX.
00086 """
00087 if __name__ == "__main__":
00088     rospy.init_node("sr_ronex_read_analog_data")
00089 
00090     # Note that you may have to set the value of ronex_id,
00091     # depending on which General I/O board the input device is connected to.
00092     ronex_id = raw_input("Please enter the ronex id: ")
00093     findModule = SrRonexFindGeneralIOModule(str(ronex_id))
00094     path = findModule.get_path()
00095 
00096     if path is not None:
00097         # For example "/ronex/general_io/1" + "/state".
00098         topic = path + "/state"
00099         rospy.Subscriber(topic, GeneralIOState, generalIOState_callback)
00100         rospy.spin()
00101     else:
00102         rospy.loginfo("Failed to find the General I/O module with the given ronex_id %s.", ronex_id)
00103 
00104 # --------------------------------------------------------------------------------


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