|
| 1 | +// ----- serial port |
| 2 | +import processing.serial.*; //import the serial library |
| 3 | +Serial myPort; //the Serial port object |
| 4 | +final int Baud_rate = 115200; //communication speed |
| 5 | +String Input_string; //used for incoming data |
| 6 | + |
| 7 | +// -----custom shape |
| 8 | +PShape Object; //give the shape a name |
| 9 | +int Frame_count = 0; //frame counter |
| 10 | +boolean Frame_visible = true; //true=visible; false=invisible |
| 11 | + |
| 12 | +// ----- display graphics |
| 13 | +PGraphics Canvas; //name of drawing area to be created |
| 14 | +PFont myFont; //name of font to be created |
| 15 | +float Baseline = 50; //triangle baseline (cm) |
| 16 | +float X; //X coordinate in (cm) |
| 17 | +float Y; //Y coordinate in (cm) |
| 18 | + |
| 19 | +// ----- sensor distance BELOW baseline (cm) |
| 20 | +float Offset = 50; //assumes square display |
| 21 | + |
| 22 | +// ========================= |
| 23 | +// setup |
| 24 | +// ========================== |
| 25 | +void setup() { |
| 26 | + |
| 27 | + // ----- configure screen |
| 28 | + size(800, 800, P3D); //define window size, 3D |
| 29 | + background(0); //black |
| 30 | + frameRate(60); //60 frames per second |
| 31 | + |
| 32 | + // ----- create a drawing area for fading the beam |
| 33 | + Canvas = createGraphics(width, height); |
| 34 | + |
| 35 | + // ------ create the screen font |
| 36 | + myFont = createFont("Arial Black", 20); |
| 37 | + |
| 38 | + // ----- configure the Object |
| 39 | + Object = createShape(ELLIPSE, 0, 0, 30, 30); //create the Object |
| 40 | + Object.setFill(color(255, 0, 0, 255)); //red, opaque |
| 41 | + Object.setStroke(color(255, 0, 0, 255)); //red, opaque |
| 42 | + |
| 43 | + // ----- initialize the serial port |
| 44 | + /* |
| 45 | + IMPORTANT: |
| 46 | + If your display can't see the arduino try changing the [number] associated |
| 47 | + with your COM port. |
| 48 | + |
| 49 | + The code line " printArray(Serial.list());" generates a list |
| 50 | + of [numbers] within brackets. e.g: [0] "COM5" |
| 51 | + |
| 52 | + The [number] inside the square bracket MUST match the [number] in the |
| 53 | + code line "myPort = new Serial(this, Serial.list()[0], Baud_rate);" |
| 54 | + */ |
| 55 | + printArray(Serial.list()); //lists your COM ports on screen |
| 56 | + myPort = new Serial(this, Serial.list()[0], Baud_rate); |
| 57 | + myPort.bufferUntil('\n'); |
| 58 | +} |
| 59 | + |
| 60 | +// ========================== |
| 61 | +// draw |
| 62 | +// ========================== |
| 63 | +void draw() |
| 64 | +{ |
| 65 | + // ----- refresh the screen |
| 66 | + background(0); //black background |
| 67 | + textFont(myFont, 20); //specify font to be used |
| 68 | + draw_grid(); //draw grid |
| 69 | + draw_object(); |
| 70 | +} |
| 71 | + |
| 72 | +// ======================= |
| 73 | +// serial event (called with each Arduino data string) |
| 74 | +// ======================= |
| 75 | +void serialEvent(Serial myPort) |
| 76 | +{ |
| 77 | + // ----- wait for a line-feed |
| 78 | + Input_string = myPort.readStringUntil('\n'); |
| 79 | + println(Input_string); //visual feedback |
| 80 | + |
| 81 | + // ----- validate |
| 82 | + if (Input_string != null) |
| 83 | + { |
| 84 | + // ----- trim whitespace |
| 85 | + Input_string = trim(Input_string); |
| 86 | + String[] values = split(Input_string, ','); |
| 87 | + |
| 88 | + // ----- gather Heron variables |
| 89 | + float a = float(values[1]) - float(values[0]); //d2 (vertex -> sensor B) |
| 90 | + float b = float(values[0]); //d1 (vertex -> sensor A) |
| 91 | + float c = Baseline; //baseline |
| 92 | + //float d = c*1.414; //display diagonal (square) |
| 93 | + float d = sqrt(150*150 + 100*100); //diagonal (display + offset) |
| 94 | + float s = (a + b + c)/2; //semi-perimeter |
| 95 | + |
| 96 | + // ----- validate distances |
| 97 | + /* eliminate bogus errors */ |
| 98 | + boolean distances_valid = true; |
| 99 | + if |
| 100 | + ( |
| 101 | + (a < 0) || //d1 must be less than d2 |
| 102 | + (b > d) || //d1 out-of-range |
| 103 | + (a > d) || //d2 out-of-range |
| 104 | + ((s - a) < 0) || //these values must be positive |
| 105 | + ((s - b) < 0) || |
| 106 | + ((s - c) < 0) |
| 107 | + ) |
| 108 | + { |
| 109 | + distances_valid=false; |
| 110 | + X=1000; //move flashing dot off-screen |
| 111 | + Y=1000; //move flashing dot off-screen |
| 112 | + } |
| 113 | + |
| 114 | + // ----- apply Heron's formula |
| 115 | + if (distances_valid) |
| 116 | + { |
| 117 | + float area = sqrt(s * (s - a) * (s - b) * (s - c)); |
| 118 | + Y = area * 2 / c; |
| 119 | + X = sqrt(b * b - Y * Y); |
| 120 | + |
| 121 | + // ----- display data for valid echos |
| 122 | + print(" d1: "); |
| 123 | + println(b); |
| 124 | + print(" d2: "); |
| 125 | + println(a); |
| 126 | + print(" base: "); |
| 127 | + println(c); |
| 128 | + print("offset: "); |
| 129 | + println(Offset); |
| 130 | + print(" s: "); |
| 131 | + println(s); |
| 132 | + print(" area: "); |
| 133 | + println(area); |
| 134 | + print(" X: "); |
| 135 | + println(X); |
| 136 | + print(" Y: "); |
| 137 | + println(Y-Offset); |
| 138 | + println(""); |
| 139 | + } |
| 140 | + myPort.clear(); //clear the receive buffer |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// ========================== |
| 145 | +// draw_grid |
| 146 | +// ========================== |
| 147 | +void draw_grid() |
| 148 | +{ |
| 149 | + pushMatrix(); |
| 150 | + |
| 151 | + scale(0.8); |
| 152 | + translate(width*0.1, height*0.10); |
| 153 | + fill(0); |
| 154 | + stroke(255); |
| 155 | + |
| 156 | + // ----- border |
| 157 | + strokeWeight(4); |
| 158 | + rect(0, 0, width, height, 20, 20, 20, 20); |
| 159 | + |
| 160 | + // ----- horizontal lines |
| 161 | + strokeWeight(1); |
| 162 | + line(0, height*0.1, width, height*0.1); |
| 163 | + line(0, height*0.2, width, height*0.2); |
| 164 | + line(0, height*0.3, width, height*0.3); |
| 165 | + line(0, height*0.4, width, height*0.4); |
| 166 | + line(0, height*0.5, width, height*0.5); |
| 167 | + line(0, height*0.6, width, height*0.6); |
| 168 | + line(0, height*0.7, width, height*0.7); |
| 169 | + line(0, height*0.8, width, height*0.8); |
| 170 | + line(0, height*0.9, width, height*0.9); |
| 171 | + |
| 172 | + // ----- vertical lines |
| 173 | + line(width*0.1, 0, width*0.1, height); |
| 174 | + line(width*0.2, 0, width*0.2, height); |
| 175 | + line(width*0.3, 0, width*0.3, height); |
| 176 | + line(width*0.4, 0, width*0.4, height); |
| 177 | + line(width*0.5, 0, width*0.5, height); |
| 178 | + line(width*0.6, 0, width*0.6, height); |
| 179 | + line(width*0.7, 0, width*0.7, height); |
| 180 | + line(width*0.8, 0, width*0.8, height); |
| 181 | + line(width*0.9, 0, width*0.9, height); |
| 182 | + |
| 183 | + // ----- label the X-axis |
| 184 | + fill(255); //white text |
| 185 | + textAlign(LEFT, TOP); |
| 186 | + text("0", -20, height+10); //0cm |
| 187 | + text("50", width*0.5-20, height+10); //50cm |
| 188 | + text("100cm", width-20, height+10); //100cm |
| 189 | + |
| 190 | + // ----- label the y-axis |
| 191 | + textAlign(RIGHT, BOTTOM); |
| 192 | + text("100cm", 10, -10); //100cm |
| 193 | + textAlign(RIGHT, CENTER); |
| 194 | + text("50", -10, height/2); //100cm |
| 195 | + |
| 196 | + popMatrix(); |
| 197 | +} |
| 198 | + |
| 199 | +// ========================== |
| 200 | +// draw_object |
| 201 | +// ========================== |
| 202 | +void draw_object() |
| 203 | +{ |
| 204 | + pushMatrix(); |
| 205 | + scale(0.8); |
| 206 | + stroke(0, 255, 0); |
| 207 | + strokeWeight(1); |
| 208 | + translate(width*0.1, height*1.1); //(0,0) now lower-left corner |
| 209 | + |
| 210 | + // ----- make the object flash |
| 211 | + if ((frameCount-Frame_count)>4) |
| 212 | + { |
| 213 | + Frame_visible = !Frame_visible; |
| 214 | + Frame_count = frameCount; |
| 215 | + } |
| 216 | + |
| 217 | + // ----- object color scheme |
| 218 | + if (Frame_visible) |
| 219 | + { |
| 220 | + // ----- make object visible |
| 221 | + Object.setFill(color(255, 0, 0, 255)); //opaque |
| 222 | + Object.setStroke(color(255, 0, 0, 255)); //opaque |
| 223 | + } else |
| 224 | + { |
| 225 | + // ----- hide the object |
| 226 | + Object.setFill(color(255, 0, 0, 0)); //clear |
| 227 | + Object.setStroke(color(255, 0, 0, 0)); //clear |
| 228 | + } |
| 229 | + |
| 230 | + // ----- draw the object |
| 231 | + pushMatrix(); |
| 232 | + translate(X/100*width, -(Y-Offset)/100*height); |
| 233 | + shape(Object); |
| 234 | + popMatrix(); |
| 235 | + popMatrix(); |
| 236 | +} |
0 commit comments