コード例 #1
0
ファイル: agent.py プロジェクト: rrouf/photon-controller
    def _register_services(self):
        common.services.register(ServiceName.AGENT_CONFIG, self._config)
        common.services.register(ServiceName.LOCKED_VMS, ExclusiveSet())

        threadpool = RequestIdExecutor(
            ThreadPoolExecutor(self._config.workers))
        common.services.register(ThreadPoolExecutor, threadpool)

        self._registrant = ChairmanRegistrant(self._config.chairman_list)
        self._config.on_config_change(self._config.CHAIRMAN,
                                      self._registrant.update_chairman_list)
        common.services.register(ServiceName.REGISTRANT, self._registrant)

        state_json_file = os.path.join(
            self._config.options.config_path,
            self._config.DEFAULT_STATE_FILE)
        state = State(state_json_file)

        mode = Mode(state)
        mode.on_change(self._registrant.trigger_chairman_update)
        common.services.register(ServiceName.MODE, mode)

        ds_tags = DatastoreTags(state)
        ds_tags.on_change(self._registrant.trigger_chairman_update)
        common.services.register(ServiceName.DATASTORE_TAGS, ds_tags)
コード例 #2
0
ファイル: vehicle_ctl.py プロジェクト: rotor-ai/rtrcmd
    def __init__(self, config_handler: ConfigHandler) -> None:
        super().__init__()

        self._config_handler = config_handler
        self._time_since_last_throttle_steering_eval = time.time()

        self._request_handler = RequestHandler(config_handler)

        self._mode = Mode()

        self._thread = VehicleCtlThread()

        # This represents the last command we sent to the vehicle. It is needed to interpolate
        # between the current command and the target command.
        self._last_cmd_sent = Command()

        # The logical "Gear" we are working with. FIRST, REVERSE, or NEUTRAL
        # This is used when we are preparing the command to send to the vehicle
        self._gear = Gear.DRIVE

        # These represents the intended speed/direction of the vehicle
        # VehicleCtlThread uses the target_acceleration to compute a target throttle,
        # allowing us to roll onto the throttle, rather than FLOOR IT when the user presses 'forward'.
        # Steering, however, is NOT interpolated, and for good reason. If a user steers hard to the left,
        # we want the wheels to go to that position as quickly as possible.
        self._target_acceleration = 0
        self._target_steering = self._last_cmd_sent.get_steering()

        # Defines the behavior for our thread loop
        self._thread.behave = lambda: self.throttle_steering_eval()

        self._stream_port = self._config_handler.get_config_value_or(
            'stream_port', 4000)
コード例 #3
0
 def setup(self):
     self.state_file = tempfile.mktemp()
     self._host_handler = MagicMock()
     common.services.register(Host.Iface, self._host_handler)
     common.services.register(ServiceName.MODE,
                              Mode(State(self.state_file)))
     common.services.register(ServiceName.AGENT_CONFIG, MagicMock())
コード例 #4
0
 def setUp(self):
     self.agent_conf_dir = mkdtemp(delete=True)
     state = State(os.path.join(self.agent_conf_dir, "state.json"))
     common.services.register(ServiceName.MODE, Mode(state))
     common.services.register(ServiceName.DATASTORE_TAGS,
                              DatastoreTags(state))
     self.agent = AgentConfig(["--config-path", self.agent_conf_dir])
コード例 #5
0
    def __init__(self, id, networks, datastores, cpu, mem, disk, overcommit):
        self.id = id
        self.cpu = cpu
        self.mem = mem
        self.disk = disk
        self.parent = ""
        self.constraints = set()
        host_constraint = ResourceConstraint(ResourceConstraintType.HOST,
                                             ["host-" + str(id)])
        self.constraints.add(host_constraint)
        [self.constraints.add(net) for net in networks]
        [self.constraints.add(ds) for ds in datastores]
        self.address = ""
        self.port = ""
        conf_dir = mkdtemp(delete=True)
        state = State(os.path.join(conf_dir, "state.json"))
        common.services.register(ServiceName.MODE, Mode(state))
        self.hv = self._get_hypervisor_instance(
            id, cpu, mem, disk, [ds.values[0] for ds in datastores],
            [network.values[0] for network in networks], overcommit)

        # need agent_config for create/delete vm.
        agent_config = AgentConfig([
            "--config-path", conf_dir, "--hostname", "localhost", "--port",
            "1234", "--host-id", id
        ])
        common.services.register(ServiceName.AGENT_CONFIG, agent_config)
        super(Host, self).__init__(self.hv)
