Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00041 publish_to = dict()
00042
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
00058
00059
00060
00061 def inputFromRetalis(data):
00062
00063
00064
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
00079
00080
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
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
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
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()