retalis_ros_converter.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ##################################################################################################
00004 #    This is part of the Retalis Language for Information Processing and Management in Robotics
00005 #    Copyright (C) 2014 __Pouyan Ziafati__ pziafati@gmail.com 
00006 #
00007 #    Retalis is free software: you can redistribute it and/or modify
00008 #    it under the terms of the GNU General Public License as published by
00009 #    the Free Software Foundation, either version 3 of the License, or
00010 #    (at your option) any later version.
00011 #
00012 #    Retalis is distributed in the hope that it will be useful,
00013 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 #    GNU General Public License for more details.                   
00016 #
00017 #    You should have received a copy of the GNU General Public License
00018 #    along with Retalis.  If not, see <http://www.gnu.org/licenses/>.   
00019 #####################################################################################################
00020 
00021 
00022 import roslib; roslib.load_manifest('retalis')
00023 import xml.etree.ElementTree as ET
00024 from retalis_ros_helper import *
00025 from retalis.srv import *
00026 from std_msgs.msg import String
00027 from threading import *
00028 import rospy
00029 import roslib.message
00030 import rostopic
00031 import rosmsg
00032 import SocketServer
00033 SocketServer.TCPServer.allow_reuse_address = True 
00034 import os
00035 import os.path
00036 import sys
00037 from datetime import datetime
00038 import rospkg
00039 
00040 # a dictionary which keeps key-value pairs of the form (topic-name,ros-publisher-to-this-topic)
00041 publish_to = dict()
00042 # a dictionary which keeps key-value pairs of the form (topic-name,message-type-of-this-topic). 
00043 publish_to_msg_type = dict()
00044 
00045 subscribed_to = dict()
00046 
00047 
00048 """
00049 handler for messages received on topics to which we are subscribed. It converts ROS messages of any type to events.     
00050 """
00051 def inputFromRosCB(data):
00052         sendRosMessageToRetalis(data)
00053 
00054 """
00055 handler for messages of type etalis_ros/RegWinSMC received from the topic "smc_query". Each message specifies a subscription window. This handler converts the subscription window query to corresponding etalis query and sent it to etalis.
00056 """
00057 #def smc_query_callback(data):
00058 #       sendSMCQueryMessageToEtalis(data)
00059 
00060 
00061 def inputFromRetalis(data):
00062                 #print data
00063                 #ee = data.data.split('\\ \\ \\ \\ \\ \\ ');
00064                 #eee = ee[1].split('-') 
00065                 conv = extractInfoFromSMC(data.data)
00066 
00067                 rosMsgClass = roslib.message.get_message_class(publish_to_msg_type[conv[1]])
00068                 rosMsg = rosMsgClass()
00069                 
00070                 convertEventToRos(conv[0],rosMsg,rosMsgClass)
00071                 publish_to[conv[1]].publish(rosMsg)     
00072 
00073 
00074 
00075 def initialize():
00076         rospy.init_node('retalis_ros_converter')
00077 
00078         #xml processing of the topics of interest to subscribe to and to publish to.
00079         #basepath = os.path.dirname(__file__)
00080         #pub_sub_filepath = os.path.abspath(os.path.join(basepath, '..', 'data', 'pub_sub.xml'))
00081         rospack = rospkg.RosPack()
00082         pub_sub_filepath = rospack.get_path('retalis')+'/application_source/pub_sub.xml'        
00083         tree = ET.parse(pub_sub_filepath)
00084         root = tree.getroot()
00085 
00086         #subscribe to topics of interest
00087         global subscribed_to
00088         for to_subscribe in root.iter('subscribe_to'):
00089                 try:                    
00090                         message_class = roslib.message.get_message_class(to_subscribe.get('msg_type'))
00091                 except ImportError:
00092                         raise IOError("cannot load [%s]"%(type_))
00093                 subscribed_to[to_subscribe.get('name')] = rospy.Subscriber(to_subscribe.get('name'), message_class, inputFromRosCB)
00094                 print "[INFO] Sucessfully registered to: ", to_subscribe.get('name')    
00095  
00096         #make publisher for topic of interest that we want to publish to in future
00097         global publish_to
00098         for to_publish in root.iter('publish_to'):
00099                 try:                    
00100                         message_class = roslib.message.get_message_class(to_publish.get('msg_type'))
00101                 except ImportError:
00102                         raise IOError("cannot load [%s]"%(type_))               
00103                 publish_to[to_publish.get('name')] = rospy.Publisher(to_publish.get('name'),message_class)
00104                 publish_to_msg_type[to_publish.get('name')] = to_publish.get('msg_type')
00105 
00106 
00107         
00108 
00109         #rospy.Subscriber("smc_sub", RegWinSMC, smc_query_callback)
00110         rospy.Subscriber("retalisOutputEvents", String, inputFromRetalis, queue_size = 10)
00111         s1 = rospy.Service('add_input_subscription', AddInputSubscription, handle_add_input_subscription)
00112         s2 = rospy.Service('delete_input_subscription', DeleteInputSubscription, handle_delete_input_subscription)       
00113 
00114 
00115         
00116 def handle_add_input_subscription(req):
00117     global subscribed_to
00118     try:                        
00119                 message_class = roslib.message.get_message_class(req.message_type)
00120     except ImportError:
00121                 raise IOError("cannot load [%s]"%(req.message_type))
00122     subscribed_to[req.topic] = rospy.Subscriber(req.topic, message_class, inputFromRosCB)
00123     print "[INFO] Sucessfully registered to: ", req.topic
00124     return AddInputSubscriptionResponse('ok')
00125 
00126 
00127         
00128 def handle_delete_input_subscription(req):
00129     global subscribed_to
00130     subscribed_to[req.topic].unregister()
00131     return DeleteInputSubscriptionResponse('ok')
00132 
00133         
00134         
00135 
00136 if __name__ == "__main__":
00137         initialize()
00138         rospy.spin()


retalis
Author(s): Pouyan Ziafati
autogenerated on Fri Aug 28 2015 12:23:31