test_translation.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2012, Yujin Robot
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Yujin Robot nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
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         # constructor
00046         def __init__(self):
00047                 rospy.init_node("test_translation")
00048                 rospy.loginfo('hello kobuki')
00049                 self._init_params()     # initialize params
00050                 self._init_pubsub()     # initialize pub & sub  
00051         
00052         # initialize params
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     # Odometry Information
00061                 self.distance = 1       # reference distance
00062 
00063                 self.bumper = 0 # bumper 
00064                 self.state = 0  # bumper state
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                 #self.sound_sub = rospy.Subscriber("/mobile_base/commands/sound", Sound, self.SoundInfoCallback)
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         #def SoundInfoCallback(self,data):
00101         #       self.sound = data.value
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() # odom
00150         
00151 #               test_trans_obj.ShowBumperEventInfo()
00152 
00153 #               test_trans_obj.ShowImuInfo()    
00154 
00155 #               test_trans.obj.ShowSoundInfo()
00156 
00157 
00158 
00159         rospy.spin()
00160 
00161 
00162 if __name__ == '__main__':
00163         test_trans_main()


kobuki_testsuite
Author(s): Jorge Santos Simon
autogenerated on Wed Sep 16 2015 04:35:48