Guest User

Untitled

a guest
Apr 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. import rospy
  3. import thread
  4. import sys
  5. import math
  6. from mavros_msgs.msg import RCIn
  7. from clever import srv
  8. from time import sleep
  9. from mavros_msgs.srv import SetMode
  10. from mavros_msgs.srv import CommandBool
  11.  
  12. states = ('start','stop','unknown')
  13. state = states[2]
  14.  
  15. rospy.init_node('Clever3_RC_Script')
  16.  
  17. navigate = rospy.ServiceProxy('/navigate', srv.Navigate)
  18. set_mode = rospy.ServiceProxy('/mavros/set_mode', SetMode)
  19. get_telemetry = rospy.ServiceProxy('/get_telemetry', srv.GetTelemetry)
  20. arming = rospy.ServiceProxy('/mavros/cmd/arming', CommandBool)
  21.  
  22. def get_distance(x1, y1, z1, x2, y2, z2):
  23. return math.sqrt((x1-x2)**2 + (y1-y2)**2 +(z1-z2)**2)
  24.  
  25. def takeoff (zp, sp = 1, tolerance = 0.2):
  26. start = get_telemetry()
  27. print navigate(z=zp, speed=sp, frame_id='fcu_horiz', auto_arm=True)
  28. while True:
  29. telem = get_telemetry()
  30. delta = abs(abs(telem.z - start.z)-zp)
  31. if delta < tolerance:
  32. break
  33. rospy.sleep(0.2)
  34.  
  35. def land(sp = 1, land_height = -1, tolerance = 0.25):
  36. print 'land!'
  37. z0 = get_telemetry(frame_id='local_origin').z
  38. print z0
  39. h = get_telemetry(frame_id='aruco_map').z
  40. print h
  41. print navigate(z=-h+land_height, speed=sp, frame_id='fcu_horiz')
  42. while True:
  43. z = get_telemetry(frame_id='local_origin').z
  44. delta = z0-z-h
  45. print delta
  46. if (abs(delta) < tolerance):
  47. print get_telemetry(frame_id='local_origin')
  48. arming(False)
  49. break
  50. rospy.sleep(0.2)
  51.  
  52. def flight_to_point(xp, yp, zp, sp = 1, breakable = True, tolerance = 0.2, constant_yaw = True):
  53. frame_id = 'aruco_map'
  54. if constant_yaw:
  55. current_yaw = get_telemetry(frame_id = 'aruco_map').yaw
  56. print navigate(frame_id=frame_id, x=xp, y=yp, z=zp, speed=sp, yaw = current_yaw)
  57. else:
  58. print navigate(frame_id=frame_id, x=xp, y=yp, z=zp, speed=sp)
  59. while True:
  60. if breakable and state == 'stop':
  61. return
  62. telem = get_telemetry(frame_id=frame_id)
  63. if get_distance(xp, yp, zp, telem.x, telem.y, telem.z) < tolerance:
  64. break
  65. rospy.sleep(0.2)
  66.  
  67. # copter parameters
  68.  
  69. speed = 1
  70. z = 1
  71.  
  72. # rectangle parameters
  73.  
  74. width = 1
  75. height = 1
  76. x0 = 0.1
  77. y0 = 0.6
  78.  
  79. # flight program
  80.  
  81. def flight_program (param):
  82. while True:
  83.  
  84. global state
  85. print 'waiting for stop!'
  86. while state != 'stop':
  87. rospy.sleep(0.1)
  88. print 'waiting for start...'
  89. while state == 'stop':
  90. rospy.sleep(0.1)
  91. print 'start!'
  92.  
  93. takeoff(z) #takeoff
  94. flight_to_point(x0, y0, z, speed)
  95. while True:
  96. flight_to_point(x0, y0 + height, z, speed)
  97. flight_to_point(x0 + width, y0 + height, z, speed)
  98. flight_to_point(x0 + width, y0, z, speed)
  99. flight_to_point(x0, y0, z, speed, breakable = False)
  100. if state == 'stop':
  101. break
  102. land()
  103.  
  104. # Вызывается при обновлении данных из топика
  105. def callback(data):
  106. global state
  107. # Обрабатываем данные с 6 канала пульта
  108. if data.channels[5] < 1100:
  109. state = states[1]
  110. elif data.channels[5] > 1900:
  111. state = states[0]
  112. else:
  113. state = states[2]
  114.  
  115. def listener():
  116.  
  117. # In ROS, nodes are uniquely named. If two nodes with the same
  118. # name are launched, the previous one is kicked off. The
  119. # anonymous=True flag means that rospy will choose a unique
  120. # name for our 'listener' node so that multiple listeners can
  121. # run simultaneously.
  122.  
  123. rospy.Subscriber('mavros/rc/in', RCIn, callback)
  124.  
  125. # spin() simply keeps python from exiting until this node is stopped
  126.  
  127. rospy.spin()
  128.  
  129. param = []
  130. thread.start_new_thread(flight_program, (param,))
  131. listener()
Add Comment
Please, Sign In to add comment