How to display Microbit's orientation as a 3D model in Processing?

I am working on a project with my rocketry club. I want to have a microbit control the orientation of the fins to auto-stabilize the rocket. But first, I tried to make a processing code to display in real-time my micro-bit’s orientation using its integrated gyroscope.

Here’s my processing code :

`import processing.serial.*; // import the serial library

Serial myPort; // create a serial object

float xRot = 0; // variables to store the rotation angles
float yRot = 0;
float zRot = 0;
String occ;
void setup() {
size(400, 400, P3D); // set the size of the window and enable 3D rendering

String portName = Serial.list()[0]; // get the name of the first serial port
myPort = new Serial(this, portName, 115200); // open a connection to the serial port
println((Object) Serial.list());
}

void draw() {
background(255); // clear the screen
translate(width/2, height/2, 0); // center the cube on the screen
rotateX(xRot); // apply the rotations
rotateZ(yRot);
rotateY(zRot);
fill(200); // set the fill color
box(100); // draw the cube
}

void serialEvent(Serial myPort) {
// this function is called whenever new data is available
// read the incoming data from the serial port
String data = myPort.readStringUntil(‘\n’); // read the data as a string
// print the incoming data to the console if it is not an empty string
if (data != null) {
println(data);
}
delay(10);
if (data != null) {
// split the string into separate values
String values = split(data, ‘,’);
// convert the values to floats and store them in the rotation variables
xRot = radians(float(values[0]));
yRot = radians(float(values[1]));
zRot = radians(float(values[2]));
}
}`
and here’s the python code I have on my microbit

`pitch = 0
roll = 0
x = 0
y = 0
z = 0

def on_forever():
global pitch, roll, x, y, z
pitch = input.rotation(Rotation.PITCH) + 90
roll = input.rotation(Rotation.ROLL) + 90
servos.P2.set_angle(pitch)
servos.P1.set_angle(roll)
x = input.rotation(Rotation.PITCH)
y = input.rotation(Rotation.ROLL)
z = 1
serial.set_baud_rate(BaudRate.BAUD_RATE115200)
serial.write_string(str(x) + “,” + str(y) + “,” + str(z) + “\n”)
basic.forever(on_forever)`
What happens when I run my code is that a cube appears and rotates weirdly for a short time, then, the cube stops and processing prints “Error, disabling serialEvent() for COM5 null”.

Please help me out, I really need this code to be working!``

1 Like

Hi @neospace, welcome to the forum. I don’t have a microbit but can see you are simply sending three numbers separated by commas, and a line-feed. All good so far. How often does the microbit run round its program and send this? With Arduino I always like to have a known rate, rather than ‘as fast as possible’. Then I can calculate whether everything can keep up. I would put a delay of 100ms initially, see if everything works. If required try faster later. (If you don’t make progress like that, try a lower baud rate.) I would start with a sketch that simply prints all the received text on the console. When that is fully reliable add in the display code.

Your post would be easier to read if you edit it and correct the format, code markers around the code sections. Some characters are showing as small squares, don’t know what’s gone wrong there.

2 Likes

I think that’s [] gone

Hello,

Please format your code as per instructions here:
https://discourse.processing.org/faq#format-your-code

I will certainly take a look once you have formatted it.

:)

Are you sure you receive data as degrees?

Because you use radians()?

in my version you can rotate with the mouse



import processing.serial.*; // import the serial library

Serial myPort; // create a serial object

float xRot = 0.9; // variables to store the rotation angles
float yRot = 0.3;
float zRot = 0.2;
String occ;

void setup() {
  size(400, 400, P3D); // set the size of the window and enable 3D rendering

  String portName = Serial.list()[0]; // get the name of the first serial port
  myPort = new Serial(this, portName, 115200); // open a connection to the serial port
  println((Object[]) Serial.list());
}

void draw() {
  background(255); // clear the screen
  lights(); 

  translate(width/2, height/2, 0); // center the cube on the screen

  rotateX(xRot); // apply the rotations
  rotateZ(yRot);
  rotateY(zRot);

  fill(200); // set the fill color
  box(100); // draw the cube

  // red nose 
  translate( 60, 0, 0 );
  fill(255, 0, 0); 
  box(17); 

  yRot = map(mouseX, 0, width, 0, TWO_PI);
  xRot = map(mouseY, 0, height, 0, TWO_PI);

  //serialEventMy();
}

void serialEventMy() {
  // this function is called whenever new data is available
  // read the incoming data from the serial port
  String data = myPort.readStringUntil('\n'); // read the data as a string

  // print the incoming data to the console if it is not an empty string
  if (data != null) {
    println(data);
  }

  delay(100);
  if (data != null) {
    // split the string into separate values
    String[] values = split(data, ',');
    // convert the values to floats and store them in the rotation variables
    xRot = radians(float(values[0]));
    yRot = radians(float(values[1]));
    zRot = radians(float(values[2]));
  }
}
//

the rotation might not be realistic, because the way
rotation is done in processing.

see 3D Rotations: Rotating around the screen axes - #26 by behreajj

1 Like

Is this the same problem that was posted on StackOverflow: https://stackoverflow.com/questions/74844527/how-to-display-microbits-orientation-as-a-3d-model-in-processing

2 Likes