Revision f150aab5 server/src/MiddlewareROS.cpp

View differences:

server/src/MiddlewareROS.cpp
1 1
/*
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
*/
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 28

  
29 29
#include "MiddlewareROS.h"
30 30

  
......
32 32

  
33 33
#include <cstdio>
34 34

  
35
//CallbackWrapper:
35
// CallbackWrapper:
36 36
#include <boost/range/algorithm/remove_if.hpp>
37 37
#include <boost/algorithm/string/classification.hpp>
38 38
#include <actionlib/client/simple_action_client.h>
......
42 42
using namespace hlrc_server;
43 43

  
44 44
template <typename actionspec>
45
actionlib::SimpleActionClient<actionspec> * create_action_client(string scope){
46
    printf("> starting SimpleActionClient on %s\n",scope.c_str());
45
actionlib::SimpleActionClient<actionspec>* create_action_client(string scope) {
46
	printf("> starting SimpleActionClient on %s\n", scope.c_str());
47 47

  
48
    actionlib::SimpleActionClient<actionspec> *ac = new actionlib::SimpleActionClient<actionspec>(scope, true);
48
	actionlib::SimpleActionClient<actionspec>* ac = new actionlib::SimpleActionClient<actionspec>(scope, true);
49 49

  
50
    if (!ac->waitForServer(ros::Duration(1))){
51
        printf("> ERROR: action service %s not ready, wait timed out...\n", scope.c_str());
52
        return NULL;
53
    }
54
    return ac;
50
	if (!ac->waitForServer(ros::Duration(1))) {
51
		printf("> ERROR: action service %s not ready, wait timed out...\n", scope.c_str());
52
		return NULL;
53
	}
54
	return ac;
55 55
}
56 56

  
57

  
58
MiddlewareROS::MiddlewareROS(Arbiter *arbiter, std::string scope) : Middleware(arbiter, scope){
59
    init();
57
MiddlewareROS::MiddlewareROS(Arbiter* arbiter, std::string scope) : Middleware(arbiter, scope) {
58
	init();
60 59
}
61 60

  
62
MiddlewareROS::~MiddlewareROS(){
63

  
61
MiddlewareROS::~MiddlewareROS() {
64 62
}
65 63

  
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
        //FIXME: Instead of deciding based on ros::isInitialized() to tick or not,
76
        //we shoud use our own MessageQueue and AsyncSpinner.
77
        tick_necessary = false;
78
    }else{
79
        printf("> ROS not initialized, calling ros::init()\n");
80
        ros::M_string no_remapping;
81
        ros::init(no_remapping, node_name);
82
        tick_necessary = true;
83
    }
84

  
85
    //create node handle
86
    //printf("> creating ros node handle\n");
87
    //ros::NodeHa
88
    ros::NodeHandle n;
89

  
90
    printf("> setting up ROS services...\n");
91
    //ros::AsyncSpinner *spinner = new ros::AsyncSpinner(4); // Use 4 threads
92
    //spinner->start();
93

  
94
    tfListener.reset(new tf2_ros::TransformListener(tfBuffer));
95
    tfBuffer.clear();
96

  
97
    animation_action_server = new AnimationCallbackWrapper(this, scope, "animation");
98
    utterance_action_server = new UtteranceCallbackWrapper(this, scope, "utterance");
99
    current_emotion_action_server = new EmotionCallbackWrapper(this, scope, "currentEmotion");
100
    default_emotion_action_server = new EmotionCallbackWrapper(this, scope, "defaultEmotion");
101
    gaze_action_server = new GazeCallbackWrapper(this, scope, "gaze");
102
    lookat_action_server = new LookatCallbackWrapper(this, scope, "lookat", tfBuffer);
103
    mouth_action_server = new MouthCallbackWrapper(this, scope, "mouth");
104
    speech_action_server = new SpeechCallbackWrapper(this, scope, "speech");
105

  
106
    //create tts client
107
    tts_ac = create_action_client<hlrc_server::ttsAction>(base_scope + "/tts_provider");
108
    if (tts_ac == NULL){
109
        printf("> tts action service not available, will not do any TTS !\n");
110
    }
111

  
112
    printf("> init done\n");
64
void MiddlewareROS::init() {
65
	string scope = base_scope + "/set/";
66

  
67
	string node_name = "hlrc_server__" + base_scope;
68
	node_name.erase(boost::remove_if(node_name, boost::is_any_of("/")), node_name.end());
69

  
70
	printf("> registering on ROS as node %s\n", node_name.c_str());
71
	if (ros::isInitialized()) {
72
		// ros is already active, no need to take care of that
73
		// FIXME: Instead of deciding based on ros::isInitialized() to tick or not,
74
		// we shoud use our own MessageQueue and AsyncSpinner.
75
		tick_necessary = false;
76
	}
77
	else {
78
		printf("> ROS not initialized, calling ros::init()\n");
79
		ros::M_string no_remapping;
80
		ros::init(no_remapping, node_name);
81
		tick_necessary = true;
82
	}
83

  
84
	// create node handle
85
	// printf("> creating ros node handle\n");
86
	// ros::NodeHa
87
	ros::NodeHandle n;
88

  
89
	printf("> setting up ROS services...\n");
90
	// ros::AsyncSpinner *spinner = new ros::AsyncSpinner(4); // Use 4 threads
91
	// spinner->start();
92

  
93
	tfListener.reset(new tf2_ros::TransformListener(tfBuffer));
94
	tfBuffer.clear();
95

  
96
	animation_action_server = new AnimationCallbackWrapper(this, scope, "animation");
97
	utterance_action_server = new UtteranceCallbackWrapper(this, scope, "utterance");
98
	current_emotion_action_server = new EmotionCallbackWrapper(this, scope, "currentEmotion");
99
	default_emotion_action_server = new EmotionCallbackWrapper(this, scope, "defaultEmotion");
100
	gaze_action_server = new GazeCallbackWrapper(this, scope, "gaze");
101
	lookat_action_server = new LookatCallbackWrapper(this, scope, "lookat", tfBuffer);
102
	mouth_action_server = new MouthCallbackWrapper(this, scope, "mouth");
103
	speech_action_server = new SpeechCallbackWrapper(this, scope, "speech");
104

  
105
	// create tts client
106
	tts_ac = create_action_client<hlrc_server::ttsAction>(base_scope + "/tts_provider");
107
	if (tts_ac == NULL) {
108
		printf("> tts action service not available, will not do any TTS !\n");
109
	}
110

  
111
	printf("> init done\n");
113 112
}
114 113

  
115
void MiddlewareROS::tick(){
116
    if (tick_necessary){
117
        ros::spinOnce();
118
    }
114
void MiddlewareROS::tick() {
115
	if (tick_necessary) {
116
		ros::spinOnce();
117
	}
119 118
}
120 119

  
121
//call a tts system to convert a string to an utterance
122
boost::shared_ptr<Utterance> MiddlewareROS::tts_call(string text){
123
    boost::shared_ptr<Utterance> utterance(new Utterance());
120
// call a tts system to convert a string to an utterance
121
boost::shared_ptr<Utterance> MiddlewareROS::tts_call(string text) {
122
	boost::shared_ptr<Utterance> utterance(new Utterance());
124 123

  
125
    //double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
126
    if (tts_ac == NULL){
127
        printf("> tts action service not available. will not speak '%s'\n", text.c_str());
128
        return utterance;
129
    }
124
	// double tts_timeout = 1.0; //seconds. DO NOT CHANGE THIS!
125
	if (tts_ac == NULL) {
126
		printf("> tts action service not available. will not speak '%s'\n", text.c_str());
127
		return utterance;
128
	}
130 129

  
131
    hlrc_server::ttsGoal goal;
130
	hlrc_server::ttsGoal goal;
132 131

  
133
    goal.text = text;
132
	goal.text = text;
134 133

  
135
    //send
136
    tts_ac->sendGoal(goal);
134
	// send
135
	tts_ac->sendGoal(goal);
137 136

  
138
    //call ros:
139
    bool finished_before_timeout = tts_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
137
	// call ros:
138
	bool finished_before_timeout = tts_ac->waitForResult(ros::Duration(ROS_ACTION_CALL_TIMEOUT));
140 139

  
141
    if (!finished_before_timeout){
142
        printf("> ERROR: NO REPLY to utterance action call received within %4.2f s\n",ROS_ACTION_CALL_TIMEOUT);
143
    }else{
144
        //done, return utterance ptr
145
        ttsResultConstPtr tts_res = tts_ac->getResult();
146
        boost::shared_ptr<Utterance> utterance(new UtteranceROS(tts_res));
147
        printf("> done. got utterance (text=%s)\n",utterance->get_text().c_str());
148
        return utterance;
149
    }
140
	if (!finished_before_timeout) {
141
		printf("> ERROR: NO REPLY to utterance action call received within %4.2f s\n", ROS_ACTION_CALL_TIMEOUT);
142
	}
143
	else {
144
		// done, return utterance ptr
145
		ttsResultConstPtr tts_res = tts_ac->getResult();
146
		boost::shared_ptr<Utterance> utterance(new UtteranceROS(tts_res));
147
		printf("> done. got utterance (text=%s)\n", utterance->get_text().c_str());
148
		return utterance;
149
	}
150 150

  
151
    printf("> failed... got no utterance\n");
152
    return utterance;
151
	printf("> failed... got no utterance\n");
152
	return utterance;
153 153
}
154 154

  
155

  
156 155
#endif

Also available in: Unified diff