一个Cruzr机器人项目,要实现人离开显示迎宾表情,人接近跳到程序主页,
方法是:在onCreate 中加传感器
监听,在每2s内检测数据变动,判断距离是否在指定范围内,在范围内则发出 迎接语音提示,然后再判断是否在主页,否 则用finish 和 startActivity跳转到主页
下面是代码
class="java">
void initWelcome(){
try {
// List<SensorDevice> sensorDeviceList = sensorManager.getDeviceList();
// for (SensorDevice sd:sensorDeviceList ) {
// Log.d(TAG, sd.getDescription()+sd.getId()+sd.getName()+sd.getType());
// }
SensorManager sensorManager = Robot.globalContext().getSystemService(SensorManager.SERVICE);
String sensorId="human_detect";
//判断是否打开,否则打开
if(sensorManager.isEnabled("human_detect")){
Log.d(TAG, "human_detect isEnabled");
}else{
Log.d(TAG, "human_detect is not Enabled");
sensorManager.enable(sensorId).get();
}
//传感器监听
SensorListener sensorListener = new SensorListener() {
//传感器变化事件
@Override
public void onSensorChanged(SensorDevice sensorDevice, SensorEvent sensorEvent/* [1] */) {
VisualManager mVisualManager = VisualManager.getInstance();
EmotionManager emotionManager = Robot.globalContext().getSystemService(EmotionManager.SERVICE);
ExpressingOption /* [1] */ option = new ExpressingOption.Builder(
Uri.parse("emotion://va/littlestar_smile")).setSticky(true).setLoops(1).build();
if(sensorEvent.getTimestamp()-lastTime>2000){//间隔2秒 判断一次
float[] tmpValues=sensorEvent.getValues(); //传感器的值
if(tmpValues[0]<2&&tmpValues[1]<0.8){ //现在有人接近了
//关闭空闲功能
LeisureManager leisureManager=LeisureManager.get();
leisureManager.prohibitedLeisure();
leisureManager.wakeup();
if(!humanStand){ //之前没有人,现在有人了
mVisualManager.stop("visualIndex");//人脸检测关闭
// RobotAssist.motionRest();
//Date 的 getHours()过时的方法
Calendar cal = Calendar.getInstance();
int hours = cal.get( Calendar.HOUR_OF_DAY );
String greet="你好";
if (hours>7&&hours<11){
greet="上午好";
}else if(hours>12){
greet="下午好";
}else if(hours>10&&hours<14){
greet="中午好";
}
greet+="我是门诊导诊机器人,有什么需要帮忙的?";
RobotAssist.speeekWithMotion2(greet).done(new DoneCallback() {
@Override
public void onDone(Object o) {
if(!isCurrent)
restartApp2();//回到首页
}
});
emotionManager.dismiss();//关表情
// initVisual();
// getCurr();
}
humanStand=true;
}else {
if(humanStand=true){
humanStand=false;
emotionManager.express(option); //promise =
RobotAssist.motionRest();
}
}
lastTime=sensorEvent.getTimestamp();
values = tmpValues; //[2.0, 0.0]
// initVisual();
}
// Log.d(TAG, sensorEvent.toString());
}
};
sensorManager.registerListener("human_detect", sensorListener);
} catch (SensorException e) {
e.printStackTrace();
} catch (CancelledException e) {
e.printStackTrace();
}
}
// void restartApp1(){
// ActivityManager manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
// manager.restartPackage("com.example.test");
// }
void restartApp2(){
Intent intent= new Intent(IndexActivity.this,IndexActivity.class);
startActivity(intent);
// System.exit(0);
finish();
}