Revision d7c8e3b7

View differences:

client/java/pom.xml
1 1

  
2 2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
    <modelVersion>4.0.0</modelVersion>
4
    <groupId>de.citec.csra</groupId>
5
    <artifactId>hlrc-client</artifactId>
6
    <version>0.2</version>
7
    <packaging>jar</packaging>
8
    <name>High Level Robot Control java client</name>
9
    <url>http://maven.apache.org</url>
10
    <build>
11
        <plugins>
12
            <plugin>
13
                <groupId>org.apache.maven.plugins</groupId>
14
                <artifactId>maven-compiler-plugin</artifactId>
15
                <version>3.1</version>
16
                <configuration>
17
                    <source>1.7</source>
18
                    <target>1.7</target>
19
                    <showDeprecation>true</showDeprecation>
20
                </configuration>
21
            </plugin>
22
            <plugin>
23
                <groupId>org.codehaus.mojo</groupId>
24
                <artifactId>appassembler-maven-plugin</artifactId>
25
                <version>1.8</version>
26
                <configuration>
27
                    <programs>
28
                        <program>
29
                            <mainClass>de.uni-bielefeld.cit-ec.opensource.hlrc.main.Cmd</mainClass>
30
                            <id>hlrc-client</id>
3
	<modelVersion>4.0.0</modelVersion>
4
	<groupId>de.cit_ec.opensource.hlrc</groupId>
5
	<artifactId>hlrc-client</artifactId>
6
	<version>0.2</version>
7
	<packaging>jar</packaging>
8
	<name>High Level Robot Control java client</name>
9
	<url>http://maven.apache.org</url>
10
	<build>
11
		<plugins>
12
			<plugin>
13
				<groupId>org.apache.maven.plugins</groupId>
14
				<artifactId>maven-compiler-plugin</artifactId>
15
				<version>3.1</version>
16
				<configuration>
17
					<source>1.7</source>
18
					<target>1.7</target>
19
					<showDeprecation>true</showDeprecation>
20
				</configuration>
21
			</plugin>
22
			<plugin>
23
				<groupId>org.codehaus.mojo</groupId>
24
				<artifactId>appassembler-maven-plugin</artifactId>
25
				<version>1.8</version>
26
				<configuration>
27
					<programs>
28
						<program>
29
							<mainClass>de.cit_ec.opensource.hlrc.main.Cmd</mainClass>
30
							<id>hlrc-client</id>
31 31
                            
32
                        </program>
33
                    </programs>
34
                    <!--                    <assembleDirectory>${project.build.directory}/dist</assembleDirectory>
35
                    <repositoryName>lib</repositoryName>
36
                    <repositoryLayout>flat</repositoryLayout>-->
37
                </configuration>
38
                <executions>
39
                    <execution>
40
                        <id>assemble</id>
41
                        <goals>
42
                            <goal>assemble</goal>
43
                        </goals>
44
                    </execution>
45
                </executions>
46
            </plugin>
47
        </plugins>
48
    </build>
49
    <licenses>
50
        <license>
51
            <name>GPLv3</name>
52
            <url>http://www.gnu.org/licenses/gpl.html</url>
53
        </license>
54
    </licenses>
55
    <organization>
56
        <name>Citec, Bielefeld University</name>
57
        <url>http://www.cit-ec.de/</url>
58
    </organization>
59
    <developers>
60
        <developer>
61
            <id>flier</id>
62
            <name>Florian Lier</name>
63
            <email>flier@techfak.uni-bielefeld.de</email>
64
            <url>https://www.cit-ec.de/de/users/flier</url>
65
            <roles>
66
                <role>architect</role>
67
                <role>developer</role>
68
            </roles>
69
            <timezone>+1</timezone>
70
        </developer>
71
    </developers>
72
    <properties>
73
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
74
    </properties>
75
    <dependencies>
76
        <dependency>
77
            <groupId>org.slf4j</groupId>
78
            <artifactId>slf4j-api</artifactId>
79
            <version>1.7.7</version>
80
        </dependency>
81
        <dependency>
82
            <groupId>org.slf4j</groupId>
83
            <artifactId>slf4j-simple</artifactId>
84
            <version>1.7.7</version>
85
        </dependency>            
86
        <dependency>
87
            <groupId>junit</groupId>
88
            <artifactId>junit</artifactId>
89
            <version>4.11</version>
90
            <scope>test</scope>
91
        </dependency>
92
        <dependency>
93
            <groupId>rsb</groupId>
94
            <artifactId>rsb</artifactId>
95
            <version>[0.15.0,)</version>
96
        </dependency>
97
        <dependency>
98
            <groupId>rsb</groupId>
99
            <artifactId>rst</artifactId>
100
            <version>[0.15.0,)</version>
101
        </dependency>
102
        <dependency>
103
            <groupId>commons-cli</groupId>
104
            <artifactId>commons-cli</artifactId>
105
            <version>1.2</version>
106
        </dependency>
107
	<dependency>
108
            <groupId>commons-lang</groupId>
109
            <artifactId>commons-lang</artifactId>
110
            <version>2.6</version>
111
        </dependency>
112
    </dependencies>
113
    <distributionManagement>
114
        <repository>
115
            <id>citec</id>
116
            <name>CITEC Maven Repository Server</name>
117
            <url>https://repo.cit-ec.uni-bielefeld.de/content/repositories/releases/</url>
118
            <layout>default</layout>
119
        </repository>
120
    </distributionManagement>
32
						</program>
33
					</programs>
34
					<!--                    <assembleDirectory>${project.build.directory}/dist</assembleDirectory>
35
					<repositoryName>lib</repositoryName>
36
					<repositoryLayout>flat</repositoryLayout>-->
37
				</configuration>
38
				<executions>
39
					<execution>
40
						<id>assemble</id>
41
						<goals>
42
							<goal>assemble</goal>
43
						</goals>
44
					</execution>
45
				</executions>
46
			</plugin>
47
		</plugins>
48
	</build>
49
	<licenses>
50
		<license>
51
			<name>GPLv3</name>
52
			<url>http://www.gnu.org/licenses/gpl.html</url>
53
		</license>
54
	</licenses>
55
	<organization>
56
		<name>Citec, Bielefeld University</name>
57
		<url>http://www.cit-ec.de/</url>
58
	</organization>
59
	<developers>
60
		<developer>
61
			<id>flier</id>
62
			<name>Florian Lier</name>
63
			<email>flier@techfak.uni-bielefeld.de</email>
64
			<url>https://www.cit-ec.de/de/users/flier</url>
65
			<roles>
66
				<role>architect</role>
67
				<role>developer</role>
68
			</roles>
69
			<timezone>+1</timezone>
70
		</developer>
71
	</developers>
72
	<properties>
73
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
74
	</properties>
75
	<dependencies>
76
		<dependency>
77
			<groupId>org.slf4j</groupId>
