Example #1
0
def main():
    configurations.parseArgs(sys.argv)
    listener = Listener()
    if configurations.ARGS["quiet"]:
        print("Running in quiet mode")
    while True:
        try:
            print("What's up?")
            if not configurations.ARGS["quiet"]:
                command = Command(listener.listen())
            else:
                command = Command(input())

            if command is not None and command.name is not None:
                print("Command " + command.name)
                if command.command_type == CommandTypes.AUDIO:
                    # Music procssor here
                    audioProcessor = AudioProcessor()
                    audioProcessor.process(command)
                elif command.command_type == CommandTypes.SYSTEM:
                    #System processor here
                    systemProcessor = SystemProcessor()
                    systemProcessor.process(command)
                elif command.command_type == CommandTypes.SOCIAL:
                    mediaProcessor = SocialMediaProcessor()
                    mediaProcessor.process(command)
                elif command.name.lower() == "exit":
                    print("Exiting")
                    sys.exit(0)
                else:
                    print("Command not recognized.")
            else:
                print("I'm sorry, I didn't catch that")
        except CancelCommand:
            pass
Example #2
0
    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)
Example #3
0
    def __init__(self, *args, **kwargs):
        super(CommandThread, self).__init__(*args, **kwargs)
        self._stop_event = threading.Event()
        self.command = Command()
        self.last_cmd_time = None
        self.config_handler = ConfigHandler.get_instance()

        # Flag noting whether this is the vehicle or if it's a test server
        self.is_vehicle = self.config_handler.get_config_value_or(
            'is_vehicle', True)

        # Pull in the trim from the config file
        self.trim = Trim()
        self.trim.from_json(self.config_handler.get_config_value_or(
            'trim', {}))

        if self.is_vehicle:  # initialize throttle and steering values
            self.throttle = Throttle(self.trimmed_throttle())
            self.steering = Steering(self.trimmed_steering())

        self.lock = threading.Lock()
        self.loop_delay = 0.01

        logging.debug(
            "Main thread for vehicle controller started. Awaiting first command..."
        )
Example #4
0
	def do_cmds(self, msg):
		if (msg.startswith("recv")):
			return self.recv_file(msg)
		elif (msg.startswith("send")):
			local_file = msg.split('#')[1]
			return self.send_file(local_file, local_file, True)
		elif (msg.startswith("cmd")):
			cmd,shell = self.parse_cmd_args(msg)
			ctl = Command()
			if (shell == 'shell'):
				ret_info = ctl.run_sys_cmd(cmd)
				return self.send_msg(self.sock, Message.MSG_CTRL, ret_info)
			elif (shell == 'update'):
				args = sys.argv[:]
				args.insert(0, sys.executable)
				os.chdir(os._startup_cwd)
				msg = 'success'
				self.send_msg(self.sock, Message.MSG_CTRL, msg)
				self.sock.close()
				Log.logmsg("Update self:%s,%s" % (args, os._startup_cwd))
				os.execv(sys.executable, args)
				#no return
			else:
				Log.logerr("Invalid cmd!%s" % msg)
				return False
Example #5
0
    def test_should_enforce_lower_boundary_on_throttle(self):
        subject = Command()

        # test just above the upper bound to make sure it does throw an exception
        self.assertRaises(Exception, lambda: subject.set_throttle(-1.1))

        # test the upper bound to make sure it does not throw an exception
        subject.set_throttle(-1.0)
Example #6
0
    def test_setters_work(self):
        subject = Command(0.1, 0.2)
        subject.set_steering(0.3)
        subject.set_throttle(0.4)

        self.assertEqual(0.3, subject.values['steering'])
        self.assertEqual(0.4, subject.values['throttle'])

        subject.set_steering(0.5)
        subject.set_throttle(0.6)
        self.assertEqual(0.5, subject.values['steering'])
        self.assertEqual(0.6, subject.values['throttle'])
Example #7
0
    def check_recv(self):
        while self.game.up:
            socket_list = [self.sock]
            read_sockets, write_sockets, error_sockets = select.select(
                socket_list, [], [])
            for sock in read_sockets:
                # incoming message from remote server
                try:
                    data = read_message(sock)
                except TCPConnectionError as e:
                    logger.error(str(e))
                    self.reconnect()
                    continue

                if not data:
                    logger.error('Disconnected from server. Try to reconnect')
                    self.reconnect()
                    continue
                else:
                    # execute all commands from server
                    json_data = json.loads(data)
                    logger.debug("received message {}".format(
                        data[:GLOBAL.MAX_LOG_LENGTH]))
                    if json_data['type'] == MSG_TYPE.COMMAND:
                        command_obj = Command.from_json(json_data['payload'])
                        command_obj.apply(self.game)
                    elif json_data['type'] == MSG_TYPE.EXIT:
                        gLogger.error("Died, disconnect from server")
                        self.shutdown()
                        return
                    else:
                        logger.warning("Unknown message type received")
Example #8
0
    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
