00001 package edu.tum.cs.ias.knowrob.vis.applets;
00002
00003 import java.awt.Color;
00004 import java.util.ArrayList;
00005
00006 import processing.core.*;
00007 import ros.NodeHandle;
00008 import ros.Ros;
00009 import ros.RosException;
00010 import ros.ServiceClient;
00011 import ros.ServiceServer;
00012 import ros.pkg.mod_vis.srv.CommVisSetLeftImg;
00013 import ros.pkg.mod_vis.srv.CommVisSetRightImg;
00014 import ros.pkg.mod_vis.srv.CommVisSetReqText;
00015 import ros.pkg.mod_vis.srv.CommVisSetResText;
00016 import edu.tum.cs.ias.knowrob.utils.ResourceRetriever;
00017
00018 public class CommunicationVisApplet extends PApplet {
00019
00020 private static final long serialVersionUID = -3913679727924919169L;
00021 private PrologVisualizationCanvas prologVisCanvas = null;
00022
00024
00025
00026 PFont verdana;
00027 PFont verdanaBold;
00028 PFont dejavu;
00029
00030 PImage leftImg, rightImg;
00031
00032
00033 String imagePath = "";
00034
00036
00037 ArrayList<Integer> colors = new ArrayList<Integer>(12);
00038
00039 int transmitting = 0;
00040 int receiving = 0;
00041 int FRAMES_TO_DISPLAY_MESSAGES = 70;
00042
00043 int LINE_LEFT_START = 250;
00044 int LINE_RIGHT_END = 550;
00045 int LINE_WIDTH=LINE_RIGHT_END-LINE_LEFT_START;
00046
00047 int UPPER_LINE_YPOS = 280;
00048 int LOWER_LINE_YPOS = 330;
00049
00050 int transmitPosition=LINE_LEFT_START;
00051 int receivePosition=LINE_RIGHT_END;
00052
00053 SetLeftImgCallback leftImgCallback;
00054 SetRightImgCallback rightImgCallback;
00055 SetRequestTextCallback requestTextCallback;
00056 SetResponseTextCallback responseTextCallback;
00057
00058
00059 String request = "";
00060
00061 String response = "";
00062
00063
00064 static Ros ros;
00065 public static NodeHandle n;
00066
00067
00071 protected static void initRos() {
00072
00073 ros = Ros.getInstance();
00074
00075 if(!Ros.getInstance().isInitialized()) {
00076 ros.init("knowrob_comm_vis_applet");
00077 }
00078 n = ros.createNodeHandle();
00079
00080 }
00081
00082
00086 public void setup() {
00087
00088 size(800, 600, P2D);
00089 this.frame.setTitle("CommunicationVisualization");
00090 this.frame.setBackground(new Color(20, 20, 20));
00091 background(20, 20, 20);
00092
00093 initRos();
00094
00095 imagePath = ResourceRetriever.findPackage("mod_vis");
00096
00097 dejavu = createFont("DejaVu Sans",13);
00098 textFont(dejavu);
00099 hint(ENABLE_ACCURATE_TEXTURES);
00100 ellipseMode(RADIUS);
00101 frameRate(25);
00102
00103
00104
00105 leftImg = loadImage(imagePath+"/images/pr2.jpg");
00106 rightImg = loadImage(imagePath+"/images/roboearth.png");
00107
00108
00109
00110 leftImgCallback = new SetLeftImgCallback();
00111 rightImgCallback = new SetRightImgCallback();
00112 requestTextCallback = new SetRequestTextCallback();
00113 responseTextCallback = new SetResponseTextCallback();
00114
00115 Thread startRosServices = new Thread( new RosServiceThread() );
00116 startRosServices.start();
00117
00118
00119 draw();
00120 if(prologVisCanvas != null) prologVisCanvas.validate();
00121
00122 }
00123
00124
00128 public void draw() {
00129
00130 this.frame.setBackground(new Color(20, 20, 20));
00131 background(20, 20, 20);
00132
00133 cursor(CROSS);
00134
00135 textFont(dejavu);
00136 textMode(SCREEN);
00137 fill(0xFFFFFFFF);stroke(0xFFFFFFFF);
00138
00139
00140 synchronized(leftImg) {
00141 if(leftImg != null)
00142 image(leftImg, ((int)(150-0.5*leftImg.width)), ((int)(300-0.5*leftImg.height)));
00143 }
00144
00145 synchronized(rightImg) {
00146 if(rightImg != null)
00147 image(rightImg, ((int)(650-0.5*rightImg.width)), ((int)(300-0.5*rightImg.height)));
00148 }
00149
00150
00151 for(int x=LINE_LEFT_START;x<LINE_RIGHT_END;x+=20) {
00152 line(x, UPPER_LINE_YPOS, x+10, UPPER_LINE_YPOS);
00153 line(x, LOWER_LINE_YPOS, x+10, LOWER_LINE_YPOS);
00154 }
00155
00156
00157 synchronized(request) {
00158 textAlign(LEFT, BOTTOM);
00159 text(request, 250, 30, 300, 230);
00160 }
00161 if(transmitting>0) {
00162 ellipse(transmitPosition, UPPER_LINE_YPOS, 3, 3);
00163 transmitPosition= LINE_LEFT_START + ((transmitPosition - LINE_LEFT_START + 10)%LINE_WIDTH);
00164 transmitting--;
00165 } else {
00166 this.request="";
00167 }
00168
00169
00170 synchronized(response) {
00171 textAlign(LEFT, TOP);
00172 text(response, 250, 370, 300, 250);
00173 }
00174 if(receiving>0) {
00175
00176 ellipse(receivePosition, LOWER_LINE_YPOS, 3, 3);
00177 receivePosition = LINE_RIGHT_END - ((LINE_RIGHT_END - receivePosition + 10)%LINE_WIDTH);
00178 receiving--;
00179 } else {
00180 this.response="";
00181 }
00182 }
00183
00188 public void setRequest(String req) {
00189
00190 synchronized(request) {
00191 this.request = req;
00192 this.response = "";
00193 }
00194 this.setTransmitting(true);
00195
00196 draw();
00197 }
00198
00199
00204 public void setResponse(String response) {
00205 synchronized(response) {
00206 this.response = response;
00207 }
00208 this.setReceiving(true);
00209
00210 draw();
00211 }
00212
00213
00218 public void setLeftImage(String filename) {
00219 synchronized(leftImg) {
00220 if(filename.length()>0)
00221 this.leftImg = loadImage(imagePath+"/images/"+filename);
00222 else
00223 this.leftImg = new PImage();
00224 }
00225 }
00226
00227
00232 public void setRightImage(String filename) {
00233 synchronized(rightImg) {
00234 if(filename.length()>0)
00235 this.rightImg = loadImage(imagePath+"/images/"+filename);
00236 else
00237 this.rightImg = new PImage();
00238 }
00239 }
00240
00245 public void setTransmitting(boolean on) {
00246 if(on)
00247 this.transmitting += FRAMES_TO_DISPLAY_MESSAGES;
00248 else
00249 this.transmitting = 0;
00250 }
00251
00256 public void setReceiving(boolean on) {
00257 if(on)
00258 this.receiving += FRAMES_TO_DISPLAY_MESSAGES;
00259 else
00260 this.receiving = 0;
00261 }
00262
00266 public void keyPressed(){
00267
00268 switch(keyCode) {
00269 case 82:
00270 setReceiving(true);
00271 break;
00272
00273 case 84:
00274 setTransmitting(true);
00275 break;
00276
00277 case ESC:
00278 leftImg = new PImage();
00279 rightImg = loadImage(imagePath+"/images/rosie.png");
00280 this.request = "";
00281 this.response = "";
00282 key=0;
00283 break;
00284 }
00285
00286 delay(50);
00287 redraw();
00288
00289
00290 }
00291
00292 public void delay(int millis)
00293 {
00294
00295 try {
00296 Thread.sleep(100);
00297 } catch (InterruptedException e) {
00298 }
00299 }
00300
00301
00309 public class RosServiceThread implements Runnable {
00310
00311 @Override public void run() {
00312
00313 try {
00314
00315 initRos();
00316
00317 n.advertiseService("comm_vis_set_left_img", new CommVisSetLeftImg(), new SetLeftImgCallback());
00318 n.advertiseService("comm_vis_set_right_img", new CommVisSetRightImg(), new SetRightImgCallback());
00319 n.advertiseService("comm_vis_set_req_text", new CommVisSetReqText(), new SetRequestTextCallback());
00320 n.advertiseService("comm_vis_set_res_text", new CommVisSetResText(), new SetResponseTextCallback());
00321 ros.spin();
00322
00323 } catch(RosException e) {
00324 e.printStackTrace();
00325 }
00326 }
00327 }
00328
00329
00337 class SetLeftImgCallback implements ServiceServer.Callback<CommVisSetLeftImg.Request, CommVisSetLeftImg.Response> {
00338
00339 @Override
00340 public CommVisSetLeftImg.Response call(CommVisSetLeftImg.Request req) {
00341
00342 CommVisSetLeftImg.Response res = new CommVisSetLeftImg.Response();
00343 res.success = 0;
00344
00345 if (req.filename != null) {
00346 setLeftImage(req.filename);
00347 }
00348
00349 return res;
00350 }
00351 }
00352
00360 class SetRightImgCallback implements ServiceServer.Callback<CommVisSetRightImg.Request, CommVisSetRightImg.Response> {
00361
00362 @Override
00363 public CommVisSetRightImg.Response call(CommVisSetRightImg.Request req) {
00364
00365 CommVisSetRightImg.Response res = new CommVisSetRightImg.Response();
00366 res.success = 0;
00367
00368 if (req.filename != null) {
00369 setRightImage(req.filename);
00370 }
00371
00372 return res;
00373 }
00374 }
00375
00383 class SetRequestTextCallback implements ServiceServer.Callback<CommVisSetReqText.Request, CommVisSetReqText.Response> {
00384
00385 @Override
00386 public CommVisSetReqText.Response call(CommVisSetReqText.Request req) {
00387
00388 CommVisSetReqText.Response res = new CommVisSetReqText.Response();
00389 res.success = 0;
00390
00391 if (req.request != null) {
00392 setRequest(req.request);
00393 }
00394
00395 return res;
00396 }
00397 }
00398
00406 class SetResponseTextCallback implements ServiceServer.Callback<CommVisSetResText.Request, CommVisSetResText.Response> {
00407
00408 @Override
00409 public CommVisSetResText.Response call(CommVisSetResText.Request req) {
00410
00411 CommVisSetResText.Response res = new CommVisSetResText.Response();
00412 res.success = 0;
00413
00414 if (req.response != null) {
00415 setResponse(req.response);
00416 }
00417
00418 return res;
00419 }
00420 }
00421
00422
00423
00434 public static void visualizeCommunication(String req, String res, String leftImg, String rightImg) {
00435
00436 try {
00437 initRos();
00438
00439 if(leftImg!=null) {
00440 try {
00441
00442 CommVisSetLeftImg.Request vis_req = new CommVisSetLeftImg.Request();
00443 vis_req.filename = leftImg;
00444 ServiceClient<CommVisSetLeftImg.Request, CommVisSetLeftImg.Response, CommVisSetLeftImg> cl =
00445 n.serviceClient("/comm_vis_set_left_img", new CommVisSetLeftImg());
00446 cl.call(vis_req);
00447 cl.shutdown();
00448 } catch(RosException e) {
00449
00450
00451 }
00452 }
00453
00454 if(rightImg!=null) {
00455 try {
00456
00457 CommVisSetRightImg.Request vis_req = new CommVisSetRightImg.Request();
00458 vis_req.filename = rightImg;
00459 ServiceClient<CommVisSetRightImg.Request, CommVisSetRightImg.Response, CommVisSetRightImg> cl =
00460 n.serviceClient("/comm_vis_set_right_img", new CommVisSetRightImg());
00461 cl.call(vis_req);
00462 cl.shutdown();
00463 } catch(RosException e) {
00464
00465
00466 }
00467 }
00468
00469 if(req!=null) {
00470 try {
00471
00472 CommVisSetReqText.Request vis_req = new CommVisSetReqText.Request();
00473 vis_req.request = req;
00474 ServiceClient<CommVisSetReqText.Request, CommVisSetReqText.Response, CommVisSetReqText> cl =
00475 n.serviceClient("/comm_vis_set_req_text", new CommVisSetReqText());
00476 cl.call(vis_req);
00477 cl.shutdown();
00478 } catch(RosException e) {
00479
00480 }
00481 }
00482
00483 if(res!=null) {
00484
00485 try {
00486 CommVisSetResText.Request vis_req = new CommVisSetResText.Request();
00487 vis_req.response = res;
00488 ServiceClient<CommVisSetResText.Request, CommVisSetResText.Response, CommVisSetResText> cl =
00489 n.serviceClient("/comm_vis_set_res_text", new CommVisSetResText());
00490 cl.call(vis_req);
00491 cl.shutdown();
00492
00493 } catch(RosException e) {
00494
00495 }
00496
00497 }
00498
00499 } catch (Exception e) {
00500 e.printStackTrace();
00501 }
00502 }
00503
00504
00505
00506 public void setPrologVisCanvas(PrologVisualizationCanvas c){
00507 prologVisCanvas = c;
00508 }
00509
00510 public static void main(String args[]) {
00511
00512 PApplet.main(new String[] { "edu.tum.cs.ias.knowrob.vis.applets.CommunicationVisApplet" });
00513 }
00514 }
00515