adafruit_bno055 / examples / bunny / processing / cuberotate / cuberotate.pde @ f0ccfd82
History | View | Annotate | Download (4.687 KB)
1 | 8cc46552 | Kevin Townsend | import processing.serial.*; |
---|---|---|---|
2 | import java.awt.datatransfer.*; |
||
3 | import java.awt.Toolkit; |
||
4 | import processing.opengl.*; |
||
5 | import saito.objloader.*; |
||
6 | import g4p_controls.*; |
||
7 | |||
8 | float roll = 0.0F; |
||
9 | float pitch = 0.0F; |
||
10 | float yaw = 0.0F; |
||
11 | float temp = 0.0F; |
||
12 | float alt = 0.0F; |
||
13 | |||
14 | OBJModel model; |
||
15 | |||
16 | // Serial port state. |
||
17 | Serial port; |
||
18 | final String serialConfigFile = "serialconfig.txt"; |
||
19 | boolean printSerial = false; |
||
20 | |||
21 | // UI controls. |
||
22 | GPanel configPanel; |
||
23 | GDropList serialList; |
||
24 | GLabel serialLabel; |
||
25 | 2fa9c672 | Tony DiCola | GLabel calLabel; |
26 | 8cc46552 | Kevin Townsend | GCheckbox printSerialCheckbox; |
27 | |||
28 | void setup() |
||
29 | { |
||
30 | 2fa9c672 | Tony DiCola | size(640, 480, OPENGL); |
31 | 8cc46552 | Kevin Townsend | frameRate(30); |
32 | model = new OBJModel(this); |
||
33 | model.load("bunny.obj"); |
||
34 | model.scale(20); |
||
35 | |||
36 | // Serial port setup. |
||
37 | // Grab list of serial ports and choose one that was persisted earlier or default to the first port. |
||
38 | int selectedPort = 0; |
||
39 | String[] availablePorts = Serial.list(); |
||
40 | if (availablePorts == null) { |
||
41 | println("ERROR: No serial ports available!"); |
||
42 | exit(); |
||
43 | } |
||
44 | String[] serialConfig = loadStrings(serialConfigFile); |
||
45 | if (serialConfig != null && serialConfig.length > 0) { |
||
46 | String savedPort = serialConfig[0]; |
||
47 | // Check if saved port is in available ports. |
||
48 | for (int i = 0; i < availablePorts.length; ++i) { |
||
49 | if (availablePorts[i].equals(savedPort)) { |
||
50 | selectedPort = i; |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | // Build serial config UI. |
||
55 | configPanel = new GPanel(this, 10, 10, width-20, 90, "Configuration (click to hide/show)"); |
||
56 | serialLabel = new GLabel(this, 0, 20, 80, 25, "Serial port:"); |
||
57 | configPanel.addControl(serialLabel); |
||
58 | serialList = new GDropList(this, 90, 20, 200, 200, 6); |
||
59 | serialList.setItems(availablePorts, selectedPort); |
||
60 | configPanel.addControl(serialList); |
||
61 | 2fa9c672 | Tony DiCola | calLabel = new GLabel(this, 300, 20, 350, 25, "Calibration: Sys=? Gyro=? Accel=? Mag=?"); |
62 | configPanel.addControl(calLabel); |
||
63 | 8cc46552 | Kevin Townsend | printSerialCheckbox = new GCheckbox(this, 5, 50, 200, 20, "Print serial data"); |
64 | printSerialCheckbox.setSelected(printSerial); |
||
65 | configPanel.addControl(printSerialCheckbox); |
||
66 | // Set serial port. |
||
67 | setSerialPort(serialList.getSelectedText()); |
||
68 | } |
||
69 | |||
70 | void draw() |
||
71 | { |
||
72 | background(0,0,0); |
||
73 | |||
74 | // Set a new co-ordinate space |
||
75 | pushMatrix(); |
||
76 | |||
77 | // Simple 3 point lighting for dramatic effect. |
||
78 | // Slightly red light in upper right, slightly blue light in upper left, and white light from behind. |
||
79 | pointLight(255, 200, 200, 400, 400, 500); |
||
80 | pointLight(200, 200, 255, -400, 400, 500); |
||
81 | pointLight(255, 255, 255, 0, 0, -500); |
||
82 | |||
83 | 2fa9c672 | Tony DiCola | // Move bunny from 0,0 in upper left corner to roughly center of screen. |
84 | translate(300, 380, 0); |
||
85 | 8cc46552 | Kevin Townsend | |
86 | // Rotate shapes around the X/Y/Z axis (values in radians, 0..Pi*2) |
||
87 | dd57d4fa | Kevin Townsend | rotateZ(radians(roll)); |
88 | 8cc46552 | Kevin Townsend | rotateX(radians(pitch)); |
89 | dd57d4fa | Kevin Townsend | rotateY(radians(yaw)); |
90 | 8cc46552 | Kevin Townsend | |
91 | pushMatrix(); |
||
92 | noStroke(); |
||
93 | model.draw(); |
||
94 | popMatrix(); |
||
95 | popMatrix(); |
||
96 | //print("draw"); |
||
97 | } |
||
98 | |||
99 | void serialEvent(Serial p) |
||
100 | { |
||
101 | String incoming = p.readString(); |
||
102 | if (printSerial) { |
||
103 | println(incoming); |
||
104 | } |
||
105 | |||
106 | if ((incoming.length() > 8)) |
||
107 | { |
||
108 | String[] list = split(incoming, " "); |
||
109 | if ( (list.length > 0) && (list[0].equals("Orientation:")) ) |
||
110 | { |
||
111 | roll = float(list[3]); // Roll = Z |
||
112 | pitch = float(list[2]); // Pitch = Y |
||
113 | yaw = float(list[1]); // Yaw/Heading = X |
||
114 | } |
||
115 | if ( (list.length > 0) && (list[0].equals("Alt:")) ) |
||
116 | { |
||
117 | alt = float(list[1]); |
||
118 | } |
||
119 | if ( (list.length > 0) && (list[0].equals("Temp:")) ) |
||
120 | { |
||
121 | temp = float(list[1]); |
||
122 | 2fa9c672 | Tony DiCola | } |
123 | if ( (list.length > 0) && (list[0].equals("Calibration:")) ) |
||
124 | { |
||
125 | int sysCal = int(list[1]); |
||
126 | int gyroCal = int(list[2]); |
||
127 | int accelCal = int(list[3]); |
||
128 | int magCal = int(list[4]); |
||
129 | calLabel.setText("Calibration: Sys=" + sysCal + " Gyro=" + gyroCal + " Accel=" + accelCal + " Mag=" + magCal); |
||
130 | 8cc46552 | Kevin Townsend | } |
131 | } |
||
132 | } |
||
133 | |||
134 | // Set serial port to desired value. |
||
135 | void setSerialPort(String portName) { |
||
136 | // Close the port if it's currently open. |
||
137 | if (port != null) { |
||
138 | port.stop(); |
||
139 | } |
||
140 | try { |
||
141 | // Open port. |
||
142 | port = new Serial(this, portName, 115200); |
||
143 | port.bufferUntil('\n'); |
||
144 | // Persist port in configuration. |
||
145 | saveStrings(serialConfigFile, new String[] { portName }); |
||
146 | } |
||
147 | catch (RuntimeException ex) { |
||
148 | // Swallow error if port can't be opened, keep port closed. |
||
149 | port = null; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // UI event handlers |
||
154 | |||
155 | void handlePanelEvents(GPanel panel, GEvent event) { |
||
156 | // Panel events, do nothing. |
||
157 | } |
||
158 | |||
159 | void handleDropListEvents(GDropList list, GEvent event) { |
||
160 | // Drop list events, check if new serial port is selected. |
||
161 | if (list == serialList) { |
||
162 | setSerialPort(serialList.getSelectedText()); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | void handleToggleControlEvents(GToggleControl checkbox, GEvent event) { |
||
167 | // Checkbox toggle events, check if print events is toggled. |
||
168 | if (checkbox == printSerialCheckbox) { |
||
169 | printSerial = printSerialCheckbox.isSelected(); |
||
170 | } |
||
171 | 2fa9c672 | Tony DiCola | } |