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
00033
00034
00035 import roslib; roslib.load_manifest('kobuki_testsuite')
00036 import rospy
00037 import math
00038 from geometry_msgs.msg import Twist
00039 from nav_msgs.msg import Odometry
00040 from sensor_msgs.msg import Imu
00041 from kobuki_msgs.msg import BumperEvent
00042 from kobuki_msgs.msg import Sound
00043
00044 class Test_Translation:
00045
00046 def __init__(self):
00047 rospy.init_node("test_translation")
00048 rospy.loginfo('hello kobuki')
00049 self._init_params()
00050 self._init_pubsub()
00051
00052
00053 def _init_params(self):
00054
00055 self.twist = Twist()
00056
00057 self.imu = 0
00058 self.last_odom = 0
00059 self.odom = 0
00060 self.odom_count = 0
00061 self.distance = 1
00062
00063 self.bumper = 0
00064 self.state = 0
00065
00066 self.sound = 0
00067
00068 self.freq = 5
00069 self.rate = rospy.Rate(self.freq)
00070
00071 self.twist.linear.x = 0
00072 self.twist.linear.y = 0
00073 self.twist.linear.z = 0
00074 self.twist.angular.x = 0
00075 self.twist.angular.y = 0
00076 self.twist.angular.z = 0
00077
00078 def _init_pubsub(self):
00079 self.cmd_vel_pub = rospy.Publisher("/mobile_base/commands/velocity", Twist)
00080 self.odom_sub = rospy.Subscriber("/odom", Odometry, self.OdomInfoCallback)
00081 self.bumper_event_sub = rospy.Subscriber("/mobile_base/events/bumper", BumperEvent, self.BumperEventCallback)
00082 self.imu_sub = rospy.Subscriber("mobile_base/sensors/imu_data", Imu, self.ImuInfoCallback)
00083
00084
00085
00086 def OdomInfoCallback(self,data):
00087 self.odom = data.pose.pose.position.x
00088
00089 if(self.odom_count == 0):
00090 self.last_odom = self.odom
00091 self.odom_count = self.odom_count + 1
00092
00093 def BumperEventCallback(self,data):
00094 self.state = data.state
00095 self.bumper = data.bumper
00096
00097 def ImuInfoCallback(self, data):
00098 self.imu = data.orientation.w
00099
00100
00101
00102
00103
00104 def ShowOdomInfo(self):
00105
00106 if math.fabs((self.last_odom+1)-self.odom) <= 0.01:
00107 self.twist.linear.x = 0.02
00108 self.cmd_vel_pub.publish(self.twist)
00109
00110 elif self.odom >= self.last_odom+1 :
00111 self.twist.linear.x = 0
00112 self.cmd_vel_pub.publish(self.twist)
00113 else:
00114 self.twist.linear.x = 0.2
00115 self.cmd_vel_pub.publish(self.twist)
00116
00117 if self.state == 1:
00118 while not self.state == 0:
00119 rospy.loginfo("bumper event")
00120 self.twist.linear.x = 0
00121 self.cmd_vel_pub.publish(self.twist)
00122 print('---- bumper event ----')
00123 self.state = 0
00124
00125 if self.odom > 0:
00126 rospy.loginfo("info_odom : %.4f start_odom : %.4f dist_percent %.4f cmd_vel : %.4f ", self.odom, self.last_odom, (self.odom -self.last_odom)*100, self.twist.linear.x)
00127
00128 def ShowBumperEventInfo(self):
00129 if(self.state == BumperEvent.RELEASED):
00130 state = "released"
00131 else:
00132 state = "pressed"
00133 if(self.bumper == BumperEvent.LEFT):
00134 bumper = "Left"
00135 elif(self.bumper == BumperEvent.CENTER):
00136 bumper = "Center"
00137 else:
00138 bumper = "Right"
00139 rospy.loginfo("%s bumper is %s", bumper, state)
00140
00141 def ShowImuInfo(self):
00142 print "imu : %.6f"%self.imu
00143
00144
00145 def test_trans_main():
00146 test_trans_obj = Test_Translation()
00147
00148 while not rospy.is_shutdown():
00149 test_trans_obj.ShowOdomInfo()
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 rospy.spin()
00160
00161
00162 if __name__ == '__main__':
00163 test_trans_main()