Skip to content

Commit

Permalink
added chatBot nwc
Browse files Browse the repository at this point in the history
  • Loading branch information
colombraf committed Sep 27, 2023
1 parent 70f930e commit 18cece4
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 137 deletions.
9 changes: 5 additions & 4 deletions app/r1Obr-orchestrator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
set(appname r1Obr-orchestrator)

file(GLOB conf ${CMAKE_CURRENT_SOURCE_DIR}/conf/*.ini)
file(GLOB templates ${CMAKE_CURRENT_SOURCE_DIR}/scripts/*.template)
file(GLOB apps ${CMAKE_CURRENT_SOURCE_DIR}/scripts/*.xml)
file(GLOB conf ${CMAKE_CURRENT_SOURCE_DIR}/conf/*.ini)
file(GLOB robotInterface ${CMAKE_CURRENT_SOURCE_DIR}/conf/robotInterface/*.xml)
file(GLOB templates ${CMAKE_CURRENT_SOURCE_DIR}/scripts/*.template)
file(GLOB apps ${CMAKE_CURRENT_SOURCE_DIR}/scripts/*.xml)

yarp_install(FILES ${conf} DESTINATION ${${PROJECT_NAME}_CONTEXTS_INSTALL_DIR}/${appname})
yarp_install(FILES ${apps} DESTINATION ${${PROJECT_NAME}_APPLICATIONS_INSTALL_DIR})
yarp_install(FILES ${scripts} DESTINATION ${${PROJECT_NAME}_APPLICATIONS_TEMPLATES_INSTALL_DIR})

yarp_install(FILES ${robotInterface} DESTINATION ${${PROJECT_NAME}_CONTEXTS_INSTALL_DIR}/${appname}/robotInterface)
1 change: 1 addition & 0 deletions app/r1Obr-orchestrator/conf/chatBot_nwc_test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config robotInterface/chatBot_nwc_test.xml
7 changes: 6 additions & 1 deletion app/r1Obr-orchestrator/conf/r1Obr-orchestrator_R1.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
input_rpc_port /r1Obr-orchestrator/rpc
input_port /r1Obr-orchestrator/input:i
sensor_network_rpc_port /r1Obr-orchestrator/sensor_network:rpc
next_loc_planner_rpc_port /r1Obr-orchestrator/nextLocPlanner/request:rpc
goandfindit_rpc_port /r1Obr-orchestrator/goAndFindIt/request:rpc
goandfindit_result_port /r1Obr-orchestrator/goAndFindIt/result:i
faceexpression_rpc_port /r1Obr-orchestrator/faceExpression:rpc

[INPUT_PORT_GROUP]
[INPUT_MANAGER]
voice_command_port /r1Obr-orchestrator/voice_command:i
input_command_port /r1Obr-orchestrator/input_command:i
chatbot_active true
device chatBot_nwc_yarp
local /r1Obr-orchestrator/chatBot
remote /chatBot_nws/rpc

[SPEECH_SYNTHESIZER]
active true
Expand Down
7 changes: 6 additions & 1 deletion app/r1Obr-orchestrator/conf/r1Obr-orchestrator_R1_demo.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
input_rpc_port /r1Obr-orchestrator/rpc
input_port /r1Obr-orchestrator/input:i
sensor_network_rpc_port /r1Obr-orchestrator/sensor_network:rpc
next_loc_planner_rpc_port /r1Obr-orchestrator/nextLocPlanner/request:rpc
goandfindit_rpc_port /r1Obr-orchestrator/goAndFindIt/request:rpc
goandfindit_result_port /r1Obr-orchestrator/goAndFindIt/result:i
faceexpression_rpc_port /r1Obr-orchestrator/faceExpression:rpc

[INPUT_PORT_GROUP]
[INPUT_MANAGER]
voice_command_port /r1Obr-orchestrator/voice_command:i
input_command_port /r1Obr-orchestrator/input_command:i
chatbot_active true
device chatBot_nwc_yarp
local /r1Obr-orchestrator/chatBot
remote /chatBot_nws/rpc

[SPEECH_SYNTHESIZER]
active true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
input_rpc_port /r1Obr-orchestrator/rpc
input_port /r1Obr-orchestrator/input:i
sensor_network_rpc_port /r1Obr-orchestrator/sensor_network:rpc
next_loc_planner_rpc_port /r1Obr-orchestrator/nextLocPlanner/request:rpc
goandfindit_rpc_port /r1Obr-orchestrator/goAndFindIt/request:rpc
goandfindit_result_port /r1Obr-orchestrator/goAndFindIt/result:i
faceexpression_rpc_port /r1Obr-orchestrator/faceExpression:rpc

[INPUT_PORT_GROUP]
[INPUT_MANAGER]
voice_command_port /r1Obr-orchestrator/voice_command:i
input_command_port /r1Obr-orchestrator/input_command:i
chatbot_active true
device chatBot_nwc_yarp
local /r1Obr-orchestrator/chatBot
remote /chatBot_nws/rpc

[SPEECH_SYNTHESIZER]
active true
Expand Down
18 changes: 18 additions & 0 deletions app/r1Obr-orchestrator/conf/robotInterface/chatBot_nwc_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
SPDX-License-Identifier: BSD-3-Clause
-->
<!DOCTYPE robot PUBLIC "-//YARP//DTD yarprobotinterface 3.0//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV3.0.dtd">
<robot name="chatBotTest" build="2" portprefix="" xmlns:xi="http://www.w3.org/2001/XInclude">
<devices>
<device name="chatBotNwc" type="chatBot_nwc_yarp">
<param extern-name="test_local" name="local">
/test/chatBot
</param>
<param extern-name="test_remote" name="remote">
/chatBot_nws/rpc
</param>
</device>
</devices>
</robot>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
SPDX-License-Identifier: BSD-3-Clause
-->
<!DOCTYPE robot PUBLIC "-//YARP//DTD yarprobotinterface 3.0//EN" "http://www.yarp.it/DTD/yarprobotinterfaceV3.0.dtd">
<robot name="speechSynthesizerTest" build="2" portprefix="" xmlns:xi="http://www.w3.org/2001/XInclude">
<devices>
<device name="speechSynthesizerNwc" type="speechSynthesizer_nwc_yarp">
<param extern-name="test_local" name="local">
/test/speechSynthesizer
</param>
<param extern-name="test_remote" name="remote">
/speechSynthesizer_nws_yarp
</param>
</device>
</devices>
</robot>
1 change: 1 addition & 0 deletions app/r1Obr-orchestrator/conf/speechSynthesizer_nwc_test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config robotInterface/chatBot_nwc.xml
157 changes: 157 additions & 0 deletions modules/r1Obr-orchestrator/inputManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "inputManager.h"

YARP_LOG_COMPONENT(INPUT_MANAGER, "r1_obr.orchestrator.inputManager")


InputManager::InputManager(double _period, yarp::os::ResourceFinder &rf)
: PeriodicThread(_period), m_rf(rf) {}


// ****************************************************** //
bool InputManager::threadInit()
{
// Defaults
string voiceCommandPortName = "/r1Obr-orchestrator/voice_command:i";
string inputCommandPortName = "/r1Obr-orchestrator/input_command:i";
string inputManagerRPCPortName = "/r1Obr-orchestrator/inputManager:rpc";
string device_chatBot_nwc = "chatBot_nwc_yarp";
string local_chatBot_nwc = "/r1Obr-orchestrator/chatBot";
string remote_chatBot_nwc = "/chatBot_nws";
string language_chatbot = "auto";

// ------------------ out ------------------ //
if(!m_inputManagerRPCPort.open(inputManagerRPCPortName))
{
yCError(INPUT_MANAGER, "Unable to open input manager RPC port");
return false;
}

// ------------------ in ------------------ //
if(!m_rf.check("INPUT_MANAGER"))
{
yCWarning(INPUT_MANAGER,"INPUT_MANAGER section missing in ini file. Using the default values");
}

Searchable& inputs_config = m_rf.findGroup("INPUT_MANAGER");
if(inputs_config.check("voice_command_port")) {voiceCommandPortName = inputs_config.find("voice_command_port").asString();}
if(inputs_config.check("input_command_port")) {inputCommandPortName = inputs_config.find("input_command_port").asString();}


// Voice Command Port
if (!m_voiceCommandPort.open(voiceCommandPortName))
{
yCError(INPUT_MANAGER, "Unable to open voice command port");
return false;
}
m_voiceCommandPort.useCallback(*this);

// Other Input Port
if (!m_inputCommandPort.open(inputCommandPortName))
{
yCError(INPUT_MANAGER, "Unable to open input command port");
return false;
}

// iChatBot
m_chatBot_active = inputs_config.check("chatbot_active") ? (inputs_config.find("chatbot_active").asString() == "true") : false;

if(m_chatBot_active)
{
Property chatBotProp;
chatBotProp.put("device", inputs_config.check("device", Value(device_chatBot_nwc)));
chatBotProp.put("local", inputs_config.check("local", Value(local_chatBot_nwc)));
chatBotProp.put("remote", inputs_config.check("remote", Value(remote_chatBot_nwc)));
m_PolyCB.open(chatBotProp);
if(!m_PolyCB.isValid())
{
yCWarning(INPUT_MANAGER,"Error opening PolyDriver check parameters. Using the default values");
}
m_PolyCB.view(m_iChatBot);
if(!m_iChatBot){
yCError(INPUT_MANAGER,"Error opening IChatBot interface. Device not available");
return false;
}

if(inputs_config.check("language")) language_chatbot = inputs_config.find("language").asString();
m_iChatBot->setLanguage(language_chatbot);
}



return true;
}


// ****************************************************** //
void InputManager::threadRelease()
{
if(!m_voiceCommandPort.isClosed())
m_voiceCommandPort.close();

if(!m_inputCommandPort.isClosed())
m_inputCommandPort.close();

if (m_inputManagerRPCPort.asPort().isOpen())
m_inputManagerRPCPort.close();

if(m_PolyCB.isValid())
m_PolyCB.close();

yCInfo(INPUT_MANAGER, "Input Manager thread released");
}


// ****************************************************** //
void InputManager::run()
{
Bottle* inputCommand = m_inputCommandPort.read(false);
if(inputCommand != nullptr)
{
writeToRPC(*inputCommand);
}
}


// ****************************************************** //
void InputManager::onRead(Bottle& b)
{
string b_str = b.toString();
yCInfo(INPUT_MANAGER,"Received: %s",b_str.c_str());

string msg;
m_iChatBot->interact(b_str, msg);
yCInfo(INPUT_MANAGER,"ChatBot replied: %s",msg.c_str());

Bottle msg_btl;
msg_btl.fromString(msg);
writeToRPC(msg_btl);
}


// ****************************************************** //
void InputManager::writeToRPC(Bottle& req)
{
Bottle _rep_;
m_inputManagerRPCPort.write(req,_rep_);

yCInfo(INPUT_MANAGER, "Replied from orchestrator: %s", _rep_.toString().c_str() );
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,46 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef INPUT_COLLECTOR_H
#define INPUT_COLLECTOR_H
#ifndef INPUT_MANAGER_H
#define INPUT_MANAGER_H

#include <yarp/os/all.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/IChatBot.h>

using namespace yarp::os;
using namespace yarp::dev;
using namespace std;

class InputCollector : public PeriodicThread
class InputManager : public PeriodicThread, public TypedReaderCallback<Bottle>
{
private:

BufferedPort<Bottle> m_voiceCommandPort;
BufferedPort<Bottle> m_inputCommandPort;

BufferedPort<Bottle> m_outputPort;
RpcClient m_inputManagerRPCPort;

ResourceFinder &m_rf;
bool m_chatBot_active;
PolyDriver m_PolyCB;
IChatBot* m_iChatBot = nullptr;

ResourceFinder &m_rf;

public:

InputCollector(double _period, yarp::os::ResourceFinder &rf);
~InputCollector() = default;
InputManager(double _period, yarp::os::ResourceFinder &rf);
~InputManager() = default;

virtual bool threadInit() override;
virtual void threadRelease() override;
virtual void run() override;

//Port inherited from TypedReaderCallback
using TypedReaderCallback<Bottle>::onRead;
virtual void onRead(Bottle& b) override;

void writeToRPC(Bottle& req);

};

#endif
Loading

0 comments on commit 18cece4

Please sign in to comment.