00001
00002 import roslib
00003 roslib.load_manifest('iri_segway_rmp400_odom')
00004 import os
00005 import sys
00006 import rospy
00007 import tf
00008 from geometry_msgs.msg import Twist
00009 from nav_msgs.msg import Odometry
00010 import math
00011 from tf.transformations import euler_from_quaternion
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 SB_='\033[1m'
00045 CY_='\033[33m'
00046 CB_='\033[34m'
00047 CLG_='\033[37m'
00048 CR_='\033[31m'
00049 _EC='\033[0m'
00050
00051
00052 p_v = rospy.Publisher('cmd_vel', Twist)
00053
00054
00055
00056 test_type = "line"
00057 test_steer = "cw"
00058 length = 3
00059
00060 tw_straight = Twist()
00061 tw_straight.linear.x = 0.25
00062 tw_straight.linear.y = 0.0
00063 tw_straight.linear.z = 0.0
00064 tw_straight.angular.x = 0.0
00065 tw_straight.angular.y = 0.0
00066 tw_straight.angular.z = 0.0
00067
00068 tw_turn_left = Twist()
00069 tw_turn_left.linear.x = 0.0
00070 tw_turn_left.linear.y = 0.0
00071 tw_turn_left.linear.z = 0.0
00072 tw_turn_left.angular.x = 0.0
00073 tw_turn_left.angular.y = 0.0
00074 tw_turn_left.angular.z = 0.2
00075
00076 tw_turn_right = Twist()
00077 tw_turn_right.linear.x = 0.0
00078 tw_turn_right.linear.y = 0.0
00079 tw_turn_right.linear.z = 0.0
00080 tw_turn_right.angular.x = 0.0
00081 tw_turn_right.angular.y = 0.0
00082 tw_turn_right.angular.z =-tw_turn_left.angular.z
00083
00084 tw_zero = Twist()
00085 tw_zero.linear.x = 0.0
00086 tw_zero.linear.y = 0.0
00087 tw_zero.linear.z = 0.0
00088 tw_zero.angular.x = 0.0
00089 tw_zero.angular.y = 0.0
00090 tw_zero.angular.z = 0.0
00091
00092
00093 turns_done = int()
00094 girant = bool()
00095 tw = Twist()
00096 ref_rotation=[]
00097 old_x = float()
00098 old_y = float()
00099 distance = float()
00100
00101
00102
00103
00104
00105
00106 def callback_odom(data):
00107
00108 global girant, turns_done, tw, ref_rotation, old_x, old_y, distance
00109
00110
00111 q = [data.pose.pose.orientation.x, data.pose.pose.orientation.y, data.pose.pose.orientation.z, data.pose.pose.orientation.w]
00112 rotation = euler_from_quaternion(q)
00113
00114 if(old_x == 999999):
00115 old_x = data.pose.pose.position.x
00116 old_y = data.pose.pose.position.y
00117 rospy.loginfo("Starting Program \n pose x: %f y: %f \n rotation: z: %f",data.pose.pose.position.x,data.pose.pose.position.y,math.degrees(rotation[2]))
00118
00119 distance += math.sqrt((data.pose.pose.position.x-old_x)**2+(data.pose.pose.position.y-old_y)**2)
00120 old_x = data.pose.pose.position.x
00121 old_y = data.pose.pose.position.y
00122
00123 if(test_type=="line"):
00124 if(distance >= length):
00125 p_v.publish(tw_zero)
00126 rospy.loginfo("Program Finished \n distance: %f \n pose x: %f y: %f \n rotation: z: %f \n turns: %d",distance,data.pose.pose.position.x,data.pose.pose.position.y,math.degrees(rotation[2]),turns_done)
00127 rospy.signal_shutdown("PF")
00128
00129 elif(test_type=="square"):
00130 if(not girant and distance >= length*(turns_done+1) and turns_done < 4):
00131 rospy.loginfo("Translation > length. Turning %s\n distance: %f \n pose x: %f y: %f \n rotation: z: %f \n turns: %d",test_steer,distance,data.pose.pose.position.x,data.pose.pose.position.y,math.degrees(rotation[2]),turns_done)
00132 if(test_steer=="cw"):
00133 tw = tw_turn_right
00134 else:
00135 tw = tw_turn_left
00136 ref_rotation = rotation;
00137 girant = True
00138
00139 elif(girant and math.fabs(rotation[2]-ref_rotation[2]) >= math.pi/2 and turns_done < 4):
00140 rospy.loginfo("%s turn done. Going straight\n distance: %f \n pose x: %f y: %f \n rotation: z: %f \n turns: %d",test_steer,distance,data.pose.pose.position.x,data.pose.pose.position.y,math.degrees(rotation[2]),turns_done)
00141 tw = tw_straight
00142 turns_done+=1
00143 girant = False
00144
00145 if(turns_done>=4):
00146 tw = tw_zero
00147 print CY_
00148 rospy.loginfo("Program Finished")
00149 rospy.loginfo("%s turn done. Going straight\n distance: %f \n pose x: %f y: %f \n rotation: z: %f \n turns: %d",test_steer,distance,data.pose.pose.position.x,data.pose.pose.position.y,math.degrees(rotation[2]),turns_done)
00150 print _EC
00151 rospy.signal_shutdown("PF")
00152
00153 p_v.publish(tw)
00154
00155
00156
00157
00158
00159 def main(args):
00160
00161 if len(sys.argv) < 3:
00162 print " "
00163 print CR_+"Error: Please use at least 2 arguments as following:"+_EC
00164 print " "
00165 print CY_+"$ get_covariance.py"+SB_+" TYPE LENGTH (STEER)"+_EC
00166 print " |-line |- in [m] |-cw "
00167 print " |-square |-ccw "
00168 print " "
00169
00170 else:
00171 global test_type,length,test_steer
00172 test_type = str(sys.argv[1])
00173 if(test_type!="line" and test_type!="square"):
00174 print CR_+"Error: Please type 'line' or 'square'"+_EC
00175 sys.exit(1)
00176 length = float(sys.argv[2])
00177 if(length<=0 or length >=10):
00178 print CR_+"Error: Please type a length between 0 and 10"+_EC
00179 sys.exit(1)
00180 if(len(sys.argv)>3):
00181 test_steer = str(sys.argv[3])
00182 if(test_steer!="cw" and test_steer!="ccw"):
00183 print CR_+"Error: Please type 'cw' or 'ccw'"+_EC
00184 sys.exit(1)
00185
00186 rospy.init_node('validation')
00187
00188 print ""
00189 print CY_+"-------------------------------------------------------------------------------------------------"+_EC
00190 print SB_+" Odometry Validation Node"+_EC
00191 print CY_+"-------------------------------------------------------------------------------------------------"+_EC
00192 print ""
00193
00194 r = rospy.Rate(50)
00195
00196 global girant,turns_done,tw,ref_rotation, old_x, old_y, distance
00197 girant = False
00198 turns_done=0
00199 tw = tw_straight
00200 old_x = old_y = 999999
00201 distance = 0.0
00202
00203 print CB_
00204 rospy.loginfo("Test: %s", test_type)
00205 rospy.loginfo("Length: %f", length)
00206 rospy.loginfo("Turn: %s", test_steer)
00207 rospy.loginfo("vT: %f m/s", tw_straight.linear.x)
00208 rospy.loginfo("vR: %f rad/s", tw_turn_left.angular.z)
00209 print ""+_EC
00210
00211 raw_input('Press INTRO as soon as you are ready')
00212 print ""
00213 rospy.Subscriber('odom', Odometry, callback_odom)
00214 p_v.publish(tw_straight)
00215
00216 rospy.spin()
00217
00218
00219 if __name__ == '__main__':
00220 main(sys.argv)