78
			<artifactId>slf4j-api</artifactId>
79
			<version>1.7.7</version>
80
		</dependency>
81
		<dependency>
82
			<groupId>org.slf4j</groupId>
83
			<artifactId>slf4j-simple</artifactId>
84
			<version>1.7.7</version>
85
		</dependency>            
86
		<dependency>
87
			<groupId>junit</groupId>
88
			<artifactId>junit</artifactId>
89
			<version>4.11</version>
90
			<scope>test</scope>
91
		</dependency>
92
		<dependency>
93
			<groupId>rsb</groupId>
94
			<artifactId>rsb</artifactId>
95
			<version>[0.15.0,)</version>
96
		</dependency>
97
		<dependency>
98
			<groupId>rsb</groupId>
99
			<artifactId>rst</artifactId>
100
			<version>[0.15.0,)</version>
101
		</dependency>
102
		<dependency>
103
			<groupId>commons-cli</groupId>
104
			<artifactId>commons-cli</artifactId>
105
			<version>1.2</version>
106
		</dependency>
107
		<dependency>
108
			<groupId>commons-lang</groupId>
109
			<artifactId>commons-lang</artifactId>
110
			<version>2.6</version>
111
		</dependency>
112
	</dependencies>
113
	<repositories>
114
		<repository>
115
			<id>citec-releases</id>
116
			<name>CITEC Maven Repository Server</name>
117
			<url>https://mvn.cit-ec.de/nexus/content/repositories/releases/</url>
118
			<layout>default</layout>
119
			<releases>
120
				<enabled>true</enabled>
121
			</releases>
122
		</repository>
123
	</repositories>
121 124
</project>
client/java/src/main/java/de/cit_ec/opensource/hlrc/client/Emotions.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6

  
7
package de.cit_ec.opensource.hlrc.client;
8

  
9
public enum Emotions {
10
    NEUTRAL,
11
    HAPPY,	
12
    SAD,	
13
    ANGRY,
14
    SURPRISED,	
15
    FEAR
16
}
client/java/src/main/java/de/cit_ec/opensource/hlrc/client/HeadAnimations.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6

  
7
// TODO: Rename/refactor this package to somthing more self explantory
8

  
9
package de.cit_ec.opensource.hlrc.client;
10

  
11
public enum HeadAnimations {
12
    IDLE,
13
    HEAD_NOD,
14
    HEAD_SHAKE,
15
    EYEBLINK_L,
16
    EYEBLINK_R,
17
    EYEBLINK_BOTH,
18
    EYEBROWS_RAISE,
19
    EYEBROWS_LOWER
20
}
client/java/src/main/java/de/cit_ec/opensource/hlrc/client/MiddlewareRSB.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6
package de.cit_ec.opensource.hlrc.client;
7

  
8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.Map;
11
import java.util.concurrent.ExecutionException;
12
import java.util.concurrent.TimeoutException;
13
import java.util.logging.Level;
14
import java.util.logging.Logger;
15

  
16
import javax.sound.sampled.AudioFormat;
17
import javax.sound.sampled.AudioFormat.Encoding;
18

  
19
import rsb.RSBException;
20
import rsb.converter.DefaultConverterRepository;
21
import rsb.converter.ProtocolBufferConverter;
22
import rsb.patterns.RemoteServer;
23

  
24
import rst.audition.SoundChunkType.SoundChunk;
25
import rst.audition.UtteranceType.Utterance;
26
import rst.animation.HeadAnimationType.HeadAnimation;
27
import rst.animation.EmotionExpressionType.EmotionExpression;
28
import rst.animation.BinocularHeadGazeType.BinocularHeadGaze;
29

  
30
import com.google.protobuf.ByteString;
31

  
32
// Create _abstract_ middleware class and function _interface_ for 
33
// future integration of additional Middleware implementations...
34
import rsb.Factory;
35
import rsb.Informer;
36
import rst.animation.EmotionExpressionType.EmotionExpression.Emotion;
37
import rst.animation.HeadAnimationType.HeadAnimation.Animation;
38
import rst.audition.PhonemeCollectionType.PhonemeCollection;
39
import rst.audition.PhonemeType.Phoneme;
40
import rst.geometry.SphericalDirectionFloatType.SphericalDirectionFloat;
41

  
42
public class MiddlewareRSB {
43

  
44
    final RemoteServer server;
45
    final String robotname;
46
    static Map<Emotions, Emotion> emo_map;
47
    static Map<HeadAnimations, Animation> han_map;
48

  
49
    public class RSBInformer {
50

  
51
        final Factory factory;
52
        final Informer<Object> informer;
53

  
54
        public RSBInformer(String scope) throws Throwable {
55

  
56
            // Get a factory instance to create RSB objects.
57
            factory = Factory.getInstance();
58

  
59
            // Create an informer on scope "/exmaple/informer".
60
            informer = factory.createInformer(scope);
61

  
62
            // Activate the informer to be ready for work
63
            informer.activate();
64

  
65
        }
66

  
67
        public void pushMsg() {
68
            try {
69
                // Send and event using a method that accepts the data and
70
                // automatically creates an appropriate event internally.
71
                this.informer.publish("TODO");
72
            } catch (RSBException ex) {
73
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
74
            }
75
        }
76

  
77
        public void closeInf() throws InterruptedException {
78
            try {
79
                // As there is no explicit removal model in java, always manually
80
                // deactivate the informer if it is not needed anymore
81
                this.informer.deactivate();
82
            } catch (RSBException ex) {
83
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
84
            }
85
        }
86

  
87
    }
88

  
89
    @SuppressWarnings("unchecked")
90
    public MiddlewareRSB(String robotname) {
91

  
92
        this.robotname = robotname;
93

  
94
        server = Factory.getInstance().createRemoteServer(this.robotname + "/set/");
95
        System.out.println("--> Robot: Talking to " + this.robotname);
96
        try {
97
            server.activate();
98
        } catch (RSBException ex) {
99
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
100
        }
101

  
102
        {
103
            final ProtocolBufferConverter<EmotionExpression> converter = new ProtocolBufferConverter<>(EmotionExpression.getDefaultInstance());
104
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
105
        }
106
        {
107
            final ProtocolBufferConverter<BinocularHeadGaze> converter = new ProtocolBufferConverter<>(BinocularHeadGaze.getDefaultInstance());
108
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
109
        }
110
        {
111
            final ProtocolBufferConverter<HeadAnimation> converter = new ProtocolBufferConverter<>(HeadAnimation.getDefaultInstance());
112
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
113
        }
114
        {
115
            final ProtocolBufferConverter<Utterance> converter = new ProtocolBufferConverter<>(Utterance.getDefaultInstance());
116
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
117
        }
118
        {
119
            final ProtocolBufferConverter<SoundChunk> converter = new ProtocolBufferConverter<>(SoundChunk.getDefaultInstance());
120
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
121
        }
122
        {
123
            final ProtocolBufferConverter<Phoneme> converter = new ProtocolBufferConverter<>(Phoneme.getDefaultInstance());
124
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
125
        }
126

  
127
        emo_map = new HashMap();
128
        emo_map.put(Emotions.SAD, Emotion.SAD);
129
        emo_map.put(Emotions.ANGRY, Emotion.ANGRY);
130
        emo_map.put(Emotions.FEAR, Emotion.FEAR);
131
        emo_map.put(Emotions.NEUTRAL, Emotion.NEUTRAL);
132
        emo_map.put(Emotions.SURPRISED, Emotion.SURPRISED);
133
        emo_map.put(Emotions.HAPPY, Emotion.HAPPY);
134

  
135
        han_map = new HashMap();
136
        han_map.put(HeadAnimations.HEAD_NOD, Animation.HEAD_NOD);
137
        han_map.put(HeadAnimations.HEAD_SHAKE, Animation.HEAD_SHAKE);
138
        han_map.put(HeadAnimations.IDLE, Animation.IDLE);
139
        han_map.put(HeadAnimations.EYEBLINK_R, Animation.EYEBLINK_RIGHT);
140
        han_map.put(HeadAnimations.EYEBLINK_L, Animation.EYEBLINK_LEFT);
141
        han_map.put(HeadAnimations.EYEBLINK_BOTH, Animation.EYEBLINK_BOTH);
142
        han_map.put(HeadAnimations.EYEBROWS_LOWER, Animation.EYEBROWS_LOWER);
143
        han_map.put(HeadAnimations.EYEBROWS_RAISE, Animation.EYEBROWS_RAISE);
144
    }
145

  
146
    private SoundChunk compileSoundChunk(AudioFormat audio, byte[] data) {
147
        SoundChunk.Builder sc = SoundChunk.newBuilder();
148
        ByteString bs = ByteString.copyFrom(data);
149
        boolean isbig = audio.isBigEndian();
150
        sc.setChannels(audio.getChannels());
151
        sc.setData(bs);
152
        sc.setRate((int) audio.getSampleRate());
153

  
154
        if (isbig) {
155
            sc.setEndianness(SoundChunk.EndianNess.ENDIAN_BIG);
156
        } else {
157
            sc.setEndianness(SoundChunk.EndianNess.ENDIAN_LITTLE);
158
        }
159

  
160
        sc.setSampleCount(data.length / audio.getFrameSize());
161
        Encoding enc = audio.getEncoding();
162

  
163
        if (enc.equals(Encoding.PCM_SIGNED)) {
164
            int size = audio.getSampleSizeInBits();
165
            if (size == 8) {
166
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S8);
167
            } else if (size == 16) {
168
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S16);
169
            } else if (size == 24) {
170
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S24);
171
            } else {
172
                System.err.println("Audio Sample Size Not Valid");
173
            }