コード例 #6
0
    def setUp(self):
        self.agent_conf_dir = mkdtemp(delete=True)
        state = State(os.path.join(self.agent_conf_dir, "state.json"))
        common.services.register(ServiceName.MODE, Mode(state))
        common.services.register(ServiceName.DATASTORE_TAGS,
                                 DatastoreTags(state))
        self.multi_agent = MultiAgent(2200, AgentConfig.DEFAULT_CONFIG_PATH,
                                      AgentConfig.DEFAULT_CONFIG_FILE)

        self.agent = AgentConfig(["--config-path", self.agent_conf_dir])
コード例 #7
0
    def _register_services(self):
        common.services.register(ServiceName.AGENT_CONFIG, self._config)
        common.services.register(ServiceName.LOCKED_VMS, ExclusiveSet())

        state_json_file = os.path.join(self._config.options.config_path,
                                       self._config.DEFAULT_STATE_FILE)
        state = State(state_json_file)

        mode = Mode(state)
        common.services.register(ServiceName.MODE, mode)
コード例 #8
0
 def setup(self):
     self._threadpool = ThreadPoolExecutor(16)
     self.state_file = tempfile.mktemp()
     common.services.register(ThreadPoolExecutor, self._threadpool)
     self._host_handler = MagicMock()
     common.services.register(Host.Iface, self._host_handler)
     common.services.register(ServiceName.MODE,
                              Mode(State(self.state_file)))
     common.services.register(ServiceName.AGENT_CONFIG, MagicMock())
     common.services.register(ServiceName.DATASTORE_TAGS,
                              DatastoreTags(State(self.state_file)))
コード例 #9
0
    def _register_services(self):
        common.services.register(ServiceName.AGENT_CONFIG, self._config)
        common.services.register(ServiceName.LOCKED_VMS, ExclusiveSet())

        threadpool = RequestIdExecutor(ThreadPoolExecutor(
            self._config.workers))
        common.services.register(ThreadPoolExecutor, threadpool)

        state_json_file = os.path.join(self._config.options.config_path,
                                       self._config.DEFAULT_STATE_FILE)
        state = State(state_json_file)

        mode = Mode(state)
        common.services.register(ServiceName.MODE, mode)
コード例 #10
0
ファイル: peripheral_mgr.py プロジェクト: rotor-ai/rtrcmd
    def __init__(self):
        self.config_handler = ConfigHandler.get_instance()

        # Populate our peripheral list from the config
        self.peripherals = {}
        peripheral_list = self.config_handler.get_config_value_or(
            'peripheral_list', [])
        for peripheral in peripheral_list:
            if peripheral == 'distance_sensor':
                self.peripherals['distance_sensor'] = DistanceSensor()
            if peripheral == 'segmented_display':
                self.peripherals['segmented_display'] = SegmentedDisplay()
            if peripheral == 'cpu_fan':
                self.peripherals['cpu_fan'] = CpuFan()
            if peripheral == 'stereo_camera':
                self.peripherals['stereo_camera'] = StereoCamera()

        self.mode = Mode()
コード例 #11
0
    def setUp(self):
        self.hostname = "localhost"
        self.host_port = 1234
        self.availability_zone_id = "test"
        self.host_addr = ServerAddress(self.hostname, self.host_port)
        self.chairman_list = []
        self.agent_id = "foo"
        self.host_config = HostConfig(self.agent_id, self.availability_zone_id,
                                      [Datastore("bar")], self.host_addr,
                                      [Network("nw1")])
        self.registrant = ChairmanRegistrant(self.chairman_list)
        host_handler = MagicMock()
        host_handler.get_host_config_no_logging.return_value = \
            GetConfigResponse(hostConfig=self.host_config)
        common.services.register(Host.Iface, host_handler)
        self.request = RegisterHostRequest("foo", self.host_config)

        self.state_file = tempfile.mktemp()
        common.services.register(ServiceName.MODE,
                                 Mode(State(self.state_file)))
コード例 #12
0
    def setUp(self):
        self._threadpool = ThreadPoolExecutor(16)
        self.state_file = tempfile.mktemp()
        self.state = CommonState(self.state_file)

        common.services.register(ThreadPoolExecutor, self._threadpool)

        common.services.register(ServiceName.REQUEST_ID, threading.local())
        common.services.register(ServiceName.LOCKED_VMS, ExclusiveSet())
        common.services.register(ServiceName.MODE, Mode(self.state))

        self.agent_conf_dir = mkdtemp(delete=True)
        self.hostname = "localhost"

        self._config = MagicMock()
        self._config.hypervisor = "fake"
        self._config.agent_id = stable_uuid("agent_id")
        self._config.hostname = "localhost"
        self._config.host_port = 1234
        self._config.host_version = "X.X.X"
        self._config.reboot_required = False
        self._config.image_datastores = []
        common.services.register(ServiceName.AGENT_CONFIG, self._config)
