Skip to content

Commit c7fdf35

Browse files
Integration with updated planning code
1 parent 09ff281 commit c7fdf35

3 files changed

Lines changed: 339 additions & 78 deletions

File tree

2.64 MB
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from __future__ import print_function
2+
3+
# Python Headers
4+
import os
5+
import cv2
6+
import numpy as np
7+
8+
import rospy
9+
from cv_bridge import CvBridge, CvBridgeError
10+
from sensor_msgs.msg import Image
11+
12+
import cv2
13+
import numpy as np
14+
15+
image_path = os.path.join(os.path.dirname(__file__), "highbay_image.pgm")
16+
17+
class OccupancyGrid2:
18+
19+
global image_path
20+
21+
def __init__(self):
22+
23+
# Read image in BGR format
24+
self.map_image = cv2.imread(image_path)
25+
26+
# Create the cv_bridge object
27+
self.bridge = CvBridge()
28+
self.map_image_pub = rospy.Publisher("/motion_image", Image, queue_size=1)
29+
30+
# Subscribe information from sensors
31+
self.lat = 0
32+
self.lon = 0
33+
self.heading = 0
34+
35+
self.lat_start_bt = 40.092722 # 40.09269
36+
self.lon_start_l = -88.236365 # -88.23628
37+
self.lat_scale = 0.00062 # 0.0007
38+
self.lon_scale = 0.00136 # 0.00131
39+
40+
self.arrow = 40
41+
self.img_width = 2107
42+
self.img_height = 1313
43+
44+
def image_to_gnss(self, pix_x, pix_y):
45+
"""
46+
Given image coordinates (pix_x, pix_y), return (latitude, longitude).
47+
Inverse of your:
48+
pix_x = img_width * (lon - lon_start_l) / lon_scale
49+
pix_y = img_height - img_height*(lat - lat_start_bt) / lat_scale
50+
"""
51+
# longitude:
52+
lon = self.lon_start_l + (pix_x / float(self.img_width)) * self.lon_scale
53+
lat = self.lat_start_bt + ((self.img_height - pix_y) / float(self.img_height)) * self.lat_scale
54+
55+
return lat, lon
56+
57+
def image_heading(self, lon_x, lat_y, heading):
58+
59+
if(heading >=0 and heading < 90):
60+
angle = np.radians(90-heading)
61+
lon_xd = lon_x + int(self.arrow * np.cos(angle))
62+
lat_yd = lat_y - int(self.arrow * np.sin(angle))
63+
64+
elif(heading >= 90 and heading < 180):
65+
angle = np.radians(heading-90)
66+
lon_xd = lon_x + int(self.arrow * np.cos(angle))
67+
lat_yd = lat_y + int(self.arrow * np.sin(angle))
68+
69+
elif(heading >= 180 and heading < 270):
70+
angle = np.radians(270-heading)
71+
lon_xd = lon_x - int(self.arrow * np.cos(angle))
72+
lat_yd = lat_y + int(self.arrow * np.sin(angle))
73+
74+
else:
75+
angle = np.radians(heading-270)
76+
lon_xd = lon_x - int(self.arrow * np.cos(angle))
77+
lat_yd = lat_y - int(self.arrow * np.sin(angle))
78+
79+
return lon_xd, lat_yd
80+
81+
82+
def gnss_to_image_with_heading(self, lon, lat, heading):
83+
84+
lon_x = int(self.img_width*(lon - self.lon_start_l)/self.lon_scale)
85+
lat_y = int(self.img_height-self.img_height*(lat - self.lat_start_bt)/self.lat_scale)
86+
lon_xd, lat_yd = self.image_heading(lon_x, lat_y, heading)
87+
88+
pub_image = np.copy(self.map_image)
89+
90+
if(lon_x >= 0 and lon_x <= self.img_width and lon_xd >= 0 and lon_xd <= self.img_width and
91+
lat_y >= 0 and lat_y <= self.img_height and lat_yd >= 0 and lat_yd <= self.img_height):
92+
cv2.arrowedLine(pub_image, (lon_x, lat_y), (lon_xd, lat_yd), (0, 0, 255), 2)
93+
cv2.circle(pub_image, (lon_x, lat_y), 12, (0,0,255), 2)
94+
95+
## Debug check if you can convert back to GNSS
96+
# tmp_lat, tmp_lon = self.image_to_gnss(lon_x, lat_y)
97+
# print(f'Lat: {self.lat}, Lon: {self.lon}, Converted Lat: {tmp_lat}, Converted Lon: {tmp_lon}')
98+
## End Debug check
99+
try:
100+
# Convert OpenCV image to ROS image and publish
101+
self.map_image_pub.publish(self.bridge.cv2_to_imgmsg(pub_image, "bgr8"))
102+
except CvBridgeError as e:
103+
rospy.logerr("CvBridge Error: {0}".format(e))
104+
105+
def gnss_to_image(self, lon, lat):
106+
107+
lon_x = int(self.img_width - self.img_width*(lon - self.lon_start_l) /self.lon_scale)
108+
lat_y = int(self.img_height - self.img_height*(lat - self.lat_start_bt) * -1/self.lat_scale)
109+
print(f"GNSS ({lat}, {lon}) → pixel ({lon_x}, {lat_y})")
110+
pub_image = np.copy(self.map_image)
111+
112+
if(lon_x >= 0 and lon_x <= self.img_width and
113+
lat_y >= 0 and lat_y <= self.img_height):
114+
cv2.circle(pub_image, (lon_x, lat_y), 12, (0,0,255), 2)
115+
116+
117+
try:
118+
# Convert OpenCV image to ROS image and publish
119+
self.map_image_pub.publish(self.bridge.cv2_to_imgmsg(pub_image, "bgr8"))
120+
except CvBridgeError as e:
121+
rospy.logerr("CvBridge Error: {0}".format(e))
122+
123+
def gnss_to_image_coords(self, lon, lat):
124+
125+
lon_x = int(self.img_width - self.img_width*(lon - self.lon_start_l) /self.lon_scale)
126+
lat_y = int(self.img_height - self.img_height*(lat - self.lat_start_bt) * -1/self.lat_scale)
127+
print(f"GNSS ({lat}, {lon}) → pixel ({lon_x}, {lat_y})")
128+
return lon_x, lat_y
129+

0 commit comments

Comments
 (0)