Project: engagement_test License: BSD Dependencies:
Used by:
None |
engagement_test/src/edu/wpi/hri/ros/testing/demo/Demo.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, this 00010 * list of conditions and the following disclaimer. * Redistributions in binary 00011 * form must reproduce the above copyright notice, this list of conditions and 00012 * the following disclaimer in the documentation and/or other materials provided 00013 * with the distribution. * Neither the name of Worcester Polytechnic Institute. 00014 * nor the names of its contributors may be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 package edu.wpi.hri.ros.testing.demo; 00030 00031 import java.io.BufferedReader; 00032 import java.io.IOException; 00033 import java.io.InputStreamReader; 00034 00035 import javax.swing.JOptionPane; 00036 00037 import ros.NodeHandle; 00038 import ros.Ros; 00039 import ros.RosException; 00040 import ros.ServiceClient; 00041 import ros.pkg.bml_srvs.srv.BMLRealization; 00042 import ros.pkg.engagement_srvs.srv.TurnFragment; 00043 00051 public class Demo { 00052 00053 final String generationSamples; 00054 final String realizerSamples; 00055 private final ServiceClient<BMLRealization.Request, BMLRealization.Response, BMLRealization> bmlSrv; 00056 private final ServiceClient<TurnFragment.Request, TurnFragment.Response, TurnFragment> genSrv; 00057 00058 private Demo(NodeHandle handle) throws IOException { 00059 realizerSamples = getFolder("bml_realizer"); 00060 generationSamples = getFolder("engagement_generation"); 00061 00062 bmlSrv = handle.serviceClient("bml/realize", new BMLRealization()); 00063 genSrv = handle.serviceClient("generation/turn_fragment", 00064 new TurnFragment()); 00065 } 00066 00067 private String getFolder(String pkg) throws IOException { 00068 Process p = Runtime.getRuntime().exec("rospack find " + pkg); 00069 BufferedReader stdInput = new BufferedReader(new InputStreamReader( 00070 p.getInputStream())); 00071 return stdInput.readLine() + "/samples"; 00072 } 00073 00077 void shutdown() { 00078 bmlSrv.shutdown(); 00079 genSrv.shutdown(); 00080 } 00081 00088 void sendRealizer(String contents) { 00089 try { 00090 BMLRealization.Request req = new BMLRealization.Request(); 00091 req.bml = contents; 00092 BMLRealization.Response res = bmlSrv.call(req); 00093 String result = "Overall result: " + res.success.value; 00094 00095 JOptionPane.showMessageDialog(null, result); 00096 } catch (RosException e) { 00097 JOptionPane.showMessageDialog(null, "Could not send to realizer"); 00098 } 00099 } 00100 00107 void sendGeneration(String contents) { 00108 try { 00109 TurnFragment.Request frag = new TurnFragment.Request(); 00110 frag.fragment = contents; 00111 00112 TurnFragment.Response res = genSrv.call(frag); 00113 String result = "Overall result: " + res.result.value; 00114 00115 JOptionPane.showMessageDialog(null, result); 00116 } catch (RosException e) { 00117 JOptionPane.showMessageDialog(null, "Could not send to realizer"); 00118 } 00119 } 00120 00129 public static void main(String[] args) throws IOException { 00130 Ros.getInstance().init("demo"); 00131 Demo demo = new Demo(Ros.getInstance().createNodeHandle()); 00132 00133 DemoFrame frame = new DemoFrame(demo); 00134 frame.setLocationRelativeTo(null); 00135 frame.setVisible(true); 00136 } 00137 } |