コード例 #13
0
    def mode_changed(self, index):
        mode_int = self._cbo_mode.currentData()
        mode = Mode()
        mode.set_mode(mode_int)

        self._vehicle_ctl.set_mode(mode)
コード例 #14
0
 def setUp(self):
     self.file_location = tempfile.mkstemp()[1]
     self.state = State(self.file_location)
     self.mode = Mode(self.state)
コード例 #15
0
class TestState(unittest.TestCase):
    def setUp(self):
        self.file_location = tempfile.mkstemp()[1]
        self.state = State(self.file_location)
        self.mode = Mode(self.state)

    def tearDown(self):
        os.remove(self.file_location)

    def test_get_set_mode(self):
        self.mode.set_mode(MODE.NORMAL)
        assert_that(self.mode.get_mode(), equal_to(MODE.NORMAL))

    def test_get_default_mode(self):
        assert_that(self.mode.get_mode(), equal_to(MODE.NORMAL))

    @raises(AssertionError)
    def test_set_invalid_type(self):
        self.mode.set_mode("NORMAL")

    @raises(ValueError)
    def test_get_invalid_type(self):
        # State.set_mode() will check the type of parameter. State.set() does
        # not check parameter. The value could be corrupted. State.get_mode()
        # will raise ValueError.
        self.state.set(Mode.MODE_KEY, "123")
        self.mode.get_mode()

    @raises(ModeTransitionError)
    def test_invalid_transition(self):
        try:
            self.mode.set_mode(MODE.MAINTENANCE, [MODE.ENTERING_MAINTENANCE])
        except ModeTransitionError as e:
            assert_that(e.from_mode, equal_to(MODE.NORMAL))
            raise

    def test_transition_to_self(self):
        # Should allow to change mode from A to A (e.g. NORMAL to NORMAL)
        assert_that(self.mode.get_mode(), equal_to(MODE.NORMAL))
        self.mode.set_mode(MODE.NORMAL, [MODE.MAINTENANCE])
        assert_that(self.mode.get_mode(), equal_to(MODE.NORMAL))

    def test_callbacks(self):
        enter_maintenance = MagicMock()
        exit_normal = MagicMock()
        change = MagicMock()
        self.mode.on_enter_mode(MODE.MAINTENANCE, enter_maintenance)
        self.mode.on_exit_mode(MODE.NORMAL, exit_normal)
        self.mode.on_change(change)

        self.mode.set_mode(MODE.ENTERING_MAINTENANCE)
        assert_that(enter_maintenance.called, equal_to(False))
        assert_that(exit_normal.called, equal_to(True))
        assert_that(change.call_count, equal_to(1))

        self.mode.set_mode(MODE.MAINTENANCE)
        assert_that(enter_maintenance.called, equal_to(True))
        assert_that(change.call_count, equal_to(2))
