Example #1
0
        def mavlink_thread_out():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while self._alive:
                    try:
                        msg = self.out_queue.get(True, timeout=0.01)
                        self.master.write(msg)
                    except Empty:
                        continue
                    except socket.error as error:
                        # If connection reset (closed), stop polling.
                        if error.errno == ECONNABORTED:
                            raise APIException('Connection aborting during read')
                        raise
                    except Exception as e:
                        errprinter('>>> mav send error:', e)
                        break
            except APIException as e:
                errprinter('>>> ' + str(e.message))
                self._alive = False
                self.master.close()
                self._death_error = e

            except Exception as e:
                # http://bugs.python.org/issue1856
                if not self._alive:
                    pass
                else:
                    self._alive = False
                    self.master.close()
                    self._death_error = e

            # Explicitly clear out buffer so .close closes.
            self.out_queue = Queue()
Example #2
0
        def mavlink_thread_out():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while self._alive:
                    try:
                        msg = self.out_queue.get(True, timeout=0.01)
                        self.master.write(msg)
                    except Empty:
                        continue
                    except socket.error as error:
                        # If connection reset (closed), stop polling.
                        if error.errno == ECONNABORTED:
                            raise APIException('Connection aborting during read')
                        raise
                    except Exception as e:
                        errprinter('>>> mav send error:', e)
                        break
            except APIException as e:
                errprinter('>>> ' + str(e.message))
                self._alive = False
                self.master.close()
                self._death_error = e

            except Exception as e:
                # http://bugs.python.org/issue1856
                if not self._alive:
                    pass
                else:
                    self._alive = False
                    self.master.close()
                    self._death_error = e

            # Explicitly clear out buffer so .close closes.
            self.out_queue = Queue()
Example #3
0
 def notify_message_listeners(self, msg):
     for fn in self._message_listeners:
         try:
             fn(self, msg)
         except Exception as e:
             errprinter('>>> Exception in message handler for %s' %
                        msg.get_type())
             errprinter('>>> ' + str(e))
Example #4
0
        def mavlink_thread_in():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while self._alive:
                    # Downtime
                    time.sleep(0.05)

                    # Loop listeners.
                    for fn in self.loop_listeners:
                        self.send_heartbeat()
                        fn(self)

                    while self._accept_input:
                        if not self._alive:
                            break
                        try:
                            msg = self.master.recv_msg()
#                            print("msg=%s" % (str(msg)))
                        except socket.error as error:
                            # If connection reset (closed), stop polling.
                            if error.errno == ECONNABORTED:
                                raise APIException('Connection aborting during send')
                            raise
                        except Exception as e:
                            # TODO this should be more rigorous. How to avoid
                            #   invalid MAVLink prefix '73'
                            #   invalid MAVLink prefix '13'
                            errprinter('mav recv error:', e)
                            msg = None
                        if not msg:
                            break

                        # Message listeners.
                        for fn in self.message_listeners:
                            try:
                                fn(self, msg)
                            except Exception as e:
                                errprinter('>>> Exception in message handler for %s' %
                                           msg.get_type())
                                errprinter('>>> ' + str(e))

            except APIException as e:
                errprinter('>>> APIException (' + str(e.message) + ")")
                self._alive = False
                self.master.close()
                self._death_error = e
                return

            except Exception as e:
                # http://bugs.python.org/issue1856
                if not self._alive:
                    pass
                else:
                    self._alive = False
                    self.master.close()
                    self._death_error = e
                    errprinter('>>> Exception (' + str(e.message) + ")")
                    traceback.print_exc()
Example #5
0
 def callback(_, msg):
     try:
         target.out_queue.put(msg.pack(target.master.mav))
     except:
         try:
             assert len(msg.get_msgbuf()) > 0
             target.out_queue.put(msg.get_msgbuf())
         except:
             errprinter('>>> Could not pack this object on receive: %s' % type(msg))
