ntrip_ros.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os
4 import sys
5 import json
6 import importlib
7 
8 import rospy
9 from std_msgs.msg import Header
10 from nmea_msgs.msg import Sentence
11 
12 from ntrip_client.ntrip_client import NTRIPClient
13 from ntrip_client.nmea_parser import NMEA_DEFAULT_MAX_LENGTH, NMEA_DEFAULT_MIN_LENGTH
14 
15 # Try to import a couple different types of RTCM messages
16 _MAVROS_MSGS_NAME = "mavros_msgs"
17 _RTCM_MSGS_NAME = "rtcm_msgs"
18 have_mavros_msgs = False
19 have_rtcm_msgs = False
20 if importlib.util.find_spec(_MAVROS_MSGS_NAME) is not None:
21  have_mavros_msgs = True
22  from mavros_msgs.msg import RTCM as mavros_msgs_RTCM
23 if importlib.util.find_spec(_RTCM_MSGS_NAME) is not None:
24  have_rtcm_msgs = True
25  from rtcm_msgs.msg import Message as rtcm_msgs_RTCM
26 
27 class NTRIPRos:
28  def __init__(self):
29  # Read a debug flag from the environment that should have been set by the launch file
30  try:
31  self._debug = json.loads(os.environ["NTRIP_CLIENT_DEBUG"].lower())
32  except:
33  self._debug = False
34 
35  # Init the node and read some mandatory config
36  if self._debug:
37  rospy.init_node('ntrip_client', anonymous=True, log_level=rospy.DEBUG)
38  else:
39  rospy.init_node('ntrip_client', anonymous=True)
40  host = rospy.get_param('~host', '127.0.0.1')
41  port = rospy.get_param('~port', '2101')
42  mountpoint = rospy.get_param('~mountpoint', 'mount')
43 
44  # Optionally get the ntrip version from the launch file
45  ntrip_version = rospy.get_param('~ntrip_version', None)
46  if ntrip_version == '':
47  ntrip_version = None
48 
49  # If we were asked to authenticate, read the username and password
50  username = None
51  password = None
52  if rospy.get_param('~authenticate', False):
53  username = rospy.get_param('~username', None)
54  password = rospy.get_param('~password', None)
55  if username is None:
56  rospy.logerr(
57  'Requested to authenticate, but param "username" was not set')
58  sys.exit(1)
59  if password is None:
60  rospy.logerr(
61  'Requested to authenticate, but param "password" was not set')
62  sys.exit(1)
63 
64  # Read an optional Frame ID from the config
65  self._rtcm_frame_id = rospy.get_param('~rtcm_frame_id', 'odom')
66 
67  # Determine the type of RTCM message that will be published
68  rtcm_message_package = rospy.get_param('~rtcm_message_package', _MAVROS_MSGS_NAME)
69  if rtcm_message_package == _MAVROS_MSGS_NAME:
70  if have_mavros_msgs:
71  self._rtcm_message_type = mavros_msgs_RTCM
73  else:
74  rospy.logfatal('The requested RTCM package {} is a valid option, but we were unable to import it. Please make sure you have it installed'.format(rtcm_message_package))
75  elif rtcm_message_package == _RTCM_MSGS_NAME:
76  if have_rtcm_msgs:
77  self._rtcm_message_type = rtcm_msgs_RTCM
79  else:
80  rospy.logfatal('The requested RTCM package {} is a valid option, but we were unable to import it. Please make sure you have it installed'.format(rtcm_message_package))
81  else:
82  rospy.logfatal('The RTCM package {} is not a valid option. Please choose between the following packages {}'.format(rtcm_message_package, str.join([_MAVROS_MSGS_NAME, _RTCM_MSGS_NAME])))
83 
84  # Setup the RTCM publisher
85  self._rtcm_timer = None
86  self._rtcm_pub = rospy.Publisher('rtcm', self._rtcm_message_type, queue_size=10)
87 
88  # Initialize the client
90  host=host,
91  port=port,
92  mountpoint=mountpoint,
93  ntrip_version=ntrip_version,
94  username=username,
95  password=password,
96  logerr=rospy.logerr,
97  logwarn=rospy.logwarn,
98  loginfo=rospy.loginfo,
99  logdebug=rospy.logdebug
100  )
101 
102  # Get some SSL parameters for the NTRIP client
103  self._client.ssl = rospy.get_param('~ssl', False)
104  self._client.cert = rospy.get_param('~cert', None)
105  self._client.key = rospy.get_param('~key', None)
106  self._client.ca_cert = rospy.get_param('~ca_cert', None)
107 
108  # Set parameters on the client
109  self._client.nmea_parser.nmea_max_length = rospy.get_param('~nmea_max_length', NMEA_DEFAULT_MAX_LENGTH)
110  self._client.nmea_parser.nmea_min_length = rospy.get_param('~nmea_min_length', NMEA_DEFAULT_MIN_LENGTH)
111  self._client.reconnect_attempt_max = rospy.get_param('~reconnect_attempt_max', NTRIPClient.DEFAULT_RECONNECT_ATTEMPT_MAX)
112  self._client.reconnect_attempt_wait_seconds = rospy.get_param('~reconnect_attempt_wait_seconds', NTRIPClient.DEFAULT_RECONNECT_ATEMPT_WAIT_SECONDS)
113  self._client.rtcm_timeout_seconds = rospy.get_param('~rtcm_timeout_seconds', NTRIPClient.DEFAULT_RTCM_TIMEOUT_SECONDS)
114 
115  def run(self):
116  # Setup a shutdown hook
117  rospy.on_shutdown(self.stop)
118 
119  # Connect the client
120  if not self._client.connect():
121  rospy.logerr('Unable to connect to NTRIP server')
122  return 1
123 
124  # Setup our subscriber
125  self._nmea_sub = rospy.Subscriber('nmea', Sentence, self.subscribe_nmea, queue_size=10)
126 
127  # Start the timer that will check for RTCM data
128  self._rtcm_timer = rospy.Timer(rospy.Duration(0.1), self.publish_rtcm)
129 
130  # Spin until we are shutdown
131  rospy.spin()
132  return 0
133 
134  def stop(self):
135  rospy.loginfo('Stopping RTCM publisher')
136  if self._rtcm_timer:
137  self._rtcm_timer.shutdown()
138  self._rtcm_timer.join()
139  rospy.loginfo('Disconnecting NTRIP client')
140  self._client.shutdown()
141 
142  def subscribe_nmea(self, nmea):
143  # Just extract the NMEA from the message, and send it right to the server
144  self._client.send_nmea(nmea.sentence)
145 
146  def publish_rtcm(self, event):
147  for raw_rtcm in self._client.recv_rtcm():
148  self._rtcm_pub.publish(self._create_rtcm_message(raw_rtcm))
149 
151  return mavros_msgs_RTCM(
152  header=Header(
153  stamp=rospy.Time.now(),
154  frame_id=self._rtcm_frame_id
155  ),
156  data=rtcm
157  )
158 
160  return rtcm_msgs_RTCM(
161  header=Header(
162  stamp=rospy.Time.now(),
163  frame_id=self._rtcm_frame_id
164  ),
165  message=rtcm
166  )
167 
168 
169 if __name__ == '__main__':
170  ntrip_ros = NTRIPRos()
171  sys.exit(ntrip_ros.run())
def publish_rtcm(self, event)
Definition: ntrip_ros.py:146
def _create_rtcm_msgs_rtcm_message(self, rtcm)
Definition: ntrip_ros.py:159
def __init__(self)
Definition: ntrip_ros.py:28
def stop(self)
Definition: ntrip_ros.py:134
def _create_mavros_msgs_rtcm_message(self, rtcm)
Definition: ntrip_ros.py:150
def subscribe_nmea(self, nmea)
Definition: ntrip_ros.py:142


ntrip_client
Author(s): Parker Hannifin Corp
autogenerated on Thu Aug 18 2022 02:39:24