test_translation.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2012, Yujin Robot
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Yujin Robot nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 import roslib; roslib.load_manifest('kobuki_testsuite')
36 import rospy
37 import math
38 from geometry_msgs.msg import Twist
39 from nav_msgs.msg import Odometry
40 from sensor_msgs.msg import Imu
41 from kobuki_msgs.msg import BumperEvent
42 from kobuki_msgs.msg import Sound
43 
45  # constructor
46  def __init__(self):
47  rospy.init_node("test_translation")
48  rospy.loginfo('hello kobuki')
49  self._init_params() # initialize params
50  self._init_pubsub() # initialize pub & sub
51 
52  # initialize params
53  def _init_params(self):
54 
55  self.twist = Twist()
56 
57  self.imu = 0
58  self.last_odom = 0
59  self.odom = 0
60  self.odom_count = 0 # Odometry Information
61  self.distance = 1 # reference distance
62 
63  self.bumper = 0 # bumper
64  self.state = 0 # bumper state
65 
66  self.sound = 0
67 
68  self.freq = 5
69  self.rate = rospy.Rate(self.freq)
70 
71  self.twist.linear.x = 0
72  self.twist.linear.y = 0
73  self.twist.linear.z = 0
74  self.twist.angular.x = 0
75  self.twist.angular.y = 0
76  self.twist.angular.z = 0
77 
78  def _init_pubsub(self):
79  self.cmd_vel_pub = rospy.Publisher("/mobile_base/commands/velocity", Twist)
80  self.odom_sub = rospy.Subscriber("/odom", Odometry, self.OdomInfoCallback)
81  self.bumper_event_sub = rospy.Subscriber("/mobile_base/events/bumper", BumperEvent, self.BumperEventCallback)
82  self.imu_sub = rospy.Subscriber("mobile_base/sensors/imu_data", Imu, self.ImuInfoCallback)
83  #self.sound_sub = rospy.Subscriber("/mobile_base/commands/sound", Sound, self.SoundInfoCallback)
84 
85 
86  def OdomInfoCallback(self,data):
87  self.odom = data.pose.pose.position.x
88 
89  if(self.odom_count == 0):
90  self.last_odom = self.odom
91  self.odom_count = self.odom_count + 1
92 
93  def BumperEventCallback(self,data):
94  self.state = data.state
95  self.bumper = data.bumper
96 
97  def ImuInfoCallback(self, data):
98  self.imu = data.orientation.w
99 
100  #def SoundInfoCallback(self,data):
101  # self.sound = data.value
102 
103 
104  def ShowOdomInfo(self):
105 
106  if math.fabs((self.last_odom+1)-self.odom) <= 0.01:
107  self.twist.linear.x = 0.02
108  self.cmd_vel_pub.publish(self.twist)
109 
110  elif self.odom >= self.last_odom+1 :
111  self.twist.linear.x = 0
112  self.cmd_vel_pub.publish(self.twist)
113  else:
114  self.twist.linear.x = 0.2
115  self.cmd_vel_pub.publish(self.twist)
116 
117  if self.state == 1:
118  while not self.state == 0:
119  rospy.loginfo("bumper event")
120  self.twist.linear.x = 0
121  self.cmd_vel_pub.publish(self.twist)
122  print('---- bumper event ----')
123  self.state = 0
124 
125  if self.odom > 0:
126  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)
127 
129  if(self.state == BumperEvent.RELEASED):
130  state = "released"
131  else:
132  state = "pressed"
133  if(self.bumper == BumperEvent.LEFT):
134  bumper = "Left"
135  elif(self.bumper == BumperEvent.CENTER):
136  bumper = "Center"
137  else:
138  bumper = "Right"
139  rospy.loginfo("%s bumper is %s", bumper, state)
140 
141  def ShowImuInfo(self):
142  print "imu : %.6f"%self.imu
143 
144 
146  test_trans_obj = Test_Translation()
147 
148  while not rospy.is_shutdown():
149  test_trans_obj.ShowOdomInfo() # odom
150 
151 # test_trans_obj.ShowBumperEventInfo()
152 
153 # test_trans_obj.ShowImuInfo()
154 
155 # test_trans.obj.ShowSoundInfo()
156 
157 
158 
159  rospy.spin()
160 
161 
162 if __name__ == '__main__':


kobuki_testsuite
Author(s): Jorge Santos Simon
autogenerated on Mon Jun 10 2019 13:45:22