174
        } else if (enc.equals(Encoding.PCM_UNSIGNED)) {
175
            int size = audio.getSampleSizeInBits();
176
            if (size == 8) {
177
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U8);
178
            } else if (size == 16) {
179
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U16);
180
            } else if (size == 24) {
181
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U24);
182
            } else {
183
                System.err.println("Audio Sample Size Not Valid");
184
                // This is too rigorous, maybe.
185
                // System.exit(-1);
186
            }
187
        } else {
188
            System.err.println("Audio Encoding is invalid");
189
        }
190
        return sc.build();
191
    }
192

  
193
    public void deactivateServer() throws InterruptedException {
194
        try {
195
            this.server.deactivate();
196
        } catch (RSBException ex) {
197
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
198
        }
199
    }
200

  
201
    // OBSOLETE
202
    /*
203
     public MouthAnimation createMouthAnimationMessage(int duration, boolean active) {
204
     MouthAnimation.Builder ma = MouthAnimation.newBuilder();
205
     ma.setDuration(duration);
206
     ma.setActive(active);
207
     return ma.build();
208
     }
209
     */
210
    public Utterance createSetSpeak(SoundChunk soundchunck, ArrayList tuList, String text) {
211
        Utterance.Builder ut = Utterance.newBuilder();
212
        ut.setAudio(soundchunck);
213
        Phoneme.Builder ph = Phoneme.newBuilder();
214
        PhonemeCollection.Builder phCollection = PhonemeCollection.newBuilder();
215

  
216
        for (Object tuList1 : tuList) {
217
            Tuple t = (Tuple) tuList1;
218
            ph.setSymbol(t.s);
219
            ph.setDuration(t.d);
220
            phCollection.addElement(ph.build());
221
        }
222
        ut.setPhonemes(phCollection);
223
        ut.setTextualRepresentation(text);
224
        return ut.build();
225
    }
226

  
227
    public HeadAnimation createHeadAnimationMessage(HeadAnimations ani, int duration, int repetition, float scale) {
228
        HeadAnimation.Builder ha = HeadAnimation.newBuilder();
229
        ha.setRepetitions(repetition);
230
        ha.setAnimation(han_map.get(ani));
231
        ha.setDurationEach(duration);
232
        ha.setEmphasisScale(scale);
233
        return ha.build();
234
    }
235

  
236
    public EmotionExpression createEmotionMessage(Emotions emo, int duration) {
237
        EmotionExpression.Builder es = EmotionExpression.newBuilder();
238
        es.setDuration(duration);
239
        es.setEmotion(emo_map.get(emo));
240
        return es.build();
241
    }
242

  
243
    public EmotionExpression createDefaultEmotionMessage(Emotions emo) {
244
        EmotionExpression.Builder es = EmotionExpression.newBuilder();
245
        es.setEmotion(emo_map.get(emo));
246
        return es.build();
247
    }
248

  
249
    public BinocularHeadGaze createGazeTargetMessage(float pan, float tilt, float offsetPan, float offsetTilt) {
250
        BinocularHeadGaze.Builder gt = BinocularHeadGaze.newBuilder();
251
        SphericalDirectionFloat.Builder spBuilder = SphericalDirectionFloat.newBuilder();
252
        spBuilder.setAzimuth(pan);
253
        spBuilder.setElevation(tilt);
254
        gt.setEyeVergence(0);
255
        gt.setTarget(spBuilder.build());
256
        spBuilder.setAzimuth(offsetTilt);
257
        spBuilder.setElevation(offsetPan);
258
        gt.setOffset(spBuilder.build());
259
        return gt.build();
260
    }
261

  
262
    public BinocularHeadGaze createGazeTargetMessage(float pan, float tilt) {
263
        return createGazeTargetMessage(pan, tilt, 0, 0);
264
    }
