mecanum_pedal.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Copyright (c) 2009, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Cressel Anderson (Healthcare Robotics Lab, Georgia Tech.)
00030 #  \author Marc Killpack (Healthcare Robotics Lab, Georgia Tech.)
00031 
00032 import pygame
00033 import pygame.joystick
00034 import sys, optparse
00035 import segway_command as sc
00036 import time as time
00037 import roslib
00038 roslib.load_manifest('segway_omni')
00039 import rospy
00040 
00041 import hrl_lib.util as ut
00042 import numpy as np
00043 
00044 from pygame.locals import *
00045 
00046 
00047 if __name__=='__main__':
00048     p = optparse.OptionParser()
00049 
00050     p.add_option('-z', action='store_true', dest='zenither',
00051                  help='control the zenither also')
00052 
00053     opt, args = p.parse_args()
00054     zenither_flag = opt.zenither
00055 
00056     if zenither_flag:
00057         import zenither.zenither as zenither
00058         z = zenither.Zenither(robot='HRL2')
00059 
00060     cmd_node = sc.SegwayCommand()
00061 
00062     max_xvel = 0.18 
00063     max_yvel = 0.15
00064     max_speed = 0.18 # don't exceed 0.18 under any condition.
00065     max_avel = 0.18
00066 
00067     xvel = 0.0
00068     yvel = 0.0
00069     avel = 0.0
00070 
00071     #init pygame
00072     pygame.init()
00073 
00074     #joystick_status
00075     joystick_count = pygame.joystick.get_count()
00076 
00077     print "Joysticks found: %d\n" % joystick_count
00078 
00079     try:
00080         js = pygame.joystick.Joystick(0)
00081         js.init()
00082     except pygame.error:
00083         print "joystick error"
00084         js = None
00085 
00086     js_status = js.get_init()
00087     print js_status
00088 
00089     screen = pygame.display.set_mode((320,80))
00090     pygame.display.set_caption('Snozzjoy')
00091 
00092     background = pygame.Surface(screen.get_size())
00093     background = background.convert()
00094     background.fill((250,250,250))
00095 
00096     x=0.
00097     y=0.
00098     a=0.
00099 
00100     len = 5
00101     xvel_history = np.matrix(np.zeros([len,1]))
00102 
00103     connected = False
00104     while not rospy.is_shutdown():
00105 
00106         move_zenither_flag=False
00107         for event in pygame.event.get():
00108             
00109             if (event.type == JOYAXISMOTION):
00110                 if event.axis == 0:   #left pedal
00111                     x = -0.5 * event.value - 0.5
00112                 elif event.axis == 1 and x == 0:   #right pedal
00113                     x = 0.5 * event.value + 0.5
00114                 if event.axis == 2:
00115                     a = event.value
00116                 print 'event.axis: ',event.axis,'event.value: ',event.value
00117 
00118 #            elif (event.type == JOYBUTTONDOWN):
00119 #                if zenither_flag:
00120 #                    if(event.button == 0):
00121 #                        z.zenith(z.calib['up_slow_torque'])
00122 #                        move_zenither_flag=True
00123 #                    if(event.button == 1):
00124 #                        z.nadir(z.calib['down_snail_torque'])
00125 #                        move_zenither_flag=True
00126 
00127 
00128         if x == 0 and y == 0 and a ==0:
00129             connected = True
00130 
00131         # detect a joystick disconnect
00132         try:
00133             js.quit()
00134             js.init()
00135         except pygame.error:
00136             print "joystick error"
00137             rospy.signal_shutdown()
00138 
00139 #        if zenither_flag and (move_zenither_flag == False):
00140 #            z.estop()
00141 
00142         #send segway commands
00143         if connected:
00144             xvel = x*max_xvel
00145 #            xvel_history[0:(len-1)] = xvel_history[1:len]
00146 #            xvel_history[(len-1)] = xvel
00147 #            xvel = np.mean(xvel_history)
00148 
00149             yvel = y*max_yvel
00150             avel = a*max_avel
00151 
00152             vel_vec = np.matrix([xvel,yvel]).T
00153             vel_mag = np.linalg.norm(vel_vec)
00154             speed = ut.bound(vel_mag,max_speed,0.)
00155             if speed >= 0.05:
00156                 vel_vec = vel_vec*speed/vel_mag
00157             
00158             xvel,yvel = vel_vec[0,0],vel_vec[1,0]
00159             cmd_node.set_velocity(xvel,yvel,avel)               
00160             print '*******xvel=',xvel,'; avel=',avel            
00161     # stop the segway
00162     cmd_node.set_velocity(0.,0.,0.)
00163 
00164 
00165 


hrl_segway_omni
Author(s): Cressel Anderson, Hai Nguyen, Marc Killpack, Advait Jain Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:55:25