summaryrefslogtreecommitdiff
path: root/roombacomm-client/src/processing-examples/roombaview/roombaview.pde
blob: 4c03600edbce88e49adc36a988d40b50a27d3a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//
// roombaview -- 
//
// 2006, Tod E. Kurt, tod@todbot.com,  http://roombahacking.com/
//

import mkv.MyGUI.*;
import roombacomm.*;

// if you set roombaCommPort to null, it will bring up the port picker dialog
// if you set it to "testdummy", it will be a bring up a non-functioning test of the GUI
String roombacommPort = "testdummy";
//String roombacommPort = "/dev/cu.KeySerial1";
//String roombacommPort = "/dev/cu.BlueRadios-COM0-1";
//String roombacommPort = null;

RoombaCommSerial roombacomm;

MyGUI gui;
MyGUIButton butForward, butBackward, butStop;
MyGUIButton butSpinLeft, butSpinRight, butTurnLeft, butTurnRight;
MyGUIButton butReset, butSafe, butFull, butSpy, butVacuum, butBeep;
MyGUIPinSlider sliderSpeed;
PFont fontA;
// all the colors used
color cBack = color(150);
color cOff  = color(100);
color cWhl  = color(80);
color cOn   = color(255, 0, 0);
color cTxt  = color(40);
color cGrid = color(120);
color cFlor = color(190);
color cStat = color(170);
color cBut = color(170,170,210);

float rx,ry,rangle;  // location of displayed roomba icon                  
float scalex = 0.4;  // convert mm to pixels
float scaley = 0.4;  // convert mm to pixels

int frameCounter=0;

// called once on startup
void setup() {
  size(600, 500);
  frameRate(5);
  smooth();
  background(cFlor);
  stroke(100);
  ellipseMode(CENTER_RADIUS);
  rectMode(CENTER);
  // Initiate GUI objects
  fontA = loadFont("CourierNewPSMT-9.vlw");
  textFont(fontA, 12);
  fill(0);
  gui = new MyGUI(this);
  makeMoveButtons(width-75,height-100);
  makeControlButtons(width-75,height-225);
  
  roombacommSetup();
  reset();
}

void reset() {
  rx = width/2;
  ry = height/2;
  rangle = 0;
}
  
// called 'framerate' times a second
void draw() {
  frameCounter++;
  background(cFlor);
  fill(0);
  stroke(100);
  
  drawGridlines();
  drawStatus();
  if( roombacomm != null ) {
      computeRoombaLocation();
      drawRoombaStatus( rx, ry, rangle );
      updateRoombaState();
  }
}
 
//                                                                  
// Get roomba sensors and update display                                        
//                                                                              
void updateRoombaState() {
    roombacomm.sensors();
}

// 
void computeRoombaLocation() {
  int distance = roombacomm.distance();
  float angle  = roombacomm.angleInRadians();
  rangle = rangle - angle ;
  rangle %= TWO_PI;
  float dx = distance * sin(rangle);
  float dy = distance * cos(rangle);
  rx = rx + (dx * scaley);
  ry = ry - (dy * scalex);
  
  // torroidial mapping, sir
  if( rx > width  ) rx = 0;
  if( rx < 0 ) rx = width;
  if( ry > height ) ry = 0;
  if( ry < 0 ) ry = height;
}  

//
// 
void actionPerformed(ActionEvent e) {
  println("e="+e);
  if( !roombacomm.connected() ) return;
  String cmd = e.getActionCommand();
  Object src = e.getSource();
  
  if( cmd.equals("speed-update") ) {
    roombacomm.setSpeed( sliderSpeed.getValue() ); 
    return;
  }
  else if( src == butStop ) {
    roombacomm.stop();
  } else if( src == butForward ) {
    roombacomm.goForward();
  } else if( src == butBackward ) {
    roombacomm.goBackward();
  } else if( src == butSpinLeft ) {
    roombacomm.spinLeft();
  } else if( src == butSpinRight ) {
    roombacomm.spinRight();
  } else if( src == butTurnLeft ) {
    roombacomm.turnLeft();
  } else if( src == butTurnRight ) {
    roombacomm.turnRight();
  }  else if( src == butReset ) {
    roombacomm.reset();
    reset();
  }  else if( src == butSafe ) {
    roombacomm.start(); 
    roombacomm.control();
  }  else if( src == butFull ) {
    roombacomm.start(); 
    roombacomm.control();
    roombacomm.full();
  } else if( src == butSpy ) {
    roombacomm.start();
    roombacomm.clean();  // clean mode
  } else if( src == butBeep ) {
    int song[] = { 69,8, 72,18, 76,8};
    roombacomm.createSong(1, song );
    roombacomm.playSong(1);
  }
}

//
// called on key press
//
void keyPressed() {
  if( key == CODED ) {
    if( keyCode == UP )
      roombacomm.goForward();
    else if( keyCode == DOWN )
      roombacomm.goBackward();
    else if( keyCode == LEFT )
      roombacomm.spinLeft();
    else if( keyCode == RIGHT )
      roombacomm.spinRight();
  }
  else if( keyCode == RETURN || keyCode == ENTER ) {
    println("resetting");
    roombacomm.reset();
    reset();
  }
  else if( key == ' ' ) {
    roombacomm.stop();
  }
  else if( key == '.' || key == '>' ) {
    sliderSpeed.setValue( sliderSpeed.getValue() + 25 );
    roombacomm.setSpeed( sliderSpeed.getValue() ); 
  }
  else if( key == '.' || key == '>' ) {
    sliderSpeed.setValue( sliderSpeed.getValue() - 25 );
    roombacomm.setSpeed( sliderSpeed.getValue() ); 
  }
}

//
// drawRoombaStatus -- called during every draw()
// draws a little virtual roomba with in-built sensor icons
//
void drawRoombaStatus(float posx, float posy, float angle) {
  pushMatrix();
  translate( (int)posx,(int)posy);
  rotate(angle);
  smooth();
  textFont(fontA, 10);
  textAlign(CENTER);
  
  fill(cBack);
  strokeWeight(2);
  stroke(100);
  ellipseMode(CENTER_RADIUS);
  //rectMode(CENTER);
  ellipse(0,0, 50,50);

  fill(0);
  stroke(cTxt);
  strokeWeight(4);
  text("cliff", 0, -30);
  if( roombacomm.cliffLeft()       ) stroke(cOn);
  else stroke(cOff);
  line(-35,-25, -40,-15);
  if( roombacomm.cliffRight()      ) stroke(cOn);
  else stroke(cOff);
  line( 35,-25,  40,-15);

  if( roombacomm.cliffFrontLeft()  ) stroke(cOn);
  else stroke(cOff);
  line(-30,-30, -20,-30);
  if( roombacomm.cliffFrontRight() ) stroke(cOn);
  else stroke(cOff);
  line( 30,-30,  20,-30);
  
  stroke(cTxt);
  strokeWeight(7);
  text("bump", 0,-17);
  if( roombacomm.bumpLeft()  ) stroke(cOn);
  else stroke(cOff);
  line(-25,-20, -20,-20);
  if( roombacomm.bumpRight() ) stroke(cOn);
  else stroke(cOff);
  line( 25,-20,  20,-20);

  stroke(cTxt);
  text("wheeldrop", 0,3);
  if( roombacomm.wheelDropLeft()  ) stroke(cOn);
  else stroke(cWhl);
  line(-35,8, -20, 8);
  if( roombacomm.wheelDropRight() ) stroke(cOn);
  else stroke(cWhl);
  line( 35,8,  20, 8);
  if( roombacomm.wheelDropCenter()) stroke(cOn);
  else stroke(cWhl);
  line(-2,-8,   2,-8);
  
  stroke(cTxt);
  strokeWeight(5);
  text("over",0,18);
  if( roombacomm.motorOvercurrentDriveLeft()  ) stroke(cOn);
  else stroke(cOff);
  line(-30,16, -20, 16);
  if( roombacomm.motorOvercurrentDriveRight() ) stroke(cOn);
  else stroke(cOff);
  line( 30,16,  20, 16);
  
  stroke(cTxt);
  strokeWeight(7);
  text("dirt",0,42);
  if( roombacomm.dirtLeft()>0 ) stroke(40,40,roombacomm.dirtLeft());
  else stroke(cOff);
  line(-22,40,-18,40);
  if( roombacomm.dirtRight()>0 ) stroke(40,40,roombacomm.dirtRight());
  else stroke(cOff);
  line( 22,40, 18,40);
  
  popMatrix();
}

// called during each draw()
void drawStatus() {
  fill(cStat);
  rectMode(CENTER);
  rect( width-75,height-125, 150,250 );

  rectMode(CORNER);
  rect(0,0, width,50); 
  fill(0);
  textAlign(LEFT);
  textFont(fontA,14);
  String status = "unknown";
  int batt_percent=0, batt_charge=0, batt_volts=0, batt_mA=0, batt_cap=0;
  if( roombacomm == null ) {
    status = "not connected. no roomba. please restart.";
  } else if( ! roombacomm.connected() ) {
    status = "not connected. no roomba. please restart.";
  } else if( ! roombacomm.sensorsValid() ) {
     status = "connected. sensors invalid. unplugged?";
  } else if( roombacomm.safetyFault() ) {
     status = "connected. safety fault. reposition roomba and reset.";
  } else {
    status = "connected. roomba detected. ok.";
  }
  if( roombacomm != null ) {
    batt_mA     = roombacomm.current();
    batt_volts  = roombacomm.voltage();
    batt_charge = roombacomm.charge();
    batt_cap    = roombacomm.capacity();
    batt_percent = (batt_cap == 0) ? 0 : (batt_charge*100)/batt_cap;
  }
  text(" status: "+status, 8,12);
  text("   mode: "+((roombacomm==null)?"no roomba":roombacomm.modeAsString()), 8,24);
  text("battery: "+batt_percent+"% ("+batt_charge+"/"+batt_cap+" mAh)  voltage: "
                  +batt_volts+" mV @ "+batt_mA+" mA", 8,36);               
}

// called during each draw()
void drawGridlines() {
  int gridspacing = 50;
  stroke(cGrid);
  strokeWeight(1);
  for( int i=0; i< 15; i++ ) {
    line(0, i*gridspacing, width, i*gridspacing);
    line(i*gridspacing,0, i*gridspacing, height);
  }
}

//
void makeMoveButtons(int posx, int posy) {
  MyGUIGroup buttonGroup = new MyGUIGroup(this, posx, posy);
  buttonGroup.setStyle( new MyGUIStyle(this, cBut) );
  PImage forward   = loadImage("but_forward.png");
  PImage backward  = loadImage("but_backward.png");
  PImage spinleft  = loadImage("but_spinleft.png");
  PImage spinright = loadImage("but_spinright.png");
  PImage stopit    = loadImage("but_stop.png");
  PImage turnFL    = loadImage("but_turnleft.png");
  PImage turnFR    = loadImage("but_turnright.png");
  butForward   = new MyGUIButton(this,  0,-45, forward);
  butBackward  = new MyGUIButton(this,  0, 45, backward);
  butSpinLeft  = new MyGUIButton(this,-45,  0, spinleft);
  butSpinRight = new MyGUIButton(this, 45,  0, spinright);
  butStop      = new MyGUIButton(this,  0,  0, stopit);
  butTurnLeft  = new MyGUIButton(this,-45,-45, turnFL);
  butTurnRight = new MyGUIButton(this, 45,-45, turnFR);
  sliderSpeed = new MyGUIPinSlider(this, 0,80, 100,20, 0,500);
  sliderSpeed.setValue(200);
  sliderSpeed.setActionCommand("speed-update");
  
  gui.add(buttonGroup);
  //buttonGroup.add( label );
  buttonGroup.add( butForward );
  buttonGroup.add( butBackward );
  buttonGroup.add( butSpinLeft );
  buttonGroup.add( butSpinRight);
  buttonGroup.add( butStop );
  buttonGroup.add( butTurnLeft );
  buttonGroup.add( butTurnRight );
  buttonGroup.add( sliderSpeed );
}

//
void makeControlButtons(int posx, int posy) {
  MyGUIGroup buttonGroup = new MyGUIGroup(this, posx,posy);
  buttonGroup.setStyle( new MyGUIStyle(this, cBut) );
  butReset    = new MyGUIButton(this, -40, 25, "reset", 35,20 );
  butSafe     = new MyGUIButton(this,   0, 25, "safe", 35,20 );
  butFull     = new MyGUIButton(this,  40, 25, "full", 35,20 );
  butSpy      = new MyGUIButton(this, -40,  0, "SPY", 35,20 );  
  butBeep     = new MyGUIButton(this,  40,  0, "beep", 35,20 );  
//  butVacuum   = new MyGUIButton(this,  40, 25, "vacuum", 35,20 );
  
  gui.add( buttonGroup );
  buttonGroup.add( butReset );
  buttonGroup.add( butSafe );
  buttonGroup.add( butFull );
  buttonGroup.add( butSpy );
  buttonGroup.add( butBeep );
  //buttonGroup.add( butVacuum );
}

//
void roombacommPortSelected()
{
  roombacomm = new RoombaCommSerial();
  if( roombacommPort.equals("testdummy") ) return;  // so we can test UI w/o Roomba
  println("opening roomba serial port '" +roombacommPort+"'");
  if( ! roombacomm.connect( roombacommPort ) ) {
    println("oh noes");
  }
  println("Roomba startup");
  roombacomm.startup();
  roombacomm.control();
  roombacomm.pause(50);
  println("Playing some notes");
  roombacomm.playNote( 72, 8 );
  roombacomm.pause( 200 );
  roombacomm.playNote( 72, 10 );
  roombacomm.pause( 200 );
}

//
// On startup, popup a dialog to choose roomba port
//
void roombacommSetup()
{
  if( roombacommPort != null ) {
    roombacommPortSelected();
    return;
  }

  RoombaComm rc = new RoombaCommSerial();     // throwaway just to find ports   
  final String portlist[] = rc.listPorts();   // FIXME: should be made static   
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      try {
        String port = (String) javax.swing.JOptionPane.showInputDialog( null,
                              "Select Roomba Port",
                              "RoombaView",
                              javax.swing.JOptionPane.QUESTION_MESSAGE,
                              null, portlist, null);
        if( port == null )  {
          javax.swing.JOptionPane.showMessageDialog(null, "No port chosen, goodbye");
          System.exit(1);
        }
        roombacommPort = port;
        roombacommPortSelected();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  } );

}