Exemplo n.º 1
0
  def run(self):
    try:
      self.main.set_systemstate(self)

      self.log_filename = spooky.find_next_log_filename(self.main.config.get_my("full-state-logs-prefix"))
      print "Opening logfile: %s" % self.log_filename 
      with open(self.log_filename, 'wb') as f:
        self.f = f

        with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as state_udp_out:
          state_udp_out.setblocking(1)
          state_udp_out.settimeout(0.05)

          print "Module %s sending data on %s" % (self, str(self.state_destinations))
    
          self.ready()

          send_state = spooky.DoPeriodically(lambda: self.send_state_as_json(state_udp_out), self._state_transmit_period)
          
          while not self.wait_on_stop(self._service_queue_period):
            while not self._inputQueue.empty():
              self._handlePartialUpdate(self._inputQueue.get_nowait())
            send_state.tick()

    except SystemExit:
      print "Exit Forced. We're dead."
    except:
      traceback.print_exc()
    finally:
      self.main.unset_systemstate()
Exemplo n.º 2
0
    def run(self):
        try:

            CheckIntegrity = spooky.DoPeriodically(self.check_vehicle, 1.0)

            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_DGRAM)) as camapi_udp:
                camapi_udp.setblocking(1)
                camapi_udp.settimeout(0.02)  # 50Hz
                camapi_udp.bind((self.bind_ip, self.bind_port))

                print "Module %s listening on %s:%s" % (self, self.bind_ip,
                                                        self.bind_port)

                self.ready()

                while not self.stopped():

                    # Exposing a Camera API
                    try:
                        camapi_data, camapi_addr = camapi_udp.recvfrom(4096)
                        self.handle_camapi(camapi_data, camapi_addr)
                    except (socket.error, socket.timeout) as e:
                        pass

                    CheckIntegrity.tick()

        except SystemExit:
            pass
        except:
            traceback.print_exc()
        finally:
            self.disconnect()
Exemplo n.º 3
0
    def mainloop(self):

        if self.config.get_my("be-the-basestation"):
            print "I AM THE BASE STATION"
            self.modules.load_module('SBPUDPBroadcast')
        else:
            print "I AM NOT THE BASE STATION"
            piksi = self.modules.load_module('piksihandler')
            bcastmodule = self.modules.load_module('sbpbroadcastlistener')
            bcastmodule.set_data_callback(piksi.send_to_piksi)
            pixhawk = self.modules.load_module('pixhawkhandler',
                                               waitTimeout=10.0)

        try:

            # Here we do UDP command and control
            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_DGRAM)) as cc_udp:
                cc_udp.setblocking(1)
                cc_udp.settimeout(0.1)  # run this at 10hz
                cc_udp.bind((self.bind_ip, self.cc_local_port))
                self.cc_udp = cc_udp

                print "CC bound to %s : %d" % (self.bind_ip,
                                               self.cc_local_port)

                heartbeat = spooky.DoPeriodically(
                    lambda: self.send_heartbeat(), 1.0)
                while not self.dying:
                    try:
                        # For command and control, we're going to use JSON over UDP
                        # UDP *already* has a simple checksum and delivers a complete packet at a time.
                        # It also returns exactly one datagram per recv() call, so it's all good!
                        # Our only requirement is, a cc message consists of at the very least:
                        # {'msgtype': 'something', 'payload': {}}
                        heartbeat.tick()
                        cc_data, cc_addr = cc_udp.recvfrom(4096)
                        self.handle_cc(cc_data, cc_addr)
                    except (socket.error, socket.timeout) as e:
                        pass

        except KeyboardInterrupt:
            self.stop()