-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathdriver.py
387 lines (331 loc) · 16.6 KB
/
driver.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# Software License Agreement (BSD License)
#
# Copyright (c) 2013, Eric Perko
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the names of the authors nor the names of their
# affiliated organizations may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import math
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import NavSatFix, NavSatStatus, TimeReference, Imu
from geometry_msgs.msg import TwistStamped, QuaternionStamped
from ublox_msgs.msg import NavPVT
from autoware_sensing_msgs.msg import GnssInsOrientationStamped
from tf_transformations import quaternion_from_euler
from libnmea_navsat_driver.checksum_utils import check_nmea_checksum
from libnmea_navsat_driver import parser
import math
import numpy as np
from std_msgs.msg import Float32
def eulerFromQuaternion(x, y, z, w):
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + y * y)
roll_x = math.atan2(t0, t1)
t2 = +2.0 * (w * y - z * x)
t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
pitch_y = math.asin(t2)
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (y * y + z * z)
yaw_z = math.atan2(t3, t4)
return roll_x, pitch_y, yaw_z
def get_quaternion_from_euler(roll, pitch, yaw):
"""
Convert an Euler angle to a quaternion.
Input
:param roll: The roll (rotation around x-axis) angle in radians.
:param pitch: The pitch (rotation around y-axis) angle in radians.
:param yaw: The yaw (rotation around z-axis) angle in radians.
Output
:return qx, qy, qz, qw: The orientation in quaternion [x,y,z,w] format
"""
qx = np.sin(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) - np.cos(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
qy = np.cos(roll/2) * np.sin(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.cos(pitch/2) * np.sin(yaw/2)
qz = np.cos(roll/2) * np.cos(pitch/2) * np.sin(yaw/2) - np.sin(roll/2) * np.sin(pitch/2) * np.cos(yaw/2)
qw = np.cos(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
return [qx, qy, qz, qw]
class Ros2NMEADriver(Node):
def __init__(self):
super().__init__('nmea_navsat_driver')
self.fix_pub = self.create_publisher(NavSatFix, 'fix', 10)
self.vel_pub = self.create_publisher(TwistStamped, 'vel', 10)
self.ublox_navpvt_pub = self.create_publisher(NavPVT, "navpvt", 10)
self.imu_pub = self.create_publisher(Imu, 'chc/imu', 10)
self.pub_pitch = self.create_publisher(Float32, 'chc/pitch', 2)
self.pub_heading = self.create_publisher(Float32, 'chc/heading', 2)
self.pub_orientation = self.create_publisher(GnssInsOrientationStamped, '/autoware_orientation', 2)
self.data = []
self.pitch_msg = Float32()
self.heading_msg = Float32()
self.imu_msg = Imu()
self.orientation_msg = GnssInsOrientationStamped()
self.heading_pub = self.create_publisher(QuaternionStamped, 'heading', 10)
self.time_ref_pub = self.create_publisher(TimeReference, 'time_reference', 10)
self.time_ref_source = self.declare_parameter('time_ref_source', 'gps').value
self.use_RMC = self.declare_parameter('useRMC', False).value
self.valid_fix = False
# epe = estimated position error
self.default_epe_quality0 = self.declare_parameter('epe_quality0', 1000000).value
self.default_epe_quality1 = self.declare_parameter('epe_quality1', 4.0).value
self.default_epe_quality2 = self.declare_parameter('epe_quality2', 0.1).value
self.default_epe_quality4 = self.declare_parameter('epe_quality4', 0.02).value
self.default_epe_quality5 = self.declare_parameter('epe_quality5', 4.0).value
self.default_epe_quality9 = self.declare_parameter('epe_quality9', 3.0).value
self.using_receiver_epe = False
self.lon_std_dev = float("nan")
self.lat_std_dev = float("nan")
self.alt_std_dev = float("nan")
"""Format for this dictionary is the fix type from a GGA message as the key, with
each entry containing a tuple consisting of a default estimated
position error, a NavSatStatus value, and a NavSatFix covariance value."""
self.gps_qualities = {
# Unknown
-1: [
self.default_epe_quality0,
NavSatStatus.STATUS_NO_FIX,
NavSatFix.COVARIANCE_TYPE_UNKNOWN
],
# Invalid
0: [
self.default_epe_quality0,
NavSatStatus.STATUS_NO_FIX,
NavSatFix.COVARIANCE_TYPE_UNKNOWN
],
# SPS
1: [
self.default_epe_quality1,
NavSatStatus.STATUS_FIX,
NavSatFix.COVARIANCE_TYPE_APPROXIMATED
],
# DGPS
2: [
self.default_epe_quality2,
NavSatStatus.STATUS_SBAS_FIX,
NavSatFix.COVARIANCE_TYPE_APPROXIMATED
],
# RTK Fix
4: [
self.default_epe_quality4,
NavSatStatus.STATUS_GBAS_FIX,
NavSatFix.COVARIANCE_TYPE_APPROXIMATED
],
# RTK Float
5: [
self.default_epe_quality5,
NavSatStatus.STATUS_GBAS_FIX,
NavSatFix.COVARIANCE_TYPE_APPROXIMATED
],
# WAAS
9: [
self.default_epe_quality9,
NavSatStatus.STATUS_GBAS_FIX,
NavSatFix.COVARIANCE_TYPE_APPROXIMATED
]
}
# Returns True if we successfully did something with the passed in
# nmea_string
def add_sentence(self, nmea_string, frame_id, timestamp=None):
if not check_nmea_checksum(nmea_string):
self.get_logger().warn("Received a sentence with an invalid checksum. " +
"Sentence was: %s" % nmea_string)
return False
parsed_sentence = parser.parse_nmea_sentence(nmea_string)
if not parsed_sentence:
self.get_logger().debug("Failed to parse NMEA sentence. Sentence was: %s" % nmea_string)
return False
if timestamp:
current_time = timestamp
else:
current_time = self.get_clock().now().to_msg()
current_fix = NavSatFix()
current_fix.header.stamp = current_time
current_fix.header.frame_id = frame_id
current_time_ref = TimeReference()
current_time_ref.header.stamp = current_time
current_time_ref.header.frame_id = frame_id
if self.time_ref_source:
current_time_ref.source = self.time_ref_source
else:
current_time_ref.source = frame_id
if not self.use_RMC and 'GGA' in parsed_sentence:
current_fix.position_covariance_type = NavSatFix.COVARIANCE_TYPE_APPROXIMATED
data = parsed_sentence['GGA']
fix_type = data['fix_type']
if not (fix_type in self.gps_qualities):
fix_type = -1
gps_qual = self.gps_qualities[fix_type]
default_epe = gps_qual[0]
current_fix.status.status = gps_qual[1]
current_fix.position_covariance_type = gps_qual[2]
if current_fix.status.status > 0:
self.valid_fix = True
else:
self.valid_fix = False
current_fix.status.service = NavSatStatus.SERVICE_GPS
latitude = data['latitude']
if data['latitude_direction'] == 'S':
latitude = -latitude
current_fix.latitude = latitude
longitude = data['longitude']
if data['longitude_direction'] == 'W':
longitude = -longitude
current_fix.longitude = longitude
# Altitude is above ellipsoid, so adjust for mean-sea-level
altitude = data['altitude'] + data['mean_sea_level']
current_fix.altitude = altitude
# use default epe std_dev unless we've received a GST sentence with epes
if not self.using_receiver_epe or math.isnan(self.lon_std_dev):
self.lon_std_dev = default_epe
if not self.using_receiver_epe or math.isnan(self.lat_std_dev):
self.lat_std_dev = default_epe
if not self.using_receiver_epe or math.isnan(self.alt_std_dev):
self.alt_std_dev = default_epe * 2
hdop = data['hdop']
current_fix.position_covariance[0] = (hdop * self.lon_std_dev) ** 2
current_fix.position_covariance[4] = (hdop * self.lat_std_dev) ** 2
current_fix.position_covariance[8] = (2 * hdop * self.alt_std_dev) ** 2 # FIXME
self.fix_pub.publish(current_fix)
if not math.isnan(data['utc_time']):
current_time_ref.time_ref = rclpy.time.Time(seconds=data['utc_time']).to_msg()
self.last_valid_fix_time = current_time_ref
self.time_ref_pub.publish(current_time_ref)
elif not self.use_RMC and 'VTG' in parsed_sentence:
data = parsed_sentence['VTG']
# Only report VTG data when you've received a valid GGA fix as well.
if self.valid_fix:
current_vel = TwistStamped()
current_vel.header.stamp = current_time
current_vel.header.frame_id = frame_id
current_vel.twist.linear.x = data['speed'] * math.sin(data['true_course'])
current_vel.twist.linear.y = data['speed'] * math.cos(data['true_course'])
self.vel_pub.publish(current_vel)
elif 'RMC' in parsed_sentence:
data = parsed_sentence['RMC']
# Only publish a fix from RMC if the use_RMC flag is set.
if self.use_RMC:
if data['fix_valid']:
current_fix.status.status = NavSatStatus.STATUS_FIX
else:
current_fix.status.status = NavSatStatus.STATUS_NO_FIX
current_fix.status.service = NavSatStatus.SERVICE_GPS
latitude = data['latitude']
if data['latitude_direction'] == 'S':
latitude = -latitude
current_fix.latitude = latitude
longitude = data['longitude']
if data['longitude_direction'] == 'W':
longitude = -longitude
current_fix.longitude = longitude
current_fix.altitude = float('NaN')
current_fix.position_covariance_type = \
NavSatFix.COVARIANCE_TYPE_UNKNOWN
self.fix_pub.publish(current_fix)
if not math.isnan(data['utc_time']):
current_time_ref.time_ref = rclpy.time.Time(seconds=data['utc_time']).to_msg()
self.time_ref_pub.publish(current_time_ref)
# Publish velocity from RMC regardless, since GGA doesn't provide it.
if data['fix_valid']:
current_vel = TwistStamped()
current_vel.header.stamp = current_time
current_vel.header.frame_id = frame_id
current_vel.twist.linear.x = data['speed'] * math.sin(data['true_course'])
current_vel.twist.linear.y = data['speed'] * math.cos(data['true_course'])
self.vel_pub.publish(current_vel)
elif 'GST' in parsed_sentence:
data = parsed_sentence['GST']
# Use receiver-provided error estimate if available
self.using_receiver_epe = True
self.lon_std_dev = data['lon_std_dev']
self.lat_std_dev = data['lat_std_dev']
self.alt_std_dev = data['alt_std_dev']
elif 'HDT' in parsed_sentence:
data = parsed_sentence['HDT']
if data['heading']:
current_heading = QuaternionStamped()
current_heading.header.stamp = current_time
current_heading.header.frame_id = frame_id
q = quaternion_from_euler(0, 0, math.radians(data['heading']))
current_heading.quaternion.x = q[0]
current_heading.quaternion.y = q[1]
current_heading.quaternion.z = q[2]
current_heading.quaternion.w = q[3]
self.heading_pub.publish(current_heading)
elif 'CHC' in parsed_sentence:
data = parsed_sentence['CHC']
if data['fix_valid']==4:
navpvt_msg = NavPVT()
navpvt_msg.heading = int(math.degrees(data['heading'])*100000)
self.ublox_navpvt_pub.publish(navpvt_msg)
try:
self.pitch_msg.data = data["pitch"]
self.data.append(data["pitch"])
self.pub_pitch.publish(self.pitch_msg)
self.heading_msg.data = data['heading']
self.pub_heading.publish(self.heading_msg)
self.imu_msg.header.stamp = self.get_clock().now().to_msg()
self.imu_msg.header.frame_id = "gnss"
# orientation
heading = math.radians(90.0-data['heading'])
pitch = math.radians(data['pitch'])
roll = math.radians(data['roll'])
[qx, qy, qz, qw] = get_quaternion_from_euler(roll, pitch, heading)
self.imu_msg.orientation.x = qx
self.imu_msg.orientation.y = qy
self.imu_msg.orientation.z = qz
self.imu_msg.orientation.w = qw
# angular velocity the coordinate of imu is y-front x-right z-up,
# so it has to be converted to right-handed coordinate
self.imu_msg.angular_velocity.x = math.radians(data["angular_velocity_y"])
self.imu_msg.angular_velocity.y = math.radians(-data["angular_velocity_x"])
self.imu_msg.angular_velocity.z = math.radians(data["angular_velocity_z"])
self.imu_msg.angular_velocity_covariance[0] = 0.001
self.imu_msg.angular_velocity_covariance[4] = 0.001
self.imu_msg.angular_velocity_covariance[8] = 0.001
self.imu_msg.linear_acceleration.x = data["linear_acceleration_y"] * 9.80665
self.imu_msg.linear_acceleration.y = -data["linear_acceleration_x"]* 9.80665
self.imu_msg.linear_acceleration.z = data["linear_acceleration_z"]* 9.80665
self.imu_pub.publish(self.imu_msg)
# orientation msg of autoware
self.orientation_msg.header = self.imu_msg.header
self.orientation_msg.orientation.orientation = self.imu_msg.orientation
self.orientation_msg.orientation.rmse_rotation_x = 0.001745329 # 0.1 degree
self.orientation_msg.orientation.rmse_rotation_y = 0.001745329 # 0.1 degree
self.orientation_msg.orientation.rmse_rotation_z = 0.001745329 # 0.1 degree
self.pub_orientation.publish(self.orientation_msg)
except UnicodeDecodeError as err:
self.get_logger().warn("UnicodeDecodeError: {0}".format(err))
else:
return False
return True
"""Helper method for getting the frame_id with the correct TF prefix"""
def get_frame_id(self):
frame_id = self.declare_parameter('frame_id', 'gps').value
prefix = self.declare_parameter('tf_prefix', '').value
if len(prefix):
return '%s/%s' % (prefix, frame_id)
return frame_id