joint_panel.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 #***********************************************************
00004 #* Software License Agreement (BSD License)
00005 #*
00006 #* Copyright (c) 2011, A.M.Howard, S.Williams
00007 #* All rights reserved.
00008 #* 
00009 #* Redistribution and use in source and binary forms, with or without
00010 #* modification, are permitted provided that the following conditions are met:
00011 #*     * Redistributions of source code must retain the above copyright
00012 #*       notice, this list of conditions and the following disclaimer.
00013 #*     * Redistributions in binary form must reproduce the above copyright
00014 #*       notice, this list of conditions and the following disclaimer in the
00015 #*       documentation and/or other materials provided with the distribution.
00016 #*     * Neither the name of the <organization> nor the
00017 #*       names of its contributors may be used to endorse or promote products
00018 #*       derived from this software without specific prior written permission.
00019 #*  
00020 #* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021 #* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022 #* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023 #* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00024 #* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 #* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026 #* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027 #* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 #* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 #* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 #***********************************************************
00031 
00032 import wx
00033 
00034 class JointPanel(wx.Panel):
00035     def __init__(self, parent, joint_name="joint", min_position=-3.14159, max_position=3.14159, max_velocity=100.0, max_effort=100.0, input_mode=True):
00036         
00037         # Create Joint Properties and initialize to None
00038         self.joint_name = joint_name
00039         self.min_position = min_position
00040         self.max_position = max_position
00041         self.max_velocity = max_velocity
00042         self.max_effort = max_effort
00043         self.position = (max_position - min_position)/2 + min_position
00044         self.velocity = max_velocity/2
00045         self.effort = max_effort
00046         self.input_mode = input_mode
00047         
00048         # Set some control properties
00049         self.slider_increments = 100
00050         
00051         # Create Control Panel
00052         wx.Panel.__init__(self, parent, wx.ID_ANY, style=wx.BORDER_SUNKEN)
00053         
00054         # Create slider: min_label - slider - max_label
00055         self.min_position_label = wx.StaticText(self, wx.ID_ANY, str(self.min_position))
00056         self.position_slider = wx.Slider(self, wx.ID_ANY, minValue=0, maxValue=self.slider_increments, style=wx.SL_AUTOTICKS | wx.SL_HORIZONTAL)
00057         self.position_slider.Enable(self.input_mode)
00058         self.max_position_label = wx.StaticText(self, wx.ID_ANY, str(self.max_position))
00059         
00060         sizer_slider = wx.BoxSizer(wx.HORIZONTAL)
00061         sizer_slider.Add(self.min_position_label, 0, wx.CENTER | wx.ALL, 5)
00062         sizer_slider.Add(self.position_slider, 1, wx.CENTER)
00063         sizer_slider.Add(self.max_position_label, 0, wx.CENTER | wx.ALL, 5)
00064         
00065         # Create Joint Properties display boxes
00066         self.position_label = wx.StaticText(self, wx.ID_ANY, "Position")
00067         self.position_text = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_PROCESS_ENTER)
00068         self.velocity_label = wx.StaticText(self, wx.ID_ANY, "Velocity")
00069         self.velocity_text = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_PROCESS_ENTER)
00070         self.effort_label = wx.StaticText(self, wx.ID_ANY, "Effort")
00071         self.effort_text = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_PROCESS_ENTER)
00072         
00073         sizer_properties = wx.BoxSizer(wx.HORIZONTAL)
00074         sizer_properties.Add(self.position_label, 0, wx.CENTER | wx.ALL, 5)
00075         sizer_properties.Add(self.position_text, 0, wx.CENTER | wx.TOP | wx.BOTTOM | wx.LEFT, 5)
00076         sizer_properties.Add((1,1), 1, wx.CENTER)
00077         sizer_properties.Add(self.velocity_label, 0, wx.CENTER | wx.ALL, 5)
00078         sizer_properties.Add(self.velocity_text, 0, wx.CENTER | wx.TOP | wx.BOTTOM, 5)
00079         sizer_properties.Add((1,1), 1, wx.CENTER)
00080         sizer_properties.Add(self.effort_label, 0, wx.CENTER | wx.ALL, 5)
00081         sizer_properties.Add(self.effort_text, 0, wx.CENTER | wx.TOP | wx.BOTTOM | wx.RIGHT, 5)
00082 
00083         # Stack the Slider and the Properties vertically
00084         sizer_panel = wx.BoxSizer(wx.VERTICAL)
00085         sizer_panel.Add(sizer_slider, 0, wx.EXPAND)
00086         sizer_panel.Add(sizer_properties, 0, wx.EXPAND)
00087         
00088         self.SetSizer(sizer_panel)
00089         
00090         # Register Callbacks
00091         if self.input_mode:
00092             self.position_slider.Bind(wx.EVT_SCROLL, self._on_slider_update)
00093             self.position_text.Bind(wx.EVT_TEXT_ENTER, self._on_position_update)
00094             self.position_text.Bind(wx.EVT_KILL_FOCUS, self._on_position_update)
00095             self.velocity_text.Bind(wx.EVT_TEXT_ENTER, self._on_velocity_update)
00096             self.velocity_text.Bind(wx.EVT_KILL_FOCUS, self._on_velocity_update)
00097             self.effort_text.Bind(wx.EVT_TEXT_ENTER, self._on_effort_update)
00098             self.effort_text.Bind(wx.EVT_KILL_FOCUS, self._on_effort_update)
00099 
00100         # Perform an initial update/redraw of the panel
00101         self.update_panel()
00102 
00103     def update_panel(self):
00104         if (self.position is not None):
00105             self.position_slider.SetValue(self.position2slider(self.position))
00106             self.position_text.SetValue('%.5f' % self.position)
00107         else:
00108             self.position_slider.SetValue(self.position2slider(self.min_position))
00109             self.position_text.SetValue('')
00110         if (self.velocity is not None):
00111             self.velocity_text.SetValue('%.5f' % self.velocity)
00112         else:
00113             self.velocity_text.SetValue('')
00114         if (self.effort is not None):
00115             self.effort_text.SetValue('%.5f' % self.effort)
00116         else:
00117             self.effort_text.SetValue('')
00118 
00119     def position2slider(self, position):
00120         # Convert a process value into a slider increment
00121         return int(round(self.slider_increments * (position - self.min_position) / (self.max_position - self.min_position)))
00122     
00123     def slider2position(self, slider):
00124         # Convert a process value into a slider increment
00125         return (float(slider) / self.slider_increments * (self.max_position - self.min_position) + self.min_position) 
00126 
00127     def _on_slider_update(self, event):
00128         self.position = self.slider2position(self.position_slider.GetValue())
00129         self.update_panel()
00130 
00131     def _on_position_update(self, event):
00132         try:
00133             self.position = float(self.position_text.GetValue())
00134             self.position = min(max(self.position, self.min_position), self.max_position)
00135         except:
00136             self.position = None
00137         self.update_panel()
00138 
00139     def _on_velocity_update(self, event):
00140         try:
00141             self.velocity = float(self.velocity_text.GetValue())
00142             if self.velocity > self.max_velocity:
00143                 self.velocity = self.max_velocity
00144             elif self.velocity < -self.max_velocity:
00145                 self.velocity = -self.max_velocity
00146         except:
00147             self.velocity = None
00148         self.update_panel()
00149 
00150     def _on_effort_update(self, event):
00151         try:
00152             self.effort = float(self.effort_text.GetValue())
00153             if self.effort > self.max_effort:
00154                 self.effort = self.max_effort
00155         except:
00156             self.effort = None
00157         self.update_panel()
00158 


actuator_array_gui
Author(s): Stephen Williams
autogenerated on Wed Nov 27 2013 11:39:52