265

  
266
    public void setSpeak(AudioFormat audio, byte[] bs, ArrayList tu, String text, boolean blocking) throws ExecutionException, TimeoutException {
267
        SoundChunk sc;
268
        sc = compileSoundChunk(audio, bs);
269
        if (blocking) {
270
            try {
271
                server.call("utterance", createSetSpeak(sc, tu, text));
272
                // System.out.println("Server replied: " + server.call("utterance", createSetSpeak(sc, tu)));
273
            } catch (RSBException ex) {
274
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
275
            } catch (InterruptedException ex) {
276
                Thread.currentThread().interrupt();
277
            }
278
        } else {
279
            try {
280
                server.callAsync("utterance", createSetSpeak(sc, tu, text));
281
                // System.out.println("Set Mouth Animation non-blocking, not wating for reply");
282
            } catch (RSBException ex) {
283
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
284
            }
285
        }
286
    }
287

  
288
    public void setEmotion(Emotions emo, int duration, boolean blocking) throws ExecutionException, TimeoutException {
289
        try {
290
            if (blocking) {
291
                server.call("emotion", createEmotionMessage(emo, duration));
292
                // System.out.println("Server replied: " + server.call("emotion", createEmotionMessage(emo, duration)));
293
            } else {
294
                server.callAsync("emotion", createEmotionMessage(emo, duration));
295
                // System.out.println("Set Emotion non-blocking, not wating for reply");
296
            }
297

  
298
        } catch (RSBException ex) {
299
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
300
        } catch (InterruptedException ex) {
301
            Thread.currentThread().interrupt();
302
        }
303
    }
304

  
305
    public void setGazeTarget(float pan, float tilt, boolean blocking) throws ExecutionException, TimeoutException {
306
        try {
307
            if (blocking) {
308
                server.call("gaze", createGazeTargetMessage(pan, tilt));
309
                // System.out.println("Server replied: " + server.call("gaze", createGazeTargetMessage(pan, tilt, roll)));
310
            } else {
311
                server.callAsync("gaze", createGazeTargetMessage(pan, tilt));
312
                // System.out.println("Set Gaze non-blocking, not wating for reply");
313
            }
314
        } catch (RSBException ex) {
315
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
316
        } catch (InterruptedException ex) {
317
            Thread.currentThread().interrupt();
318
        }
319
    }
320

  
321
    public void setGazeTarget(float pan, float tilt, float offsetPan, float offsetTilt, boolean blocking) throws ExecutionException, TimeoutException {
322
        try {
323
            if (blocking) {
324
                server.call("gaze", createGazeTargetMessage(pan, tilt, offsetPan, offsetTilt));
325
                // System.out.println("Server replied: " + server.call("gaze", createGazeTargetMessage(pan, tilt, roll)));
326
            } else {
327
                server.callAsync("gaze", createGazeTargetMessage(pan, tilt, offsetPan, offsetTilt));
328
                // System.out.println("Set Gaze non-blocking, not wating for reply");
329
            }
330
        } catch (RSBException ex) {
331
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
332
        } catch (InterruptedException ex) {
333
            Thread.currentThread().interrupt();
334
        }
335
    }
336

  
337
    public void setDefaultEmotion(Emotions emo, boolean blocking) throws ExecutionException, TimeoutException {
338
        try {
339
            if (blocking) {
340
                server.call("defaultEmotion", createDefaultEmotionMessage(emo));
341
                // System.out.println("Server replied: " + server.call("defaultEmotion", createDefaultEmotionMessage(emo)));
342
            } else {
343
                server.callAsync("defaultEmotion", createDefaultEmotionMessage(emo));
344
                // System.out.println("Set Default Emotion non-blocking, not wating for reply");
345
            }
346
        } catch (RSBException ex) {
347
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
348
        } catch (InterruptedException ex) {
349
            Thread.currentThread().interrupt();
350
        }
351
    }
352

  
353
    public void setHeadAnimation(HeadAnimations ani, int duration, int repetitions, float scale, boolean blocking) throws ExecutionException, TimeoutException {
354
        try {
355
            if (blocking) {
356
                server.call("animation", createHeadAnimationMessage(ani, duration, repetitions, scale));
357
                // System.out.println("Server replied: " + server.call("animation", createHeadAnimationMessage(ani, duration, repetitions, scale)));
358
            } else {
359
                server.callAsync("animation", createHeadAnimationMessage(ani, duration, repetitions, scale));
360
                // System.out.println("Set Head Animation non-blocking, not wating for reply");
361
            }
362
        } catch (RSBException ex) {
363
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
364
        } catch (InterruptedException ex) {
365
               Thread.currentThread().interrupt();
366
            }
367
    }