Example #9
0
    def execute_command(self):

        if self.last_cmd_time is None:
            return

        if time.time() - self.last_cmd_time > 1:
            logging.debug("Current command expired! Stopping vehicle")
            self.command = Command()
            self.last_cmd_time = None

        if self.is_vehicle:
            # Calculate trimmed values and update
            trimmed_throttle = self.trimmed_throttle()
            trimmed_steering = self.trimmed_steering()

            self.throttle.update_throttle(trimmed_throttle)
            self.steering.update_steering(trimmed_steering)
Example #10
0
 def get_command(self):
     with self.mic as source:
         self.recognizer.adjust_for_ambient_noise(source)
         audio = self.recognizer.listen(source)
         try:
             return Command(self.recognizer.recognize(audio))
         except LookupError:
             return None
Example #11
0
    def test_getters_work(self):
        subject = Command(0.1, 0.2)

        self.assertEqual(0.1, subject.get_steering())
        self.assertEqual(0.2, subject.get_throttle())

        subject.values['steering'] = 0.3
        subject.values['throttle'] = 0.4

        self.assertEqual(0.3, subject.get_steering())
        self.assertEqual(0.4, subject.get_throttle())
Example #12
0
	def call(self, command, *args):
		"""
			Synchronously call and get reply
		"""
		cmd = Command(command, *args)
		
		# send command
		with self._command_lock:
			self._command_store[cmd.id] = cmd
			self._network.send(str(cmd) + self._split)
		
		# await reply
		reply = cmd.wait()
		
		# delete command
		with self._command_lock:
			del self._command_store[cmd.id]
		
		return reply
Example #13
0
    def call(self, command, *args):
        """
			Synchronously call and get reply
		"""
        cmd = Command(command, *args)

        # send command
        with self._command_lock:
            self._command_store[cmd.id] = cmd
            self._network.send(str(cmd) + self._split)

        # await reply
        reply = cmd.wait()

        # delete command
        with self._command_lock:
            del self._command_store[cmd.id]

        return reply
Example #14
0
    def test_prepare_new_command_park_outputs_stationary_command(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.PARK
        subject._target_steering = 0.23
        subject._target_acceleration = -0.34
        subject._last_cmd_sent = Command(0.22, -1.0)

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), 0.0)
Example #15
0
    def test_prepare_new_command_reverse_stationary(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.REVERSE
        subject._target_steering = 0.0
        subject._target_acceleration = 0.0
        subject._last_cmd_sent = Command(0.0, 0.0)

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), 0.0)
Example #16
0
    def test_prepare_new_command_forward_max_at_1_point_0(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.DRIVE
        subject._target_steering = 0.0
        subject._target_acceleration = 1.0
        subject._last_cmd_sent = Command(s=0.0, t=1.0)

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), 1.0)
Example #17
0
    def test_prepare_new_command_reverse_coasting(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.REVERSE
        subject._target_steering = 0.0
        subject._target_acceleration = 0.0
        subject._last_cmd_sent = Command(0.0, -1.0)
        expectedAcc = (0.0 + VehicleCtl.degradation_factor) * self.dt

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), -1.0 + expectedAcc)
Example #18
0
    def test_prepare_new_command_forward_coasting(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.DRIVE
        subject._target_steering = 0.0
        subject._target_acceleration = 0.0
        subject._last_cmd_sent = Command(s=0.0, t=1.0)
        expected_instant_acc = (0.0 - VehicleCtl.degradation_factor) * self.dt

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), 1.0 + expected_instant_acc)
Example #19
0
    def test_should_init_with_expected_values(self):
        subject = VehicleCtl(self.mock_config_handler)

        # training manager, auto agent, etc...
        self.assertIsInstance(subject._mode, Mode)

        # Vehicle throttle,steering, etc...
        self.assertTrue(subject._last_cmd_sent.equal(Command()))
        self.assertEqual(Gear.DRIVE, subject._gear)
        self.assertEqual(0, subject._target_acceleration)
        self.assertEqual(subject._last_cmd_sent.get_steering(),
                         subject._target_steering)
Example #20
0
    def test_prepare_new_command_forward_braking(self):
        subject = VehicleCtl(self.mock_config_handler)
        subject._gear = Gear.DRIVE
        subject._target_steering = 0.0
        subject._target_acceleration = -1.0
        subject._last_cmd_sent = Command(s=0.0, t=1.0)
        expected_instant_acc = -1.0 * self.dt  #this test verifies that instant_acc is not less than -1, before considering dt

        new_cmd = subject.prepare_new_command(self.dt)

        self.assertEqual(new_cmd.get_steering(), 0.0)
        self.assertEqual(new_cmd.get_throttle(), 1.0 + expected_instant_acc)
