Guest User

Untitled

a guest
Mar 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import cv2
  3. import geodesy.props
  4. import geodesy.utm
  5. import geodesy.wu_point
  6. import rospy
  7. import numpy as np
  8. import itertools
  9. import socket
  10. import sys
  11. import pickle
  12. from decimal import *
  13. import rospy
  14. import sys, select, termios, tty
  15. from std_msgs.msg import String
  16. from nav_msgs.msg import Odometry
  17. from sensor_msgs.msg import NavSatFix
  18. from sensor_msgs.msg import Imu
  19. import math
  20. import os
  21. import rospkg
  22. from pyquaternion import Quaternion
  23. from std_msgs.msg import String
  24.  
  25. class MapViz(object):
  26. def __init__(self):
  27. rospy.loginfo("Map Visualization started...")
  28.  
  29. #Initialise topics for ros messages
  30. imu_topic = "imu_new"
  31. gps_topic = "gps_new"
  32. odom_topic = "camera/odom"
  33. gps_yaw_topic = "gps_yaw"
  34.  
  35. self.initial_lat = 28.5306000
  36. self.initial_long = 77.1618000
  37. #redundant variables end.
  38. self.initial_odom = None
  39. self.offset_x = None
  40. self.offset_y = None
  41. self.vo_orientation = None
  42. self.p_initial = geodesy.utm.fromLatLong(self.initial_lat,self.initial_long).toPoint()
  43.  
  44. #give the path to map file.
  45. rp = rospkg.RosPack()
  46. self.map_file_path = os.path.join(rp.get_path('odom_visualizer'), 'resources', 'map_file.jpg')
  47. #load it as an opencv image
  48. self.map_img = cv2.imread(self.map_file_path,cv2.IMREAD_COLOR)
  49. self.first_msg_received = False
  50. self.first_x = None
  51. self.first_y = None
  52.  
  53. self.last_odom_x = None
  54. self.last_odom_y = None
  55.  
  56. self.last_odom_x2 = None
  57. self.last_odom_y2 = None
  58. self.last_gps = None
  59. self.gps_yaw = None
  60. #rospy.loginfo("imuReceived assigned successfully during initialise...")
  61. #opencv window declaration.
  62. self.cv_window = cv2.namedWindow("MapDisplay",0)
  63.  
  64. #Subsribing to topics
  65. self.vo_odom_sub1 = rospy.Subscriber('/'+odom_topic, Odometry, self.odom_cb, queue_size=1)
  66. #self.vo_odom_sub2 = rospy.Subscriber('/odom_topic2', Odometry, self.odom_cb2, queue_size=1)
  67. self.gps_sub = rospy.Subscriber('/'+gps_topic, NavSatFix, self.gps_cb, queue_size=1)
  68. self.gps_yaw_sub = rospy.Subscriber('/'+ gps_yaw_topic, Imu , self.gps_yaw_cb, queue_size=1)
  69.  
  70. #Getting initial pose
  71.  
  72. rospy.loginfo("Waiting for IMU message for initial pose")
  73. message = rospy.wait_for_message("/"+imu_topic, Imu)
  74. imu_data = message
  75. print imu_data
  76. '''
  77. self.splitStrings= str(message.data).split(",")
  78. self.splitStrings2= str(message.data).split(",")
  79. '''
  80. imu_x= imu_data.orientation.x
  81. imu_y=imu_data.orientation.y
  82. imu_z=imu_data.orientation.z
  83. imu_w=imu_data.orientation.w
  84. self.initial_pose = Quaternion(imu_x,imu_y,imu_z,imu_w).inverse
  85. rospy.loginfo("IMU data received")
  86.  
  87. #Waiting for GPS signalsm
  88. rospy.loginfo("Waiting for GPS signals")
  89. while self.last_gps == None:
  90. rospy.sleep(0.020)
  91. p = geodesy.utm.fromLatLong(self.last_gps.latitude,self.last_gps.longitude).toPoint()
  92. self.offset_x = p.x-self.p_initial.x
  93. self.offset_y = p.y-self.p_initial.y
  94. def odom_cb(self, data):
  95. #print "in if block"
  96. self.last_odom_x = data.pose.pose.position.x
  97. self.last_odom_y = data.pose.pose.position.y
  98. self.vo_orientation = Quaternion(data.pose.pose.orientation.x, data.pose.pose.orientation.y, data.pose.pose.orientation.z, data.pose.pose.orientation.w ).inverse
  99. #self.first_x = data.pose.pose.position.x
  100. #self.first_y = data.pose.pose.position.y
  101. '''
  102. def odom_cb2(self, data):
  103. #print "in if block"
  104. self.last_odom_x2 = data.pose.pose.position.x
  105. self.last_odom_y2 = data.pose.pose.position.y
  106.  
  107. '''
  108.  
  109. def gps_cb(self, data):
  110. self.last_gps = data
  111.  
  112. def gps_yaw_cb(self, data):
  113. self.gps_yaw = Quaternion(data.orientation.x, data.orientation.y, data.orientation.z, data.orientation.w)
  114.  
  115.  
  116. #for all the computations.
  117. def do_work(self):
  118. p = geodesy.utm.fromLatLong(self.last_gps.latitude,self.last_gps.longitude).toPoint()
  119. cv2.circle(self.map_img,(int(p.x-self.p_initial.x),int(p.y-self.p_initial.y)),1,(0,255,0),-1)
  120. rospy.loginfo("Drawing gps on map...")
  121.  
  122.  
  123. #zx=p.x-self.p_initial.x
  124. #zy=p.y-self.p_initial.y
  125. #rospy.logwarn([self.last_odom_x, self.last_odom_y, 0])
  126. corrected_coordinates = self.initial_pose.rotate([self.last_odom_x, self.last_odom_y, 0])
  127.  
  128. if self.offset_x +self.last_odom_x >=0 and self.offset_x+ self.last_odom_x < 7164 and self.offset_y+self.last_odom_y>=0 and self.offset_y+self.last_odom_y< 3358:
  129. cv2.circle(self.map_img,(int(self.offset_x+corrected_coordinates[0]),int(self.offset_y+corrected_coordinates[1])),1,(0,0,255),-1)
  130. #cv2.circle(self.map_img,(int(self.offset_x+self.last_odom_x2),int(self.offset_y+self.last_odom_y2)),1,(0,0,255),-1)
  131.  
  132.  
  133.  
  134. if self.gps_yaw != None:
  135. temp = self.vo_orientation.rotate([self.last_odom_x, self.last_odom_y, 0])
  136. corrected_coordinates_gps = self.gps_yaw.rotate(temp)
  137. cv2.circle(self.map_img,(int(self.offset_x+corrected_coordinates_gps[0]),int(self.offset_y+corrected_coordinates_gps[1])),1,(255,0,0),-1)
  138.  
  139. cv2.imshow("MapDisplay", self.map_img)
  140. cv2.waitKey(1)
  141. #print int(zx+self.last_odom_x) - self.first_x
  142. #print int(zy+self.last_odom_y) - self.first_y
  143.  
  144. def run(self):
  145. r = rospy.Rate(1)
  146. while not rospy.is_shutdown():
  147. self.do_work()
  148. r.sleep()
  149.  
  150. if __name__ =='__main__':
  151. rospy.init_node('map_visualization')
  152. viz = MapViz()
  153. viz.run()
Add Comment
Please, Sign In to add comment