|
| 1 | +import processing.serial.*; |
| 2 | +Serial myPort; |
| 3 | + |
| 4 | +float yaw = 0.0; |
| 5 | +float pitch = 0.0; |
| 6 | +float roll = 0.0; |
| 7 | + |
| 8 | +void setup() |
| 9 | +{ |
| 10 | + size(600, 500, P3D); |
| 11 | + |
| 12 | + // if you have only ONE serial port active |
| 13 | + myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active |
| 14 | + |
| 15 | + // if you know the serial port name |
| 16 | + //myPort = new Serial(this, "COM5:", 9600); // Windows |
| 17 | + //myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux |
| 18 | + //myPort = new Serial(this, "/dev/cu.usbmodem1217321", 9600); // Mac |
| 19 | + |
| 20 | + textSize(16); // set text size |
| 21 | + textMode(SHAPE); // set text mode to shape |
| 22 | +} |
| 23 | + |
| 24 | +void draw() |
| 25 | +{ |
| 26 | + serialEvent(); // read and parse incoming serial message |
| 27 | + background(255); // set background to white |
| 28 | + lights(); |
| 29 | + |
| 30 | + translate(width/2, height/2); // set position to centre |
| 31 | + |
| 32 | + pushMatrix(); // begin object |
| 33 | + |
| 34 | + float c1 = cos(radians(roll)); |
| 35 | + float s1 = sin(radians(roll)); |
| 36 | + float c2 = cos(radians(pitch)); |
| 37 | + float s2 = sin(radians(pitch)); |
| 38 | + float c3 = cos(radians(yaw)); |
| 39 | + float s3 = sin(radians(yaw)); |
| 40 | + applyMatrix( c2*c3, s1*s3+c1*c3*s2, c3*s1*s2-c1*s3, 0, |
| 41 | + -s2, c1*c2, c2*s1, 0, |
| 42 | + c2*s3, c1*s2*s3-c3*s1, c1*c3+s1*s2*s3, 0, |
| 43 | + 0, 0, 0, 1); |
| 44 | + |
| 45 | + drawArduino(); |
| 46 | + |
| 47 | + popMatrix(); // end of object |
| 48 | + |
| 49 | + // Print values to console |
| 50 | + print(roll); |
| 51 | + print("\t"); |
| 52 | + print(pitch); |
| 53 | + print("\t"); |
| 54 | + print(yaw); |
| 55 | + println(); |
| 56 | +} |
| 57 | + |
| 58 | +void serialEvent() |
| 59 | +{ |
| 60 | + int newLine = 13; // new line character in ASCII |
| 61 | + String message; |
| 62 | + do { |
| 63 | + message = myPort.readStringUntil(newLine); // read from port until new line |
| 64 | + if (message != null) { |
| 65 | + String[] list = split(trim(message), " "); |
| 66 | + if (list.length >= 4 && list[0].equals("Orientation:")) { |
| 67 | + yaw = float(list[1]); // convert to float yaw |
| 68 | + pitch = float(list[2]); // convert to float pitch |
| 69 | + roll = float(list[3]); // convert to float roll |
| 70 | + } |
| 71 | + } |
| 72 | + } while (message != null); |
| 73 | +} |
| 74 | + |
| 75 | +void drawArduino() |
| 76 | +{ |
| 77 | + /* function contains shape(s) that are rotated with the IMU */ |
| 78 | + stroke(0, 90, 90); // set outline colour to darker teal |
| 79 | + fill(0, 130, 130); // set fill colour to lighter teal |
| 80 | + box(300, 10, 200); // draw Arduino board base shape |
| 81 | + |
| 82 | + stroke(0); // set outline colour to black |
| 83 | + fill(80); // set fill colour to dark grey |
| 84 | + |
| 85 | + translate(60, -10, 90); // set position to edge of Arduino box |
| 86 | + box(170, 20, 10); // draw pin header as box |
| 87 | + |
| 88 | + translate(-20, 0, -180); // set position to other edge of Arduino box |
| 89 | + box(210, 20, 10); // draw other pin header as box |
| 90 | +} |
| 91 | + |
0 commit comments