TeleDir.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Main tele-dir program.
00003 import yaml
00004 
00005 from rospy_message_converter import message_converter
00006 
00007 import AuxFuns
00008 import rospy
00009 from KeyBinding import KeyBinding
00010 import select, sys, tty, termios
00011 import xml.etree.ElementTree as ET
00012 import XmlHandler
00013 import rospkg
00014 import os
00015 import json
00016 
00017 
00018 
00019 global old_attr
00020 
00021 def tele_dir( config ):
00022     '''
00023     :param config:  A valid XML that holds information about what messages to be sent to what
00024                     topic when which button is pressed.
00025     :return None:
00026     This function's purpose is to handle the button inputs as the config parameter states. That is, when a key is
00027     pressed this will send the corresponding message to the corresponding topic.
00028     '''
00029 
00030     tree = ET.parse(config)
00031     configuration = tree.getroot().find("config")
00032     buttons = []
00033     keyboard = {}
00034     messages = []
00035     topics = []
00036     lspeed = 1
00037     aspeed = 1
00038     for topic in configuration.find("topics"):
00039         topics.append(topic)
00040     for button in configuration.find("buttons"):
00041         buttons.append(button)
00042     for message in configuration.find("messages"):
00043         messages.append(message)
00044     for button in buttons:
00045         message_id = button.find("message").text
00046         topic_id = button.find("topic").text
00047         topic_name = ''
00048         for message in messages:
00049             if message.attrib["id"]==message_id:
00050                 message_class = message.find('type').text
00051                 message_content = message.find('content').text
00052                 message_info = message.find('description').text
00053         for topic in topics:
00054             if topic.attrib["id"] == topic_id:
00055                 topic_name = topic.find("name").text
00056         message = message_converter.convert_dictionary_to_ros_message(message_class,json.loads(message_content))
00057         key = KeyBinding(button.find('key').text, message, topic_name, message_info)
00058         keyboard.setdefault( button.find('key').text, key )
00059 
00060     rate = rospy.Rate(60)  # 200hz
00061     global old_attr
00062 
00063     print "Press '.' to exit. \n" \
00064           "Publishing Keystrokes \n"
00065     for keys in keyboard.values():
00066         print keys.get_key() + " " + keys.get_info() + ".\n"
00067 
00068     last_input=''
00069     while not rospy.is_shutdown():
00070         if select.select([sys.stdin], [], [], 0.1)[0] == [sys.stdin]:
00071             input = sys.stdin.read(1).upper()
00072             # if input == '+':
00073             #     lspeed += 0.5
00074             #     print "Linear speed set to", lspeed
00075             # elif input == '-':
00076             #     if lspeed >= 0.5:
00077             #         lspeed -= 0.5
00078             #     else:
00079             #         lspeed = 0.0
00080             #     print "Linear speed set to", lspeed
00081             # elif input == '*':
00082             #     aspeed += 0.5
00083             #     print "Angular speed set to", aspeed
00084             # elif input == '/':
00085             #     if aspeed >= 0.5:
00086             #         aspeed -= 0.5
00087             #     else:
00088             #         aspeed = 0.0
00089             #     print "Angular speed set to", aspeed
00090 
00091             if input in keyboard:
00092                 publish = keyboard.get(input)
00093                 publish.publish_message()
00094                 if last_input!=input:
00095                     rospy.logdebug(publish.info)
00096                     last_input = input
00097 
00098                 rate.sleep()
00099             elif input == '.':
00100                 break
00101             else:
00102                 print "unrecognized input"
00103                 print input
00104 
00105 
00106 
00107 ## main maneja el menu principal y las funcionalidades a llamar
00108 if __name__ == '__main__':
00109     xml_dir = rospkg.RosPack().get_path('tele_dir') + "/src/Configs/"
00110     old_attr = termios.tcgetattr(sys.stdin)
00111     rospy.init_node('tele_dir', anonymous=True)
00112     try:
00113 
00114         print "Welcome to Tele_Dir ! \n" \
00115               "Press C to launch the configuration wizard. \n" \
00116               "Press D to use the default configuration .\n" \
00117               "Press L to load a custom configuration (XML).\n" \
00118               "Press E to edit a custom configuration (XML). \n" \
00119               "Press Q to quit. \n "
00120         while True:
00121             tty.setcbreak(sys.stdin.fileno())
00122 
00123             if select.select([sys.stdin], [], [], 0)[0] == [sys.stdin]:
00124                 input = sys.stdin.read(1).upper()
00125                 print ">" + input
00126                 if input.upper()=='C' :
00127                     termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00128                     XmlHandler.xmlCreator(xml_dir)
00129                     tty.setcbreak(sys.stdin.fileno())
00130 
00131                 elif input.upper() == 'L':
00132                     termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00133                     print "configuraciones:" #TODO:Escribirlo en ingles !!!
00134                     for file in os.listdir(xml_dir):
00135                         if file.endswith(".xml"):
00136 
00137                             print(file.split(".")[0])
00138                     xmlLoad = raw_input("Input the filename: ")
00139                     try:
00140                         if XmlHandler.xml_validator( xml_dir + xmlLoad + ".xml"):
00141                             tty.setcbreak(sys.stdin.fileno())
00142                             tele_dir( xml_dir + xmlLoad + ".xml")
00143                             termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00144                     except:
00145                         print "file "+ xmlLoad + "don't exist."
00146                 elif input.upper() == 'E':
00147                     termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00148                     print "configuraciones:"  # TODO:Escribirlo en ingles !!!
00149                     for file in os.listdir(xml_dir):
00150                         if file.endswith(".xml"):
00151                             print(file.split(".")[0])
00152                     xmlLoad = raw_input("Input the filename: ")
00153                     try:
00154                         XmlHandler.xmlEditor(xml_dir , xmlLoad+".xml")
00155                     except:
00156                         print "file "+xmlLoad+ " don't exist."
00157                     tty.setcbreak(sys.stdin.fileno())
00158 
00159                 elif input.upper() == 'D':
00160                     tele_dir(xml_dir + "default_config.xml")
00161 
00162                 elif input.upper() == 'Q':
00163                     termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00164                     exit(0)
00165 
00166 
00167 
00168     except rospy.ROSInterruptException:
00169         termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00170     except ValueError:
00171         termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
00172 
00173 
00174 


tele_dir
Author(s): Rodrigo Delgado , Steffan Wiche
autogenerated on Thu Jun 6 2019 17:32:56