Akcelerometre
Zo stránky SensorWiki
Modifikované cvičenie na DOMA:
Úloha:
Akcelerometre ktoré máte v mobile, alebo ako samostatný senzor, použijeme na ovládanie 3D modelu v prostredí Processing.
Postup:
1. Údaje z akcelerometrov v mobile prenesiete cez Bluetooth alebo cez WiFi pomocou niektorej z aplikácií, ktoré poznáte z predošlých cvičení. Kontrola: v programe Terminal.exe alebo podobnom skontrolujete, či prijímate správne údaje.
2. Údaje prijmete v aplikácii, ktorú máte ako vzor uvedenú nižšie. Treba len: a) rozparsovať prijímané dáta na tri súradnice ax, ay a az. b) prepočítať zrýchlenia na natočenia roll, yaw a pitch. c) na základe výpočtu pohybovať objektom v okne
3. Okrem vzorovej kocky, ktorú máte vo vzorovej aplikácii, vytvorte alebo stiahnite z internetu iný 3D model vo formáte .obj a demonštrujte na krátkom videu, že ním viete otáčať.
Odovzdať treba:
- video na ktorom vidno ako otáčate objektom pohybom mobilu
- zdrojový kód v Processingu (.pde)
- model objektu (.obj)
Vzorový program
// https://github.com/PaulStoffregen/NXPMotionSense/tree/master/OrientationVisualiser
// 
// https://www.pjrc.com/store/prop_shield.html
import processing.serial.*;
Serial myPort;
PImage bg;
PShape myObject;
float yaw = 0.0;
float pitch = 0.0;
float roll = 0.0;
void setup()
{
  size(600, 600, P3D);
  bg = loadImage("grid10.png");
  myObject = loadShape("tinker.obj");  // vymodelovane v tinkerCADe ak tamnahram aj obj.mtl, tak to bude farebne!
  // if you don't know anything, first list all available ports:
  printArray(Serial.list());
 // while (!keyPressed) { /* wait here, please */   } 
  
  // if you have only ONE serial port active
  //myPort = new Serial(this, Serial.list()[1], 115200); // if you have only ONE serial port active
  // if you know the serial port name
  //myPort = new Serial(this, "COM5:", 9600);        // Windows "COM#:"
  myPort = new Serial(this, "\\\\.\\COM39", 115200); // Windows, COM10 or higher
  //myPort = new Serial(this, "/dev/ttyACM0", 9600);   // Linux "/dev/ttyACM#"
  //myPort = new Serial(this, "/dev/cu.usbmodem1217321", 9600);  // Mac "/dev/cu.usbmodem######"
  // not used, zda sa...
  // textSize(16); // set text size 
  // textMode(SHAPE); // set text mode to shape
}
void draw()
{
  serialEvent();  // read and parse incoming serial message
// background(255); // set background to white
  background(bg); // set background to white
  lights();
  translate(width/2, height/2); // set position to centre
  pushMatrix(); // begin object
  float c1 = cos(radians(roll));
  float s1 = sin(radians(roll));
  float c2 = cos(radians(-pitch));
  float s2 = sin(radians(-pitch));
  float c3 = cos(radians(yaw));
  float s3 = sin(radians(yaw));
 /* applyMatrix( c2*c3, s1*s3+c1*c3*s2, c3*s1*s2-c1*s3, 0,
               -s2, c1*c2, c2*s1, 0,
               c2*s3, c1*s2*s3-c3*s1, c1*c3+s1*s2*s3, 0,
               0, 0, 0, 1);
*/
  rotateX(radians(roll));
  rotateY(radians(-pitch));
  rotateZ(radians(yaw));
  //drawPropShield();
  //drawRGBcross();
  shape(myObject);
  popMatrix(); // end of object
  // Print values to console
  print(roll);
  print("\t");
  print(-pitch);
  print("\t");
  print(yaw);
  println();
}
void serialEvent()
{
  float aX, aY, aZ;
  int newLine = 13; // new line character in ASCII
  String message;
  do {
    message = myPort.readStringUntil(newLine); // read from port until new line
    if (message != null) {
      String[] list = split(trim(message), " "); // rozdelime to medzerami (mozem sem dat napr. ciarky)
      if (list.length >= 4 && list[0].equals("Orientation:")) {  // tu vyhodime zo zaciatku slovo Orientation: ale nemusi to byt vobec
        aX = float(list[1]); // convert to float yaw
        aY = float(list[2]); // convert to float pitch
        aZ = float(list[3]); // convert to float roll
        
        aX = map(aX,-1023,1023,-9.81,9.81);
        aY = map(aY,-1023,1023,-9.81,9.81);
        aZ = map(aZ,-1023,1023,-9.81,9.81);
        
        pitch = 180 * atan (aX/sqrt(aY*aY + aZ*aZ))/ PI;
        roll = 180 * atan (aY/sqrt(aX*aX + aZ*aZ))/ PI;
        yaw = 180 * atan (aZ/sqrt(aX*aX + aZ*aZ))/ PI;
        
        
        //yaw=45;
        //pitch=-45;
        //roll=45;
      }
    }
  } while (message != null);
}
void drawRGBcross()
{
  /* function contains shape(s) that are rotated with the IMU */
  stroke(0); // black outline
  fill(100, 128, 50); // fill color PCB green
   fill(255, 215, 0); // gold color
  box(100,100, 100); // cube
  // arrows
  stroke(0, 0, 250); // set outline colour to blue
  translate(100, 100, 100); // set position to other edge of Arduino box
  box(1, 1, 200); // draw Arduino board base shape
  stroke(0, 250,0); // set outline colour to darker teal
  box(200, 1, 1); // draw Arduino board base shape
  stroke(250,0,0); // set fill colour to lighter teal
  box(1, 200, 1); // draw Arduino board base shape
/*
  stroke(0); // set outline colour to black
  fill(80); // set fill colour to dark grey
  translate(60, -10, 90); // set position to edge of Arduino box
  box(170, 20, 10); // draw pin header as box
  translate(-20, 0, -180); // set position to other edge of Arduino box
  box(210, 20, 10); // draw other pin header as box
  
*/
}
void drawPropShield()
{
  // 3D art by Benjamin Rheinland
  stroke(0); // black outline
  fill(0, 128, 0); // fill color PCB green
  box(190, 6, 70); // PCB base shape
  fill(255, 215, 0); // gold color
  noStroke();
  //draw 14 contacts on Y- side
  translate(65, 0, 30);
  for (int i=0; i<14; i++) {
    sphere(4.5); // draw gold contacts
    translate(-10, 0, 0); // set new position
  }
  //draw 14 contacts on Y+ side
  translate(10, 0, -60);
  for (int i=0; i<14; i++) {
    sphere(4.5); // draw gold contacts
    translate(10, 0, 0); // set position
  }
  //draw 5 contacts on X+ side (DAC, 3v3, gnd)
  translate(-10,0,10);
  for (int i=0; i<5; i++) {
    sphere(4.5);
    translate(0,0,10);
  }
  //draw 4 contacts on X+ side (G C D 5)
  translate(25,0,-15);
  for (int i=0; i<4; i++) {
    sphere(4.5);
    translate(0,0,-10);
  }
  //draw 4 contacts on X- side (5V - + GND)
  translate(-180,0,10);
  for (int i=0; i<4; i++) {
    sphere(4.5);
    translate(0,0,10);
  }
  //draw audio amp IC
  stroke(128);
  fill(24);    //Epoxy color
  translate(30,-6,-25);
  box(13,6,13);
  //draw pressure sensor IC
  stroke(64);
  translate(32,0,0);
  fill(192);
  box(10,6,18);
  //draw gyroscope IC
  stroke(128);
  translate(27,0,0);
  fill(24);
  box(16,6,16);
  //draw flash memory IC
  translate(40,0,-15);
  box(20,6,20);
  //draw accelerometer/magnetometer IC
  translate(-5,0,25);
  box(12,6,12);
  //draw 5V level shifter ICs
  translate(42.5,2,0);
  box(6,4,8);
  translate(0,0,-20);
  box(6,4,8);
}
Tipy:
- Na vytvorenie 3D objektu môžete použiť napr. TinkerCAD (návody na tomto YouTube kanáli: https://www.youtube.com/playlist?list=PLmh6bwr3ZDwAzcU1krZOvCDkJEuO8nUhv
- Ak k vyexportovanému objektu pridáte aj .mtl súbor, objekt bude farebný
- Tento demo program v Processingu otáča objektom pomocou myšky