예제 #1
0
    def __init__(self, language_code='it-IT', last_contexts=None):
        """Initialize all params and load data"""
        """ Constants and params """

        self.CHUNK = 4096
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 1
        self.RATE = 44100  #was 16000
        self.DEBUG = rospy.get_param('/dialogflow_client/debug', False)

        #----------------------------------------------------------------------------------
        #
        # ROS Parametrers
        #
        #-----------------------------------------------------------------------------------
        self.USE_AUDIO_SERVER = rospy.get_param(
            '/dialogflow_client/use_audio_server', False)

        # se il flag PLAY_GOOGLE_AUDIO è attivo creo l'output audio
        self.stream_out = None
        self.PLAY_GOOGLE_AUDIO = rospy.get_param(
            '/dialogflow_client/play_google_audio', True)
        self.PLAY_LOCAL_AUDIO = rospy.get_param(
            '/dialogflow_client/play_local_audio', True)

        #Snowboy parameters
        self.model_path = rospy.get_param('/dialogflow_client/model_path', '')
        self.interrupted = False
        self.detector = None
        self.use_snowboy = rospy.get_param('/dialogflow_client/use_snowboy',
                                           False)
        self.snowboy_sensitivity = rospy.get_param(
            '/dialogflow_client/snowboy_sensitivity', 0.5)

        # Dialogflow params
        project_id = rospy.get_param('/dialogflow_client/project_id',
                                     'robot-15de1')

        # Setup audio output
        self.ding_data = ding.readframes(ding.getnframes())
        self.audio = pyaudio.PyAudio()
        self._create_audio_output()
        '''        
                self.stream_out = self.audio.open(
                format=self.audio.get_format_from_width(ding.getsampwidth()),
                channels=ding.getnchannels(), rate=ding.getframerate(),
                input=False, output=True)
        '''

        # Register Ctrl-C sigint
        signal.signal(signal.SIGINT, self._signal_handler)
        """  
        ###############################################################################
        ###############################################################################
         Dialogflow setup
        ###############################################################################
        ###############################################################################
        """
        self.contaChiamateApi = 0
        # Get hints/clues
        rp = rospkg.RosPack()
        file_dir = rp.get_path('dialogflow_ros') + '/config/context.yaml'
        with open(file_dir, 'r') as f:
            try:
                self.phrase_hints = load(f)
            except YAMLError:
                rospy.logwarn(
                    "DF_CLIENT: Unable to open phrase hints yaml file!")
                self.phrase_hints = []

        self._language_code = language_code
        self.last_contexts = last_contexts if last_contexts else []

        audio_encoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16

        # Possibel models: video, phone_call, command_and_search, default
        # https://cloud.google.com/dialogflow/docs/reference/rpc/google.cloud.dialogflow.v2#google.cloud.dialogflow.v2.InputAudioConfig
        self._audio_config = InputAudioConfig(
            audio_encoding=audio_encoding,
            language_code=self._language_code,
            sample_rate_hertz=self.RATE,
            phrase_hints=self.phrase_hints,  #parametro deprecato
            model='command_and_search',
            single_utterance=True)
        self._output_audio_config = OutputAudioConfig(
            audio_encoding=OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16)

        #----------------------------------------------------------------------------------
        # Create a DIALOGFLOW session
        #----------------------------------------------------------------------------------
        session_id = str(uuid4())  # Random

        self._session_cli = dialogflow_v2beta1.SessionsClient()
        self._session = self._session_cli.session_path(project_id, session_id)
        rospy.logdebug("DF_CLIENT: Session Path: {}".format(self._session))

        # dialogflow ros  -----------------------------------------------------------------
        results_topic = rospy.get_param('/dialogflow_client/results_topic',
                                        '/dialogflow_client/results')
        requests_topic = rospy.get_param('/dialogflow_client/requests_topic',
                                         '/dialogflow_client/requests')
        text_event_topic = requests_topic + '/string_event'
        msg_req_topic = requests_topic + '/df_msg'
        event_req_topic = requests_topic + '/df_event'
        text_req_topic = requests_topic + '/string_msg'
        self._results_pub = rospy.Publisher(results_topic,
                                            DialogflowResult,
                                            queue_size=10)
        rospy.Subscriber(text_req_topic, String,
                         self._text_request_cb)  # richiesta in formato testo
        rospy.Subscriber(text_event_topic, String, self._text_event_cb)
        rospy.Subscriber(msg_req_topic, DialogflowRequest,
                         self._msg_request_cb)
        rospy.Subscriber(event_req_topic, DialogflowEvent,
                         self._event_request_cb)
        #----------------------------------------------------------------------------------

        # subscribe ai tutti i topics per i quali devo poter rispondere a voce
        self.bat_charge = 0
        rospy.Subscriber('/bat_charge', Float64, self.cbk_bat_charge)
        self.bat_volt = 0
        rospy.Subscriber('/bat_volt', Float64, self.cbk_bat_volt)
        self.bat_ampere = 0
        rospy.Subscriber('/bat_ampere', Float64, self.cbk_bat_ampere)
        self.bat_watt = 0
        rospy.Subscriber('/bat_watt', Float64, self.cbk_bat_watt)
        self.bat_ischarging = False
        rospy.Subscriber('/bat_ischarging', Bool, self.cbk_bat_ischarging)
        #-------------------------------------------------------------------------

        #  advertise di tutti i comandi eseguibili a voce-------------------------
        self.pub_chatter = rospy.Publisher('/chatter', String, queue_size=10)
        self.pub_faretto = rospy.Publisher("/faretto", Bool, queue_size=1)
        self.pub_laser = rospy.Publisher("/laser", Bool, queue_size=1)
        self.pub_start_followme = rospy.Publisher("/start_followme",
                                                  Bool,
                                                  queue_size=1)
        self.pub_cmd_vel = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
        self.pub_targetPose = rospy.Publisher("/target_pose",
                                              Twist,
                                              queue_size=1)
        #-------------------------------------------------------------------------

        # Esecutore comandi --------------------------
        self.myCmdParser = DialogflowCommandExecutor()

        #----------------------------------------------------------------------------------
        #
        # inizializzazione
        #
        #-----------------------------------------------------------------------------------
        if self.PLAY_LOCAL_AUDIO:
            self.msg_chatter = String()

        if self.USE_AUDIO_SERVER:
            print("USO AUDIOSERVER")
        else:
            print("USO IL MICROFONO")

        if self.use_snowboy:
            # Setup snowboy
            self.speech(" uso snowboy")
            self.detector = snowboydecoder.HotwordDetector(
                self.model_path, sensitivity=self.snowboy_sensitivity)
            print(
                "\n\n***Using SNOWBOY model {} \n Sensitivity:  {} \n***\n\n".
                format(self.model_path, self.snowboy_sensitivity))

        else:
            print("\n\n***NON USO SNOWBOY ***")
            self.speech("non uso snowboy")
    def __init__(self, language_code='en-US', last_contexts=None):
        """Initialize all params and load data"""
        """ Constants and params """
        self.CHUNK = 4096
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 1
        self.RATE = 16000
        self.USE_AUDIO_SERVER = rospy.get_param('/dialogflow_client/use_audio_server', False)
        self.PLAY_AUDIO = rospy.get_param('/dialogflow_client/play_audio', True)
        self.DEBUG = rospy.get_param('/dialogflow_client/debug', False)

        # Register Ctrl-C sigint
        signal.signal(signal.SIGINT, self._signal_handler)

        """ Dialogflow setup """
        # Get hints/clues
        rp = rospkg.RosPack()
        file_dir = rp.get_path('dialogflow_ros') + '/config/context.yaml'
        with open(file_dir, 'r') as f:
            try:
                self.phrase_hints = load(f)
            except YAMLError:
                rospy.logwarn("DF_CLIENT: Unable to open phrase hints yaml file!")
                self.phrase_hints = []

        # Dialogflow params
        project_id = rospy.get_param('/dialogflow_client/project_id', 'my-project-id')
        session_id = str(uuid4())  # Random
        self._language_code = language_code
        self.last_contexts = last_contexts if last_contexts else []
        # DF Audio Setup
        audio_encoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
        # Possibel models: video, phone_call, command_and_search, default
        self._audio_config = InputAudioConfig(audio_encoding=audio_encoding,
                                              language_code=self._language_code,
                                              sample_rate_hertz=self.RATE,
                                              phrase_hints=self.phrase_hints,
                                              model='command_and_search')
        self._output_audio_config = OutputAudioConfig(
                audio_encoding=OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16
        )
        # Create a session
        self._session_cli = dialogflow_v2beta1.SessionsClient()
        self._session = self._session_cli.session_path(project_id, session_id)
        rospy.logdebug("DF_CLIENT: Session Path: {}".format(self._session))

        """ ROS Setup """
        results_topic = rospy.get_param('/dialogflow_client/results_topic',
                                        '/dialogflow_client/results')
        requests_topic = rospy.get_param('/dialogflow_client/requests_topic',
                                         '/dialogflow_client/requests')
        text_req_topic = requests_topic + '/string_msg'
        text_event_topic = requests_topic + '/string_event'
        msg_req_topic = requests_topic + '/df_msg'
        event_req_topic = requests_topic + '/df_event'
        self._results_pub = rospy.Publisher(results_topic, DialogflowResult,
                                            queue_size=10)
        rospy.Subscriber(text_req_topic, String, self._text_request_cb)
        rospy.Subscriber(text_event_topic, String, self._text_event_cb)
        rospy.Subscriber(msg_req_topic, DialogflowRequest, self._msg_request_cb)
        rospy.Subscriber(event_req_topic, DialogflowEvent, self._event_request_cb)

        """ Audio setup """
        # Mic stream input setup
        self.audio = pyaudio.PyAudio()
        self._server_name = rospy.get_param('/dialogflow_client/server_name',
                                            '127.0.0.1')
        self._port = rospy.get_param('/dialogflow_client/port', 4444)

        if self.PLAY_AUDIO:
            self._create_audio_output()

        rospy.logdebug("DF_CLIENT: Last Contexts: {}".format(self.last_contexts))
        rospy.loginfo("DF_CLIENT: Ready!")
예제 #3
0
    def __init__(self, language_code='it-IT', last_contexts=None):
        """Initialize all params and load data"""
        """ Constants and params """
        self.CHUNK = 4096
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 1
        self.RATE = 44100  #was 16000
        self.USE_AUDIO_SERVER = rospy.get_param(
            '/dialogflow_client/use_audio_server', False)
        if self.USE_AUDIO_SERVER:
            print("USO AUDIOSERVER")
        else:
            print("USO IL MICROFONO")

        self.PLAY_GOOGLE_AUDIO = rospy.get_param(
            '/dialogflow_client/play_google_audio', True)
        self.PLAY_LOCAL_AUDIO = rospy.get_param(
            '/dialogflow_client/play_local_audio', True)
        self.DEBUG = rospy.get_param('/dialogflow_client/debug', False)

        # Register Ctrl-C sigint
        signal.signal(signal.SIGINT, self._signal_handler)
        """ Dialogflow setup """
        # Get hints/clues
        rp = rospkg.RosPack()
        file_dir = rp.get_path('dialogflow_ros') + '/config/context.yaml'
        with open(file_dir, 'r') as f:
            try:
                self.phrase_hints = load(f)
            except YAMLError:
                rospy.logwarn(
                    "DF_CLIENT: Unable to open phrase hints yaml file!")
                self.phrase_hints = []

        # Dialogflow params
        project_id = rospy.get_param('/dialogflow_client/project_id',
                                     'robot-15de1')
        session_id = str(uuid4())  # Random
        self._language_code = language_code
        self.last_contexts = last_contexts if last_contexts else []
        # DF Audio Setup
        audio_encoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
        # Possibel models: video, phone_call, command_and_search, default
        self._audio_config = InputAudioConfig(
            audio_encoding=audio_encoding,
            language_code=self._language_code,
            sample_rate_hertz=self.RATE,
            phrase_hints=self.phrase_hints,
            model='command_and_search')
        self._output_audio_config = OutputAudioConfig(
            audio_encoding=OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16)

        #speechCfg =SynthesizeSpeechConfig() #https://cloud.google.com/dialogflow/docs/reference/rest/v2beta1/OutputAudioConfig#SynthesizeSpeechConfig
        #speechCfg.speakingRate=1.5  # range [0.25, 4.0]. 1.0 is the normal native speed
        #speechCfg.pitch= -10    # range [-20.0, 20.0].
        #speechCfg.volumeGainDb  # range [-96.0, 16.0].
        #speechCfg.voice.ssmlGender = texttospeech.enums.SsmlVoiceGender.SSML_VOICE_GENDER_MALE
        # self._output_audio_config = OutputAudioConfig(
        #         audio_encoding=OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16 #,
        #         synthesizeSpeechConfig=SynthesizeSpeechConfig.

        # )

        # Create a session
        self._session_cli = dialogflow_v2beta1.SessionsClient()
        self._session = self._session_cli.session_path(project_id, session_id)
        rospy.logdebug("DF_CLIENT: Session Path: {}".format(self._session))
        """ ROS Setup """
        results_topic = rospy.get_param('/dialogflow_client/results_topic',
                                        '/dialogflow_client/results')
        requests_topic = rospy.get_param('/dialogflow_client/requests_topic',
                                         '/dialogflow_client/requests')
        text_req_topic = requests_topic + '/string_msg'
        text_event_topic = requests_topic + '/string_event'
        msg_req_topic = requests_topic + '/df_msg'
        event_req_topic = requests_topic + '/df_event'
        self._results_pub = rospy.Publisher(results_topic,
                                            DialogflowResult,
                                            queue_size=10)
        rospy.Subscriber(text_req_topic, String, self._text_request_cb)
        rospy.Subscriber(text_event_topic, String, self._text_event_cb)
        rospy.Subscriber(msg_req_topic, DialogflowRequest,
                         self._msg_request_cb)
        rospy.Subscriber(event_req_topic, DialogflowEvent,
                         self._event_request_cb)
        """ Audio setup """
        # Mic stream input setup
        self.audio = pyaudio.PyAudio()

        if self.USE_AUDIO_SERVER:
            self._server_name = rospy.get_param(
                '/dialogflow_client/server_name', '127.0.0.1')
            self._port = rospy.get_param('/dialogflow_client/port', 4444)

            #print("Audio Server at: ",self._server_name, ", on port ", self._port)
            print("Audio Server at: {}, on port {}".format(
                self._server_name, self._port))

        if self.PLAY_GOOGLE_AUDIO:
            self._create_audio_output()
            print("Uso Google STT")

        rospy.logdebug("DF_CLIENT: Last Contexts: {}".format(
            self.last_contexts))
        rospy.loginfo("DF_CLIENT: Ready!")

        self.bat_charge = 0
        rospy.Subscriber('/bat_charge', Float64, self.cbk_bat_charge)

        # qui tutti i comandi eseguibili a voce
        self.pub_chatter = rospy.Publisher('/chatter', String, queue_size=10)
        self.pub_faretto = rospy.Publisher("/faretto", Bool, queue_size=1)
        self.pub_laser = rospy.Publisher("/laser", Bool, queue_size=1)
        self.pub_start_followme = rospy.Publisher("/start_followme",
                                                  Bool,
                                                  queue_size=1)
        self.pub_cmd_vel = rospy.Publisher("/cmd_vel", Twist, queue_size=1)
        self.pub_targetPose = rospy.Publisher("/target_pose",
                                              Twist,
                                              queue_size=1)