Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/realizer/ControlComm.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.bml.realizer; 00035 00036 import ros.NodeHandle; 00037 import ros.RosException; 00038 import ros.ServiceClient; 00039 import ros.pkg.bml_msgs.msg.Entity; 00040 import ros.pkg.bml_msgs.msg.Flag; 00041 import ros.pkg.bml_srvs.srv.BMLFace; 00042 import ros.pkg.bml_srvs.srv.BMLGaze; 00043 import ros.pkg.bml_srvs.srv.BMLGesture; 00044 import ros.pkg.bml_srvs.srv.BMLHead; 00045 import ros.pkg.bml_srvs.srv.BMLLips; 00046 import ros.pkg.bml_srvs.srv.BMLLocomotion; 00047 import ros.pkg.bml_srvs.srv.BMLSpeech; 00048 import edu.wpi.hri.bml.behavior.FaceBehavior; 00049 import edu.wpi.hri.bml.behavior.GazeBehavior; 00050 import edu.wpi.hri.bml.behavior.GestureBehavior; 00051 import edu.wpi.hri.bml.behavior.HeadBehavior; 00052 import edu.wpi.hri.bml.behavior.LipsBehavior; 00053 import edu.wpi.hri.bml.behavior.LocomotionBehavior; 00054 import edu.wpi.hri.bml.behavior.SpeechBehavior; 00055 import edu.wpi.hri.bml.behavior.SyncRef; 00056 import edu.wpi.hri.bml.behavior.BehaviorEnums.Modality; 00057 import edu.wpi.hri.bml.behavior.SyncRef.SyncPoint; 00058 import edu.wpi.hri.log.Logger; 00059 import edu.wpi.hri.log.Logger.Colors; 00060 import edu.wpi.hri.log.Logger.LoggerLevel; 00061 00073 public class ControlComm { 00074 00075 private final ServiceClient<BMLFace.Request, BMLFace.Response, BMLFace> faceSrv; 00076 private final ServiceClient<BMLGaze.Request, BMLGaze.Response, BMLGaze> gazeSrv; 00077 private final ServiceClient<BMLGesture.Request, BMLGesture.Response, BMLGesture> gestureSrv; 00078 private final ServiceClient<BMLHead.Request, BMLHead.Response, BMLHead> headSrv; 00079 private final ServiceClient<BMLLips.Request, BMLLips.Response, BMLLips> lipsSrv; 00080 private final ServiceClient<BMLLocomotion.Request, BMLLocomotion.Response, BMLLocomotion> locomotionSrv; 00081 private final ServiceClient<BMLSpeech.Request, BMLSpeech.Response, BMLSpeech> speechSrv; 00082 private final Logger logger; 00083 00093 public ControlComm(Logger logger, NodeHandle handle) { 00094 this.logger = logger.sub(Colors.CONTROL, "CTRL"); 00095 this.faceSrv = handle.serviceClient("control/face", new BMLFace()); 00096 this.gazeSrv = handle.serviceClient("control/gaze", new BMLGaze()); 00097 this.gestureSrv = handle.serviceClient("control/gesture", 00098 new BMLGesture()); 00099 this.headSrv = handle.serviceClient("control/head", new BMLHead()); 00100 this.lipsSrv = handle.serviceClient("control/lips", new BMLLips()); 00101 this.locomotionSrv = handle.serviceClient("control/locomotion", 00102 new BMLLocomotion()); 00103 this.speechSrv = handle 00104 .serviceClient("control/speech", new BMLSpeech()); 00105 00106 this.logger.debug(LoggerLevel.INIT, "Created interface to control"); 00107 } 00108 00121 public byte sendFace(FaceBehavior face, SyncPoint point) { 00122 BMLFace.Request req = new BMLFace.Request(); 00123 req.behav.id = face.getID(); 00124 req.behav.synchPoint = point.rosValue; 00125 req.amount = face.getAmount(); 00126 req.au = face.getAu(); 00127 req.eyebrowShape = face.getEyebrow().rosValue; 00128 req.lid = face.getEyelid().rosValue; 00129 req.mouthShape = face.getMouth().rosValue; 00130 req.separation = face.getSeparation(); 00131 req.side.value = face.getSide().rosValue; 00132 req.type = face.getType().rosValue; 00133 00134 try { 00135 this.logger.debug(LoggerLevel.IO, "Sending face " 00136 + new SyncRef(face, point, 0.0)); 00137 00138 BMLFace.Response res = this.faceSrv.call(req); 00139 if (res != null) 00140 return res.status.value; 00141 else 00142 return Flag.FAILURE; 00143 } catch (RosException e) { 00144 return Flag.INVALID; 00145 } 00146 } 00147 00160 public byte sendGaze(GazeBehavior gaze, SyncPoint point) { 00161 BMLGaze.Request req = new BMLGaze.Request(); 00162 req.behav.id = gaze.getID(); 00163 req.behav.synchPoint = point.rosValue; 00164 req.angle = gaze.getOffsetAngle(); 00165 req.target.id = gaze.getTarget(); 00166 req.target.type = Entity.UNKNOWN; 00167 req.modalities = new short[gaze.getModality().size()]; 00168 int index = 0; 00169 for (Modality mod : gaze.getModality()) 00170 req.modalities[index++] = mod.rosValue; 00171 req.offset.value = gaze.getOffsetDirection().rosValue; 00172 req.polar = gaze.getPolarAngle(); 00173 00174 try { 00175 this.logger.debug(LoggerLevel.IO, "Sending gaze " 00176 + new SyncRef(gaze, point, 0.0)); 00177 BMLGaze.Response res = this.gazeSrv.call(req); 00178 if (res != null) 00179 return res.status.value; 00180 else 00181 return Flag.FAILURE; 00182 } catch (RosException e) { 00183 return Flag.INVALID; 00184 } 00185 } 00186 00199 public byte sendGesture(GestureBehavior gesture, SyncPoint point) { 00200 BMLGesture.Request req = new BMLGesture.Request(); 00201 req.behav.id = gesture.getID(); 00202 req.behav.synchPoint = point.rosValue; 00203 req.amplitude = gesture.getAmplitude().rosValue; 00204 req.distance = gesture.getDistLoc().rosValue; 00205 req.fingerDirection.value = gesture.getFingerDir().rosValue; 00206 req.handShape = gesture.getHandShape().rosValue; 00207 req.horizontalLocation = gesture.getHorzLoc().rosValue; 00208 req.lexeme = gesture.getLexeme(); 00209 req.moveDirection.value = gesture.getMoveDir().rosValue; 00210 req.palmDirection.value = gesture.getPalmDir().rosValue; 00211 req.power = gesture.getPower().rosValue; 00212 req.side.value = gesture.getHand().rosValue; 00213 req.target.id = gesture.getTarget(); 00214 req.target.type = Entity.UNKNOWN; 00215 req.trajectory = gesture.getTrajectory().rosValue; 00216 req.twoHanded = gesture.getTwoHanded().rosValue; 00217 req.type = gesture.getGestureType().rosValue; 00218 req.verticalLocation = gesture.getVertLoc().rosValue; 00219 00220 try { 00221 this.logger.debug(LoggerLevel.IO, "Sending gesture " 00222 + new SyncRef(gesture, point, 0.0)); 00223 BMLGesture.Response res = this.gestureSrv.call(req); 00224 if (res != null) 00225 return res.status.value; 00226 else 00227 return Flag.FAILURE; 00228 } catch (RosException e) { 00229 return Flag.INVALID; 00230 } 00231 } 00232 00245 public byte sendHead(HeadBehavior head, SyncPoint point) { 00246 BMLHead.Request req = new BMLHead.Request(); 00247 req.behav.id = head.getID(); 00248 req.behav.synchPoint = point.rosValue; 00249 req.amount = head.getAmount(); 00250 req.motion = head.getType().rosValue; 00251 00252 try { 00253 this.logger.debug(LoggerLevel.IO, "Sending head " 00254 + new SyncRef(head, point, 0.0)); 00255 BMLHead.Response res = this.headSrv.call(req); 00256 if (res != null) 00257 return res.status.value; 00258 else 00259 return Flag.FAILURE; 00260 } catch (RosException e) { 00261 return Flag.INVALID; 00262 } 00263 } 00264 00277 public byte sendLips(LipsBehavior lips, SyncPoint point) { 00278 BMLLips.Request req = new BMLLips.Request(); 00279 req.behav.id = lips.getID(); 00280 req.behav.synchPoint = point.rosValue; 00281 req.articulation = lips.getArticulation(); 00282 req.flapping.value = lips.isFlapping() ? Flag.TRUE : Flag.FALSE; 00283 req.viseme = lips.getViseme(); 00284 00285 try { 00286 this.logger.debug(LoggerLevel.IO, "Sending lips " 00287 + new SyncRef(lips, point, 0.0)); 00288 BMLLips.Response res = this.lipsSrv.call(req); 00289 if (res != null) 00290 return res.status.value; 00291 else 00292 return Flag.FAILURE; 00293 } catch (RosException e) { 00294 return Flag.INVALID; 00295 } 00296 } 00297 00310 public byte sendLocomotion(LocomotionBehavior locomotion, SyncPoint point) { 00311 BMLLocomotion.Request req = new BMLLocomotion.Request(); 00312 req.behav.id = locomotion.getID(); 00313 req.behav.synchPoint = point.rosValue; 00314 req.dynamic.value = locomotion.isDynamic() ? Flag.TRUE : Flag.FALSE; 00315 req.facing = locomotion.getFacing(); 00316 req.manner = locomotion.getManner().rosValue; 00317 req.proximity = locomotion.getProximity(); 00318 req.type = locomotion.getType().rosValue; 00319 req.velocity = locomotion.getVelocity(); 00320 00321 try { 00322 this.logger.debug(LoggerLevel.IO, "Sending locomotion " 00323 + new SyncRef(locomotion, point, 0.0)); 00324 BMLLocomotion.Response res = this.locomotionSrv.call(req); 00325 if (res != null) 00326 return res.status.value; 00327 else 00328 return Flag.FAILURE; 00329 } catch (RosException e) { 00330 return Flag.INVALID; 00331 } 00332 } 00333 00346 public byte sendSpeech(SpeechBehavior speech, SyncPoint point) { 00347 BMLSpeech.Request req = new BMLSpeech.Request(); 00348 req.behav.id = speech.getID(); 00349 req.behav.synchPoint = point.rosValue; 00350 req.text = speech.getText(); 00351 00352 try { 00353 this.logger.debug(LoggerLevel.IO, "Sending speech " 00354 + new SyncRef(speech, point, 0.0)); 00355 BMLSpeech.Response res = this.speechSrv.call(req); 00356 if (res != null) 00357 return res.status.value; 00358 else 00359 return Flag.FAILURE; 00360 } catch (RosException e) { 00361 return Flag.INVALID; 00362 } 00363 } 00364 } |