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

« back to all changes in this revision

Viewing changes to sr_control_gui/src/sr_control_gui/hand_step_player.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
 
#!/usr/bin/python
2
 
import sys
3
 
sys.path.append("python_hand_library/")
4
 
import time
5
 
import subprocess
6
 
 
7
 
#import wx GUI
8
 
import wx
9
 
 
10
 
#import other stuff
11
 
 
12
 
from Grasp import Grasp
13
 
from grasps_parser import GraspParser
14
 
 
15
 
 
16
 
class StepPlayer(wx.StaticBox):
17
 
    """
18
 
    A GUI widget diplaying tools to replay recorded grasps from an xml file.
19
 
    """
20
 
    def __init__(self, parent, id, title,hand):
21
 
        """
22
 
        Initializes all the variables needed by the widget
23
 
 
24
 
        @param parent : the wx.window component containing the widget
25
 
        @param id : the identifying number of the widget
26
 
        @param title : the title of the widget shown on the border
27
 
        @param hand : the library connecting to ROS 
28
 
        """
29
 
        wx.StaticBox.__init__(self,parent,id, title)
30
 
        self.myShadowHand = hand
31
 
        self.panel = wx.Panel(parent, id)
32
 
        self.file_to_play_tf = 0
33
 
        self.frequency_cb = 0
34
 
        self.frequency_tf = 0
35
 
        self.button_play = 0
36
 
        self.button_browse = 0
37
 
        self.toSend = []
38
 
        self.ready = False
39
 
        self.lastSent = -1
40
 
        self.drawStepPlayer()
41
 
 
42
 
    def drawStepPlayer(self):
43
 
        """
44
 
        Actually draws the component
45
 
        """
46
 
        sizer = wx.FlexGridSizer(rows=2, cols=1, hgap=5, vgap = 5)
47
 
        #sizer_top = wx.FlexGridSizer(cols=2)
48
 
        sizer_down = wx.GridSizer(cols=2)
49
 
        sizer_down_left = wx.FlexGridSizer(rows=2)
50
 
 
51
 
        #self.file_to_play_tf = wx.TextCtrl(self.panel, -1)
52
 
        #self.file_to_play_tf.SetEditable(False)
53
 
        
54
 
        self.button_play = wx.Button(self.panel, -1, 'Play')
55
 
        self.button_play.Enable(False)
56
 
        self.button_browse = wx.FilePickerCtrl(self.panel, -1)
57
 
        self.frequency_cb = wx.CheckBox(self.panel, -1, label='Play all the file')
58
 
        self.frequency_tf = wx.TextCtrl(self.panel, -1)
59
 
        self.frequency_tf.SetValue('Speed')
60
 
        self.frequency_tf.Enable(False)
61
 
 
62
 
        sizer.Add(self.button_browse, flag=wx.EXPAND)
63
 
        sizer_down_left.Add(self.frequency_cb)
64
 
        sizer_down_left.Add(self.frequency_tf)
65
 
        sizer_down.Add(sizer_down_left)        
66
 
        sizer_down.Add(self.button_play, 0, wx.ALIGN_CENTER)
67
 
 
68
 
        #sizer.Add(sizer_top, flag= wx.EXPAND)
69
 
        sizer.Add(sizer_down)
70
 
        border = wx.BoxSizer()
71
 
        border.Add(sizer, 0, wx.ALL, 15)
72
 
        self.panel.SetSizer(border)
73
 
 
74
 
        self.frequency_cb.Bind(wx.EVT_CHECKBOX, self.checkBoxEvent)
75
 
        self.button_play.Bind(wx.EVT_BUTTON, self.playEvent)
76
 
        self.button_browse.Bind(wx.EVT_FILEPICKER_CHANGED, self.chooseEvent)
77
 
 
78
 
    def checkBoxEvent(self, event):
79
 
        """
80
 
        Binds the checkbox control, activates the speed field
81
 
 
82
 
        @param event : the event thrown by the checkbox when clicked 
83
 
        """
84
 
        if self.frequency_cb.IsChecked():
85
 
            self.frequency_tf.Enable(True)    
86
 
        else : 
87
 
            self.frequency_tf.Enable(False)
88
 
 
89
 
    def chooseEvent(self, event):
90
 
        """
91
 
        Binds the browser to its action : parsing the choosen file    
92
 
 
93
 
        @param event : the event thrown when the button is clicked
94
 
        """
95
 
        parser = GraspParser()
96
 
        parser.parse_tree(self.button_browse.GetPath())
97
 
        grasps = parser.grasps
98
 
        for key, value in grasps.items():
99
 
            self.toSend.append(value)
100
 
        self.ready = True
101
 
        self.button_play.Enable(True)
102
 
 
103
 
    def playEvent(self, event):
104
 
        """
105
 
        Binds the play button to its action : reading the file with the specified options
106
 
 
107
 
        @param event : the event thrown when the button is clicked
108
 
        """
109
 
        print self.button_browse.GetPath()
110
 
        #print parser.grasps
111
 
        if self.frequency_cb.IsChecked() and self.ready:
112
 
            speed = float(self.frequency_tf.GetValue())
113
 
            for index in range(0, len(self.toSend)):
114
 
                time.sleep(speed)
115
 
                self.myShadowHand.sendupdate_from_dict(self.toSend[index].joints_and_positions)
116
 
        if (not self.frequency_cb.IsChecked()) and self.ready:
117
 
            if self.lastSent != len(self.toSend)-1:
118
 
                self.lastSent = self.lastSent+1
119
 
                
120