-
Notifications
You must be signed in to change notification settings - Fork 1
/
ros_amrl_visualization.py
executable file
·146 lines (135 loc) · 4.79 KB
/
ros_amrl_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
from realtime_subscriber.Realtime_subscriber_api import BCTWSConnection
import threading
import sys
import math
import rospy, roslib
roslib.load_manifest('amrl_msgs')
import rospkg
from amrl_msgs.msg import VisualizationMsg
from amrl_msgs.msg import ColoredLine2D
from amrl_msgs.msg import Point2D
stream = None
def ResetVisualizationMsg(msg):
msg.header.seq += 1
msg.header.stamp = rospy.Time.now()
msg.lines = []
# Return a list of ColorLine2D that represents a box centered at (centerX,
# centerY), with length and width, and rotation angle, and color color
def DrawBox(centerX, centerY, length, width, angle, color):
# Create a box centered at the point centerX, centerY, rotation angle,
# with width width and length length
# The box is defined by 4 points p1, p2, p3, p4 in counter-clockwise order.
p1 = Point2D()
p1.x = centerX + length/2 * math.cos(angle) - width/2 * math.sin(angle)
p1.y = centerY + length/2 * math.sin(angle) + width/2 * math.cos(angle)
p2 = Point2D()
p2.x = centerX + length/2 * math.cos(angle) + width/2 * math.sin(angle)
p2.y = centerY + length/2 * math.sin(angle) - width/2 * math.cos(angle)
p3 = Point2D()
p3.x = centerX - length/2 * math.cos(angle) + width/2 * math.sin(angle)
p3.y = centerY - length/2 * math.sin(angle) - width/2 * math.cos(angle)
p4 = Point2D()
p4.x = centerX - length/2 * math.cos(angle) - width/2 * math.sin(angle)
p4.y = centerY - length/2 * math.sin(angle) + width/2 * math.cos(angle)
line1 = ColoredLine2D()
line1.p0 = p1
line1.p1 = p2
line1.color = color
line2 = ColoredLine2D()
line2.p0 = p2
line2.p1 = p3
line2.color = color
line3 = ColoredLine2D()
line3.p0 = p3
line3.p1 = p4
line3.color = color
line4 = ColoredLine2D()
line4.p0 = p4
line4.p1 = p1
line4.color = color
return [line1, line2, line3, line4]
if __name__ == '__main__':
rospy.init_node('bluecity_example', anonymous=False)
# Check to see if a file named ".credentials" exists in the current directory,
# and if so, use it to log in
try:
with open(".credentials") as f:
username = f.readline().strip()
password = f.readline().strip()
except IOError:
# If the file doesn't exist, use the command line arguments
# Accept username as the first argument and password as the second argument
if len(sys.argv) != 3:
print("Usage: python example.py <username> <password>")
sys.exit(1)
username = sys.argv[1]
password = sys.argv[2]
print("Opening Blucity stream...")
stream = BCTWSConnection(
"BCT_3D_4G_0206001",
username,
password,
singleton=False,
subscriptions = [
BCTWSConnection.subscriptionOption.LOOP_CHANGE,
BCTWSConnection.subscriptionOption.PHASE_CHANGE,
BCTWSConnection.subscriptionOption.FRAME])
print("Stream opened")
msg = VisualizationMsg()
msg.header.frame_id = "map"
msg.header.seq = 0
msg.header.stamp = rospy.Time.now()
msg.ns = "bluecity_example"
sensorLoc = Point2D()
sensorLoc.x = 86
sensorLoc.y = -120
sensorAngle = math.radians(5)
# Create a visualization publisher
pub = rospy.Publisher('visualization', VisualizationMsg, queue_size=10)
while rospy.is_shutdown() == False:
# print("Getting frame...")
data = stream.get_frame()
# print("Frame: ")
# print(len(data.objects))
ResetVisualizationMsg(msg)
# Draw a box and a cross at the sensor location.
msg.lines.extend(DrawBox(sensorLoc.x, sensorLoc.y, 0.5, 0.5, 0, 0x000000))
msg.lines.append(ColoredLine2D(
Point2D(sensorLoc.x - 0.5, sensorLoc.y),
Point2D(sensorLoc.x + 0.5, sensorLoc.y),
0x000000))
msg.lines.append(ColoredLine2D(
Point2D(sensorLoc.x, sensorLoc.y - 0.5),
Point2D(sensorLoc.x, sensorLoc.y + 0.5),
0x000000))
for obj in data.objects:
print(obj)
obj.rotation = obj.rotation + sensorAngle
obj.centerX = obj.centerX + sensorLoc.x
obj.centerY = -obj.centerY + sensorLoc.y
if obj.classType == "10":
# pedestrian, blue
color = 0x0000FF
elif obj.classType == "2":
# car, red
color = 0xFF0000
elif obj.classType == "3":
# van, purple
color = 0x800080
elif obj.classType == "4":
# truck, orange
color = 0xFFA500
elif obj.classType == "5":
# bus, yellow
color = 0xFFFF00
elif obj.classType == "13":
# bicycle, green
color = 0x00A000
else:
# unknown, black
print("Unknown class type: " + str(obj.classType))
color = 0x000000
# msg.lines.extend(DrawBox(px, py, obj.length, obj.width, obj.rotation, color))
msg.lines.extend(DrawBox(obj.centerX, obj.centerY, obj.length, obj.width, obj.rotation, color))
pub.publish(msg)