00001 #! /usr/bin/env python 00002 # -*- coding: utf-8 -*- 00003 00004 # Software License Agreement (BSD) 00005 # 00006 # file @data.py 00007 # authors Mike Purvis <mpurvis@clearpathrobotics.com> 00008 # NovAtel <novatel.com/support> 00009 # copyright Copyright (c) 2012, Clearpath Robotics, Inc., All rights reserved. 00010 # Copyright (c) 2014, NovAtel Inc., All rights reserved. 00011 # 00012 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00013 # the following conditions are met: 00014 # * Redistributions of source code must retain the above copyright notice, this list of conditions and the 00015 # following disclaimer. 00016 # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 00017 # following disclaimer in the documentation and/or other materials provided with the distribution. 00018 # * Neither the name of Clearpath Robotics nor the names of its contributors may be used to endorse or promote 00019 # products derived from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WAR- 00022 # RANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00023 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN- 00024 # DIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00025 # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 00029 import rospy 00030 import novatel_msgs.msg 00031 00032 from port import Port 00033 from novatel_span_driver.mapping import msgs 00034 from handlers import MessageHandler 00035 import translator 00036 00037 from cStringIO import StringIO 00038 from threading import Lock 00039 00040 00041 class DataPort(Port): 00042 def run(self): 00043 # Set up handlers for translating different novatel messages as they arrive. 00044 handlers = {} 00045 pkt_counters = {} 00046 pkt_times = {} 00047 00048 for msg_id in msgs.keys(): 00049 handlers[msg_id] = MessageHandler(*msgs[msg_id]) 00050 pkt_counters[msg_id] = 0 00051 pkt_times[msg_id] = 0 00052 00053 bad_pkts = set() 00054 pkt_id = None 00055 00056 while not self.finish.is_set(): 00057 try: 00058 header, pkt_str = self.recv() 00059 if header is not None: 00060 handlers[header.id].handle(StringIO(pkt_str), header) 00061 00062 except ValueError as e: 00063 # Some problem in the recv() routine. 00064 rospy.logwarn(str(e)) 00065 continue 00066 00067 except KeyError as e: 00068 if header.id not in handlers and header.id not in pkt_counters: 00069 rospy.logwarn("No handler for message id %d" % header.id) 00070 00071 except translator.TranslatorError: 00072 if header.id not in bad_pkts: 00073 rospy.logwarn("Error parsing %s.%d" % header.id) 00074 bad_pkts.add(pkt) 00075 00076 if header.id not in pkt_counters: 00077 pkt_counters[header.id] = 0 00078 else: 00079 pkt_counters[header.id] += 1 00080 pkt_times[header.id] = header.gps_week_seconds # only track times of msgs that are part of novatel msgs