~shadowrobot/sr-ros-interface-ethercat/electric

« back to all changes in this revision

Viewing changes to sr_control_gui/src/sr_control_gui/grasps_parser.py

  • Committer: Ugo Cupcic
  • Date: 2012-08-08 10:17:21 UTC
  • mfrom: (1.1.552 shadow_robot_ethercat)
  • Revision ID: ugo@shadowrobot.com-20120808101721-lutmwmwmt06srqya
1.0.0 stable release for the etherCAT hardware

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import xml.etree.ElementTree as ET
2
 
 
3
 
from Grasp import Grasp
4
 
 
5
 
DEBUG = 0
6
 
 
7
 
class GraspParser():
8
 
    """
9
 
    Parses a XML file describing a grasp.
10
 
    """
11
 
    def __init__(self):
12
 
        #initialize stuff
13
 
        self.xml_tree = ""
14
 
        self.grasps = {}
15
 
    
16
 
    def parse_tree(self, xml_filename="grasps.xml"):
17
 
        """
18
 
        parses a given tree, returns a Grasp
19
 
        
20
 
        Keyword arguments:
21
 
        xml_filename -- the filename where the grasp is defined
22
 
                        if no filename is provided, then the default
23
 
                        value is "grasps.xml"
24
 
        """    
25
 
        #parse the xml tree
26
 
        try:
27
 
            self.xml_tree = ET.parse(xml_filename)
28
 
        except Exception, inst:
29
 
            print "Unexpected error opening %s: %s" % (xml_filename, inst)
30
 
            return
31
 
        
32
 
        tree_root = self.xml_tree.getroot()
33
 
        tree_grasp = tree_root.findall("grasp")
34
 
 
35
 
        for grasp in tree_grasp:
36
 
            grasp_tmp = Grasp()
37
 
            grasp_tmp.grasp_name = grasp.attrib.get("name")
38
 
            if DEBUG >= 2:
39
 
                print "Grasp "+ grasp_tmp.grasp_name
40
 
                
41
 
            joints = grasp.findall("joint")
42
 
            for j in joints:
43
 
                joint_name = j.attrib.get("name")
44
 
                joint_position = float(j.text)
45
 
                if DEBUG >= 2:
46
 
                    print "  "+ joint_name + ": "+ str(joint_position)
47
 
                
48
 
                grasp_tmp.joints_and_positions.update({joint_name:joint_position})                
49
 
            
50
 
            self.grasps.update({grasp_tmp.grasp_name: grasp_tmp})
51
 
            
52
 
############################
53
 
#    MAIN - simple test    #
54
 
############################
55
 
def main():
56
 
    parser = GraspParser()
57
 
    parser.parse_tree()
58
 
    
59
 
    return 0
60
 
 
61
 
 
62
 
# start the script
63
 
if __name__ == "__main__":
64
 
    main()