Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Feature: public topic /speaker_done. fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyparser committed Apr 3, 2017
1 parent 3a86bce commit 24e6fb3
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ protected void onPostExecute(Integer result) {
} else {
activity.prepareGreetingTTS();
}

Log.w(XLAB, "activity.startPlayTTS();");
activity.startPlayTTS();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cn.ac.iscas.xlab.droidfacedog;

import android.os.HandlerThread;
import android.util.Log;

import com.jilk.ros.rosbridge.ROSBridgeClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
* Created by lazyparser on 4/3/17.
*/

public final class RosBridgeCommunicateThread<T> extends HandlerThread {

private static final String TAG = "RosThread";
public static final int DEFAULT_FREQ;
public static final int DEFAULT_DELAY;

static {
DEFAULT_DELAY = 1000; // 1000ms = delay 1s
DEFAULT_FREQ = 1000; // 100ms = 10Hz
}

private boolean mSpeakerDone;
private Timer timer;
private ROSBridgeClient mRosClient;

public RosBridgeCommunicateThread(ROSBridgeClient rosClient) {
super(TAG);
timer = new Timer();
mSpeakerDone = false;
mRosClient = rosClient;
}

public boolean publishTopic(String topic, String init, int freq) {
// TODO: Stub
return true;
}

public boolean publishTopic(final String topic, final boolean init, int freq) {
// TODO: stub

return true;
}

public boolean beginPublishTopicSpeakerDone() {
final String topic = "/speaker_done";
// Spec: Advertise before publish
// https://github.com/RobotWebTools/rosbridge_suite/blob/groovy-devel/ROSBRIDGE_PROTOCOL.md
// { "op": "advertise",
// (optional) "id": <string>,
// "topic": <string>,
// "type": <string>
// }

// unadvertise the topic for debug convenience.
final String msgUnadvertise = "{\"op\": \"unadvertise\", \"topic\": \"" +
topic + "\"}";
mRosClient.send(msgUnadvertise);
Log.d(TAG, "ROS SEND " + msgUnadvertise);

try {
// sleep 3 sec for server delay.
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

final String msgAdvertise = "{\"op\": \"advertise\", \"topic\": \"" +
topic + "\", \"type\": \"std_msgs/Bool\"}";
mRosClient.send(msgAdvertise);
Log.d(TAG, "ROS SEND " + msgAdvertise);

TimerTask timerTask = new TimerTask() {
@Override
public void run() {
// TODO: spent tooooooo much time on this string. Should be more efficient.
String msg = "{\"op\":\"publish\"," +
"\"topic\":\"" + topic + "\"," +
"\"msg\":{\"data\":" +
(mSpeakerDone?"true":"false") +
"}}";

mRosClient.send(msg);
Log.w(TAG,"ros send json: " + msg);
}
};

timer.schedule(timerTask, DEFAULT_DELAY, DEFAULT_FREQ);
return true;
}

public void updateSpeakerState(boolean state) {
mSpeakerDone = state;
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/cn/ac/iscas/xlab/droidfacedog/XBotFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,20 @@ public void onError(Exception ex) {
Log.d(TAG,"ROS communication error");
}
});

RosBridgeCommunicateThread thread = new RosBridgeCommunicateThread<PublishEvent>(client);
thread.start();
thread.getLooper();
((XbotApplication)getApplication()).setRosThread(thread);
Log.i(TAG, "Background RosBridgeCommunicateThread thread started");
thread.beginPublishTopicSpeakerDone();
}

public void startPlayTTS() {
if (isPlayingTTS)
return;
isPlayingTTS = true;
((XbotApplication)getApplication()).getRosThread().updateSpeakerState(true);
playNext();
}
private void loadTTS(final XBotFace xbotface) {
Expand Down Expand Up @@ -407,6 +415,9 @@ protected void onDestroy() {
super.onDestroy();
// releaseSounds();
resetData();
isPlayingTTS = false;
((XbotApplication)getApplication()).getRosThread().updateSpeakerState(false);

for (int i = 0; i < ttsList.size(); ++i) {
MediaPlayer mp = ttsList.get(i);
mp.stop();
Expand Down Expand Up @@ -481,6 +492,7 @@ public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, i
private void playNext() {
if (ttsQueue.isEmpty()) {
isPlayingTTS = false;
((XbotApplication)getApplication()).getRosThread().updateSpeakerState(false);
return;
}
MediaPlayer mp = ttsQueue.poll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public class XbotApplication extends Application {
private static final String TAG = "XBotApplication";
ROSBridgeClient rosClient;

public RosBridgeCommunicateThread<PublishEvent> getRosThread() {
return mRosThread;
}

public void setRosThread(RosBridgeCommunicateThread<PublishEvent> rosThread) {
mRosThread = rosThread;
}

RosBridgeCommunicateThread<PublishEvent> mRosThread;

public XbotApplication() {
Log.d(TAG, "XbotApplication Instance has been created.");
}
Expand All @@ -21,6 +31,10 @@ public XbotApplication() {
public void onTerminate() {
if(rosClient != null)
rosClient.disconnect();
Log.i(TAG, "ROSBridge client destroyed");
if (mRosThread != null)
mRosThread.quit();
Log.i(TAG, "Background RosBridgeCommunicateThread thread destroyed");
super.onTerminate();
}

Expand Down

0 comments on commit 24e6fb3

Please sign in to comment.