cyberglove_library.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2011 Shadow Robot Company Ltd.
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License as published by the Free
00007 # Software Foundation, either version 2 of the License, or (at your option)
00008 # any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful, but WITHOUT
00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 # more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #
00018 import roslib; roslib.load_manifest('cyberglove')
00019 
00020 import time
00021 import os
00022 import math
00023 import rospy
00024 import subprocess
00025 import threading
00026 import rosgraph.masterapi
00027 from sr_robot_msgs.msg import sendupdate, joint, joints_data
00028 from sensor_msgs.msg import *
00029 
00030 class Joint():
00031     def __init__(self, name="", motor="", min=0, max=90):
00032         self.name = name
00033         self.motor = motor
00034         self.min = min
00035         self.max = max
00036 
00037 class Cyberglove:
00038     """
00039     Interface to the Cyberglove publisher.
00040     """
00041     def __init__(self, max_values = 2):
00042         self.joints = { "G_ThumbRotate": Joint(),
00043                         "G_ThumbMPJ": Joint(),
00044                         "G_ThumbIJ": Joint(),
00045                         "G_ThumbAb": Joint(),
00046                         "G_IndexMPJ": Joint(),
00047                         "G_IndexPIJ": Joint(),
00048                         "G_IndexDIJ": Joint(),
00049                         "G_MiddleMPJ": Joint(),
00050                         "G_MiddlePIJ": Joint(),
00051                         "G_MiddleDIJ": Joint(),
00052                         "G_MiddleIndexAb": Joint(),
00053                         "G_RingMPJ": Joint(),
00054                         "G_RingPIJ": Joint(),
00055                         "G_RingDIJ": Joint(),
00056                         "G_RingMiddleAb": Joint(),
00057                         "G_PinkieMPJ": Joint(),
00058                         "G_PinkiePIJ": Joint(),
00059                         "G_PinkieDIJ": Joint(),
00060                         "G_PinkieRingAb": Joint(),
00061                         "G_PalmArch": Joint(),
00062                         "G_WristPitch": Joint(),
00063                         "G_WristYaw": Joint() }
00064 
00065         self.raw_messages = []
00066         self.calibrated_messages = []
00067         self.max_values = max_values
00068         self.map = {}
00069         self.hasglove = 0
00070         self.isFirstMessage = True
00071         self.liste = 0
00072         self.raw = rospy.Subscriber('/cyberglove/raw/joint_states',JointState,self.callback_raw)
00073         self.calibrated = rospy.Subscriber('/cyberglove/calibrated/joint_states',JointState,self.callback_calibrated)
00074         threading.Thread(None, rospy.spin)
00075         if self.has_glove():
00076             time.sleep(1.0)
00077             self.createMap()
00078         else:
00079             raise Exception("No glove found")
00080 
00081     def callback_raw(self, data):
00082         """
00083         Adds the last values received to the list of raw values
00084         @param data: the message which called the callback
00085         """
00086         self.addValue(self.raw_messages, data)
00087 
00088     def callback_calibrated(self, data):
00089         """
00090         Adds the last values received to the list of calibrated values
00091         @param data: the message which called the callback
00092         """
00093         self.addValue(self.calibrated_messages, data)
00094 
00095     def addValue(self, vector, value):
00096         """
00097         Fills a vector with the received values, and replaces the old values
00098         when the vector is full
00099         @param vector : the vector to fill (raw or calibrated)
00100         @param value : the value to add
00101         """
00102         if len(vector) < self.max_values:
00103             vector.append(value)
00104         else:
00105             vector.pop(0)
00106             vector.append(value)
00107 
00108     def createMap(self):
00109         """
00110         Maps the name of the joints to their index in the message
00111         """
00112         for index in range(0, len(self.raw_messages[0].name)):
00113             self.map[self.raw_messages[0].name[index]] = index
00114 
00115     def read_raw_average_value(self, joint_name):
00116         """
00117         return the raw value of a given joint
00118 
00119         @param joint_name: the name of the glove of the Cyberglove
00120         """
00121         raw_value =  0
00122         joint_index = self.map[joint_name]
00123         for index in range(0, len(self.raw_messages)):
00124             raw_value = raw_value + self.raw_messages[index].position[joint_index]
00125 
00126         raw_value = raw_value / len(self.raw_messages)
00127 
00128         return raw_value
00129 
00130     def read_calibrated_average_value(self, joint_name):
00131         """
00132         return the current positions for the given joint_name
00133 
00134         @param joint_name:  the name of the joint
00135         @return: the corresponding position
00136         """
00137 
00138         calibrated_value = 0
00139 
00140         joint_index = self.map[joint_name]
00141         for index in range(0, len(self.calibrated_messages)):
00142             calibrated_value = calibrated_value + self.calibrated_messages[index].position[joint_index]
00143 
00144         calibrated_value = calibrated_value / len(self.calibrated_messages)
00145 
00146         return calibrated_value
00147 
00148     def get_joints_names(self):
00149         """
00150         Return an array containing the Cyberglove joints names
00151 
00152         @return: the joints names array
00153         """
00154         return self.joints.keys()
00155 
00156     def has_glove(self):
00157         """
00158         @return: True if a cyberglove is detected by ROS
00159         """
00160         if not self.hasglove == 0:
00161             return self.hasglove
00162         self.hasglove = False
00163         if self.liste == 0:
00164             master = rosgraph.masterapi.Master('/rostopic')
00165             self.liste = master.getPublishedTopics('/')
00166         for topic_typ in self.liste :
00167             for topic in topic_typ:
00168                 if '/cyberglove' in topic :
00169                     self.hasglove = True
00170         return self.hasglove


cyberglove
Author(s): Ugo Cupcic
autogenerated on Fri Jan 3 2014 12:04:16