Example #21
0
def api(request):
    
    cmd_op = request.GET['cmd']
    ctx = UserContext(request)
    cmd = Command(ctx)
    try:
        
        if cmd_op == 'clear-cache':
            cache.clear()
            return cmd.success(message="Cache cleared")
        elif cmd_op == 'list-data-files':
            user_dir = ctx.get_user_root() + ctx.params['path']
            files = [ f for f in listdir(user_dir) if isfile(join(user_dir,f)) ]
            dirs = [ f for f in listdir(user_dir) if isdir(join(user_dir,f)) ]
            
            return cmd.success(files=files,directories=dirs)
            
        elif cmd_op == 'upload-file':
            if request.method == 'POST':
                form = UploadFileForm(request.POST, request.FILES)
                if form.is_valid():
                    
                    if request.FILES['file'].size > 500000: #max upload file
                        return cmd.error("la limite de taille est de 500ko")
                    
                    name = request.FILES['file'].name
                    user_dir = ctx.get_user_root() + get_path(request) if 'path' in ctx.params else ''
    
                    path =  user_dir + os.sep + name
                    destination = open(path, 'wb+')
                    for chunk in request.FILES['file'].chunks():
                        destination.write(chunk)
                    destination.close()
                    
                    return cmd.success(files=[{'name': name, 'path':path}])
                else:
                    return cmd.error("%s n'est pas un fichier .CSV" % request.FILES['file'].name )
            else:
                return cmd.error('GET operation not supported')
        else:
            return cmd.error('Undefined api command %s' % cmd_op)

    except Exception, e:
        print traceback.format_exc()
        return cmd.error(str(e))
Example #22
0
def handle_message(serv, usr, data):
	try:
		msg = Command(data)
		(state, handler) = handler_map[msg.command]
		if check_state(serv, usr, state):
			handler(serv, usr, *msg.arguments)

	except ValueError:
		pass

	except KeyError:
		unknown_handler(serv, usr, msg.command)

	except TypeError:
		invalid_handler(serv, usr, msg.command)
Example #23
0
    def test_should_check_steering_bounds_on_init(self):

        self.assertRaises(Exception, lambda: Command(s=1.1))
Example #24
0
    def test_should_init_with_supplied_values(self):
        subject = Command(0.1, 0.2)

        self.assertEqual(0.1, subject.values['steering'])
        self.assertEqual(0.2, subject.values['throttle'])
 def _start_process(self, file_path):
     cmd = ['python', file_path]
     try:
         Command.run_without_wait(cmd)
     except Exception:
         self.logger.exception('Run script %s error' % file_path)
Example #26
0
 def broadcast(self, command, *args):
     """ Send a message to all connected clients """
     cmd = Command(command, *args, id='BROADCAST')
     for client in self.clients():
         client.write(str(cmd) + self._split)
Example #27
0
 def unicast(self, connection, command, *args):
     """ Send a message to a specific client """
     cmd = Command(command, *args, id='UNICAST')
     connection.write(str(cmd) + self._split)
Example #28
0
class CommandThread(threading.Thread):
    """ Main thread for the vehicle controller """
    def __init__(self, *args, **kwargs):
        super(CommandThread, self).__init__(*args, **kwargs)
        self._stop_event = threading.Event()
        self.command = Command()
        self.last_cmd_time = None
        self.config_handler = ConfigHandler.get_instance()

        # Flag noting whether this is the vehicle or if it's a test server
        self.is_vehicle = self.config_handler.get_config_value_or(
            'is_vehicle', True)

        # Pull in the trim from the config file
        self.trim = Trim()
        self.trim.from_json(self.config_handler.get_config_value_or(
            'trim', {}))

        if self.is_vehicle:  # initialize throttle and steering values
            self.throttle = Throttle(self.trimmed_throttle())
            self.steering = Steering(self.trimmed_steering())

        self.lock = threading.Lock()
        self.loop_delay = 0.01

        logging.debug(
            "Main thread for vehicle controller started. Awaiting first command..."
        )

    def stop(self):
        self._stop_event.set()

    def stopped(self):
        return self._stop_event.is_set()

    def run(self):

        while True:
            if self.stopped():
                return

            with self.lock:
                self.execute_command()

            time.sleep(self.loop_delay)

    def execute_command(self):

        if self.last_cmd_time is None:
            return

        if time.time() - self.last_cmd_time > 1:
            logging.debug("Current command expired! Stopping vehicle")
            self.command = Command()
            self.last_cmd_time = None

        if self.is_vehicle:
            # Calculate trimmed values and update
            trimmed_throttle = self.trimmed_throttle()
            trimmed_steering = self.trimmed_steering()

            self.throttle.update_throttle(trimmed_throttle)
            self.steering.update_steering(trimmed_steering)

    def trimmed_throttle(self):
        return self.trim.get_trimmed_throttle(self.command.get_throttle())

    def trimmed_steering(self):
        return self.trim.get_trimmed_steering(self.command.get_steering())
Example #29
0
    def test_should_check_throttle_bounds_on_init(self):

        self.assertRaises(Exception, lambda: Command(t=-1.1))
Example #30
0
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)
Example #31
0
    def test_should_init_with_default_values(self):
        subject = Command()

        self.assertEqual(0.0, subject.values['steering'])
        self.assertEqual(0.0, subject.values['throttle'])