Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1.   # coding: utf8
  2. import cv2
  3. import rospy
  4. import numpy as np
  5. from clever import srv
  6. from pyzbar import pyzbar
  7. from cv_bridge import CvBridge
  8. from sensor_msgs.msg import Image
  9. from clever.srv import SetLEDEffect
  10. from std_srvs.srv import Trigger
  11. bridge = CvBridge()
  12.  
  13. def findcol(lower_color, upper_color, color_name):
  14.     # Take each frame
  15.   cap = rospy.wait_for_message('main_camera/image_raw', Image)
  16.   img = bridge.imgmsg_to_cv2(cap, 'bgr8')
  17.  
  18.        # Convert BGR to HSV
  19.   hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  20.  
  21.   mask = cv2.inRange(hsv, lower_color, upper_color)
  22.   res = cv2.bitwise_and(hsv,hsv,mask = mask)
  23.    
  24.  
  25.   result = ""  
  26.  
  27.   if res.any() != False:
  28.     result = color_name
  29.  
  30.   return result
  31.  
  32.  
  33. def allcolor():
  34.   if findcol(hsv_mingreen,hsv_maxgreen, '-') == '-':
  35.     people.append('-')
  36.   elif findcol(lower_red,upper_red, '+') ==  '+':
  37.     people.append("+")
  38.     set_effect(r=128, g=0, b=128)  # пометка зараженного
  39.   elif findcol(lower_yellow,upper_yellow, '?') ==   '?':
  40.     people.append('?')
  41.     set_effect(r=128, g=0, b=128)  # пометка зараженного
  42.  
  43.  
  44.  
  45. rospy.init_node('flight')
  46.  
  47. get_telemetry = rospy.ServiceProxy('get_telemetry', srv.GetTelemetry)
  48. navigate = rospy.ServiceProxy('navigate', srv.Navigate)
  49. navigate_global = rospy.ServiceProxy('navigate_global', srv.NavigateGlobal)
  50. set_position = rospy.ServiceProxy('set_position', srv.SetPosition)
  51. set_velocity = rospy.ServiceProxy('set_velocity', srv.SetVelocity)
  52. set_attitude = rospy.ServiceProxy('set_attitude', srv.SetAttitude)
  53. set_rates = rospy.ServiceProxy('set_rates', srv.SetRates)
  54. land = rospy.ServiceProxy('land', Trigger)
  55. cap = rospy.wait_for_message('main_camera/image_raw', Image)
  56. hsv_mingreen = np.array((53, 0, 0), np.uint8)
  57. hsv_maxgreen = np.array((83, 255, 255), np.uint8)
  58. lower_red = np.array([0,70,50])
  59. upper_red = np.array([10,255,255])
  60. lower_yellow = np.array([20,100,100])
  61. upper_yellow = np.array([30,255,255])
  62. people = ["", "", "", "", "", "", "", "", ""]
  63. result = ""
  64. ans = ""
  65. # Первый взлет
  66. # Взлет
  67. navigate(x=0, y=0, z=0.6, frame_id='body', speed=0.5, auto_arm=True)
  68.  
  69.        
  70.  
  71.        
  72.        
  73. rospy.sleep(10)
  74.  
  75. # 1
  76. navigate(x=0.295, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  77. allcolor()
  78. rospy.sleep(5)
  79.  
  80. # 2
  81. navigate(x=0.885, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  82. allcolor()
  83. rospy.sleep(5)
  84.  
  85. # 3
  86. navigate(x=0.295, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  87. allcolor()
  88. rospy.sleep(5)
  89.  
  90. # 4
  91. navigate(x=0.885, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  92. allcolor()
  93. rospy.sleep(5)
  94.  
  95. # 5
  96. navigate(x=0.295, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  97. allcolor()
  98. rospy.sleep(5)
  99.  
  100. # 6
  101. navigate(x=0.885, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  102. allcolor()
  103. rospy.sleep(5)
  104.  
  105. # 7
  106. navigate(x=0.295, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  107. allcolor()
  108. rospy.sleep(5)
  109.  
  110. # 8
  111. navigate(x=0.885, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  112. allcolor()
  113. rospy.sleep(5)
  114.  
  115. # 9
  116. navigate(x=0.59, y=2.655, z=0.6, speed=0.8, frame_id='aruco_map')
  117. allcolor()
  118. rospy.sleep(5)
  119.  
  120. # возвращение домой
  121. navigate(x=0, y=0, z=0, speed=0.8, frame_id='aruco_map')
  122. rospy.sleep(5)
  123.  
  124. # Посадка
  125. land()
  126. print(people)
  127.  
  128. rospy.sleep(120)  # ожидание на базе
  129.  
  130. bad_num = []
  131. bad_value = []
  132.  
  133. for x in range(len(people)):
  134.     if people[x] == "?" or people[x] == "+":
  135.         bad_num.append(x)
  136.  
  137. rospy.init_node('barcode_test')
  138.  
  139. bridge = CvBridge()
  140.  
  141.  
  142. # Image subscriber callback function
  143. def image_callback(data):
  144.     cv_image = bridge.imgmsg_to_cv2(data, 'bgr8')  # OpenCV image
  145.     barcodes = pyzbar.decode(cv_image)
  146.     for barcode in barcodes:
  147.         b_data = barcode.data.encode("utf-8")
  148.         b_type = barcode.type
  149.         (x, y, w, h) = barcode.rect
  150.         xc = x + w / 2
  151.         yc = y + h / 2
  152.         cv2.putText(cv_image, "QR-код!", (xc, yc), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  153.         print("Был найден {} со значением {} с центром в точках x={}, y={}".format(b_type, b_data, xc, yc))
  154.         if b_data == "COVID - 2019":
  155.             set_effect(r=255, g=0, b=0)  # пометка зараженного
  156.             print("Заражен")
  157.             rospy.sleep(5)
  158.         bad_value.append(b_data)
  159.  
  160.  
  161. image_sub = rospy.Subscriber('main_camera/image_raw', Image, image_callback, queue_size=1)
  162.  
  163. rospy.spin()
  164.  
  165. # Второй взлет
  166. # Взлет
  167. navigate(x=0, y=0, z=0.6, frame_id='body', speed=0.5, auto_arm=True)
  168.  
  169. # Ожидание 10 секунд
  170. rospy.sleep(10)
  171.  
  172. # 1
  173. navigate(x=0.295, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  174. rospy.sleep(5)
  175.  
  176. # 2
  177. navigate(x=0.885, y=0.295, z=0.6, speed=0.8, frame_id='aruco_map')
  178. rospy.sleep(5)
  179.  
  180. # 3
  181. navigate(x=0.295, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  182. rospy.sleep(5)
  183.  
  184. # 4
  185. navigate(x=0.885, y=0.885, z=0.6, speed=0.8, frame_id='aruco_map')
  186. rospy.sleep(5)
  187.  
  188. # 5
  189. navigate(x=0.295, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  190. rospy.sleep(5)
  191.  
  192. # 6
  193. navigate(x=0.885, y=1.475, z=0.6, speed=0.8, frame_id='aruco_map')
  194. rospy.sleep(5)
  195.  
  196. # 7
  197. navigate(x=0.295, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  198. rospy.sleep(5)
  199.  
  200. # 8
  201. navigate(x=0.885, y=2.065, z=0.6, speed=0.8, frame_id='aruco_map')
  202. rospy.sleep(5)
  203.  
  204. # 9
  205. navigate(x=0.59, y=2.655, z=0.6, speed=0.8, frame_id='aruco_map')
  206. rospy.sleep(5)
  207.  
  208. # возвращение домой
  209. navigate(x=0, y=0, z=0, speed=0.8, frame_id='aruco_map')
  210. rospy.sleep(5)
  211.  
  212. # Посадка
  213. land()
  214.  
  215. print(bad_num)
  216. print(bad_value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement