Statistics
| Branch: | Tag: | Revision:

hlrc / server / src / MiddlewareROS.cpp @ 1e1c61a8

History | View | Annotate | Download (4.865 KB)

1 0c286af0 Simon Schulz
/*
2
* This file is part of hlrc_server
3
*
4
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/hlrc_server
6
*
7
* This file may be licensed under the terms of the
8
* GNU General Public License Version 3 (the ``GPL''),
9
* or (at your option) any later version.
10
*
11
* Software distributed under the License is distributed
12
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
* express or implied. See the GPL for the specific language
14
* governing rights and limitations.
15
*
16
* You should have received a copy of the GPL along with this
17
* program. If not, go to http://www.gnu.org/licenses/gpl.html
18
* or write to the Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
*
21
* The development of this software was supported by the
22
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
23
* The Excellence Cluster EXC 277 is a grant of the Deutsche
24
* Forschungsgemeinschaft (DFG) in the context of the German
25
* Excellence Initiative.
26
*
27
*/
28
29
#include "MiddlewareROS.h"
30
31
#ifdef ROS_SUPPORT
32
33
#include <cstdio>
34
35
//CallbackWrapper:
36
#include <boost/range/algorithm/remove_if.hpp>
37
#include <boost/algorithm/string/classification.hpp>
38 0c15613f Simon Schulz
#include <actionlib/client/simple_action_client.h>
39
#include "ROS/UtteranceROS.h"
40 0c286af0 Simon Schulz
41
using namespace std;
42
using namespace hlrc_server;
43
44 0c15613f Simon Schulz
template <typename actionspec>
45
actionlib::SimpleActionClient<actionspec> * create_action_client(string scope){
46
    printf("> starting SimpleActionClient on %s\n",scope.c_str());
47
48
    actionlib::SimpleActionClient<actionspec> *ac = new actionlib::SimpleActionClient<actionspec>(scope, true);
49
50
    if (!ac->waitForServer(ros::Duration(1))){
51 b1a51350 Simon Schulz
        printf("> ERROR: action service %s not ready, wait timed out...\n", scope.c_str());
52
        return NULL;
53 0c15613f Simon Schulz
    }
54
    return ac;
55
}
56
57
58 0c286af0 Simon Schulz
MiddlewareROS::MiddlewareROS(Arbiter *arbiter, std::string scope) : Middleware(arbiter, scope){
59
    init();
60
}
61
62
MiddlewareROS::~MiddlewareROS(){
63
64
}
65
66
void MiddlewareROS::init(){
67
    string scope = base_scope + "/set/";
68
69
    string node_name = "hlrc_server__"+ base_scope;
70
    node_name.erase(boost::remove_if(node_name, boost::is_any_of("/")), node_name.end());
71
72
    printf("> registering on ROS as node %s\n",node_name.c_str());
73
    if (ros::isInitialized()){
74
        //ros is already active, no need to take care of that
75
        tick_necessary = false;
76
    }else{
77
        printf("> ROS not initialized, calling ros::init()\n");
78
        ros::M_string no_remapping;
79
        ros::init(no_remapping, node_name);
80
        tick_necessary = true;
81
    }
82
83
    //create node handle
84
    //printf("> creating ros node handle\n");
85
    //ros::NodeHa
86
    ros::NodeHandle n;
87
88
    printf("> setting up ROS services...\n");
89
    //FIXME//init_callback("speech",    LocalServer::CallbackPtr(new SpeechCallbackWrapper(this)));
90
91
    //ros::AsyncSpinner *spinner = new ros::AsyncSpinner(4); // Use 4 threads
92
    //spinner->start();
93
94
    animation_action_server = new AnimationCallbackWrapper(this, scope, "animation");
95
    utterance_action_server = new UtteranceCallbackWrapper(this, scope, "utterance");
96
    current_emotion_action_server = new EmotionCallbackWrapper(this, scope, "currentEmotion");
97
    default_emotion_action_server = new EmotionCallbackWrapper(this, scope, "defaultEmotion");
98
    gaze_action_server = new GazeCallbackWrapper(this, scope, "gaze");
99
    mouth_action_server = new MouthCallbackWrapper(this, scope, "mouth");
100
    speech_action_server = new SpeechCallbackWrapper(this, scope, "speech");
101
102 0c15613f Simon Schulz
    //create tts client
103
    tts_ac = create_action_client<hlrc_server::ttsAction>(base_scope + "/tts_provider");
104 b1a51350 Simon Schulz
    if (tts_ac == NULL){
105
        printf("> tts action service not available, will not do any TTS !\n");
106
    }
107 0c15613f Simon Schulz
108
109
110 0c286af0 Simon Schulz
    printf("> init done\n");
111
}
112
113
void MiddlewareROS::tick(){
114
    if (tick_necessary){
115
        ros::spinOnce();
116
    }
117
}
118
119
//call a tts system to convert a string to an utterance
120 2e526a15 Simon Schulz
boost::shared_ptr<Utterance> MiddlewareROS::tts_call(string text){
121
    boost::shared_ptr<Utterance> utterance(new Utterance());
122 0c286af0 Simon Schulz
123 b1a51350 Simon Schulz
    //double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
124
    if (tts_ac == NULL){
125
        printf("> tts action service not available. will not speak '%s'\n", text.c_str());
126
        return utterance;
127
    }
128
129 0c15613f Simon Schulz
    hlrc_server::ttsGoal goal;
130 0c286af0 Simon Schulz
131 0c15613f Simon Schulz
    goal.text = text;
132 0c286af0 Simon Schulz
133 0c15613f Simon Schulz
    //send
134
    tts_ac->sendGoal(goal);
135 0c286af0 Simon Schulz
136 0c15613f Simon Schulz
    //call ros:
137
    bool finished_before_timeout = tts_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
138 0c286af0 Simon Schulz
139 0c15613f Simon Schulz
    if (!finished_before_timeout){
140
        printf("> ERROR: NO REPLY to utterance action call received within %4.2f s\n",ROS_ACTION_CALL_TIMEOUT);
141
    }else{
142
        //done, return utterance ptr
143
        ttsResultConstPtr tts_res = tts_ac->getResult();
144
        boost::shared_ptr<Utterance> utterance(new UtteranceROS(tts_res));
145
        printf("> done. got utterance (text=%s)\n",utterance->get_text().c_str());
146
        return utterance;
147 0c286af0 Simon Schulz
    }
148
149 0c15613f Simon Schulz
    printf("> failed... got no utterance\n");
150 0c286af0 Simon Schulz
    return utterance;
151
}
152
153
154
#endif