Rotate Cube (Sensor)
in
Core Library Questions
•
1 year ago
Hi.
I am trying to do something that I though would be quite simple but it has quickly confused me. I have an IMU connected to an Arduino and it is sending Roll, Pitch and Yaw values to Processing over the serial port. I am trying to map these values to the RotateX,Y & Z values of a cube. The values are printing in processing but when I try to map these to the cubes rotation strange things happen, the cube flashes and bounces all over the screen. The code that I am using is below, any help would be greatly appreciated, Thanks!!
PS. this is what prints in the terminal, values range from -180 to +180 :
Sensor 0: 0.92 Roll
Sensor 1: 2.36 Pitch
Sensor 2: -51.58 Yaw
I am trying to do something that I though would be quite simple but it has quickly confused me. I have an IMU connected to an Arduino and it is sending Roll, Pitch and Yaw values to Processing over the serial port. I am trying to map these values to the RotateX,Y & Z values of a cube. The values are printing in processing but when I try to map these to the cubes rotation strange things happen, the cube flashes and bounces all over the screen. The code that I am using is below, any help would be greatly appreciated, Thanks!!
PS. this is what prints in the terminal, values range from -180 to +180 :
Sensor 0: 0.92 Roll
Sensor 1: 2.36 Pitch
Sensor 2: -51.58 Yaw
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
float bgcolor; // Background color
float fgcolor; // Fill color
float xpos, ypos; // Starting position of the ball
void setup() {
frameRate(30);
size(640,480, P3D);
background(0);
lights();
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// draw with smooth edges:
smooth();
}
void draw() {
}
void serialEvent(Serial myPort) {
background(0);
noStroke();
pushMatrix();
translate(width/2, height/2);
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
float sensors[] = float(split(myString, ','));
//println("roll: " + sensors[0] + " pitch: " + sensors[1] + " yaw: " + sensors[2] + "\t");
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
if (sensorNum == 0) {
println("Roll");
//rotateX(sensors[0]);
}
if (sensorNum == 1) {
println("Pitch");
//rotateY(sensors[1]);
}
if (sensorNum == 2) {
println("Yaw");
//rotateZ(sensors[2]);
}
}
// add a linefeed after all the sensor values are printed:
println();
box(100);
popMatrix();
if (sensors.length > 1) {
rotateX(sensors[0]);
rotateY(sensors[1]);
rotateZ(sensors[2]);
}
// send a byte to ask for more data:
myPort.write("A");
}
1