00001 package de.tum.in.fipm.kipm.gui.visualisation.applets;
00002
00003 import java.awt.Color;
00004 import java.io.BufferedReader;
00005 import java.io.IOException;
00006 import java.io.InputStreamReader;
00007 import java.util.ArrayList;
00008
00009 import processing.core.*;
00010 import ros.NodeHandle;
00011 import ros.Ros;
00012 import ros.RosException;
00013 import ros.ServiceClient;
00014 import ros.ServiceServer;
00015 import ros.pkg.mod_vis.srv.CommVisSetLeftImg;
00016 import ros.pkg.mod_vis.srv.CommVisSetRightImg;
00017 import ros.pkg.mod_vis.srv.CommVisSetReqText;
00018 import ros.pkg.mod_vis.srv.CommVisSetResText;
00019 import de.tum.in.fipm.kipm.gui.visualisation.base.PrologVisualizationCanvas;
00020
00021 public class CommunicationVisApplet extends PApplet {
00022
00023 private static final long serialVersionUID = -3913679727924919169L;
00024 private PrologVisualizationCanvas prologVisCanvas = null;
00025
00027
00028
00029 PFont verdana;
00030 PFont verdanaBold;
00031 PFont dejavu;
00032
00033 PImage leftImg, rightImg;
00034
00035
00036 String imagePath = "";
00037
00039
00040 ArrayList<Integer> colors = new ArrayList<Integer>(12);
00041
00042 int transmitting = 0;
00043 int receiving = 0;
00044 int FRAMES_TO_DISPLAY_MESSAGES = 100;
00045
00046 int LINE_LEFT_START = 250;
00047 int LINE_RIGHT_END = 550;
00048 int LINE_WIDTH=LINE_RIGHT_END-LINE_LEFT_START;
00049
00050 int UPPER_LINE_YPOS = 280;
00051 int LOWER_LINE_YPOS = 330;
00052
00053 int transmitPosition=LINE_LEFT_START;
00054 int receivePosition=LINE_RIGHT_END;
00055
00056 SetLeftImgCallback leftImgCallback;
00057 SetRightImgCallback rightImgCallback;
00058 SetRequestTextCallback requestTextCallback;
00059 SetResponseTextCallback responseTextCallback;
00060
00061
00062 String request = "";
00063
00064 String response = "";
00065
00066
00067 static Ros ros;
00068 static NodeHandle n;
00069
00070
00074 protected static void initRos() {
00075
00076 ros = Ros.getInstance();
00077
00078 if(!Ros.getInstance().isInitialized()) {
00079 ros.init("knowrob_comm_vis_applet");
00080 }
00081 n = ros.createNodeHandle();
00082
00083 }
00084
00085
00089 public void setup() {
00090
00091 size(800, 600, P2D);
00092 this.frame.setTitle("CommunicationVisualization");
00093 this.frame.setBackground(new Color(20, 20, 20));
00094 background(20, 20, 20);
00095
00096 imagePath = findPackage("mod_vis");
00097
00098 dejavu = createFont("DejaVu Sans",13);
00099 textFont(dejavu);
00100 hint(ENABLE_ACCURATE_TEXTURES);
00101 ellipseMode(RADIUS);
00102 frameRate(25);
00103
00104
00105 leftImg = new PImage();
00106 rightImg = loadImage(imagePath+"/images/rosie.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 }
00166
00167
00168 synchronized(response) {
00169 textAlign(LEFT, TOP);
00170 text(response, 250, 370, 300, 250);
00171 }
00172 if(receiving>0) {
00173
00174 ellipse(receivePosition, LOWER_LINE_YPOS, 3, 3);
00175 receivePosition = LINE_RIGHT_END - ((LINE_RIGHT_END - receivePosition + 10)%LINE_WIDTH);
00176 receiving--;
00177 }
00178 }
00179
00184 public void setRequest(String req) {
00185
00186 synchronized(request) {
00187 this.request = req;
00188 this.response = "";
00189 }
00190 this.setTransmitting(true);
00191
00192 draw();
00193 }
00194
00195
00200 public void setResponse(String response) {
00201 synchronized(response) {
00202 this.response = response;
00203 }
00204 this.setReceiving(true);
00205
00206 draw();
00207 }
00208
00209
00214 public void setLeftImage(String filename) {
00215 synchronized(leftImg) {
00216 if(filename.length()>0)
00217 this.leftImg = loadImage(imagePath+"/images/"+filename);
00218 else
00219 this.leftImg = new PImage();
00220 }
00221 }
00222
00223
00228 public void setRightImage(String filename) {
00229 synchronized(rightImg) {
00230 if(filename.length()>0)
00231 this.rightImg = loadImage(imagePath+"/images/"+filename);
00232 else
00233 this.rightImg = new PImage();
00234 }
00235 }
00236
00241 public void setTransmitting(boolean on) {
00242 if(on)
00243 this.transmitting += FRAMES_TO_DISPLAY_MESSAGES;
00244 else
00245 this.transmitting = 0;
00246 }
00247
00252 public void setReceiving(boolean on) {
00253 if(on)
00254 this.receiving += FRAMES_TO_DISPLAY_MESSAGES;
00255 else
00256 this.receiving = 0;
00257 }
00258
00262 public void keyPressed(){
00263
00264 switch(keyCode) {
00265 case 82:
00266 setReceiving(true);
00267 break;
00268
00269 case 84:
00270 setTransmitting(true);
00271 break;
00272
00273 case ESC:
00274 leftImg = new PImage();
00275 rightImg = loadImage(imagePath+"/images/rosie.png");
00276 this.request = "";
00277 this.response = "";
00278 key=0;
00279 break;
00280 }
00281
00282 delay(50);
00283 redraw();
00284
00285
00286 }
00287
00288
00296 public class RosServiceThread implements Runnable {
00297
00298 @Override public void run() {
00299
00300 try {
00301
00302 initRos();
00303
00304 n.advertiseService("comm_vis_set_left_img", new CommVisSetLeftImg(), new SetLeftImgCallback());
00305 n.advertiseService("comm_vis_set_right_img", new CommVisSetRightImg(), new SetRightImgCallback());
00306 n.advertiseService("comm_vis_set_req_text", new CommVisSetReqText(), new SetRequestTextCallback());
00307 n.advertiseService("comm_vis_set_res_text", new CommVisSetResText(), new SetResponseTextCallback());
00308 ros.spin();
00309
00310 } catch(RosException e) {
00311 e.printStackTrace();
00312 }
00313 }
00314 }
00315
00316
00324 class SetLeftImgCallback implements ServiceServer.Callback<CommVisSetLeftImg.Request, CommVisSetLeftImg.Response> {
00325
00326 @Override
00327 public CommVisSetLeftImg.Response call(CommVisSetLeftImg.Request req) {
00328
00329 CommVisSetLeftImg.Response res = new CommVisSetLeftImg.Response();
00330 res.success = 0;
00331
00332 if (req.filename != null) {
00333 setLeftImage(req.filename);
00334 }
00335
00336 return res;
00337 }
00338 }
00339
00347 class SetRightImgCallback implements ServiceServer.Callback<CommVisSetRightImg.Request, CommVisSetRightImg.Response> {
00348
00349 @Override
00350 public CommVisSetRightImg.Response call(CommVisSetRightImg.Request req) {
00351
00352 CommVisSetRightImg.Response res = new CommVisSetRightImg.Response();
00353 res.success = 0;
00354
00355 if (req.filename != null) {
00356 setRightImage(req.filename);
00357 }
00358
00359 return res;
00360 }
00361 }
00362
00370 class SetRequestTextCallback implements ServiceServer.Callback<CommVisSetReqText.Request, CommVisSetReqText.Response> {
00371
00372 @Override
00373 public CommVisSetReqText.Response call(CommVisSetReqText.Request req) {
00374
00375 CommVisSetReqText.Response res = new CommVisSetReqText.Response();
00376 res.success = 0;
00377
00378 if (req.request != null) {
00379 setRequest(req.request);
00380 }
00381
00382 return res;
00383 }
00384 }
00385
00393 class SetResponseTextCallback implements ServiceServer.Callback<CommVisSetResText.Request, CommVisSetResText.Response> {
00394
00395 @Override
00396 public CommVisSetResText.Response call(CommVisSetResText.Request req) {
00397
00398 CommVisSetResText.Response res = new CommVisSetResText.Response();
00399 res.success = 0;
00400
00401 if (req.response != null) {
00402 setResponse(req.response);
00403 }
00404
00405 return res;
00406 }
00407 }
00408
00409
00410
00421 public static void visualizeCommunication(String req, String res, String leftImg, String rightImg) {
00422
00423 try {
00424
00425 initRos();
00426
00427 if(leftImg!=null) {
00428 try {
00429
00430 CommVisSetLeftImg.Request vis_req = new CommVisSetLeftImg.Request();
00431 vis_req.filename = leftImg;
00432 ServiceClient<CommVisSetLeftImg.Request, CommVisSetLeftImg.Response, CommVisSetLeftImg> cl =
00433 n.serviceClient("/comm_vis_set_left_img", new CommVisSetLeftImg());
00434 cl.call(vis_req);
00435 cl.shutdown();
00436 } catch(RosException e) {
00437
00438 }
00439 }
00440
00441 if(rightImg!=null) {
00442 try {
00443
00444 CommVisSetRightImg.Request vis_req = new CommVisSetRightImg.Request();
00445 vis_req.filename = rightImg;
00446 ServiceClient<CommVisSetRightImg.Request, CommVisSetRightImg.Response, CommVisSetRightImg> cl =
00447 n.serviceClient("/comm_vis_set_right_img", new CommVisSetRightImg());
00448 cl.call(vis_req);
00449 cl.shutdown();
00450 } catch(RosException e) {
00451
00452 }
00453 }
00454
00455 if(req!=null) {
00456 try {
00457
00458 CommVisSetReqText.Request vis_req = new CommVisSetReqText.Request();
00459 vis_req.request = req;
00460 ServiceClient<CommVisSetReqText.Request, CommVisSetReqText.Response, CommVisSetReqText> cl =
00461 n.serviceClient("/comm_vis_set_req_text", new CommVisSetReqText());
00462 cl.call(vis_req);
00463 cl.shutdown();
00464 } catch(RosException e) {
00465
00466 }
00467 }
00468
00469 if(res!=null) {
00470
00471 try {
00472 CommVisSetResText.Request vis_req = new CommVisSetResText.Request();
00473 vis_req.response = res;
00474 ServiceClient<CommVisSetResText.Request, CommVisSetResText.Response, CommVisSetResText> cl =
00475 n.serviceClient("/comm_vis_set_res_text", new CommVisSetResText());
00476 cl.call(vis_req);
00477 cl.shutdown();
00478
00479 } catch(RosException e) {
00480
00481 }
00482
00483 }
00484
00485 } catch (Exception e) {
00486 e.printStackTrace();
00487 }
00488 }
00489
00490
00491
00492 protected static String findPackage(String pkgname) {
00493
00494 try {
00495 String line;
00496 Process p = Runtime.getRuntime().exec( "rospack find " + pkgname +"");
00497 BufferedReader pathreader = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8"));
00498 if( (line = pathreader.readLine()) != null) {
00499 return line;
00500 }
00501 } catch (IOException e) {
00502 e.printStackTrace(System.err);
00503 }
00504 return null;
00505 }
00506
00507
00508
00509 public void setPrologVisCanvas(PrologVisualizationCanvas c){
00510 prologVisCanvas = c;
00511 }
00512
00513 public static void main(String args[]) {
00514
00515 System.err.println(findPackage("mod_vis"));
00516
00517 PApplet.main(new String[] { "de.tum.in.fipm.kipm.gui.visualisation.applets.CommunicationVisApplet" });
00518 }
00519 }
00520