Example #6
0
 def callback(_, msg):
     try:
         target.out_queue.put(msg.pack(target.master.mav))
     except:
         try:
             assert len(msg.get_msgbuf()) > 0
             target.out_queue.put(msg.get_msgbuf())
         except:
             errprinter('>>> Could not pack this object on receive: %s' % type(msg))
Example #7
0
 def callback(_, msg):
     msg = copy.copy(msg)
     try:
         self.out_queue.put(msg.pack(self.master.mav))
     except:
         try:
             assert len(msg.get_msgbuf()) > 0
             self.out_queue.put(msg.get_msgbuf())
         except:
             errprinter('>>> Could not pack this object on forward: %s' % type(msg))
Example #8
0
 def callback(_, msg):
     msg = copy.copy(msg)
     target.fix_targets(msg)
     try:
         self.out_queue.put(msg.pack(self.master.mav))
     except:
         try:
             assert len(msg.get_msgbuf()) > 0
             self.out_queue.put(msg.get_msgbuf())
         except:
             errprinter('>>> Could not pack this object on forward: %s' % type(msg))
Example #9
0
        def mavlink_thread_in():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while self._alive:
                    # Loop listeners.
                    for fn in self.loop_listeners:
                        fn(self)

                    # Sleep
                    self.master.select(0.05)

                    while self._accept_input:
                        try:
                            msg = self.master.recv_msg()
                        except socket.error as error:
                            # If connection reset (closed), stop polling.
                            if error.errno == ECONNABORTED:
                                raise APIException(
                                    'Connection aborting during send')
                            raise
                        except mavutil.mavlink.MAVError as e:
                            # Avoid
                            #   invalid MAVLink prefix '73'
                            #   invalid MAVLink prefix '13'
                            # errprinter('mav recv error:', e)
                            msg = None
                        except Exception as e:
                            # Log any other unexpected exception
                            errprinter(
                                '>>> Exception while receiving message: ', e)
                            msg = None
                        if not msg:
                            break

                        # Message listeners.
                        for fn in self.message_listeners:
                            try:
                                fn(self, msg)
                            except Exception as e:
                                errprinter(
                                    '>>> Exception in message handler for %s' %
                                    msg.get_type())
                                errprinter('>>> ' + str(e))

            except APIException as e:
                errprinter('>>> ' + str(e))
                self._alive = False
                self.master.close()
                self._death_error = e
                return

            except Exception as e:
                # http://bugs.python.org/issue1856
                if not self._alive:
                    pass
                else:
                    self._alive = False
                    self.master.close()
                    self._death_error = e
Example #10
0
        def mavlink_thread_in():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while True:
                    # Downtime
                    time.sleep(0.05)

                    # Loop listeners.
                    for fn in self.loop_listeners:
                        fn(self)

                    while self._accept_input:
                        try:
                            msg = self.master.recv_msg()
                        except socket.error as error:
                            # If connection reset (closed), stop polling.
                            if error.errno == ECONNABORTED:
                                raise APIException('Connection aborting during send')
                            raise
                        except Exception as e:
                            # TODO this should be more rigorous. How to avoid
                            #   invalid MAVLink prefix '73'
                            #   invalid MAVLink prefix '13'
                            # errprinter('mav recv error:', e)
                            msg = None
                        if not msg:
                            break

                        # Message listeners.
                        for fn in self.message_listeners:
                            try:
                                fn(self, msg)
                            except Exception as e:
                                errprinter('>>> Exception in message handler for %s' %
                                           msg.get_type())
                                errprinter('>>> ' + str(e))

            except APIException as e:
                errprinter('>>> ' + str(e.message))
                self._alive = False
                self.master.close()
                self._death_error = e
                return

            except Exception as e:
                # http://bugs.python.org/issue1856
                if not self._alive:
                    pass
                else:
                    self._alive = False
                    self.master.close()
                    self._death_error = e
Example #11
0
 def read(self):
     errprinter('writer should not have had a read request')
     os._exit(43)
