Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
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 
00065     max_avel = 0.18
00066 
00067     xvel = 0.0
00068     yvel = 0.0
00069     avel = 0.0
00070 
00071     
00072     pygame.init()
00073 
00074     
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:   
00111                     x = -0.5 * event.value - 0.5
00112                 elif event.axis == 1 and x == 0:   
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 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128         if x == 0 and y == 0 and a ==0:
00129             connected = True
00130 
00131         
00132         try:
00133             js.quit()
00134             js.init()
00135         except pygame.error:
00136             print "joystick error"
00137             rospy.signal_shutdown()
00138 
00139 
00140 
00141 
00142         
00143         if connected:
00144             xvel = x*max_xvel
00145 
00146 
00147 
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     
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