368
}
client/java/src/main/java/de/cit_ec/opensource/hlrc/client/Robot.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6

  
7
package de.cit_ec.opensource.hlrc.client;
8

  
9
import java.util.ArrayList;
10
import java.util.concurrent.ExecutionException;
11
import java.util.concurrent.TimeoutException;
12
import java.util.logging.Level;
13
import java.util.logging.Logger;
14
import javax.sound.sampled.AudioFormat;
15

  
16
public class Robot {
17

  
18
    private final String name;
19
    private final String middlwarename;
20
    public final MiddlewareRSB mw;
21

  
22
    
23
    /**
24
    * 
25
    * Create a new Robot
26
    * 
27
    * @param name String robot name i.e. "/home/wardrobe/flobi", "/home/kitchen/flobi"
28
    * @param middleware String what Middleware Layer to use
29
    *
30
    */
31
    
32
    // TODO: Make the Robot Class a singleton
33
    public Robot(String name, String middleware) {
34
        this.name = name;
35
        this.middlwarename = middleware;
36
        if (middleware.toUpperCase().equals("RSB")) {
37
            System.out.println("--> New Robot: "+name+" using middleware: "+middleware+ " has been initialized");
38
            this.mw = new MiddlewareRSB(this.name);
39
        } else {
40
            System.out.println("Only RSB Supported for now. Sorry.");
41
            this.mw = new MiddlewareRSB(this.name);
42
        }
43
    }
44

  
45
    /**
46
    * 
47
    * Start the Robot's mouth animation 
48
    * 
49
    * @param audio Java std auto format
50
    * @param bytes Byte Array containing the actual audio stream
51
    * @param tuList A list of Tuple phoneme plus duration 
52
    * @param blocking invoke the command blocking or non blocking TRUE/FALSE
53
    *
54
    */
55

  
56
    public void setSpeak(AudioFormat audio, byte[] bytes, ArrayList tuList, String text, boolean blocking) {
57
        try {
58
            // System.out.println(">> Sending Set Speak: Blocking: " + blocking);
59
            mw.setSpeak(audio, bytes, tuList, text, blocking);
60
        } catch (ExecutionException | TimeoutException ex) {
61
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
62
        }
63
        
64
    }
65
    
66
    /**
67
    * 
68
    * Start the Robot's mouth animation 
69
    * 
70
    * @param  emo      set the desired EMOTION
71
    * @param  duration the length of the animation in MILLISECONDS
72
    * @param  blocking invoke the command blocking or non blocking TRUE/FALSE
73
    *
74
    */
75
    public void setEmotion(Emotions emo, int duration, boolean blocking) {
76
        try {
77
            // System.out.println(">> Sending Emotion: " + emo + " / duration: " + duration + " / Blocking: " + blocking);
78
            mw.setEmotion(emo, duration, blocking);
79
        } catch (ExecutionException | TimeoutException ex) {
80
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
81
        }
82
    }
83

  
84
    public void setGazeTarget(float pan, float tilt,  boolean blocking) {
85
        try {
86
            // System.out.println(">> Sending Gaze: pan|tilt|roll: " + pan + " / " + tilt + " / " + roll + " / Blocking: " + blocking);
87
            mw.setGazeTarget(pan, tilt, blocking);
88
        } catch (ExecutionException | TimeoutException ex) {
89
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
90
        }
91
    }
92
    
93
     public void setGazeTarget(float pan, float tilt, float offsetPan, float offsetTilt,  boolean blocking) {
94
        try {
95
            // System.out.println(">> Sending Gaze: pan|tilt|roll: " + pan + " / " + tilt + " / " + roll + " / Blocking: " + blocking);
96
            mw.setGazeTarget(pan, tilt, blocking);
97
        } catch (ExecutionException | TimeoutException ex) {
98
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
99
        }
100
    }
101

  
102
    public void setDefaultEmotion(Emotions emo, boolean blocking) {
103
        try {
104
            // System.out.println(">> Sending Default Emotion: " + emo + " / Blocking: " + blocking);
105
            mw.setDefaultEmotion(emo, blocking);
106
        } catch (ExecutionException | TimeoutException ex) {
107
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
108
        }
109
    }
110

  
111
    public void setHeadAnimation(HeadAnimations ani, int duration, int repetitions, float scale, boolean blocking) {
112
        try {
113
            // System.out.println(">> Sending Animation: " + ani + " / Duration: " + duration + " / Repetitions: " + repetitions + " / Blocking: " + blocking);
114
            mw.setHeadAnimation(ani, duration, repetitions, scale, blocking);
115
        } catch (ExecutionException | TimeoutException ex) {
116
            Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
117
        }
118
    }
119

  
120
    public void getMouthAnimation() {
121
        System.out.println("Get Mouth Animation - Not implemented.");
122
    }
123

  
124
    public void getEmotion() {
125
        System.out.println("Get Emotion - Not implemented.");
126
    }
127

  
128
    public void getGazeTarget() {
129
        System.out.println("Get Gaze Target - Not implemented");
130
    }
131

  
132
    public void getDefaultEmotion() {
133
        System.out.println("Get Default Emotion - Not implemented");
134
    }
135

  
136
    public void getHeadAnimation() {
137
        System.out.println("Get Head Animation - Not implemented");
138
    }
139

  
140
    public String getRobotName() {
141
        return this.name;
142
    }
143
    
144
    public String getMiddleware() {
145
        return this.middlwarename;
146
    }
147

  
148
}
client/java/src/main/java/de/cit_ec/opensource/hlrc/client/Tuple.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6

  
7
package de.cit_ec.opensource.hlrc.client;
8

  
9
/**
10
 *
11
 * @param <Symbol>
12
 * @param <double>
13
 * 
14
 */
15
public class Tuple { 
16
  protected final String s; 
17
  protected final int d; 
18
  
19
  public Tuple(String phone, int d) { 
20
    this.s = phone; 
21
    this.d = d; 
22
  } 
23
  
24
} 
client/java/src/main/java/de/cit_ec/opensource/hlrc/examples/SimpleExample.java
1
package de.cit_ec.opensource.hlrc.examples;
2

  
3
import de.cit_ec.opensource.hlrc.client.Emotions;
4
import de.cit_ec.opensource.hlrc.client.Robot;
5

  
6
/**
7
 *
8
 * @author flier
9
 */
10

  
11
public class SimpleExample {
12
    
13
    public SimpleExample(String robotname, String middleware) {
14
        String name = robotname;
15
        String mw = middleware;
16
        // Create a new Robot
17
        Robot r = new Robot(name, mw);
18
        
19
        // Do something HIGH LEVEL with ease!
20
        r.setEmotion(Emotions.SAD, 1000, true);
21
        // r.setGazeTarget(70.0f, 50.0f, 40.0f, true);
22
    }
23

  
24
    public static void main(String [] args) {
25
            SimpleExample se = new SimpleExample("/home/kitchen/flobi", "RSB");
26
    }
27
    
28
}
29

  
client/java/src/main/java/de/cit_ec/opensource/hlrc/main/Cmd.java
1
/**
2
 *
3
 * @author flier@techfak.uni-bielefeld.de
4
 *
5
 */