Example #12
0
        def mavlink_thread():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while True:
                    # Downtime
                    time.sleep(0.05)

                    # Loop listeners.
                    for fn in self.loop_listeners:
                        fn(self)

                    while True:
                        try:
                            msg = self.out_queue.get_nowait()
                            self.fix_targets(msg)
                            self.master.write(msg)
                        except socket.error as error:
                            if error.errno == ECONNABORTED:
                                errprinter(
                                    'reestablishing connection after read timeout'
                                )
                                if hasattr(self.master, 'reset'):
                                    self.master.reset()
                                else:
                                    try:
                                        self.master.close()
                                    except:
                                        pass
                                    self.master = mavutil.mavlink_connection(
                                        self.master.address)
                                continue

                            # If connection reset (closed), stop polling.
                            return
                        except Empty:
                            break
                        except Exception as e:
                            errprinter('mav send error:', e)
                            break

                    while True:
                        try:
                            msg = self.master.recv_msg()
                        except socket.error as error:
                            if error.errno == ECONNABORTED:
                                errprinter(
                                    'reestablishing connection after send timeout'
                                )
                                if hasattr(self.master, 'reset'):
                                    self.master.reset()
                                else:
                                    try:
                                        self.master.close()
                                    except:
                                        pass
                                    self.master = mavutil.mavlink_connection(
                                        self.master.address)
                                continue

                            # If connection reset (closed), stop polling.
                            return
                        except Exception as e:
                            # TODO this should be more rigorous. How to avoid
                            #   invalid MAVLink prefix '73'
                            #   invalid MAVLink prefix '13'
                            # errprinter('mav recv error:', e)
                            msg = None
                        if not msg:
                            break

                        # Message listeners.
                        for fn in self.message_listeners:
                            fn(self, msg)

            except Exception as e:
                # http://bugs.python.org/issue1856
                if self.exiting:
                    pass
                else:
                    raise
Example #13
0
 def read(self):
     errprinter('writer should not have had a read request')
     os._exit(43)
        def mavlink_thread():
            # Huge try catch in case we see http://bugs.python.org/issue1856
            try:
                while True:
                    # Downtime                    
                    time.sleep(0.05)

                    # Loop listeners.
                    for fn in self.loop_listeners:
                        fn(self)

                    while True:
                        try:
                            msg = self.out_queue.get_nowait()
                            self.fix_targets(msg)
                            self.master.write(msg)
                        except socket.error as error:
                            if error.errno == ECONNABORTED:
                                errprinter('reestablishing connection after read timeout')
                                if hasattr(self.master, 'reset'):
                                    self.master.reset()
                                else:
                                    try:
                                        self.master.close()
                                    except:
                                        pass
                                    self.master = mavutil.mavlink_connection(self.master.address)
                                continue

                            # If connection reset (closed), stop polling.
                            return
                        except Empty:
                            break
                        except Exception as e:
                            errprinter('mav send error:', e)
                            break

                    while True:
                        try:
                            msg = self.master.recv_msg()
                        except socket.error as error:
                            if error.errno == ECONNABORTED:
                                errprinter('reestablishing connection after send timeout')
                                if hasattr(self.master, 'reset'):
                                    self.master.reset()
                                else:
                                    try:
                                        self.master.close()
                                    except:
                                        pass
                                    self.master = mavutil.mavlink_connection(self.master.address)
                                continue

                            # If connection reset (closed), stop polling.
                            return
                        except Exception as e:
                            # TODO this should be more rigorous. How to avoid
                            #   invalid MAVLink prefix '73'
                            #   invalid MAVLink prefix '13'
                            # errprinter('mav recv error:', e)
                            msg = None
                        if not msg:
                            break

                        # Message listeners.
                        for fn in self.message_listeners:
                            fn(self, msg)

            except Exception as e:
                # http://bugs.python.org/issue1856
                if self.exiting:
                    pass
                else:
                    raise