forked from Lauszus/LaunchPadFlightController
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI.pde
More file actions
144 lines (119 loc) · 4.07 KB
/
Copy pathGUI.pde
File metadata and controls
144 lines (119 loc) · 4.07 KB
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
/* Copyright (C) 2015 Kristian Sloth Lauszus. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Kristian Sloth Lauszus
Web : http://www.lauszus.com
e-mail : lauszus@gmail.com
*/
import processing.serial.*;
import controlP5.*;
import java.awt.event.*;
import java.awt.Image;
Serial serial;
float roll, pitch, yaw;
float yawStart;
boolean resetYaw = true;
boolean drawValues = false;
final int width = 600;
final int height = 600;
void setup() {
/*frame.setTitle("Balanduino Processing App");
frame.setIconImage((Image) loadImage("data/logo.png").getNative());*/
controlP5 = new ControlP5(this);
size(width, height, P3D);
initDropdownlist();
}
void draw() {
if (!connectedSerial || drawValues) {
background(250);
lights();
}
if (drawValues) { // Draw plane
drawValues = false;
pushMatrix();
translate(width/2, height/2, 0);
float radRoll = radians(roll);
float radPitch = radians(pitch);
float radYaw = radians(yaw);
rotateX(radPitch * cos(radYaw) - radRoll * sin(radYaw) + HALF_PI);
rotateY(radRoll * cos(radYaw) + radPitch * sin(radYaw));
rotateZ(radYaw * cos(radPitch) + radRoll * sin(radPitch));
box(200, 200, 30);
popMatrix();
System.out.printf("%.02f, %.02f, %.02f\n", roll, pitch, yaw);
}
}
byte[] buffer;
boolean append;
void serialEvent (Serial serial) {
if (append)
buffer = concat(buffer, serial.readBytes());
else
buffer = serial.readBytes();
int[] data = new int[buffer.length];
for (int i = 0; i < data.length; i++)
data[i] = buffer[i] & 0xFF; // Cast to unsigned value
//println(data);
if (new String(buffer).startsWith(responseHeader)) {
int cmd = data[responseHeader.length()];
int length = data[responseHeader.length() + 1];
if (length != (data.length - responseHeader.length() - 5)) {
append = true; // If it's not the correct length, then the rest will come in the next package
return;
}
int input[] = new int[length];
int i;
for (i = 0; i < length; i++)
input[i] = data[i + responseHeader.length() + 2];
int checksum = data[i + responseHeader.length() + 2];
if (checksum == (cmd ^ length ^ getChecksum(input))) {
switch(cmd) {
case SEND_ANGLES:
int rollInt = input[0] | ((byte) input[1] << 8); // This can be negative as well
int pitchInt = input[2] | ((byte) input[3] << 8); // This can be negative as well
int yawInt = input[4] | (input[5] << 8); // This is always positive
//println(rollInt + " " + pitchInt + " " + yawInt);
roll = (float) rollInt / 100.0f; // Convert values to floating point numbers
pitch = (float) pitchInt / 100.0f; // Convert values to floating point numbers
yaw = (float) yawInt / 100.0f; // Convert values to floating point numbers
if (resetYaw) {
resetYaw = false;
yawStart = yaw;
}
yaw -= yawStart; // Subtract starting angle
if (yaw < 0) // Convert heading range to 0-360
yaw += 360;
break;
default:
println("Unknown command");
break;
}
} else
println("Checksum error!");
} else {
println("Wrong header!");
println(new String(buffer));
}
serial.clear(); // Clear buffer
drawValues = true; // Draw the plane
append = false;
}
void reset() {
resetYaw = true; // Reset yaw position
}
void keyPressed() {
if (key == 'r')
reset();
else if (key == ENTER || key == 'c')
connect(); // Connect to serial port
else if (key == ESC || key == 'd' ) {
disconnect(); // Disconnect serial connection
key = 0; // Disable Processing from quiting when pressing ESC
}
}