6

  
7
package de.cit_ec.opensource.hlrc.main;
8

  
9
import de.cit_ec.opensource.hlrc.client.HeadAnimations;
10
import de.cit_ec.opensource.hlrc.client.Emotions;
11
import de.cit_ec.opensource.hlrc.client.Robot;
12

  
13

  
14
import org.apache.commons.cli.BasicParser;
15
import org.apache.commons.cli.CommandLine;
16
import org.apache.commons.cli.CommandLineParser;
17
import org.apache.commons.cli.HelpFormatter;
18
import org.apache.commons.cli.Options;
19
import org.apache.commons.cli.ParseException;
20

  
21
import rsb.RSBException;
22

  
23
public class Cmd {
24

  
25
    public static void main(String[] args) throws RSBException {
26

  
27
        try {
28
            
29
            Robot robot;
30
            CommandLineParser parser = new BasicParser();
31
            CommandLine line;
32
            
33
            Options lvOptions = new Options();
34
            lvOptions.addOption("de", "default",       false, "set the robot's default emotion");
35
            lvOptions.addOption("ga", "gaze",          false, "make the robot look somewhere");
36
            lvOptions.addOption("rg", "resetgaze",     false, "make the robot look towards you");
37
            lvOptions.addOption("sa", "sad",           false, "make robot look sad");
38
            lvOptions.addOption("ha", "happy",         false, "make robot look happy");
39
            lvOptions.addOption("ne", "neutral",       false, "make the robot look neutral");
40
            lvOptions.addOption("an", "angry",         false, "make the robot look angry");
41
            lvOptions.addOption("su", "surprised",     false, "make the robot look surprised");
42
            lvOptions.addOption("fe", "fear",          false, "make the robot look afraid");
43
            lvOptions.addOption("id", "idle",          false, "make robot look idle");
44
            lvOptions.addOption("no", "nodding",       false, "make the robot nodding his head");
45
            lvOptions.addOption("he", "headshake",     false, "make the robot shake his head");
46
            lvOptions.addOption("er", "eyeblinkright", false, "make the robot's right eye blink");
47
            lvOptions.addOption("el", "eyeblinkleft",  false, "make the robot's left eye blink");
48
            lvOptions.addOption("br", "eyebrowsraise", false, "make the robot's brows raise");
49
            lvOptions.addOption("bl", "eyebrowslower", false, "make the robot's brows lower");
50
            lvOptions.addOption("eb", "eyeblinkboth",  false, "make both robot's eyes blink");
51
            lvOptions.addOption("r",  "robotname",     true,  "set the robot name i.e. /home/kitchen/flobi");
52
            lvOptions.addOption("l",  "length",        true, "set the length for animations in milliseconds");
53
            lvOptions.addOption("re", "repetitions",   true, "set the number of repetitions for a movement");
54
            lvOptions.addOption("s",  "scale",         true, "set the scale for a movement");
55
            lvOptions.addOption("h",  "help",          false, "show help");
56
            
57
            System.out.println("-- High Level Control Client successfully initialized --");
58
            
59
            try {
60
                line = parser.parse(lvOptions, args);
61
            } catch (ParseException ex) {
62
                // Logger.getLogger(Cmd.class.getName()).log(Level.SEVERE, null, ex);
63
                System.out.println("--> Unrecognized Option. Exiting...");
64
                return;
65
            }
66
            if (line.hasOption("help")) {
67
                HelpFormatter lvFormater = new HelpFormatter();
68
                lvFormater.printHelp("High Level Control Client", lvOptions);
69
                return;
70
            }
71
            
72
            int duration    = 1000;
73
            int repetitions = 1;
74
            float scale     = 1.0f;
75

  
76
            if (line.getOptionValue("length") != null) {
77
                duration = Integer.parseInt(line.getOptionValue("length"));
78
                System.out.println("--> Animation Duration: "+duration+" milliseconds");
79
            } else {
80
                System.out.println("--> Animation Duration Default: "+duration+" milliseconds");
81
            }
82
            
83
            if (line.getOptionValue("scale") != null) {
84
                scale = Float.parseFloat(line.getOptionValue("length"));
85
                System.out.println("--> Animation Scale: "+scale);
86
            } else {
87
                System.out.println("--> Animation Scale Default: "+scale);
88
            }
89
            
90
            if (line.getOptionValue("repetitions") != null) {
91
                repetitions = Integer.parseInt(line.getOptionValue("repetitions"));
92
                System.out.println("--> Animation Repetitions: "+repetitions);
93
            } else {
94
                System.out.println("--> Animation Repetitions Default: "+repetitions);
95
            }
96
            
97
            if (line.getOptionValue("robotname") != null) {
98
                robot = new Robot(line.getOptionValue("robotname"), "RSB");
99
            } else {
100
                System.out.println("--> You need to provide a robot name, see --help");
101
                return;
102
            }
103
            
104
            if (line.hasOption("gaze")) {
105
                robot.setGazeTarget(35.0f, 20.0f,  true);
106
            }else if (line.hasOption("resetgaze")) {
107
                robot.setGazeTarget(0.0f, 0.0f,  true);
108
            }else if (line.hasOption("idle")) {
109
                robot.setHeadAnimation(HeadAnimations.IDLE, duration, repetitions, scale, true);
110
            }else if (line.hasOption("nodding")) {
111
                robot.setHeadAnimation(HeadAnimations.HEAD_NOD, duration, repetitions, scale, true);
112
            }
113
            else if (line.hasOption("headshake")) {
114
                robot.setHeadAnimation(HeadAnimations.HEAD_SHAKE, duration, repetitions, scale, true);
115
            }
116
            else if (line.hasOption("eyeblinkright")) {
117
                robot.setHeadAnimation(HeadAnimations.EYEBLINK_R, duration, repetitions, scale, true);
118
            }
119
            else if (line.hasOption("eyeblinkleft")) {
120
                robot.setHeadAnimation(HeadAnimations.EYEBLINK_L, duration, repetitions, scale, true);
121
            }
122
            else if (line.hasOption("eyeblinkboth")) {
123
                robot.setHeadAnimation(HeadAnimations.EYEBLINK_BOTH, duration, repetitions, scale, true);
124
            }
125
            else if (line.hasOption("eyebrowsraise")) {
126
                robot.setHeadAnimation(HeadAnimations.EYEBROWS_RAISE, duration, repetitions, scale, true);
127
            }
128
            else if (line.hasOption("eyebrowslower")) {
129
                robot.setHeadAnimation(HeadAnimations.EYEBROWS_LOWER, duration, repetitions, scale, true);
130
            }
131
            else if (line.hasOption("default")) {
132
                robot.setDefaultEmotion(Emotions.NEUTRAL, true);
133
            }
134
            else if (line.hasOption("sad")) {
135
                robot.setEmotion(Emotions.SAD, duration, true);
136
            }
137
            else if (line.hasOption("happy")) {
138
                robot.setEmotion(Emotions.HAPPY, duration, true);
139
            }
140
            else if (line.hasOption("neutral")) {
141
                robot.setEmotion(Emotions.NEUTRAL, duration, true);
142
            }
143
            else if (line.hasOption("angry")) {
144
                robot.setEmotion(Emotions.ANGRY, duration, true);
145
            }
146
            else if (line.hasOption("surprised")) {
147
                robot.setEmotion(Emotions.SURPRISED, duration, true);
148
            }
149
            else if (line.hasOption("fear")) {
150
                robot.setEmotion(Emotions.FEAR, duration, true);
151
            }else{
152
                System.out.println("--> Invalid Command "+line.toString());
153
            }
154
            
155
            // This is currently mandatory due to
156
            // the RSB RemoteServer implementation
157
            robot.mw.deactivateServer();
158
        } catch (InterruptedException ex) {
159
            // Logger.getLogger(Cmd.class.getName()).log(Level.SEVERE, null, ex);
160
            System.out.println("Unrecognized Option. Exiting...");
161
        }
162
    }
163
}
client/java/src/main/java/de/uni-bielefeld/cit-ec/opensource/hlrc/client/Emotions.java
1
/*
2
* This file is part of hlrc
3
*
4
* Copyright(c) {flier,sschulz} <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/hlrc
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
package de.uni_bielefeld.cit_ec.opensource.hlrc.client;
30

  
31
public enum Emotions {
32
    NEUTRAL,
33
    HAPPY,	
34
    SAD,	
35
    ANGRY,
36
    SURPRISED,	
37
    FEAR
38
}
client/java/src/main/java/de/uni-bielefeld/cit-ec/opensource/hlrc/client/HeadAnimations.java
1
/*
2
* This file is part of hlrc
3
*
4
* Copyright(c) {flier,sschulz} <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/hlrc
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
package de.uni_bielefeld.cit_ec.opensource.hlrc.client;
30

  
31
public enum HeadAnimations {
32
    IDLE,
33
    HEAD_NOD,
34
    HEAD_SHAKE,
35
    EYEBLINK_L,
36
    EYEBLINK_R,
37
    EYEBLINK_BOTH,
38
    EYEBROWS_RAISE,
39
    EYEBROWS_LOWER
40
}
client/java/src/main/java/de/uni-bielefeld/cit-ec/opensource/hlrc/client/MiddlewareRSB.java
1
/*
2
* This file is part of hlrc
3
*
4
* Copyright(c) {flier,sschulz} <AT> techfak.uni-bielefeld.de
5
* http://opensource.cit-ec.de/projects/hlrc
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
package de.uni_bielefeld.cit_ec.opensource.hlrc.client;
30

  
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.Map;
34
import java.util.concurrent.ExecutionException;
35
import java.util.concurrent.TimeoutException;
36
import java.util.logging.Level;
37
import java.util.logging.Logger;
38

  
39
import javax.sound.sampled.AudioFormat;
40
import javax.sound.sampled.AudioFormat.Encoding;
41

  
42
import rsb.Factory;
43
import rsb.RSBException;
44
import rsb.converter.DefaultConverterRepository;
45
import rsb.converter.ProtocolBufferConverter;
46
import rsb.patterns.RemoteServer;
47

  
48
import rst.audition.PhonemeType.Phoneme;
49
import rst.audition.SoundChunkType.SoundChunk;
50
import rst.audition.UtteranceType.Utterance;
51
import rst.robot.AnimationType.Animation;
52
import rst.robot.AnimationType.Animation.AnimationType_t;
53
import rst.robot.EmotionStateType.EmotionState;
54
import rst.robot.EmotionStateType.EmotionState.EmotionType;
55
import rst.robot.GazeTargetType.GazeTarget;
56

  
57
import com.google.protobuf.ByteString;
58

  
59
// Create _abstract_ middleware class and function _interface_ for 
60
// future integration of additional Middleware implementations...
61

  
62
import rsb.Factory;
63
import rsb.Informer;
64

  
65

  
66
public class MiddlewareRSB {
67

  
68
    final RemoteServer server;
69
    final String robotname;
70
    static Map<Emotions, EmotionType> emo_map;
71
    static Map<HeadAnimations, AnimationType_t> han_map;
72
    
73
    public class RSBInformer {
74

  
75
    final Factory factory;
76
    final Informer<Object> informer;
77
        
78
    public RSBInformer(String scope) throws Throwable {
79

  
80
        // Get a factory instance to create RSB objects.
81
        factory = Factory.getInstance();
82

  
83
        // Create an informer on scope "/exmaple/informer".
84
        informer = factory.createInformer(scope);
85

  
86
        // Activate the informer to be ready for work
87
        informer.activate();
88

  
89
    }
90
    
91
    public void pushMsg(){
92
        try {
93
            // Send and event using a method that accepts the data and
94
            // automatically creates an appropriate event internally.
95
            this.informer.send("TODO");
96
        } catch (RSBException ex) {
97
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
98
        }
99
    }
100
    
101
    public void closeInf() throws InterruptedException {
102
        try {
103
            // As there is no explicit removal model in java, always manually
104
            // deactivate the informer if it is not needed anymore
105
            this.informer.deactivate();
106
        } catch (RSBException ex) {
107
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
108
        }
109
    }
110
    
111
}
112
    
113
    
114
    @SuppressWarnings("unchecked")
115
	public MiddlewareRSB(String robotname) {
116
        
117
        this.robotname = robotname;
118
            
119
        server = Factory.getInstance().createRemoteServer(this.robotname+"/set/");
120
        System.out.println("--> Robot: Talking to "+this.robotname);
121
        try {
122
            server.activate();
123
        } catch (RSBException ex) {
124
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
125
        }
126

  
127
        {
128
            final ProtocolBufferConverter<EmotionState> converter = new ProtocolBufferConverter<>(EmotionState.getDefaultInstance());
129
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
130
        }
131
        {
132
            final ProtocolBufferConverter<GazeTarget> converter = new ProtocolBufferConverter<>(GazeTarget.getDefaultInstance());
133
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
134
        }
135
        {
136
            final ProtocolBufferConverter<Animation> converter = new ProtocolBufferConverter<>(Animation.getDefaultInstance());
137
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
138
        }
139
        {
140
            final ProtocolBufferConverter<Utterance> converter = new ProtocolBufferConverter<>(Utterance.getDefaultInstance());
141
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
142
        }
143
        {
144
            final ProtocolBufferConverter<SoundChunk> converter = new ProtocolBufferConverter<>(SoundChunk.getDefaultInstance());
145
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
146
        }
147
        {
148
            final ProtocolBufferConverter<Phoneme> converter = new ProtocolBufferConverter<>(Phoneme.getDefaultInstance());
149
            DefaultConverterRepository.getDefaultConverterRepository().addConverter(converter);
150
        }
151

  
152
        emo_map = new HashMap();
153
        emo_map.put(Emotions.SAD,       EmotionType.SAD);
154
        emo_map.put(Emotions.ANGRY,     EmotionType.ANGRY);
155
        emo_map.put(Emotions.FEAR,      EmotionType.FEAR);
156
        emo_map.put(Emotions.NEUTRAL,   EmotionType.NEUTRAL);
157
        emo_map.put(Emotions.SURPRISED, EmotionType.SURPRISED);
158
        emo_map.put(Emotions.HAPPY,     EmotionType.HAPPY);
159

  
160
        han_map = new HashMap();
161
        han_map.put(HeadAnimations.HEAD_NOD,       AnimationType_t.HEAD_NOD);
162
        han_map.put(HeadAnimations.HEAD_SHAKE,     AnimationType_t.HEAD_SHAKE);
163
        han_map.put(HeadAnimations.IDLE,           AnimationType_t.IDLE);
164
        han_map.put(HeadAnimations.EYEBLINK_R,     AnimationType_t.EYEBLINK_R);
165
        han_map.put(HeadAnimations.EYEBLINK_L,     AnimationType_t.EYEBLINK_L);
166
        han_map.put(HeadAnimations.EYEBLINK_BOTH,  AnimationType_t.EYEBLINK_BOTH);
167
        han_map.put(HeadAnimations.EYEBROWS_LOWER, AnimationType_t.EYEBROWS_LOWER);
168
        han_map.put(HeadAnimations.EYEBROWS_RAISE, AnimationType_t.EYEBROWS_RAISE);
169
    }
170

  
171
    private SoundChunk compileSoundChunk(AudioFormat audio, byte[] data) {
172
       SoundChunk.Builder sc = SoundChunk.newBuilder();
173
        ByteString bs = ByteString.copyFrom(data);
174
        boolean isbig = audio.isBigEndian();
175
        sc.setChannels(audio.getChannels());
176
        sc.setData(bs);
177
        sc.setRate((int) audio.getSampleRate());
178

  
179
        if (isbig) {
180
            sc.setEndianness(SoundChunk.EndianNess.ENDIAN_BIG);
181
        } else {
182
            sc.setEndianness(SoundChunk.EndianNess.ENDIAN_LITTLE);
183
        }
184

  
185
        sc.setSampleCount(data.length / audio.getFrameSize());
186
        Encoding enc = audio.getEncoding();
187

  
188
        if (enc.equals(Encoding.PCM_SIGNED)) {
189
            int size = audio.getSampleSizeInBits();
190
            if (size == 8) {
191
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S8);
192
            } else if (size == 16) {
193
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S16);
194
            } else if (size == 24) {
195
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_S24);
196
            } else {
197
                System.err.println("Audio Sample Size Not Valid");
198
            }
199
        } else if (enc.equals(Encoding.PCM_UNSIGNED)) {
200
            int size = audio.getSampleSizeInBits();
201
            if (size == 8) {
202
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U8);
203
            } else if (size == 16) {
204
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U16);
205
            } else if (size == 24) {
206
                sc.setSampleType(SoundChunk.SampleType.SAMPLE_U24);
207
            } else {
208
                System.err.println("Audio Sample Size not valid");
209
                // This is too rigorous, maybe.
210
                // System.exit(-1);
211
            }
212
        } else {
213
            System.err.println("Audio Encoding is invalid");
214
        }
215
        return sc.build();
216
    }
217

  
218
    public void deactivateServer() throws InterruptedException {
219
        try {
220
            this.server.deactivate();
221
        } catch (RSBException ex) {
222
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
223
        }
224
    }
225

  
226
    // OBSOLETE
227
    /*
228
     public MouthAnimation createMouthAnimationMessage(int duration, boolean active) {
229
     MouthAnimation.Builder ma = MouthAnimation.newBuilder();
230
     ma.setDuration(duration);
231
     ma.setActive(active);
232
     return ma.build();
233
     }
234
     */
235
    
236
    public Utterance createSetSpeak(SoundChunk soundchunck, ArrayList tuList, String text) {
237
        Utterance.Builder ut = Utterance.newBuilder();
238
        ut.setAudio(soundchunck);
239
        for (int i=0; i< tuList.size();i++) {
240
        	Phoneme.Builder ph = Phoneme.newBuilder();
241
        	Tuple t = (Tuple) tuList.get(i);
242
        	ph.setSymbol(t.s);
243
            ph.setDuration(t.d);
244
            ut.addPhonemes(i, ph);
245
            
246
        }
247
        System.out.println("Phonem count " + ut.getPhonemesCount() + " Phonem list " + ut.getPhonemesList());
248
        ut.setText(text);
249
        return ut.build();
250
    }
251

  
252
    public Animation createHeadAnimationMessage(HeadAnimations ani, int duration, int repetition, float scale) {
253
        Animation.Builder ha = Animation.newBuilder();
254
        ha.setRepetitions(repetition);
255
        ha.setTarget(han_map.get(ani));
256
        ha.setDurationEach(duration);
257
        ha.setScale(scale);
258
        return ha.build();
259
    }
260

  
261
    public EmotionState createCurrentEmotionMessage(Emotions emo, int duration) {
262
        EmotionState.Builder es = EmotionState.newBuilder();
263
        es.setDuration(duration);
264
        es.setValue(emo_map.get(emo));
265
        return es.build();
266
    }
267

  
268
    public EmotionState createDefaultEmotionMessage(Emotions emo) {
269
        EmotionState.Builder es = EmotionState.newBuilder();
270
        es.setValue(emo_map.get(emo));
271
        return es.build();
272
    }
273

  
274
    public GazeTarget createGazeTargetMessage(float pan, float tilt, float roll) {
275
        GazeTarget.Builder gt = GazeTarget.newBuilder();
276
        gt.setPan(pan);
277
        gt.setTilt(tilt);
278
        gt.setRoll(roll);
279
        return gt.build();
280
    }
281

  
282
    public void setSpeak(AudioFormat audio, byte[] bs, ArrayList tu, String text, boolean blocking) throws ExecutionException, TimeoutException {
283
        SoundChunk sc;
284
        sc = compileSoundChunk(audio, bs);
285
        if (blocking) {
286
            try {
287
                server.call("utterance", createSetSpeak(sc, tu, text));
288
                // System.out.println("Server replied: " + server.call("utterance", createSetSpeak(sc, tu)));
289
            } catch (RSBException ex) {
290
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
291
            }
292
        } else {
293
            try {             
294
                server.callAsync("utterance", createSetSpeak(sc, tu, text));
295
                // System.out.println("Set Mouth Animation non-blocking, not wating for reply");
296
            } catch (RSBException ex) {
297
                Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
298
            }
299
        }
300
    }
301
    
302
    public void setCurrentEmotion(Emotions emo, int duration, boolean blocking) throws ExecutionException, TimeoutException {
303
        try {
304
            if (blocking) {
305
                server.call("currentEmotion", createCurrentEmotionMessage(emo, duration));
306
                // System.out.println("Server replied: " + server.call("currentEmotion", createEmotionMessage(emo, duration)));
307
            } else {        
308
                server.callAsync("currentEmotion", createCurrentEmotionMessage(emo, duration));
309
                // System.out.println("Set Emotion non-blocking, not wating for reply");
310
            }
311

  
312
        } catch (RSBException ex) {
313
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
314
        }
315
    }
316

  
317
    public void setGazeTarget(float pan, float tilt, float roll, boolean blocking) throws ExecutionException, TimeoutException {
318
        try {
319
            if (blocking) {
320
                server.call("gaze", createGazeTargetMessage(pan, tilt, roll));
321
                // System.out.println("Server replied: " + server.call("gaze", createGazeTargetMessage(pan, tilt, roll)));
322
            } else {               
323
                server.callAsync("gaze", createGazeTargetMessage(pan, tilt, roll));
324
                // System.out.println("Set Gaze non-blocking, not wating for reply");
325
            }
326
        } catch (RSBException ex) {
327
            Logger.getLogger(MiddlewareRSB.class.getName()).log(Level.SEVERE, null, ex);
328
        }
329
    }
330

  
331
    public void setDefaultEmotion(Emotions emo, boolean blocking) throws ExecutionException, TimeoutException {
332
        try {
333
            if (blocking) {
334
                server.call("defaultEmotion", createDefaultEmotionMessage(emo));
335
                // System.out.println("Server replied: " + server.call("defaultEmotion", createDefaultEmotionMessage(emo)));
336
            } else {              
337
                server.callAsync("defaultEmotion", createDefaultEmotionMessage(emo));
338
                // System.out.println("Set Default Emotion non-blocking, not wating for reply");
339
            }
340
        } catch (RSBException ex) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff