Beispiel #1
0
    def start(self, sid: SimulationID, vid: VehicleID) -> None:
        from drivebuildclient.AIExchangeService import AIExchangeService
        from drivebuildclient.aiExchangeMessages_pb2 import SimStateResponse, DataRequest, Control
        service = AIExchangeService("localhost", 8383)
        while True:
            print(sid.sid + ": Test status: " + service.get_status(sid))
            # Wait for the simulation to request this AI
            sim_state = service.wait_for_simulator_request(sid, vid)
            if sim_state is SimStateResponse.SimState.RUNNING:  # Check whether simulation is still running
                # Request data this AI needs
                request = DataRequest()
                request.request_ids.extend([])  # Add all IDs of data to be requested
                data = service.request_data(sid, vid, request)  # Request the actual data

                # Calculate commands controlling the car
                control = Control()
                # Define a control command like
                # control.avCommand.accelerate = <Some value between 0.0 and 1.0>
                # control.avCommand.steer = <Some value between -1.0 (left) and 1.0 (right)
                # control.avCommand.brake = <Some value between 0.0 and 1.0>
                service.control(sid, vid, control)
            else:
                print(sid.sid + ": The simulation is not running anymore (Final state: "
                      + SimStateResponse.SimState.Name(sim_state) + ").")
                print(sid.sid + ": Final test result: " + service.get_result(sid))
                # Clean up everything you have to
                break
 def control(self, sid: SimulationID, vid: VehicleID,
             commands: Control) -> Optional[Void]:
     """
     Control the simulation or a certain vehicle in the simulation.
     :param sid: The ID the simulation either to control or containing th vehicle to control.
     :param vid: The ID of the vehicle to possibly control.
     :param commands: The command either controlling a simulation or a vehicle in a simualtion. To define a command
     controlling a simulation you can use commands like:
     control = Control()
     control.simCommand.command = Control.SimCommand.Command.SUCCEED  # Force simulation to succeed
     control.simCommand.command = Control.SimCommand.Command.FAIL  # Force simulation to fail
     control.simCommand.command = Control.SimCommand.Command.CANCEL  # Force simulation to be cancelled/skipped
     For controlling a vehicle you have to define steering, acceleration and brake values:
     control = Control()
     control.avCommand.accelerate = <Acceleration intensity having a value between 0.0 and 1.0>
     control.avCommand.steer = <A steering value between -1.0 and 1.0 (Negative value steers left; a positive one
     steers right)>
     control.avCommand.brake = <Brake intensity having a value between 0.0 and 1.0>
     :return: A Void object possibly containing a info message.
     """
     from drivebuildclient.httpUtil import do_mixed_request
     response = do_mixed_request(self.host, self.port, "/ai/control", {
         "sid": sid.SerializeToString(),
         "vid": vid.SerializeToString()
     }, commands.SerializeToString())
     if response.status == 200:
         void = Void()
         void.ParseFromString(b"".join(response.readlines()))
         return void
     else:
         AIExchangeService._print_error(response)
Beispiel #3
0
    def take_action(self, action):
        steering, throttle = action
        steering = steering.item()
        throttle = throttle.item()
        self.last_action = action

        control = Control()
        control.avCommand.accelerate = throttle
        control.avCommand.steer = steering
        control.avCommand.brake = 0
        self.service.control(self.sid, self.vid, control)
Beispiel #4
0
def _handle_vehicle(sid: SimulationID, vid: str, requests: List[str]) -> None:
    vid_obj = VehicleID()
    vid_obj.vid = vid

    while True:
        print(sid.sid + ": Test status: " + service.get_status(sid))
        print(vid + ": Wait")
        sim_state = service.wait_for_simulator_request(sid, vid_obj)  # wait()
        if sim_state is SimStateResponse.SimState.RUNNING:
            print(vid + ": Request data")
            request = DataRequest()
            request.request_ids.extend(requests)
            data = service.request_data(sid, vid_obj, request)  # request()
            # print(data)
            print(vid + ": Wait for control")
            control = Control()
            while not is_pressed("space"):  # Wait for the user to trigger manual drive
                pass
            print(vid + ": Control")
            if is_pressed("s"):
                control.simCommand.command = Control.SimCommand.Command.SUCCEED
            elif is_pressed("f"):
                control.simCommand.command = Control.SimCommand.Command.FAIL
            elif is_pressed("c"):
                control.simCommand.command = Control.SimCommand.Command.CANCEL
            else:
                accelerate = 0
                steer = 0
                brake = 0
                if is_pressed("up"):
                    accelerate = 1
                if is_pressed("down"):
                    brake = 1
                if is_pressed("right"):
                    steer = steer + 1
                if is_pressed("left"):
                    steer = steer - 1
                control.avCommand.accelerate = accelerate
                control.avCommand.steer = steer
                control.avCommand.brake = brake
            service.control(sid, vid_obj, control)  # control()
        else:
            print(sid.sid + ": The simulation is not running anymore (State: "
                  + SimStateResponse.SimState.Name(sim_state) + ").")
            print(sid.sid + ": Final result: " + service.get_result(sid))
            break
    def take_action(self, action):
        """
        Execute a given action.

        :param action: ([float])
        :return: (None)
        """
        steering, throttle = action
        steering = steering.item()
        throttle = throttle.item()
        self.last_action = action

        control = Control()
        control.avCommand.accelerate = throttle
        control.avCommand.steer = steering
        control.avCommand.brake = 0
        self.service.control(self.sid, self.vid, control)
Beispiel #6
0
 def _control(sid: SimulationID, vid: VehicleID, control: Control) -> Void:
     _logger.info("ai_control: enter for " + vid.vid)
     result = Void()
     if _is_simulation_running(sid):
         command_type = control.WhichOneof("command")
         if command_type == "simCommand":
             _control_sim(sid, control.simCommand.command, True)
         elif command_type == "avCommand":
             sim = _get_simulation(sid)
             if sim and sim.get_current_movement_mode(
                     vid.vid) is MovementMode.AUTONOMOUS:
                 _control_av(sid, vid, control.avCommand)
         else:
             raise NotImplementedError("Interpreting commands of type " +
                                       command_type +
                                       " is not implemented, yet.")
         result.message = "Controlled vehicle " + vid.vid + " in simulation " + sid.sid + "."
     else:
         result.message = "Simulation " + sid.sid + " does not run."
     _logger.info("ai_control: leave for " + vid.vid)
     return result
Beispiel #7
0
 def start(self, sid: SimulationID, vid: VehicleID, dynamic_stats_callback: Callable[[], None]) -> None:
     from drivebuildclient.aiExchangeMessages_pb2 import SimStateResponse, DataRequest, Control
     from PIL import Image
     from io import BytesIO
     while True:
         sim_state = self.service.wait_for_simulator_request(sid, vid)
         if sim_state == SimStateResponse.SimState.RUNNING:
             dynamic_stats_callback()
             request = DataRequest()
             request.request_ids.append("egoFrontCamera_" + vid.vid)
             data = self.service.request_data(sid, vid, request)
             speed = data.data["egoSpeed_" + vid.vid].speed.speed
             if data:
                 color_image = Image.open(BytesIO(data.data["egoFrontCamera_" + vid.vid].camera.color))
                 controls = CONTROLLER.getControl(AI._preprocess(color_image, BRIGHTNESS), speed)
                 control = Control()
                 control.avCommand.steer = controls.steering
                 control.avCommand.accelerate = controls.throttle
                 self.service.control(sid, vid, control)
             else:
                 print("The request for data returned None.")
         else:
             break
