1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| #!/usr/bin/env python #coding: utf-8 import rospy from geometry_msgs.msg import Point, Twist import threading import actionlib import serial import time from actionlib_msgs.msg import GoalStatus from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal from geometry_msgs.msg import PoseWithCovarianceStamped from tf_conversions import transformations from ar_track_alvar_msgs.msg import AlvarMarkers from math import pi import subprocess class Navigation: def __init__(self): self.set_pose_pub = rospy.Publisher('/initialpose', PoseWithCovarianceStamped, queue_size=5) self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction) self.move_base.wait_for_server(rospy.Duration(60)) def set_pose(self, p): if self.move_base is None: return False x, y, th = p pose = PoseWithCovarianceStamped() pose.header.stamp = rospy.Time.now() pose.header.frame_id = 'map' pose.pose.pose.position.x = x pose.pose.pose.position.y = y q = transformations.quaternion_from_euler(0.0, 0.0, th/180.0*pi) pose.pose.pose.orientation.x = q[0] pose.pose.pose.orientation.y = q[1] pose.pose.pose.orientation.z = q[2] pose.pose.pose.orientation.w = q[3] self.set_pose_pub.publish(pose) return True def _feedback_cb(self, feedback): msg = feedback #rospy.loginfo("[Navi] navigation feedback\r\n%s"%feedback) def goto(self, p): rospy.loginfo("[Navigation] goto %s"%p) goal = MoveBaseGoal() goal.target_pose.header.frame_id = 'map' goal.target_pose.header.stamp = rospy.Time.now() goal.target_pose.pose.position.x = p[0] goal.target_pose.pose.position.y = p[1] q = transformations.quaternion_from_euler(0.0, 0.0, p[2]/180.0*pi) goal.target_pose.pose.orientation.x = q[0] goal.target_pose.pose.orientation.y = q[1] goal.target_pose.pose.orientation.z = q[2] goal.target_pose.pose.orientation.w = q[3] self.move_base.send_goal(goal, self._done_cb, self._active_cb, self._feedback_cb) result = self.move_base.wait_for_result(rospy.Duration(60)) if not result: self.move_base.cancel_goal() rospy.loginfo("Timed out achieving goal") else: state = self.move_base.get_state() if state == GoalStatus.SUCCEEDED: rospy.loginfo("reach goal %s succeeded!"%p) return True def _done_cb(self, status, result): rospy.loginfo("navigation done! status:%d result:%s"%(status, result)) def _active_cb(self): rospy.loginfo("[Navi] navigation has be actived") def cancel(self): self.move_base.cancel_all_goals() return True class ARTracker: def __init__(self): self.ar_sub = rospy.Subscriber('/ar_pose_marker', AlvarMarkers, self.ar_cb) def ar_cb(self,data): global target_id global ar_x global ar_y global ar_z global ar_id for marker in data.markers: if marker.id == target_id : ar_x = marker.pose.pose.position.x ar_y = marker.pose.pose.position.y ar_z = marker.pose.pose.position.z ar_id = marker.id #print('AR Marker Position (x,y,z):', ar_x, ar_y,ar_z) break class Object_position: def __init__(self): self.ar_sub = rospy.Subscriber('/object_position', Point, self.find_cb) def find_cb(self,data): global find_id global find_x global find_y point_msg = data if(point_msg.z>=1 and point_msg.z<=5): find_id = 1 find_x=point_msg.x find_y=point_msg.y else: find_id = 0 def process(): rospy.spin() find_id = 0 find_x=0.0 find_y=0.0 target_id = 0 ar_id = 0 ar_x =0.0 ar_y =0.0 ar_z =0.0 if __name__ == '__main__': rospy.init_node('navigation_demo',anonymous=True) ser = serial.Serial(port="/dev/shoot", baudrate=9600, parity="N", bytesize=8, stopbits=1) pub = rospy.Publisher("/cmd_vel", Twist, queue_size=1000) goals = [ [1.1 , -0.37,0.0], [1.1 , -1.45,.0], [1.0 , -2.72,.0], [0.07 , -2.72,.0] ] object_position = Object_position() ar_acker = ARTracker() executor_thread = threading.Thread(target=process).start() navi = Navigation() time.sleep(15) # ------first-------------------------------------------------------- navi.goto(goals[0]) start=time.time() is_shoot=0 while True: if find_id == 1: flog0 = find_x - 320 flog1 = abs(flog0) print(flog0) if abs(flog1) >10: msg = Twist() msg.angular.z = -0.01 * flog0 pub.publish(msg) print(msg.angular.z) elif abs(flog1) <= 10: print('1 is shoot') ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.2) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') time.sleep(2) is_shoot=1 break end=time.time() if end-start>12: break if is_shoot==0: ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.2) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') time.sleep(2) #subprocess.run( ['rosnode','kill','find_object_2d'] ) # ------sencond----------------------------------------------------- navi.goto(goals[1]) target_id=1 Yaw_th = 0.003 start=time.time() is_shoot=0 while True: if ar_id == target_id: ar_x_abs = abs(ar_x) print('x:',ar_x) print('z:',ar_z) if ar_x_abs >= Yaw_th: msg = Twist() msg.angular.z = -1.5 * ar_x pub.publish(msg) elif ar_x_abs < Yaw_th and (ar_z >= 1.57 and ar_z <=1.64): print('2 is shoot') ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.1) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') is_shoot=1 break end=time.time() if end-start>20: break if is_shoot==0: ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.2) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') time.sleep(2) # # --------------------third---------------------------------- navi.goto(goals[2]) target_id=2 # ------------------------------------------------------ Yaw_th = 0.002 start=time.time() is_shoot=0 while True: if ar_id == target_id: ar_x_abs = abs(ar_x) print(ar_x) if ar_x_abs >= Yaw_th: msg = Twist() msg.angular.z = -1.5 * ar_x pub.publish(msg) elif ar_x_abs < Yaw_th: print('3 is shoot') ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.1) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') is_shoot=1 break end=time.time() if end-start>12: break if is_shoot==0: ser.write(b'\x55\x01\x12\x00\x00\x00\x01\x69') time.sleep(0.2) ser.write(b'\x55\x01\x11\x00\x00\x00\x01\x68') time.sleep(2) # # ------------------------------------------------------------------------- navi.goto(goals[3]) #slowly #rclpy.shutdown()
|