Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
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
00053 cmd = Shifter()
00054 cmd.gear = gear
00055 log_shifter_cmd(cmd)
00056 topic.publish(cmd)
00057
00058
00059
00060 rospy.sleep(1.0)
00061
00062
00063
00064
00065
00066
00067
00068
00069 reset_cmd = Shifter()
00070 reset_cmd.gear = Shifter.Reset
00071 log_shifter_cmd(reset_cmd)
00072 topic.publish(reset_cmd)
00073
00074 rospy.sleep(1.0)
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
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)