Beispiel #8
0
 def _handle_main_app_message(action: bytes, data: List[bytes]) -> bytes:
     from google.protobuf.message import DecodeError
     if action == b"runTests":
         user = User()
         try:
             user.ParseFromString(data[1])
             result = _run_tests(data[0], user)
         except DecodeError:
             _logger.exception("Running a test failed since \"" +
                               str(data[1]) +
                               "\" can not be parsed to an User")
             result = SubmissionResult()
             result.message.message = "The user parameter could not be parsed."
     elif action == b"waitForSimulatorRequest":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         result = _wait_for_simulator_request(sid, vid)
     elif action == b"control":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         control = Control()
         control.ParseFromString(data[2])
         result = _control(sid, vid, control)
     elif action == b"requestData":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         vid = VehicleID()
         vid.ParseFromString(data[1])
         request = DataRequest()
         request.ParseFromString(data[2])
         result = _request_data(sid, vid, request)
     elif action == b"requestSocket":
         client = create_client(MAIN_APP_HOST, MAIN_APP_PORT)
         client_thread = Thread(target=process_requests,
                                args=(client, _handle_main_app_message))
         client_thread.daemon = True
         _logger.info("_handle_main_app_message --> " +
                      str(client.getsockname()))
         client_thread.start()
         result = Void()
         result.message = "Connected another client socket to the main app."
     elif action == b"runningTests":
         user = User()
         user.ParseFromString(data[0])
         result = _get_running_tests(user)
     elif action == b"stop":
         sid = SimulationID()
         sid.ParseFromString(data[0])
         test_result = TestResult()
         test_result.ParseFromString(data[1])
         _control_sim(sid, test_result.result, False)
         result = Void()
         result.message = "Stopped simulation " + sid.sid + "."
     else:
         message = "The action \"" + action.decode() + "\" is unknown."
         _logger.info(message)
         result = Void()
         result.message = message
     return result.SerializeToString()
Beispiel #9
0
    def start(self, sid: SimulationID, vid: VehicleID,
              dynamic_callback: Callable[[], None]) -> None:
        from drivebuildclient.aiExchangeMessages_pb2 import SimStateResponse, DataRequest, Control
        from self_driving.simulation_data import SimulationDataRecord
        from PIL import Image
        import io
        request = DataRequest()
        request.request_ids.append("center_cam")
        simulation_data_records = []
        while True:
            sim_state = self._service.wait_for_simulator_request(sid, vid)
            dynamic_callback()
            if sim_state == SimStateResponse.SimState.RUNNING:
                data = self._service.request_data(sid, vid, request).data
                # Request camera image
                byte_im = data["center_cam"].camera.color
                image = Image.open(io.BytesIO(byte_im)).convert("RGB")

                # Determine last state
                # is_oob, oob_counter, max_oob_percentage, oob_distance = self._oob_monitor.get_oob_info(oob_bb=False, wrt="right")
                is_oob = False
                oob_counter = max_oob_percentage = oob_distance = None
                car_state = {
                    "timer": 0,
                    "damage": 0,
                    "pos": 0,
                    "dir": 0,
                    "vel": 0,
                    "gforces": 0,
                    "gforces2": 0,
                    "steering": 0,
                    "steering_input": 0,
                    "brake": 0,
                    "brake_input": 0,
                    "throttle": 0,
                    "throttle_input": 0,
                    "throttleFactor": 0,
                    "engineThrottle": 0,
                    "wheelspeed": 0,
                    "vel_kmh": data["ego_speed"].speed.speed
                }
                sim_data_record = SimulationDataRecord(
                    **car_state,
                    is_oob=is_oob,
                    oob_counter=oob_counter,
                    max_oob_percentage=max_oob_percentage,
                    oob_distance=oob_distance)
                simulation_data_records.append(sim_data_record)
                last_state = simulation_data_records[-1]
                if last_state.is_oob:
                    break

                # Compute control command
                steering_angle, throttle = self._prediction.predict(
                    image, last_state)
                control = Control.AvCommand()
                control.steering_angle = steering_angle
                control.accelerate = throttle
                self._service.control(sid, vid, control)
            else:
                break