Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import rospy
  4.  
  5. import cv2
  6. import numpy as np
  7. import math
  8.  
  9. from sensor_msgs.msg import LaserScan
  10. from mavros_msgs.msg import OverrideRCIn
  11.  
  12.  
  13. def callback(data):
  14.     pub = rospy.Publisher('/mavros/rc/override', OverrideRCIn, queue_size=10)
  15.     frame = np.zeros((500, 500, 3), np.uint8)
  16.  
  17.     pitch = 1500
  18.     yaw = 1500
  19.     max_yaw = 1601
  20.     max_pitch = 1445
  21.  
  22.     min_yaw = 1345
  23.     min_pitch = 1455
  24.  
  25.     angle = data.angle_max
  26.     Vx = 250
  27.     Vy = 250
  28.     for r in data.ranges:
  29.         if r == float('Inf'):
  30.             r = data.range_max
  31.         x = math.trunc((r * 10) * math.cos(angle + (-90 * 3.1416 / 180)))
  32.         y = math.trunc((r * 10) * math.sin(angle + (-90 * 3.1416 / 180)))
  33.         cv2.line(frame, (250, 250), (x + 250, y + 250), (255, 0, 0), 1)
  34.         Vx += x
  35.         Vy += y
  36.         angle = angle - data.angle_increment
  37.  
  38.     cv2.line(frame, (250, 250), (250 + Vx, 250 + Vy), (0, 0, 255), 3)
  39.     cv2.circle(frame, (250, 250), 2, (255, 255, 0))
  40.     ang = -(math.atan2(Vx, Vy) - 3.1416) * 180 / 3.1416
  41.     if ang > 180:
  42.         ang -= 360
  43.     cv2.putText(frame, str(ang)[:10], (50, 400), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255))
  44.  
  45.     cv2.imshow('frame', frame)
  46.     cv2.waitKey(1)
  47.  
  48.     # calculate YAW with data from OpenCV
  49.     if ang > 0:
  50.         yaw = 1500 + ang * 40 / 6 + 80
  51.         print ""
  52.         print "POSITIVE ang " + str(ang)
  53.         print "  yaw " + str(ang * 40 / 6)
  54.         print "  pos " + str(yaw)
  55.         print ""
  56.         if yaw > max_yaw:
  57.             yaw = max_yaw
  58.     else:
  59.         yaw = 1500 + ang * 40 / 6 - 110
  60.         print ""
  61.         print "NEGATIVE ang " + str(ang)
  62.         print "  yaw " + str(ang * 40 / 6)
  63.         print "  pos " + str(yaw)
  64.         print ""
  65.         if yaw < min_yaw:
  66.             yaw = min_yaw
  67.  
  68.     # stabelizen
  69.     if round(yaw) == 1600:
  70.         yaw = 1500
  71.  
  72.     if 1580 > round(yaw) > 1450:
  73.         pitch = max_pitch
  74.  
  75.     if round(yaw) == max_yaw or round(yaw) == min_yaw:
  76.         pitch = 1500
  77.  
  78.     throttle = 1500
  79.  
  80.     msg = OverrideRCIn()
  81.     msg.channels[0] = 1500  # x > 1500 = rechts, x < 1500 = links
  82.     msg.channels[1] = pitch  # x > 1500 = achter, x < 1500 = naar voren (MODE LOITER NODIG)
  83.     msg.channels[2] = throttle  # x > 1500 = stijgen, x < 1500 = dalen
  84.     msg.channels[3] = round(yaw)
  85.     msg.channels[4] = 1000
  86.     msg.channels[5] = 1000
  87.     msg.channels[6] = 1000
  88.     msg.channels[7] = 1000
  89.     print msg
  90.     pub.publish(msg)
  91.  
  92.  
  93. def laser_listener():
  94.     rospy.init_node('laser_listener', anonymous=True)
  95.  
  96.     rospy.Subscriber("/scan", LaserScan, callback)
  97.     rospy.spin()
  98.  
  99. if __name__ == '__main__':
  100.     laser_listener()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement