shift.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Send command to ART shifter interface.
00004 #
00005 #   Copyright (C) 2005, 2007, 2009 Austin Robot Technology
00006 #
00007 #   License: Modified BSD Software License Agreement
00008 #
00009 # $Id: shift.py 865 2010-11-26 19:49:19Z jack.oquin $
00010 #
00011 
00012 # make print compatible with python 2.6 and 3.0
00013 from __future__ import print_function
00014 
00015 import roslib;
00016 roslib.load_manifest('art_servo')
00017 
00018 import sys
00019 import string
00020 import rospy
00021 from art_msgs.msg import Shifter
00022 
00023 # global variables
00024 cur_gear = None
00025 target_gear = None
00026 gear_name = ''
00027 finished = False
00028 
00029 def shifter_state_update(state):
00030     "shifter/state message callback"
00031     global cur_gear, finished
00032     if cur_gear:
00033         if cur_gear != state.gear:
00034             rospy.logdebug("shifted from %u to %u", cur_gear, state.gear)
00035         if not finished and cur_gear == target_gear:
00036             rospy.loginfo("transmission now in " + gear_name)
00037             finished = True
00038     cur_gear = state.gear
00039 
00040 def log_shifter_cmd(cmd):
00041     "log shifter/cmd message"
00042     rospy.logdebug("sending shifter command: " + str(cmd.gear))
00043 
00044 def shift(gear):
00045     "shift transmission to desired gear"
00046     topic = rospy.Publisher('shifter/cmd', Shifter)
00047     rospy.Subscriber('shifter/state', Shifter, shifter_state_update)
00048     rospy.init_node('shift')
00049 
00050     rospy.loginfo('Shifting transmission into ' + gear_name)
00051 
00052     # initially reset shifter
00053     cmd = Shifter()                     # shifter message
00054     cmd.gear = gear
00055     log_shifter_cmd(cmd)
00056     topic.publish(cmd)
00057 
00058     # The Arens Controls hardware mechanism requires holding the shift
00059     # relay on for one second before resetting it.
00060     rospy.sleep(1.0)                    # wait a second
00061 
00062     # TODO: Make sure the shift actually occurred before continuing.
00063     # The command could possibly have been lost.  This version depends
00064     # on the user to notice if anything went wrong.
00065     #
00066     # Moreover, this script might as well not bother to send a command
00067     # if the transmission was already in the desired gear.
00068 
00069     reset_cmd = Shifter()               # reset shifter relay message
00070     reset_cmd.gear = Shifter.Reset
00071     log_shifter_cmd(reset_cmd)
00072     topic.publish(reset_cmd)
00073 
00074     rospy.sleep(1.0)                    # wait another second
00075 
00076     rospy.loginfo('Finished shifting transmission')
00077 
00078 def usage():
00079     "print usage message"
00080     print("""
00081 Usage:
00082 
00083     rosrun art_servo shift.py <gear>
00084 
00085 Where <gear> is one of: park, reverse, neutral, drive.  These names
00086 are not case-sensitive, and may be abbreviated to the first character.
00087 
00088 """)
00089 
00090 if __name__ == '__main__':
00091 
00092     if len(sys.argv) < 2:
00093         usage()
00094         exit(1)
00095 
00096     gears = {'park': Shifter.Park,
00097              'reverse': Shifter.Reverse,
00098              'neutral': Shifter.Neutral,
00099              'drive': Shifter.Drive}
00100 
00101     expand_abbrev = {'p': 'park',
00102                      'r': 'reverse',
00103                      'n': 'neutral',
00104                      'd': 'drive'}
00105 
00106     gear_name = string.lower(sys.argv[1])
00107 
00108     # expand abbreviated names
00109     if gear_name in expand_abbrev:
00110         gear_name = expand_abbrev[gear_name]
00111 
00112     if not gear_name in gears:
00113         print('unknown gear:', gear_name)
00114         usage()
00115         exit(2)
00116 
00117     target_gear = gears[gear_name]
00118 
00119     try:
00120         shift(target_gear)
00121     except rospy.ROSInterruptException: pass
00122 
00123     exit(0)


art_servo
Author(s): Austin Robot Technology, Jack O'Quin
autogenerated on Fri Jan 3 2014 11:09:12