cyberglove_library.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2011 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 
18 import roslib
19 
20 import time
21 import os
22 import math
23 import rospy
24 import subprocess
25 import threading
26 import rosgraph.masterapi
27 from sr_robot_msgs.msg import sendupdate, joint, joints_data
28 from sensor_msgs.msg import *
29 
30 
31 class Joint():
32 
33  def __init__(self, name="", motor="", min=0, max=90):
34  self.name = name
35  self.motor = motor
36  self.min = min
37  self.max = max
38 
39 
40 class Cyberglove:
41 
42  """
43  Interface to the Cyberglove publisher.
44  """
45 
46  def __init__(self, max_values=2, nb_sensors=22):
47  if nb_sensors == 22:
48  self.joints = {"G_ThumbRotate": Joint(),
49  "G_ThumbMPJ": Joint(),
50  "G_ThumbIJ": Joint(),
51  "G_ThumbAb": Joint(),
52  "G_IndexMPJ": Joint(),
53  "G_IndexPIJ": Joint(),
54  "G_IndexDIJ": Joint(),
55  "G_MiddleMPJ": Joint(),
56  "G_MiddlePIJ": Joint(),
57  "G_MiddleDIJ": Joint(),
58  "G_MiddleIndexAb": Joint(),
59  "G_RingMPJ": Joint(),
60  "G_RingPIJ": Joint(),
61  "G_RingDIJ": Joint(),
62  "G_RingMiddleAb": Joint(),
63  "G_PinkieMPJ": Joint(),
64  "G_PinkiePIJ": Joint(),
65  "G_PinkieDIJ": Joint(),
66  "G_PinkieRingAb": Joint(),
67  "G_PalmArch": Joint(),
68  "G_WristPitch": Joint(),
69  "G_WristYaw": Joint()}
70  if nb_sensors == 18:
71  self.joints = {"G_ThumbRotate": Joint(),
72  "G_ThumbMPJ": Joint(),
73  "G_ThumbIJ": Joint(),
74  "G_ThumbAb": Joint(),
75  "G_IndexMPJ": Joint(),
76  "G_IndexPIJ": Joint(),
77 
78  "G_MiddleMPJ": Joint(),
79  "G_MiddlePIJ": Joint(),
80 
81  "G_MiddleIndexAb": Joint(),
82  "G_RingMPJ": Joint(),
83  "G_RingPIJ": Joint(),
84 
85  "G_RingMiddleAb": Joint(),
86  "G_PinkieMPJ": Joint(),
87  "G_PinkiePIJ": Joint(),
88 
89  "G_PinkieRingAb": Joint(),
90  "G_PalmArch": Joint(),
91  "G_WristPitch": Joint(),
92  "G_WristYaw": Joint()}
93 
94  self.raw_messages = []
96  self.max_values = max_values
97  self.map = {}
98  self.hasglove = 0
99  self.isFirstMessage = True
100  self.liste = 0
101  self.raw = rospy.Subscriber(
102  'rh_cyberglove/raw/joint_states', JointState, self.callback_raw)
103  self.calibrated = rospy.Subscriber(
104  'cyberglove/calibrated/joint_states', JointState, self.callback_calibrated)
105  threading.Thread(None, rospy.spin)
106  if self.has_glove():
107  time.sleep(1.0)
108  self.createMap()
109  else:
110  raise Exception("No glove found")
111 
112  def callback_raw(self, data):
113  """
114  Adds the last values received to the list of raw values
115  @param data: the message which called the callback
116  """
117  self.addValue(self.raw_messages, data)
118 
119  def callback_calibrated(self, data):
120  """
121  Adds the last values received to the list of calibrated values
122  @param data: the message which called the callback
123  """
124  self.addValue(self.calibrated_messages, data)
125 
126  def addValue(self, vector, value):
127  """
128  Fills a vector with the received values, and replaces the old values
129  when the vector is full
130  @param vector : the vector to fill (raw or calibrated)
131  @param value : the value to add
132  """
133  if len(vector) < self.max_values:
134  vector.append(value)
135  else:
136  vector.pop(0)
137  vector.append(value)
138 
139  def createMap(self):
140  """
141  Maps the name of the joints to their index in the message
142  """
143 
144  for index in range(0, len(self.raw_messages[0].name)):
145  self.map[self.raw_messages[0].name[index]] = index
146 
147  def read_raw_average_value(self, joint_name):
148  """
149  return the raw value of a given joint
150 
151  @param joint_name: the name of the glove of the Cyberglove
152  """
153  raw_value = 0
154  joint_index = self.map[joint_name]
155  for index in range(0, len(self.raw_messages)):
156  raw_value = raw_value + \
157  self.raw_messages[index].position[joint_index]
158 
159  raw_value = raw_value / len(self.raw_messages)
160 
161  return raw_value
162 
163  def read_calibrated_average_value(self, joint_name):
164  """
165  return the current positions for the given joint_name
166 
167  @param joint_name: the name of the joint
168  @return: the corresponding position
169  """
170 
171  calibrated_value = 0
172 
173  joint_index = self.map[joint_name]
174  for index in range(0, len(self.calibrated_messages)):
175  calibrated_value = calibrated_value + \
176  self.calibrated_messages[index].position[joint_index]
177 
178  calibrated_value = calibrated_value / len(self.calibrated_messages)
179 
180  return calibrated_value
181 
182  def get_joints_names(self):
183  """
184  Return an array containing the Cyberglove joints names
185 
186  @return: the joints names array
187  """
188  return self.joints.keys()
189 
190  def has_glove(self):
191  """
192  @return: True if a cyberglove is detected by ROS
193  """
194  if not self.hasglove == 0:
195  return self.hasglove
196  self.hasglove = False
197  if self.liste == 0:
198  master = rosgraph.masterapi.Master('rostopic list')
199  self.liste = master.getPublishedTopics('')
200 
201  for topic_typ in self.liste:
202  for topic in topic_typ:
203  if 'cyberglove/raw' in topic:
204  self.hasglove = True
205  return self.hasglove
def __init__(self, name="", motor="", min=0, max=90)


sr_gui_cyberglove_calibrator
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 03:22:50