コード例 #16
0
ファイル: vehicle_ctl.py プロジェクト: rotor-ai/rtrcmd
class VehicleCtl():
    """
    Primary vehicle interface class for the client. This class Controls:
        - Communication to and from the vehicle
        - Autonomous agents
        - Video streaming
        - Mode control
    """

    degradation_factor = 0.1

    def __init__(self, config_handler: ConfigHandler) -> None:
        super().__init__()

        self._config_handler = config_handler
        self._time_since_last_throttle_steering_eval = time.time()

        self._request_handler = RequestHandler(config_handler)

        self._mode = Mode()

        self._thread = VehicleCtlThread()

        # This represents the last command we sent to the vehicle. It is needed to interpolate
        # between the current command and the target command.
        self._last_cmd_sent = Command()

        # The logical "Gear" we are working with. FIRST, REVERSE, or NEUTRAL
        # This is used when we are preparing the command to send to the vehicle
        self._gear = Gear.DRIVE

        # These represents the intended speed/direction of the vehicle
        # VehicleCtlThread uses the target_acceleration to compute a target throttle,
        # allowing us to roll onto the throttle, rather than FLOOR IT when the user presses 'forward'.
        # Steering, however, is NOT interpolated, and for good reason. If a user steers hard to the left,
        # we want the wheels to go to that position as quickly as possible.
        self._target_acceleration = 0
        self._target_steering = self._last_cmd_sent.get_steering()

        # Defines the behavior for our thread loop
        self._thread.behave = lambda: self.throttle_steering_eval()

        self._stream_port = self._config_handler.get_config_value_or(
            'stream_port', 4000)

    def throttle_steering_eval(self):
        dt = time.time() - self._time_since_last_throttle_steering_eval

        prepared_cmd = self.prepare_new_command(dt)

        if not prepared_cmd.equal(self._last_cmd_sent):
            self._request_handler.send_command(prepared_cmd)
            self._last_cmd_sent = prepared_cmd

        elif time.time() - self._request_handler.get_time_last_cmd_sent(
        ) > 0.5:
            self._request_handler.send_command(self._last_cmd_sent)

        self._time_since_last_throttle_steering_eval = time.time()
        time.sleep(0.05)

    def prepare_new_command(self, dt):
        if self._gear == Gear.REVERSE:
            instantaneous_acceleration = min(
                (self._target_acceleration + VehicleCtl.degradation_factor),
                1.0) * dt
            interpolated_throttle = max(
                min((self._last_cmd_sent.get_throttle() +
                     instantaneous_acceleration), 0.0), -1.0)
            interpolated_steering = self._target_steering
        elif self._gear == Gear.DRIVE:
            instantaneous_acceleration = max(
                (self._target_acceleration - VehicleCtl.degradation_factor),
                -1.0) * dt
            interpolated_throttle = min(
                max(
                    self._last_cmd_sent.get_throttle() +
                    instantaneous_acceleration, 0.0), 1.0)
            interpolated_steering = self._target_steering
        else:
            interpolated_throttle = 0.0
            interpolated_steering = 0.0

        prepared_cmd = Command(interpolated_steering, interpolated_throttle)

        return prepared_cmd

    def start(self):
        self._thread.start()

    def stop(self):
        self._thread.stop()

    def get_gear(self):
        return self._gear

    def toggle_fw_bw(self):
        if self.get_gear().value == Gear.DRIVE.value:
            self._gear = Gear.REVERSE
        else:
            self._gear = Gear.DRIVE
        logging.info("Switched vehicle gear to " + str(self._gear))

    def request_stream_stop(self):
        self._request_handler.send_image_stream_stop()

    def request_stream_start(self):
        self._request_handler.send_image_stream_start(self._stream_port)

    def restart_stream(self):
        self._request_handler.send_image_stream_stop()
        self._request_handler.send_image_stream_start(self._stream_port)

    def vehicle_ip(self):
        return self._request_handler.dest_ip()

    def vehicle_port(self):
        return self._request_handler.dest_port()

    def vehicle_proxy_address(self):
        return self._request_handler.proxy_address()

    def vehicle_proxy_port(self):
        return self._request_handler.proxy_port()

    def set_accelerator(self, a_value):
        self._target_acceleration = a_value

    def set_steering(self, s_value):
        self._target_steering = s_value

    def mode(self):
        return self._mode

    def set_mode(self, mode):
        logging.info(f"Setting to mode {mode.mode_name()}")

        # TODO: Find a place to handle all of the mode switch logic

        if self._mode.mode_type(
        ) == ModeType.TRAIN and mode.mode_type() != ModeType.TRAIN:
            # We're moving out of training mode
            pass

        if self._mode.mode_type() != ModeType.TRAIN and mode.mode_type(
        ) == ModeType.TRAIN:
            # We're moving into training mode
            pass

        if self._mode.mode_type(
        ) == ModeType.AUTO and mode.mode_type() != ModeType.AUTO:
            # We're moving out of auto mode
            pass

        if self._mode.mode_type() != ModeType.AUTO and mode.mode_type(
        ) == ModeType.AUTO:
            # Moving into auto
            pass

        self._mode = mode

    def set_proxy(self, address, port):
        self._config_handler.set_config_value('proxy_address', address)
        self._config_handler.set_config_value('proxy_port', port)
        self._request_handler.set_proxy(address, port)

        # Need to now restart the image server and image stream
        self._image_stream_server.stop()
        self._image_stream_server.start()
        self.restart_stream()

    def is_using_proxy(self):
        return self._request_handler.is_using_proxy()

    def enable_proxy(self):
        self._config_handler.set_config_value('use_proxy', True)
        self._request_handler.enable_proxy()

    def disable_proxy(self):
        self._config_handler.set_config_value('use_proxy', False)
        self._request_handler.disable_proxy()

    def set_endpoint(self, ip, port):
        self._config_handler.set_config_value('vehicle_ip', ip)
        self._config_handler.set_config_value('vehicle_port', port)
        self._request_handler.set_endpoint(ip, port)

        # Need to now restart the image server and image stream
        self._image_stream_server.stop()
        self._image_stream_server.start()
        self.restart_stream()

    def get_trim(self):
        return self._request_handler.get_trim()

    def send_trim(self, trim):
        self._request_handler.send_trim(trim)
コード例 #17
0
 def setUp(self):
     self.agent_conf_dir = mkdtemp(delete=True)
     state = State(os.path.join(self.agent_conf_dir, "state.json"))
     common.services.register(ServiceName.MODE, Mode(state))
     common.services.register(ServiceName.REGISTRANT, mock.MagicMock())
     self.agent = AgentConfig(["--config-path", self.agent_conf_dir])